Chapter 11 Exceptions and Localization Flashcards
Review Questions
1.Which of the following can be inserted on line 8 to make this code compile? (Choose all that apply.)
~~~
7: public void whatHappensNext() throws IOException {
8: // INSERT CODE HERE
9: }
~~~
A. System.out.println(“it’s ok”);
B. throw new Exception();
C. throw new IllegalArgumentException();
D. throw new java.io.IOException();
E. throw new RuntimeException();
F. None of the above
2.Which statement about the following class is correct?
~~~
1: class Problem extends Exception {
2: public Problem() {}
3: }
4: class YesProblem extends Problem {}
5: public class MyDatabase {
6: public static void connectToDatabase() throw Problem {
7: throws new YesProblem();
8: }
9: public static void main(String[] c) throw Exception {
10: connectToDatabase();
11: }
12: }
~~~
A. The code compiles and prints a stack trace for YesProblem at runtime.
B. The code compiles and prints a stack trace for Problem at runtime.
C. The code does not compile because Problem defines a constructor.
D. The code does not compile because YesProblem does not define a constructor.
E. The code does not compile but would if Problem and YesProblem were switched on lines 6 and 7.
F. None of the above
- Which of the following are common types to localize? (Choose all that apply.)
A. Dates
B. Lambda expressions
C. Class names
D. Currency
E. Numbers
F. Variable names
4.What is the output of the following snippet, assuming a and b are both 0?
~~~
3: try {
4: System.out.print(a / b);
5: } catch (RuntimeException e) {
6: System.out.print(-1);
7: } catch (ArithmeticException e) {
8: System.out.print(0);
9: } finally {
10: System.out.print(“done”);
11: }
~~~
A. -1
B. 0
C. done-1
D. done0
E. The code does not compile.
F. An uncaught exception is thrown.
G. None of the above
5.Assuming the current locale uses dollars ($) and the following method is called with a double value of 100_102.2, which of the following values are printed? (Choose all that apply.)
~~~
public void print(double t) {
System.out.print(NumberFormat.getCompactNumberInstance().format(
t));
System.out.print(
NumberFormat.getCompactNumberInstance(
Locale.getDefault(), Style.SHORT).format(t));
System.out.print(NumberFormat.getCurrencyInstance().format(t));
}
~~~
A. 100
B. $100,000.00
C. 100K
D. 100 thousand
E. 100M
F. $100,102.20
G. None of the above
6.What is the output of the following code?
~~~
LocalDate date = LocalDate.parse(“2022-04-30”,
DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(date.getYear() + “ “
+ date.getMonth() + “ “+ date.getDayOfMonth());
~~~
A. 2022 APRIL 2
B. 2022 APRIL 30
C. 2022 MAY 2
D. The code does not compile.
E. A runtime exception is thrown.
7.What does the following method print?
~~~
11: public void tryAgain(String s) {
12: try (FileReader r = null, p = new FileReader(“”)) {
13: System.out.print(“X”);
14: throw new IllegalArgumentException();
15: } catch (Exception s) {
16: System.out.print(“A”);
17: throw new FileNotFoundException();
18: } finally {
19: System.out.print(“O”);
20: }
21: }
~~~
A. XAO
B. XOA
C. One line of this method contains a compiler error.
D. Two lines of this method contain compiler errors.
E. Three or more lines of this method contain compiler errors.
F. The code compiles, but a NullPointerException is thrown at runtime.
G. None of the above
8.Assume that all of the files mentioned in the answer choices exist and define the same keys.
Which one will be used to find the key in line 8?
~~~
6: Locale.setDefault(new Locale(“en”, “US”));
7: var b = ResourceBundle.getBundle(“Dolphins”);
8: System.out.println(b.getString(“name”));
~~~
A. Dolphins.properties
B. Dolphins_US.properties
C. Dolphins_en.properties
D. Whales.properties
E. Whales_en_US.properties
F. The code does not compile.
9.For what value of pattern will the following print <005.21> <008.49> <1,234.0>
?
~~~
String pattern = “_________________”;
var message = DoubleStream.of(5.21, 8.49, 1234)
.mapToObj(v -> new DecimalFormat(pattern).format(v))
.collect(Collectors.joining(“> <”));
System.out.println(“<”+message+”>”);
~~~
A. ##.#
B. 0,000.0#
C. #,###.0
D. #,###,000.0#
E. The code does not compile regardless of what is placed in the blank.
F. None of the above
10.Which scenario is the best use of an exception?
A. An element is not found when searching a list.
B. An unexpected parameter is passed into a method.
C. The computer caught fire.
D. You want to loop through a list.
E. You don’t know how to code a method.
11.Which of the following exceptions must be handled or declared in the method in which they are thrown? (Choose all that apply.)
~~~
class Apple extends RuntimeException {}
class Orange extends Exception {}
class Banana extends Error {}
class Pear extends Apple {}
class Tomato extends Orange {}
class Peach extends Throwable {}
~~~
A. Apple
B. Orange
C. Banana
D. Pear
E. Tomato
F. Peach
12.Which of the following changes, when made independently, would make this code compile? (Choose all that apply.)
~~~
1: import java.io.*;
2: public class StuckTurkeyCage implements AutoCloseable {
3: public void close() throws IOException {
4: throw new FileNotFoundException(“Cage not closed”);
5: }
6: public static void main(String[] args) {
7: try (StuckTurkeyCage t = new StuckTurkeyCage()) {
8: System.out.println(“put turkeys in”);
9: }
10: } }
~~~
A. Remove throws IOException from the declaration on line 3.
B. Add throws Exception to the declaration on line 6.
C. Change line 9 to } catch (Exception e) {}.
D. Change line 9 to } finally {}.
E. The code compiles as is.
F. None of the above
13.Which of the following are true statements about exception handling in Java? (Choose all that apply.)
A. A traditional try statement without a catch block requires a finally block.
B. A traditional try statement without a finally block requires a catch block.
C. A traditional try statement with only one statement can omit the {}.
D. A try-with-resources statement without a catch block requires a finally block.
E. A try-with-resources statement without a finally block requires a catch block.
F. A try-with-resources statement with only one statement can omit the {}.
14.Assuming -g:vars is used when the code is compiled to include debug information, what is the output of the following code snippet?
~~~
var huey = (String)null;
Integer dewey = null;
Object louie = null;
if(louie == huey.substring(dewey.intValue())) {
System.out.println(“Quack!”);
}
~~~
A. A NullPointerException that does not include any variable names in the stack trace
B. A NullPointerException naming huey in the stack trace
C. A NullPointerException naming dewey in the stack trace
D. A NullPointerException naming louie in the stack trace
E. A NullPointerException naming huey and louie in the stack trace
F. A NullPointerException naming huey and dewey in the stack trace
G. None of the above
15.Which of the following, when inserted independently in the blank, use locale parameters that are properly formatted? (Choose all that apply.)
~~~
import java.util.Locale;
public class ReadMap implements AutoCloseable {
private Locale locale;
private boolean closed = false;
@Override public void close() {
System.out.println(“Folding map”);
locale = null;
closed = true;
}
public void open() {
this.locale = _________________;
}
public void use() {
// Implementation omitted
}
}
~~~
A. new Locale(“xM”)
B. new Locale(“MQ”, “ks”)
C. new Locale(“qw”)
D. new Locale(“wp”, “VW”)
E. Locale.create(“zp”)
F. new Locale.Builder().setLanguage(“yw”).setRegion(“PM”
)
G. The code does not compile regardless of what is placed in the blank.