Question 61
HardWhat is the output of this code?
def f():
try:
:
(f())
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What is the output of this code?
def f():
try:
:
(f())
SyntaxError
finally
try
try finally
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def g():
for i in range(3):
try:
if i == 1:
continue
print(i, end=" ")
finally:
print("f", end=" ")
g()
0 f f 2 f
0 f 2 f
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
try:
try:
raise KeyError("k")
except KeyError as e:
raise ValueError("v") from e
except ValueError as e:
print(type(e.__cause__).__name__, e)
KeyError v
KeyError k
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class MyErr(Exception):
pass
r = []
for x in [1, 0, "a"]:
try:
r.append(10 // x)
except (ZeroDivisionError, TypeError) as e:
r.append(type(e).__name__[0])
else:
r.append("ok")
print(r)
[10, 'ok', 'Z', 'T', 'ok']
[10, 'ok', 'Z', 'T']
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
#include <stdexcept>
using namespace std;
#include <stdexcept>
struct G {
const char *n;
G(const char *s) : n(s) {}
~G() { cout << "~" << n << " "; }
};
void f() {
G a("a");
throw runtime_error("x");
G b("b");
}
int main() {
try {
G c("c");
f();
} catch (const exception &e) {
cout << "caught " << e.what();
}
}
caught x ~a ~c
~c ~a caught x
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
#include <stdexcept>
using namespace std;
#include <stdexcept>
int main() {
try {
throw out_of_range("idx");
} catch (const logic_error &e) {
cout << "logic ";
} catch (const out_of_range &e) {
cout << "range ";
}
cout << "done";
}
range done
logic range done
Sign up free to see the answer and a step-by-step explanation.
Why should C++ exceptions be caught by reference (catch (const std::exception &e))?
It allows rethrowing only
It makes them faster to throw
Catching by value slices derived exceptions and copies them
References are required by the compiler
Sign up free to see the answer and a step-by-step explanation.
What does 'throw;' (with no operand) do inside a catch block in C++?
Rethrows the current exception object unchanged
Does nothing
Ends the program
Throws a new empty exception
Sign up free to see the answer and a step-by-step explanation.
What are the three exception-safety guarantees in C++?
Low, medium and high
Checked, unchecked and errors
Try, catch and finally
Basic, strong and no-throw
Sign up free to see the answer and a step-by-step explanation.
In Java, what happens to an exception thrown inside a try-with-resources block when closing the resource also throws?
The close() exception is added as a suppressed exception to the original
The program exits
The close() exception replaces the original
Both are lost
Sign up free to see the answer and a step-by-step explanation.
f f f
0 1 2 f
ValueError v
None v
[10, 'Z', 'T']
[10, 'ok', 'T', 'Z']
~a ~b ~c caught x
~a ~c caught x
Compilation error
logic done