CSC 230 Midterm Chapt 9, CSC 230 Midterm Chpt 10, CSC 230 Midterm Chpt 11
The Java compiler requires that your program handle which type of exceptions?
Checked
____________ can reduce the coupling between classes.
Interfaces
What is the purpose of the throw statement? Select one: a. It is used to pass arguments to another method. b. It is used to detect an error situation. c. It is used to pass control to an error handler when an error situation is detected. d. It is used to discard erroneous input.
It is used to pass control to an error handler when an error situation is detected
Consider the following code snippet, assuming that filename represents the name of the output file and writeData outputs the data to that file: try (PrintWriter outputFile = new PrintWriter(filename)) { writeData(outputFile); } Which of the following statements about this code is correct? Select one: a. The close method of the outputFile object will be automatically invoked when the try block ends, but only if no exception occurred. b. The program will terminate with an unhandled exception if the PrintWriter constructor fails. c. The close method of the outputFile object will be automatically invoked when the try block ends, whether or not an exception has occurred. d. The close method of the outputFile object will be automatically invoked when the try block ends, but only if an exception occurs.
The close method of the outputFile object will be automatically invoked when the try block ends, whether or not an exception has occurred.
Which method of the JFileChooser object will return the file object that describes the file chosen by the user at runtime? Select one: a. getFile() b. getSelectedFilePath() c. getSelectedFile() d. getFilePath()
getSelectedFile()
Insert the missing code in the following code fragment. This fragment is intended to read characters from a text file. Scanner in = new Scanner( . . . ); in.useDelimiter(""); while (in.hasNext()) { char ch = __________________; System.out.println(ch); } Select one: a. in.getNext(); b. in.next(); c. in.next().charAt(0); d. in.nextChar()
in.next().charAt(0);
A/an ______________ class defined in a method signals to the reader of your program that the class is not interesting beyond the scope of the method.
inner
The ____________________ method of the Character class will indicate if a character contains white space.
isWhiteSpace()
Which of the following statements about interfaces is true? Select one: a. You can define an interface variable that refers to an object of any class in the same package. b. You cannot define a variable whose type is an interface. c. You can instantiate an object from an interface class. d. you can define an interface variable that refers to an object only if the object belongs to a class that implements the interface.
you can define an interface variable that refers to an object only if the object belongs to a class that implements the interface
Consider the following code snippet: public class Vehicle { private String manufacturer; public void setVehicleClass(double numberAxles) { ... } } If a motorcycle class is created as a subclass of the Vehicle class,which of the following statements is correct? Select one: a. A Motorcycle object inherits and can directly use both the instance variable manufacturer and the method setVehicleClass. b. A Motorcycle object inherits and can directly use the instance variable manufacturer but not the method setVehicleClass. c. A Motorcycle object inherits but cannot directly use either the instance variable manufacturer or the method setVehicleClass. d. A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer.
A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer.
Which of the following statements about interfaces is NOT true? Select one: a. Interfaces can make code more reusable. b. An interface provides no implementation. c. A class can implement only one interface type. d. Interfaces can reduce the coupling between classes.
A class can implement only one interface type.
Which of the following is true regarding subclasses? Select one: a. A subclass inherits methods from its superclass but not instance variables. b. A subclass inherits instance variables from its superclass but not methods. c. A subclass inherits methods and instance variables from its superclass. Correct d. A subclass does not inherit methods or instance variables from its superclass.
A subclass inherits methods and instance variables from its superclass
Which of the following is true regarding subclasses? Select one: a. A subclass that inherits methods from its superclass may not override the methods. b. A subclass that inherits instance variables from its superclass may not declare additional instance variables. c. A subclass may inherit methods or instance variables from its superclass but not both. d. A subclass may inherit methods and instance variables from its superclass, and may also implements its own methods and declare its own instance variables.
A subclass may inherit methods and instance variables from its superclass, and may also implements its own methods and declare its own instance variables.
Which return value of the JFileChooser object's showOpenDialog method indicates that a file was chosen by the user at run time? Select one: a. APPROVE_OPTION b. CANCEL_OPTION c. OK_OPTION d. SELECTED_OPTION
APPROVE_OPTION
Consider the following code snippet: public class Inventory implements Measurable { .... public double getMeasure { return onHandCount; } } Why is it necessary to declare getMeasure as public? Select one: a. All methods in a class are not public by default. b. All methods in an interface are private by default. c. It is necessary only to allow other classes to use this method. d. It is not necessary to declare this method as public.
All methods in a class are not public by default.
Which statement about methods in an interface is true? Select one: a. All methods in an interface are automatically private. b. All methods in an interface are automatically public. c. All methods in an interface are automatically static. d. All methods in an interface must be explicitly declared as private or public.
All methods in an interface are automatically public.
Which of the following statements about abstract methods is true? Select one: a. An abstract method has a name, parameters, and a return type, but no code in the body of the method. b. An abstract method has parameters, a return type, and code in its body, but has no defined name. c. An abstract method has a name, a return type, and code in its body, but has no parameters. d. An abstract method has only a name and a return type, but no parameters or code in its body.
An abstract method has a name, parameters, and a return type, but no code in the body of the method.
What role does an interface play when using a mock class? Select one: a. The mock class should be an interface that will be implemented by the real class. b. The real class should be an interface that will be implemented by the mock class. c. Interfaces are not involved when using mock classes. d. An interface should be implemented by both the real class and the mock class to guarantee that the mock class accurately simulates the real class when used in a program.
An interface should be implemented by both the real class and the mock class to guarantee that the mock class accurately simulates the real class when used in a program.
When reading words with a Scanner object, a word is defined as __________. Select one: a. Any sequence of characters consisting of letters only. b. Any sequence of characters consisting of letters, numbers, and punctuation symbols only. c. Any sequence of characters consisting of letters and numbers only. d. Any sequence of characters that is not white space
Any sequence of characters that is not white space.
Consider the following inheritance hierarchy diagram: Vehicle ^ ^ ^ AirVehicle -- LandVehicle -- Water Vehicle ^ Auto Which of the following statements is correct? a. Auto class inherits from LandVehicle class, and LandVehicle class inherits from Vehicle class. b. Auto class inherits from LandVehicle class, and Vehicle class inherits from LandVehicle class. c. LandVehicle class inherits from Auto class, and LandVehicle class inherits from Vehicle class. d. LandVehicle class inherits from Auto class, and Vehicle class inherits from LandVehicle class.
Auto class inherits from LandVehicle class, and LandVehicle class inherits from Vehicle class.
Which of the following statements reflects the textbook's recommendations about closing files? Select one: a. Both the input and the output file do not need to be explicitly closed in the program. b. Only the input file must be explicitly closed in the program. c. Only the output file must be explicitly closed in the program. d. Both the input and the output file should be explicitly closed in the program.
Both the input and the output file should be explicitly closed in the program.
The Java compiler requires that your program handle which type of exceptions? Select one: a. Checked b. Fatal c. Unchecked d. Server
Checked
Consider the following code snippet. public interface Measurable { double getMeasure(); } public Coin implements Measurable { ... public double getMeasure() { return value; } } public class DataSet { ... public void add() { ... } } public class BankAccount { ... public void add() { ... } } Which of the following statements is true? Select one: a. Coin dime = new Coin(0.1, "dime"); Measurable x = dime; b. Coin dime = new Coin(0.1, "dime"); DataSet x = dime; c. Coin dime = new Coin(0.1, "dime"); DataSet x = (Measurable) dime; d. Coin dime = new Coin(0.1, "dime"); BankAccount x = dime;
Coin dime = new Coin(0.1, "dime"); Measurable x = dime;
Consider the following code snippet: Coin nickel = new Coin(0.1, "nickel"); Measurable meas = nickel.getMeasure(); Assume that the Coin class has implemented the Measurable interface. You now need to convert the meas object back to a Coin object. Which of the following is the correct code to do this? Select one: a. Coin nickelValue = (Coin) meas.getMeasure(); b. Coin nickelValue = meas.getMeasure(); c. Coin nickelValue = meas; d. Coin nickelValue = (Coin) meas;
Coin nickelValue = (Coin) meas;
Under which condition will the Scanner constructor generate a FileNotFoundException? Select one: a. If the input file cannot be opened due to a security error. b. If the input file does not exist. c. If the input file already exists, but has data in it. d. If the input file already exists, but is empty.
If the input file does not exist.
Which of the following statements about using the PrintWriter object is correct? Select one: a. If the output file already exists, the new data will be appended to the end of the file. b. If the output file already exists, a FileNotFoundException will occur. c. If the output file already exists, the existing data will be discarded before new data are written into the file. d. If the output file does not exist, an IllegalArgumentException will occur.
If the output file already exists, the existing data will be discarded before new data are written into the file.
Under what condition will the PrintWriter constructor generate a FileNotFoundException? Select one: a. If the output file cannot be opened or created due to a security error. b. If the output file does not exist. c. If the output file already exists, but has data in it. d. If the output file already exists, but is empty.
If the output file cannot be opened or created due to a security error.
Which of the following statements about white space in Java is correct? Select one: a. In Java, white space includes spaces only. b. In Java, white space includes spaces and tab characters only. c. In Java, white space includes spaces, tab characters and newline characters. d. In Java, white space includes spaces, tab characters, newline characters, and punctuation.
In Java, white space includes spaces, tab characters and newline characters.
Which of the following statements about interfaces is NOT true? Select one: a. Interfaces can make code more reusable. b. Interface types can be used to define a new reference data type. c. Interface types can be used to express common operations required by a service. d. Interfaces have both private and public methods.
Interfaces have both private and public methods.
Which Java class implements a file dialog box for selecting a file by a program with a graphical user interface? Select one: a. JOptionPane b. JFileOption c. JFilePane d. JFileChooser
JFileChooser
To override a superclass method in a subclass, the subclass method ____________.
Must use the same method name and the same parameter types.
If a subclass contains a method with the same name as a method in its superclass, but with different parameter types, the subclass is said to _______________ the method of the superclass.
Overload
_____________________ occurs when a single class has several methods with the same name but different parameter types.
Overloading
You have defined an interface called Sizable, and a class named PhotoImage which implements the Sizable interface. Which of the following is a valid Java statement for using the Sizable interface? Select one: a. Sizable newImage = (Sizeable) PhotoImage(); b. Sizable newImage = new PhotoImage(); c. PhotoImage newImage = new Sizeable(); d. Sizable newImage = new Sizeable();
Sizable newImage = new PhotoImage(); (Interface) object = new (Class)();
Which of the following statements about exception handling is correct? Select one: a. Statements that may cause an exception should be placed within a catch block. b. The main method of a Java program will handle any error encountered in the program. c. Statements that may cause an exception should be placed within the throws block. d. Statements that may cause an exception should be placed within the try block.
Statements that may cause an exception should be placed within the try block.
A class that represents the most general entity in an inheritance hierarchy is called a/an ________________________.
Superclass
If you have multiple classes in your program that have implemented the same interface in different ways, how is the correct method executed? Select one: a. The Java virtual machine must locate the correct method by looking at the class of the actual object. b. The compiler must determine which method implementation to use. c. The method must be qualified with the class name to determine the correct method. d. You cannot have multiple classes in the same program with different implementations of the same interface.
The Java virtual machine must locate the correct method by looking at the class of the actual object.
Consider the following code snippet: public class Inventory implements Measurable { .... double getMeasure() { return onHandCount: } } What is wrong with this code? Select one: a. The getMeasure() method must be declared as private. b. The getMeasure() method must include the implements keyword. c. The getMeasure() must be declared as public. d. The getMeasure() method must not have any code within it.
The getMeasure() must be declared as public.
Consider the following code snippet: public interface Sizable { ... double getSize() { return containerVolume; } } What is wrong with this code? Select one: a. The getSize() method must not have any code within it. b. The getSize() method must include the implements keyword. c. The getSize() method must be declared public. d. The getSize() method must be declared as private.
The getSize() method must not have any code within it.
To use an interface, a class header should include which of the following?
The keyword implements and the name of the interface.
What must a subclass do to modify a private superclass instance variable?
The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable.
Consider the following code snippet: Vehicle aVehicle = new Auto(4, "gasoline"); String s = aVehicle.toString(); Assume that the Auto class inherits from the Vehicle class, and neither class has an implementation of the toString() method. Which of the following statements is correct? Select one: a. The toString() method of the Object class will be used when the code is executed. b. The toString() method of the String class will be used when this code is executed. c. The code will not compile because there is no toString() method in the Vehicle class. d. The code will not compile because there is no toString() method in the Auto class.
The toString() method of the Object class will be used when the code is executed.
Consider the following code snippet: throw new IllegalArgumentException("This operation is not allowed"); Which of the following statements about this code is correct? Select one: a. This code constructs an object of type IllegalArgumentException and throws the object. b. This code throws an existing IllegalArgument object. c. This code constructs an object of type IllegalArgumentException and reserves it for future use. d. This code will not compile.
This code constructs an object of type IllegalArgumentException and throws the object.
Consider the following code snippet. Scanner inputFile = new Scanner("dataIn.txt"); Which of the following statements is correct? Select one: a. This code will not open a file named "dataIn.txt", but will treat the string "dataIn.txt" as an input value. b. This code will create a new file named "dataIn.txt". c. This code will open an existing file named "dataIn.txt" for reading. d. This code will open a file named "dataIn.txt" for writing.
This code will not open a file named "dataIn.txt", but will treat the string "dataIn.txt" as an input value.
Consider the following code snippet: System.out.println("%-12s%8.2f", description, totalPrice); Which of the following statements is correct? Select one: a. This code will produce 2 columns, with the description field left justified and the totalPrice field right justified. b. This code will produce 2 columns, with the description field right justified and the totalPrice field right justified. c. This code will produce 2 columns, with the description field right justified and the totalPrice field left justified. d. This code will produce 2 columns, with the description field left justified and the totalPrice field left justified.
This code will produce 2 columns, with the description field left justified and the totalPrice field right justified.
Consider the following code snippet. File inputFile = new File("dataIn.txt"); Scanner in = new Scanner(inputFile); while (in.hasNext()) { String in = in.next(); } Which of the following statements about this code is correct? Select one: a. This code will read in a word at a time from the input file. b. This code will read in the entire input file in one operation. c. This code will read in a line at a time from the input file. d. This code will read in a character at a time from the input file.
This code will read in a word at a time from the input file
Consider the following code snippet: Scanner in = new Scanner(. . .); while (in.hasNextLine() { String input = in.nextLine(); System.out.println(input); } Which of the following statements about this code is correct? Select one: a. This code will read in an entire line from the file in each iteration of the loop. b. This code will read in the entire contents of the file in a single iteration of the loop. c. This code will read in a single word from the file in each iteration of the loop. d. This code will read in a single character from the file in each iteration of the loop. Feedback
This code will read in an entire line from the file in each iteration of the loop
Which of the following code statements about exception handling is recommended by the textbook? Select one: a. All exceptions should be handled where they are first detected. b. All exceptions should be handled at the top of the chain of methods. c. Throw an exception only when the problem can be handled. d. Throw an exception as soon as a problem is detected, but only catch exceptions when the problem can be handled.
Throw an exception as soon as a problem is detected, but only catch exceptions when the problem can be handled.
The Java Compiler does not require that your program handle which type of exception? Select one: a. Checked b. Fatal c. Unchecked d. Server
Unchecked
You wish to implement a call back method for an object created from a system class that you cannot change. What approach does the textbook recommend to accomplish this? Select one: a. Create a new class that mimics the system class. b. Extend the system class. c. Use an inner class in the interface. d. Use a helper class that implements the callback method.
Use a helper class that implements the callback method.
Which of the following is a common use for inner classes? Select one: a. Methods that do not need to be kept secure. b. Utility classes that will be used by multiple classes within the program. c. Utility classes that do not need to be visible elsewhere in the program. d. Classes which many other classes will extend.
Utility classes that do not need to be visible elsewhere in the program.
Which of the following statements about converting between types is true? Select one: a. When you cast number types, you take a risk that an exception will occur. b. When you cast number types, you will not lose information. c. When you cast object types, you take a risk of losing information. d. When you cast object types, you take a risk that an exception will occur.
When you cast object types, you take a risk that an exception will occur.
Which of the following statements about inheritance is correct? Select one: a. You can always use a superclass object in place of a subclass object. b. You can always use a subclass object in place of a superclass object. c. A superclass inherits data and behavior from a subclass. d. A superclass inherits only behavior from a subclass.
You can always use a subclass object in place of a superclass object.
Which of the following is true regarding class and interface types? Select one: a. You can convert from a class type to any interface type that is in the same package as the class. b. You can convert from a class type to any interface type that the class implements. c. You can convert from a class type to any interface type that the class defines. d. You cannot convert from a class type to any interface type.
You can convert from a class type to any interface type that the class implements class > interface not interface > class
Which of the following can potentially be changed when implementing an interface? Select one: a. The parameters of a method in the interface. b. The name of the method in the interface. c. The return type of a method in the interface. d. You cannot change the name, return type, or parameters of a method in the interface.
You cannot change the name, return type, or parameters of a method in the interface.
Which of the following statements about reading and writing text files is correct? Select one: a. You use the Scanner class to read and write files. b. You use the PrintWriter class to read and write text files. c. You use the Scanner class to read text files and the PrintWriter class to write text files. d. You use the Scanner class to write text files and the PrintWriter class to read text files.
You use the Scanner class to read text files and the PrintWriter class to write text files.
Consider the following code snippet that appears in a subclass: public void deposit(double amount) { transactionCount = transactionCount + 1; deposit(amout); } Which of the following statements is true? a. This method will call itself. b. This method calls a public method in its subclass. c. This method calls a private method in its superclass. d. This method calls a private method in its subclass.
a. This method will call itself.
You are creating a class inheritance hierarchy about motor vehicles that will contain classes named Vehicle, Auto, and Motorcycle. Which of the following statements is correct? a. Vehicle should be the default class, while Auto and Motorcycle should be the subclasses. b. Vehicle should be the superclass, while Auto and Motorcycle should be the subclasses. Correct c. Vehicle should be the subclass, while Auto and Motorcycle should be the superclasses. d. Vehicle should be the subclass, while Auto and Motorcycle should be the default classes. Feedback
a. Vehicle should be the superclass, while Auto and Motorcycle should be the subclasses.
Which of the following indicates that a class named ClassA class is a superclass of the ClassB class? Select one: a. public class ClassB extends ClassA b. public class ClassB implements ClassA c. public class ClassA extends ClassB d. public class ClassA implements ClassB
a. public class ClassB extends ClassA
A method that has no implementation is called a/an _____________________ method.
abstract
Which of the following statements about comparing objects is correct? a. The equals method is used to compare whether two references are to the same object. b. The equals method is used to compare whether two objects have the same contents. c. The == operator is used to compare whether two objects have the same contents. d. The equals method and the == operator perform the same actions.
b. The equals method is used to compare whether two objects have the same contents. Correct
Consider the following code snippet: public class Coin { private String name; . . . public boolean equals(Object otherCoin) { return name.equals(otherCoin.name): } } What is wrong with this code? a. The return statement should use the == operator instead of the equals method. b. The parameter in the equals method should be declared as Coin otherCoin. c. otherCoin must be cast as a Coin object before using the name.equals(otherCoin.name) statement. d. There is nothing wrong with this code.
c. otherCoin must be cast as a Coin object before using the name.equals(otherCoin.name) statement.
Consider the following class hierarchy: public class Vehicle { private String type; public Vehicle(String atype) { type = atype; } public String getType() { return type; } } public class LandVehicle extends Vehicle { public LandVehicle(String atype) { ... } } public class Auto extends LandVehicle { public Auto(String atype) { ... } } Which of the following code fragments in NOT valid in Java? Select one: a. Vehicle myAuto = new Auto("sedan"); b. LandVehicle myAuto = new Auto("sedan"); c. Auto myAuto = new Auto("sedan"); d. LandVehicle myAuto = new Vehicle("sedan");
d. LandVehicle myAuto = new Vehicle("sedan"); (Subclass) name = new (SuperClass) "type"; Should be: (SuperClass) name = new (SubClass) "type";
To create a subclass, use the _________ keyword.
extends
public static void main(String[] args) throws FileNotFoundException { String inputFileName = "dataIn.txt"; String outputFileName = "dataOut.txt"; File inputFile = new File(inputFileName); Scanner in = __________________; . . . } Select one: a. new Scanner(inputFileName) b. new Scanner(outputFileName) c. new Scanner(inputFile) d. new Scanner(System.in)
new Scanner(inputFile)
Consider the following code snippet: public class Demo { Point [] p = new Point[4]; p[0] = new Colored3DPoint(4, 4, 4, Color.BLACK); p[1] = new ThreeDimensionalPoint(2, 3, 3); p[2] = new ColoredPoint(3, 3, Color.RED); p[3] = new Point(4, 4); for (int i = 0; i < p.length; i++) { String s = p[i].toString(); System.out.println("p[" + i + "] : " + s); } return; } The code is an example of _____.
polymorphism
To ensure that an instance variable can only be accessed by the class that declared it, the variable should be declared as ________________.
private
Which reserved word must be used to call a method of a superclass from within a method of the subclass with the same method name?
super
To test whether an object belongs to a particular class type , use ___________________.
the instanceof operator
The Scanner class's __________________ method is used to specify a pattern for word boundaries when reading text. Select one: a. useDelimiter() b. usePattern() c. setDelimiter() d. setPattern()
useDelimiter()