ProgrammingModerateAccenture · Cognizant GenC
What is printed?
What is printed?
static int f() {
try { return 1; }
finally { System.out.print("F"); }
}
public static void main(String[] a) { System.out.print(f()); }- A. 1F
- B. 1
- C. F1
- D. F
Correct answer: C. F1
The finally block runs before the method actually returns, so F is printed first and then the returned value 1 is printed.
More OOP with C++ and Java questions
- In Java, an interface declares int MAX = 10;. What happens if an implementing class executes MAX = 20; ?
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct A { A() { cout << "c"; } A(const A&) { cout << "k"; } ~A() {…
- What is printed? java class P { static String w() { return "P"; } } class Q extends P { static String w() { return "Q"; } } // in main: P…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f(int x = 1) { cout << "B" << x; } }; struct…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void…
- What is printed? java class A { int x = 1; } class B extends A { int x = 2; } // in main: A a = new B(); System.out.print(a.x + " " + ((B)…