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; }
}
  1. A. 1
  2. B. 2
  3. C. 3
  4. 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

Exams that ask this topic