Quiz
1. An instance of ________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.
A). Error
2. The following code causes Java to throw ________.
int
number = Integer.MAX_VALUE + 1;
A).no exceptions
3. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
String s = "abc";
System.out.println(s.charAt(3));
}
}
A). StringIndexOutOfBoundsException
4. Analyze the following code:
public class Test {
public static void main(String[] args) {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
}
catch (Exception ex) {
System.out.println("NumberFormatException");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
}
}
A). The program has a compile error.
5. What is displayed on the console when running the following program?
public class Test {
public static void main(String[] args) {
try {
p();
System.out.println("After the method call");
}
catch (NumberFormatException ex) {
System.out.println("NumberFormatException");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
}
static void p() {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
}
}
A). The program displays NumberFormatException.
6. Which class do you use to read data from a text file?
A). Scanner
7. Which of the following statements are correct?
I:
try (PrintWriter
output = new PrintWriter("output.txt")) {
output.println("Welcome to Java");
}
II:
try (PrintWriter
output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
III:
PrintWriter output;
try (output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
IV:
try (PrintWriter
output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
finally {
output.close();
}
A). 1. I
2. II
3. III
8. Which of the following statements are correct?
. I:
try (PrintWriter
output = new PrintWriter("output.txt")) {
output.println("Welcome to Java");
}
II:
try (PrintWriter
output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
III:
PrintWriter output;
try (output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
IV:
try (PrintWriter
output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
finally {
output.close();
}
A). 1. I
2. II
3. III
9. The difference between a checked and an unchecked exception is
A). an unchecked exception requires no throws clause
10. If an exception is thrown and is not caught anywhere in the program, then the program terminates.
A). True
Comments
Post a Comment