ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
// static methods of the main class:
static int f() {
int x = 1;
try { return x; }
finally { x = 5; }
}
static int g() {
try { return 1; }
finally { return 2; }
}
// in main:
System.out.print(f() + " " + g());- A. 5 1
- B. 1 1
- C. 5 2
- D. 1 2
Correct answer: D. 1 2
In f, return x saves the value 1 before finally runs; changing x afterwards does not alter the saved int. In g, a return inside finally replaces the pending return 1, so g returns 2.
More OOP with C++ and Java questions
- What is printed? java System.out.print(1 + 2 + "3" + 4 + 5);
- What is printed? java // static methods of the main class: static void change(StringBuilder sb) { sb.append("X"); sb = new…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(1, 2, 3)); l.remove(1); System.out.print(l + " ")…
- What is printed? java List<Integer> list = new ArrayList<>(List.of(1, 2, 3)); for (Integer i : list) if (i == 2) list.remove(i)…
- What happens with this C++ code? cpp void f(int a, int b = 0) { cout << "two"; } void f(int a) { cout << "one"; } int main() { f(5); }
- What is printed? cpp struct Base { virtual void show() { cout << "Base "; } }; struct Derived : Base { void show() override { cout <<…