ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
// static methods of the main class:
static void f() {
try {
System.out.print("1");
throw new IllegalStateException();
} catch (IllegalArgumentException e) {
System.out.print("2");
} finally {
System.out.print("3");
}
System.out.print("4");
}
// in main:
try { f(); }
catch (RuntimeException e) { System.out.print("5"); }
System.out.print("6");- A. 1356
- B. 1346
- C. 12346
- D. 136
Correct answer: A. 1356
f prints 1 and throws IllegalStateException, which the IllegalArgumentException handler does not match. finally still prints 3, the exception leaves f so 4 is skipped, main's RuntimeException handler prints 5, and then 6.
More OOP with C++ and Java questions
- What is printed? java String s1 = "Java"; String s2 = "Ja" + "va"; String part = "Ja"; String s3 = part + "va"; System.out.print((s1 ==…
- What is printed? java class P { private void f() { System.out.print("P"); } void g() { f(); } } class C extends P { void f() {…
- What is printed? java class Animal { String speak() { return "animal"; } } class Dog extends Animal { String speak() { return "dog"; } }…
- What is printed? java class P { int id; P(int id) { this.id = id; } @Override public boolean equals(Object o) { return o instanceof P &&…
- What is printed? java System.out.print(5.0 / 0 + " "); System.out.print(0.0 / 0 + " "); try { System.out.print(5 / 0); } catch…
- What is printed? cpp struct R { int v = 0; R& inc() { v++; return this; } }; struct V { int v = 0; V inc() { v++; return this; } }; int…