ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
// static method of the main class:
static void down() { down(); }
// in main:
try { down(); }
catch (Exception e) { System.out.print("E"); }
finally { System.out.print("F"); }
System.out.print("X");- A. EF X
- B. EFX
- C. F is printed, then the program ends with a StackOverflowError
- D. FX
Correct answer: C. F is printed, then the program ends with a StackOverflowError
StackOverflowError is an Error, not an Exception, so catch (Exception) does not catch it. finally still runs, then the error propagates out of main and the last print never runs.
More OOP with C++ and Java questions
- What happens when this class is compiled? java class A { A() { this(1); } A(int x) { this(); } }
- What is printed? java class A { void f() { System.out.print("Af "); g(); } void g() { System.out.print("Ag "); } } class B extends A {…
- What is printed? java class Pt { int x; Pt(int x) { this.x = x; } public boolean equals(Object o) { return o instanceof Pt && ((Pt) o).x…
- What is printed? java interface Greeter { String name(); default String greet() { return "Hi " + name(); } } class G implements Greeter {…
- Which line fails to compile? java abstract class Shape { abstract double area(); } // in main: Shape a = new Shape(); // L1 Shape b = new…
- Which lines fail to compile? java class P { void f() { } static void g() { } } class C extends P { static void f() { } // L1 void g() { }…