ProgrammingHardTCS NQT · Wipro NLTH
What does this method return?
What does this method return?
static int f() {
int x = 1;
try { return x; }
finally { x = 2; }
}- A. 1
- B. 2
- C. 3
- D. Compile error
Correct answer: A. 1
The return value is evaluated and saved before finally runs; changing x afterwards does not alter the saved value.
More OOP with C++ and Java questions
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { B() { f(); } virtual void f() { cout << "B"; } }; struct…
- A Java method has this structure. What happens when it is compiled? java try { reader.read(); // throws IOException } catch (Exception e)…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f() = 0; }; void B::f() { cout << "B"; }…
- What is printed? java String s = "a"; s.concat("b"); s += "c"; System.out.print(s);
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { void f(int) { cout << "Bi"; } }; struct D : B { void…
- What is printed? java class A { static { System.out.print("S"); } { System.out.print("I"); } A() { System.out.print("C"); } } // in main…