Intermediate Java Midterm 2
Files whose contents must be handled as sequences of binary digits are called:
binary files
A ___________ block should immediately follow a try block
catch
A _________ block executes regardless of whether an exception occurs.
finally
You wish to use the Scanner class's nextDouble() method to read in floating point numbers. To avoid exceptions that would occur if the input is not a floating point number, you should use the ____ method.
hasNextDouble()
If you write a Student class, what must you do in order to write Student objects to a file using an ObjectOutputStream?
implement Serializable
A class that uses an interface must use the keyword:
implements
When defining your own exception class, you extend an existing exception class. This is an example of:
inheritance
In this main method, If the value inputted is 23e4r8, what does the program do? public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int months = 0; double input = 0; double totalIncome = 0; while (input >= 0) { try { input = keyboard.nextDouble(); totalIncome += input ; months++; } catch(InputMismatchException e) { System.out.println("Non-numeric data encountered , please enter a number"); keyboard.nextLine(); input = keyboard.nextDouble(); } } }
input will cause an InputMismatchException , the catch clause will be executed, then the program will continue by asking for the next input value
An interface type does not have ____.
instance fields
All of the following are methods of the ArrayList class except:
length()
The method that must be implemented in a class using the Comparable interface is:
public int compareTo(Object other)
Which is the correct syntax for placing the string "boat" into an ArrayList name recVehicles in position 3 without losing any other data in the ArrayList?
recVehicles.add(3, "boat");
What are some methods in the RandomAccessFile class?
seek, getFilePointer, and length
If a method throws an exception, and the exception is not caught inside the method, then the method invocation:
terminates
Try blocks contain code that could possibly:
throw an exception
The execution of a throw statement:
throws an exception
Assume we have a RandomAccessFile object, file, which stores a set of records of class BankInfo, each record size is BankInfo.RECORD_SIZE . If the file pointer is currently on the 11th record, which expression gets to the offset of the beginning of the 12th record?
BankInfo.RECORD_SIZE+file.getFilePointer()
Based on the code below, which of the following statements is correct? public interface Measurable { double getMeasure(); } public class Coin implements Measurable { public double getMeasure() { return value; } ... } public class DataSet { ... public void add() { ... } } public class BankAccount { ... public void add() { ... } }
Coin dime = new Coin(0.1, "dime"); Measurable x = dime;
A(n) ________ is a section of code that gracefully responds to exceptions when they are thrown.
Exception handler
The keyword ___________ can be used to place a bound on a type parameter.
Extends
T/F An ArrayList object has a fixed size.
False
T/F An interface specifies the method headings and body.
False
T/F Exceptions that must follow the Catch or Declare Rule are often called unchecked exceptions.
False
T/F Inner and outer classes do not have access to each other's private members.
False
T/F When an exception is thrown, the code in the surrounding try block continues executing and then the catch block begins execution.
False
T/F You can use any primitive type as the base type of an ArrayList class.
False
Class and method definitions that include parameters for types are called:
Generics
Consider the following code snippet: PrintWriter out = new PrintWriter("output.txt"); Which of the following statements about the PrintWriter object is correct?
If a file named "output.txt" already exists, existing data will be deleted before data is added to the file.
What does the following statement do when it is executed FileInputStream infile = new FileInputStream("InputFile.txt");
It creates a FileInputStream object and opens an input file called InputFile.txt
What occurs when an ObjectInputStream Objects that is reading data from a file reaches the end of a file?
It will return null
Given a RandomAccessFile which stores a set of records of class MyData, with the size of each record designated by MyData.RECORD_SIZE , which expression gets to the offset of the beginning of the 187th record?
MyData.RECORD_SIZE*186
Which class is used for input of binary data?
ObjectInputStream
Consider the following code snippet. FileInputStream inputFile = new FileInputStream("input.txt"); You wish to read the contents of this file using a Scanner object. Which of the following is the correct syntax for doing this?
Scanner in = new Scanner(inputFile);
Consider the following code snippet: public double[] readInputFile(String filename) throws IOEXception { File inputFile = new File(filename); Scanner in = new Scanner(inputFile); try { readData(in); return data; } catch (NoSuchElementException exception) { . . . } finally { inputFile.close(); } }
The file will be closed whether or not an exception occurs.
Consider the following code snippet: public double[] readInputFile(String filename) throws IOEXception { File inputFile = new File(filename); Scanner in = new Scanner(inputFile); try { readData(in); return data; } finally { inputFile.close(); } }
This method will pass any IOException type errors back to the caller.
If a method does not catch a checked exception, then it must at least warn programmers that any invocation of the method might possibly throw the exception. This warning is called a/an:
Throws clause
T/F A class definition can have more than one type parameter
True
T/F A class may implement multiple interfaces.
True
T/F ArrayList objects do not have the array square-bracket notation
True
T/F Java source code that contains a class with an inner class, when compiled, will produce a separate .class file for the inner class.
True
T/F The finally block contains code to be executed whether or not an exception is thrown in a try block.
True
T/F The throw operator causes a change in the flow of control.
True
T/F When your program is finished writing to a file, it should close the stream connected to that file.
True
The compareTo method should return _____________ if the calling object equals the parameter.
Zero