IS5103 Lesson-11

1.Which statement about the following class is correct?
class Problem extends Exception {
public Problem() {}
}
class YesProblem extends Problem {}
public class MyDatabase {
public static void connectToDatabase() throw Problem {
throws new YesProblem();
}
public static void main(String[] c) throw Exception {
connectToDatabase();
}
}
A) None of these

2.Which of the following exceptions must be handled or declared in the
method in which they are thrown?
class Apple extends RuntimeException {}
class Orange extends Exception {}
class Banana extends Error {}
class Pear extends Apple {}
class Tomato extends Orange {}
class Peach extends Throwable {}
Each correct answer represents a complete solution. Choose all that apply.
A) Orange
Tomato
Peach

3.Which of these statements can fill in the blank so that the Helper class
compiles successfully?
public class Helper {
public static<U extends Exception>
void printException(U u) {
System.out.println(u.getMessage());
}
public static void main(String[] args) {
Helper.________________________;
} }
Each correct answer represents a complete solution. Choose all that apply.
A) printException(new FileNotFoundException(“A”))
printException(new Exception(“B”))
<NullPointerException>printException(new NullPointerException
(“D”))

4.Which of the following can be inserted on line 2 to make this code
compile?
public void whatHappensNext() throws IOException {
// INSERT CODE HERE
}
Each correct answer represents a complete solution. Choose all that apply.
A) System.out.println(“it’s ok”);
throw new IllegalArgumentException();
throw new java.io.IOException();
throw new RuntimeException();

5.Which scenario is the best use of an exception?
A) An unexpected parameter is passed into a method.

6.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 naming dewey in the stack trace

7.What is printed by the following program?
public class DriveBus {
public void go() {
System.out.print(“A”);
try {
stop();
} catch (ArithmeticException e) {
System.out.print(“B”);
} finally {
System.out.print(“C”);
}
System.out.print(“D”);
}
public void stop() {
System.out.print(“E”);
Object x = null;
x.toString();
System.out.print(“F”);
}
public static void main(String n[]) {
new DriveBus().go();
} }
A) AEC followed by a stack trace

8.Which lines can fill in the blank to make the following code compile?
void rollOut() throws ClassCastException {}
public void transform(String c) {
try {
rollOut();
} catch (IllegalArgumentException | ______) {
}
}
Each correct answer represents a complete solution. Choose all that apply.
A) Error b
ClassCastException f

9.Which of the following are true statements about exception handling in
Java?
Each correct answer represents a complete solution. Choose all that apply.
A) A traditional try statement without a catch block requires a finally
block
A traditional try statement without a finally block requires a catch
block.

10.What is the output of the following snippet, assuming a and b are
both 0?
try {
System.out.print(a / b);
} catch (RuntimeException e) {
System.out.print(-1);
} catch (ArithmeticException e) {
System.out.print(0);
} finally {
System.out.print(“done”);
}
A) The code does not compile.

11.What does the following method print?
public void tryAgain(String s) {
try (FileReader r = null, p = new FileReader(“”)) {
System.out.print(“X”);
throw new IllegalArgumentException();
} catch (Exception s) {
System.out.print(“A”);
throw new FileNotFoundException();
} finally {
System.out.print(“O”);
}}
A) Three or more lines of this method contain compiler errors

12.Which changes, when made independently, allow the following
program to compile?
public class AhChoo {
static class SneezeException extends Exception {}
static class SniffleException extends SneezeException {}
public static void main(String[] args) {
try {
throw new SneezeException();
} catch (SneezeException | SniffleException e) {
} finally {}
} }
A) Change line 7 to } catch (SneezeException e) {.

13.Which of the following changes, when made independently, would make this
code compile?
import java.io.*;
public class StuckTurkeyCage implements AutoCloseable {
public void close() throws IOException {
throw new FileNotFoundException(“Cage not closed”);
}
public static void main(String[] args) {
try (StuckTurkeyCage t = new StuckTurkeyCage()) {
System.out.println(“put turkeys in”);
}
}}
Each correct answer represents a complete solution. Choose all that apply.
A) Add throws Exception to the declaration on line 6.
Change line 9 to } catch (Exception e) {}.

