Deck 11: Exceptions, and Input Output Operations
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/77
Play
Full screen (f)
Deck 11: Exceptions, and Input Output Operations
1
The purpose of the try and catch blocks is to:
A) throw an exception so a compiler error will not be generated.
B) allow the code to jump over the catch block and continue running.
C) allow the code to handle exceptions so it can continue to run.
D) throw an exception so the catch block can identify the compiler error.
A) throw an exception so a compiler error will not be generated.
B) allow the code to jump over the catch block and continue running.
C) allow the code to handle exceptions so it can continue to run.
D) throw an exception so the catch block can identify the compiler error.
C
2
At run time, if a non-integer is entered, the call to the parseInt method will generate a(n):
A) NullPointerException.
B) ArithmeticException.
C) NumberFormatException.
D) IOException.
A) NullPointerException.
B) ArithmeticException.
C) NumberFormatException.
D) IOException.
C
3
Melanie writes some code with the public class EmailChecker. She uses try and catch blocks to catch thrown exceptions. A user enters this email address: Javalovre@sw.com. Which of the following is a likely outcome?
A) The program will catch the misspelling of "lover" and throw an IllegalEmailException.
B) The program will continue executing inside the try block.
C) The attempt to instantiate an object of the Exception class will generate a compiler error.
D) Any of the these would be a likely outcome.
A) The program will catch the misspelling of "lover" and throw an IllegalEmailException.
B) The program will continue executing inside the try block.
C) The attempt to instantiate an object of the Exception class will generate a compiler error.
D) Any of the these would be a likely outcome.
B
4
What is the advantage of declaring a constant as static?
A) It only writes to the file a variable that is declared as transient.
B) It saves disk space.
C) It allows the object to override an exception.
D) All of these are correct.
A) It only writes to the file a variable that is declared as transient.
B) It saves disk space.
C) It allows the object to override an exception.
D) All of these are correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following is the exception thrown by void writeObject( Object o )?
A) FileNotFoundException
B) ArithmeticException
C) ClassNotFoundException
D) None of these is correct.
A) FileNotFoundException
B) ArithmeticException
C) ClassNotFoundException
D) None of these is correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
6
ObjectOutputStream( OutputStream out ) is a:
A) class.
B) method.
C) constructor.
D) None of these is correct.
A) class.
B) method.
C) constructor.
D) None of these is correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
7
ObjectOutputStream is a:
A) class.
B) method.
C) constructor.
D) None of these is correct.
A) class.
B) method.
C) constructor.
D) None of these is correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
8
What makes an instance variable averageGPA a good candidate for being declared as transient?
A) You can easily reproduce its value.
B) Disk space is unimportant.
C) The instance variable will never be 0.
D) None of these is correct.
A) You can easily reproduce its value.
B) Disk space is unimportant.
C) The instance variable will never be 0.
D) None of these is correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
9
What is a token?
A) A comma that separates data items
B) An individual piece of data
C) A character with special meaning
D) None of these is correct.
A) A comma that separates data items
B) An individual piece of data
C) A character with special meaning
D) None of these is correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
10
Which package, introduced with Java 8, provides support for aggregate sequential and parallel operations on a collection of elements?
A) java.io
B) java.util.stream
C) java.lang
D) None of these is correct.
A) java.io
B) java.util.stream
C) java.lang
D) None of these is correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
11
PrintWriter( File f) throws a FileNotFoundException.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
12
A Stream can easily be constructed from an ArrayList.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
13
Large amounts of data reside in which of the following?
A) Data files and databases
B) Data files only
C) Databases only
D) None of these is correct.
A) Data files and databases
B) Data files only
C) Databases only
D) None of these is correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
14
Which of the following methods in the Exception classes returns void?
A) getMessage( )
B) printStackTrace( )
C) toString( )
D) setModel( )
A) getMessage( )
B) printStackTrace( )
C) toString( )
D) setModel( )
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
15
To correct this segment, line 12 should be changed to int n = 1;.
10 public static void main( String [ ] args )
11 {
12 int n;
13 String s = JOptionPane.showInputDialog( null,
14 "Enter an integer" );
15 System.out.println( "You entered " + s );
16
17 try
18 {
19 n = Integer.parseInt( s );
20 System.out.println( "Conversion was successful." );
21 }
10 public static void main( String [ ] args )
11 {
12 int n;
13 String s = JOptionPane.showInputDialog( null,
14 "Enter an integer" );
15 System.out.println( "You entered " + s );
16
17 try
18 {
19 n = Integer.parseInt( s );
20 System.out.println( "Conversion was successful." );
21 }
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
16
Your program will work as long as it does not generate compiler errors.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
17
FileNotFoundException is related to the Exception class because FileNotFoundException is a subclass of the IOException class, which is a subclass of the Exception class.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
18
Sydney accidentally entered .5 instead of 5 in a code asking that she input an integer. She did not get a compiler error, but the program did not run because it generated an exception, which terminated the program.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
19
When is it good practice to call the close method?
A) When many files are open, or the same file is open many times
B) When the file is associated with an input or output stream
C) When you have finished processing the data
D) All of these are correct.
A) When many files are open, or the same file is open many times
B) When the file is associated with an input or output stream
C) When you have finished processing the data
D) All of these are correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
20
Professor Simmons had a text file of students in his first-year economics class last semester. This semester, he has all new students. What action should he take to create a useful file for this semester?
A) Write to the existing text file.
B) Append the existing text file.
C) Scan the existing text file.
D) All of these are correct.
A) Write to the existing text file.
B) Append the existing text file.
C) Scan the existing text file.
D) All of these are correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
21
Replacing the contents of a file with new data is called appending the file.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
22
The PrintWriter class is designed for converting basic data types to characters and writing them to a text file.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
23
Tokens separate one delimiter from the next.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
24
It is preferable to place data items from a file in an ArrayList object instead of a fixed-length array when you do not know how many lines are in the file.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
25
Lambda expressions are often used with Streams.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
26
We can use a lambda expression as an alternative to implementing a functional interface.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
27
Assuming numberStream is a Stream containing doubles, the following code is adding all the doubles in the Stream that are greater than 5.0.
double sum =
numberStream.mapToDouble( item -> Double.parseDouble( item ) )
.filter( price -> price > 5.0 )
.sum( );
double sum =
numberStream.mapToDouble( item -> Double.parseDouble( item ) )
.filter( price -> price > 5.0 )
.sum( );
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
28
The String "[1,2,3,4]" is a valid JSON-formatted String.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
29
The URL can be used to access a file located remotely (on the Internet).
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
30
Why it is important to place the catch clause with the EOFException ahead of the catch clause with the IOException?
A) The EOFException catch block will never be reached if it is not first.
B) EOFException is a subclass of IOException.
C) We should arrange the catch blocks to handle the specialized exceptions first, followed by more general exceptions.
D) All of these are correct.
A) The EOFException catch block will never be reached if it is not first.
B) EOFException is a subclass of IOException.
C) We should arrange the catch blocks to handle the specialized exceptions first, followed by more general exceptions.
D) All of these are correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
31
When reading objects from a file, until we have reached the end of the file, the condition for the while loop inside a try block is:
A) while ( for ).
B) while ( catch ).
C) for (while).
D) while ( true ).
A) while ( for ).
B) while ( catch ).
C) for (while).
D) while ( true ).
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
32
What is a delimiter?
A) A comma, space, or other character that separates tokens
B) A special character with specific functions
C) A code that throws an exception
D) None of these is correct.
A) A comma, space, or other character that separates tokens
B) A special character with specific functions
C) A code that throws an exception
D) None of these is correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
33
To read from the file favoriteMovies, what information do you need?
A) A survey of the class to identify their favorite movies
B) How the data were written to the file
C) The number of characters used in the file
D) None of these is correct.
A) A survey of the class to identify their favorite movies
B) How the data were written to the file
C) The number of characters used in the file
D) None of these is correct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
34
Why is it beneficial to be able to extend an Exception class?
A) It overrides an error message with an exception.
B) It allows programmers to associate a specific error message with an exception.
C) It allows coding of the exception without having to code the constructor.
D) It passes the code from the superclass to the derived constructor.
A) It overrides an error message with an exception.
B) It allows programmers to associate a specific error message with an exception.
C) It allows coding of the exception without having to code the constructor.
D) It passes the code from the superclass to the derived constructor.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
35
Which of the following is the best constructor for reading objects from a file?
A) FileOutputStream( String filename )
B) ObjectOutputStream( OutputStream in )
C) ObjectInputStream( InputStream in )
D) Object readObject( )
A) FileOutputStream( String filename )
B) ObjectOutputStream( OutputStream in )
C) ObjectInputStream( InputStream in )
D) Object readObject( )
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
36
When we open a file for writing and the file already exists:
A) there will be a compiler error.
B) there will be a runtime error.
C) the file contents will be overwritten.
D) we will be adding data at the end of the file.
A) there will be a compiler error.
B) there will be a runtime error.
C) the file contents will be overwritten.
D) we will be adding data at the end of the file.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
37
When we open a file for appending and the file already exists:
A) there will be a compiler error.
B) there will be a runtime error.
C) the file contents will be overwritten.
D) we will be adding data at the end of the file.
A) there will be a compiler error.
B) there will be a runtime error.
C) the file contents will be overwritten.
D) we will be adding data at the end of the file.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
38
Which method of the class Scanner do we use to set the delimiter when parsing a string?
A) parse
B) setDelimiter
C) delimiter
D) useDelimiter
A) parse
B) setDelimiter
C) delimiter
D) useDelimiter
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
39
Which technique is often used when processing a Stream-in particular, calling several methods in order to process the data in the Stream?
A) Looping
B) Iteration
C) Chaining
D) Recursion
A) Looping
B) Iteration
C) Chaining
D) Recursion
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
40
The filter method of the DoubleStream interface has the following API, DoubleStream filter( DoublePredicate predicate ). What is DoublePredicate?
A) A class
B) An interface
C) A method
D) An instance variable
A) A class
B) An interface
C) A method
D) An instance variable
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
41
What method of the Collection class can we use to convert an ArrayList to a Stream?
A) convertToStream
B) toStream
C) streamIt
D) stream
A) convertToStream
B) toStream
C) streamIt
D) stream
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
42
Which of the following is not using JSON formatting?
A) { 1,2,3,4 }
B) ["A","B","C"]
C) {"M":"Monday","T":"Tuesday"}
D) {"grades":[80,90,100]}
A) { 1,2,3,4 }
B) ["A","B","C"]
C) {"M":"Monday","T":"Tuesday"}
D) {"grades":[80,90,100]}
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
43
The return value for all Scanner methods is boolean.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
44
It is possible to code a user-defined exception class.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
45
An exception can only be thrown by a method that is not a constructor.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
46
The Java package that provides most classes for reading from and writing to files is java.io.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
47
The print and println methods of the PrintWriter class allow writing basic data types to a file.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
48
The header of a method that potentially generates an exception when called contains the keyword:
A) generates.
B) throws.
C) excepts.
A) generates.
B) throws.
C) excepts.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
49
To handle an exception, we use a _________ construct.
A) try and catch
B) try and finally
C) catch and finally
A) try and catch
B) try and finally
C) catch and finally
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
50
In which package is the class IOException?
A) java.io
B) java.exception
C) java.util
A) java.io
B) java.exception
C) java.util
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
51
If you try to use the Scanner constructor to open a file that does not exist, what type of exception will be thrown?
A) ScannerException
B) FileNotFoundException
C) FileException
D) NotFoundException
A) ScannerException
B) FileNotFoundException
C) FileException
D) NotFoundException
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
52
When we open a file for writing and the file does not exist, what will happen?
A) There will be a compiler error.
B) There will be a runtime error.
C) The file will be created.
A) There will be a compiler error.
B) There will be a runtime error.
C) The file will be created.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
53
When we open a file for appending and the file does not exist, what will happen?
A) There will be a compiler error.
B) There will be a runtime error.
C) The file will be created.
A) There will be a compiler error.
B) There will be a runtime error.
C) The file will be created.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
54
To read objects from a file, which class do we use?
A) InputObjectStream
B) ObjectInputStream
C) ObjectStream
D) ObjectInput
A) InputObjectStream
B) ObjectInputStream
C) ObjectStream
D) ObjectInput
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
55
Which classes can we use to create a Stream from a file in a single statement?
A) Path and File
B) Path and Files
C) Paths and Files
D) Path and Files
A) Path and File
B) Path and Files
C) Paths and Files
D) Path and Files
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
56
The superclass of all exception classes is the Exception class.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
57
Code that could generate an unchecked exception must be coded with a try and catch block.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
58
Code that could generate a checked exception must be coded with a try and catch block or placed inside a method that throws that exception itself.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
59
A variable declared inside a try block is still in scope after we exit that try block.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
60
It is legal to have more than one catch block to match a try block.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
61
Handling a possible illegal operation with a try and catch block will make the code run faster than handling it with a simple if/else construct.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
62
Java does not allow objects to be written to or read from a file.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
63
When an object is written to a file, transient instance variables of that object are not written to the file.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
64
Java provides classes and methods to read data from a remote file (located somewhere on the Internet).
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
65
Exceptions occur at __________.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
66
The class Scanner can be used to __________ a string.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
67
For an object of a class to be written to a file, that class must implement the __________ interface.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
68
The __________ class encapsulates the concept of a Uniform Resource Locator.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
69
__________ formatting is made up of two data structures: key/value pairs and arrays.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
70
Because the readObject method returns a generic object, what must the returned object be?
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
71
Tyler wrote a code that includes multiple catch blocks to catch files not found and characters that are not integers. Predict what will happen if a user enters an invalid file and also enters a 1.5 for the integer.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
72
How can the methods of the Exception class be useful?
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
73
When we read objects from a file using the readObject method, what happens when the end of the file is reached?
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
74
You want to handle a suspected exception in your program, so you insert the following try and catch blocks. However, the program generated a compiler error. Improve this program to avoid that problem.
1
2 try
3 {
4 n = Integer.parseInt( s );
5 System.out.println( "Conversion was successful." );
6 catch ( NumberFormatException nfe )
7 System.out.println( "Sorry. incompatible data." );
8 System.out.println( "\nOutput from getMessage: \n"
9 + nfe.getMessage( ) );
10
11 System.out.println( "\nOutput from toString: \n"
12 + nfe.toString( ) );
13 System.out.println( "\nOutput from printStackTrace: " );
14 nfe.printStackTrace( );
15 }
1
2 try
3 {
4 n = Integer.parseInt( s );
5 System.out.println( "Conversion was successful." );
6 catch ( NumberFormatException nfe )
7 System.out.println( "Sorry. incompatible data." );
8 System.out.println( "\nOutput from getMessage: \n"
9 + nfe.getMessage( ) );
10
11 System.out.println( "\nOutput from toString: \n"
12 + nfe.toString( ) );
13 System.out.println( "\nOutput from printStackTrace: " );
14 nfe.printStackTrace( );
15 }
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
75
Suppose you write a program using the Scanner class to read data from a text file. The file is not found, but the code instantiating the Scanner was inside a try block. Predict what will happen, and explain why this is advantageous.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
76
Evaluate what will happen if you run a program that attempts integer division by 0.
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck
77
What is the purpose of the following code segment?
13 String flightRecord1 = "AA123,BWI,SFO,235,239.5";
14 Scanner parse = new Scanner( flightRecord1 );
15 // set the delimiter to a comma
16 parse.useDelimiter( "," );
13 String flightRecord1 = "AA123,BWI,SFO,235,239.5";
14 Scanner parse = new Scanner( flightRecord1 );
15 // set the delimiter to a comma
16 parse.useDelimiter( "," );
Unlock Deck
Unlock for access to all 77 flashcards in this deck.
Unlock Deck
k this deck