ProgrammingVery hardHCL · Cognizant GenC
What does this Java method do when called?
What does this Java method do when called?
static int f() {
try { throw new RuntimeException("a"); }
finally { return 5; }
}- A. Throws RuntimeException
- B. Compile error: missing return
- C. Returns 0
- D. Returns 5, silently discarding the exception
Correct answer: D. Returns 5, silently discarding the exception
A return inside finally overrides any pending exception, so the method completes normally with 5.
More OOP with C++ and Java questions
- What is printed? java String a = "ja" + "va"; String b = "java"; String c = "ja"; String d = c + "va"; System.out.print((a == b) + "," +…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct A { A() { cout << "A"; } }; struct B : virtual A { }; struct…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void f(int x = 1) { cout <<…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void p() { cout << "A"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct X { string n; X(const char s) : n(s) {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { A() { f(); } virtual void f() {…