Question 1
EasyWhat is an exception?
- A.
A special kind of loop
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 an exception?
A special kind of loop
An event during execution that disrupts the normal flow, such as dividing by zero
A comment in the code
A syntax error found by the compiler
Answer: B
Exceptions can be caught and handled so the program can recover or fail gracefully.
Which block contains code that might throw an exception?
try
throw
finally
catch
Answer: A
Risky code goes in try; handlers follow in catch (or except in Python).
When does a finally block run?
Never
Only when no exception occurs
Always, whether or not an exception occurs
Only when an exception occurs
Answer: C
It's used for clean-up such as closing files. (Only extreme cases like System.exit() skip it.)
Which keyword raises an exception in Java and C++?
throw
error
catch
raise
Answer: A
Python uses raise; Java and C++ use throw.
What is the output of this code?
try:
print(10 // 0)
except ZeroDivisionError:
print("Cannot divide by zero")
Infinity
Cannot divide by zero
Answer: B
10 // 0 raises ZeroDivisionError, which the except block catches.
Which of these is a checked exception in Java?
IOException
ArithmeticException
ArrayIndexOutOfBoundsException
NullPointerException
Answer: A
Checked exceptions must be caught or declared with throws. The others are unchecked RuntimeExceptions.
What does the 'throws' clause in a Java method declaration do?
Ends the method
Declares the checked exceptions the method may pass to its caller
Throws an exception immediately
Catches every exception
Answer: B
void read() throws IOException warns callers that they must handle IOException.
What is the purpose of a catch (or except) block?
To handle an exception thrown in the try block
To declare variables
To throw an exception
To end the program
Answer: A
Control jumps to the matching catch block when an exception occurs.
Which Python keyword is used where Java and C++ use 'catch'?
rescue
except
handle
catch
Answer: B
Python's form is try … except … else … finally.
Which Python keyword raises an exception?
throw
raise
except
error
Answer: B
For example: raise ValueError('bad input').
0
Program crashes