14.What is the output of the following code?
import java.io.*;
public class FamilyCar {
static class Door implements AutoCloseable {
public void close() {
System.out.print(“D”);
} }
static class Window implements Closeable {
public void close() {
System.out.print(“W”);
throw new RuntimeException();
} }
public static void main(String[] args) {
var d = new Door();
try (d; var w = new Window()) {
System.out.print(“T”);
} catch (Exception e) {
System.out.print(“E”);
} finally {
System.out.print(“F”);
} } }
A) TWDEF

15.A class that implements _____ may be in a try-withresources statement.
Each correct answer represents a complete solution. Choose all that apply.
A) AutoCloseable
Closeable

16.What is the output of the following program?
public class SnowStorm {
static class WalkToSchool implements AutoCloseable {
public void close() {
throw new RuntimeException(“flurry”);
} }
public static void main(String[] args) {
WalkToSchool walk1 = new WalkToSchool();
try (walk1; WalkToSchool walk2 = new WalkToSchool()) {
throw new RuntimeException(“blizzard”);
} catch(Exception e) {
System.out.println(e.getMessage()
” ” + e.getSuppressed().length);
}
walk1 = null;
} }
A) None of these

17.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) A runtime exception is thrown.

18.For what value of pattern will the following code snippet 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) #,###,000.0#

19.Which of the following can be inserted into the blank to allow the
code to compile and run without throwing an exception?
var f = DateTimeFormatter.ofPattern(“hh o’clock”);
System.out.println(f.format(_____.now()));
A) None of these

20.What is the output of the following code?
try {
LocalDateTime course = LocalDateTime.of(2022, 4, 5, 12, 30, 20);
System.out.print(book.format(DateTimeFormatter.ofPattern(“m”)));
System.out.print(book.format(DateTimeFormatter.ofPattern(“z”)));
System.out.print(DateTimeFormatter.ofPattern(“y”).format(book));
} catch (Throwable e) {}
A) 30

21.Which of the following are common types to localize?
Each correct answer represents a complete solution. Choose all that apply.
A) Dates
Currency
Numbers

22.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?
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));
}
Each correct answer represents a part of the solution. Choose all that apply.
A) 100K
$100,102.20

23.Which of the following statements, when inserted independently
in the blank, use locale parameters that are properly formatted?
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
}
}
Each correct answer represents a complete solution. Choose all that apply.
A) new Locale(“qw”)
new Locale(“wp”, “VW”)

24.Assuming U.S. currency is in dollars ($) and German currency is
in euros (€), what is the output of the following program?
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Locale.Category;
public record Wallet(double money) {
private String openWallet() {
Locale.setDefault(Category.DISPLAY,
new Locale.Builder().setRegion(“us”).build());
Locale.setDefault(Category.FORMAT,
new Locale.Builder().setLanguage(“en”).build());
return NumberFormat.getCurrencyInstance(Locale.GERMANY)
.format(money);
}
public void printBalance() {
System.out.println(openWallet());
}
public static void main(String… unused) {
new Wallet(2.4).printBalance();
} }
A) 2,40 €

25.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?
Locale.setDefault(new Locale(“en”, “US”));
var b = ResourceBundle.getBundle(“Dolphins”);
System.out.println(b.getString(“name”));
A) Dolphins_en.properties

26.Which of the following statements about resource bundles are
correct?
Each correct answer represents a complete solution. Choose all that apply.
A) I.Changing the default locale lasts for only a single run of the
program.
II. It is possible to use a resource bundle for a locale without specifying
a default locale.

27.Suppose that we have the following three properties files and
code. Which bundles are used on lines 15 and 16, respectively?
Dolphins.properties
name=The Dolphin
age=0
Dolphins_en.properties
name=Dolly
age=4
Dolphins_fr.properties
name=Dolly
var fr = new Locale(“fr”);
Locale.setDefault(new Locale(“en”, “US”));
var b = ResourceBundle.getBundle(“Dolphins”, fr);
b.getString(“name”);
b.getString(“age”);
A) Dolphins_fr.properties and Dolphins.properties

Other Links:



Statistics Quiz




Networking Quiz




See other websites for quiz:



Check on QUIZLET




Check on CHEGG

Leave a Reply

Your email address will not be published. Required fields are marked *