24S - W05 - Final
24S - W05 - Final
24S - W05 - Final
PROGRAMMING IN JAVA
Assignment 5
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
Correct Answer:
d. All variables are sta�c and methods are public if interface is defined public.
Detailed Solu�on:
All methods and variables are implicitly public if interface is declared public.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
Correct Answer:
Detailed Solu�on:
Sta�c methods in an interface are accessed using the interface name, similar to sta�c methods in classes.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
interface Car {
int basePrice = 1000;
}
a. 1000
b. 2000
c. Compiler error
d. None of the above
Correct Answer:
c. Compiler error
Detailed Solu�on:
Java Interface treats its variables like constants. So, the classes implemen�ng Interfaces, can not reassign
values to the variables.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
What happens when we access the same variable defined in two interfaces implemented by the same
class?
a. Compila�on failure
b. Run�me Excep�on
c. The JVM is not able to iden�fy the correct variable
d. The interfaceName. variableName needs to be defined
Correct Answer:
Detailed Solu�on:
Explana�on: The JVM needs to dis�nctly know which value of variable it needs to use. To avoid confusion
to the JVM interfaceName.variableName is mandatory.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
class Main {
Correct Answer:
Detailed Solu�on:
In Java, the finally is always executed a�er the try-catch block. This block can be used to do the common
cleanup work.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
Correct Answer:
Detailed Solu�on:
If an excep�on is not caught in the catch block, it is propagated back to the caller method.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
class exception_handling {
a. Hello
b. World
c. HelloWorld
d. Hello World
Correct Answer:
b. World
Detailed Solu�on:
System.out.print() func�on first converts the whole parameters into a string and then prints, before
“Hello” goes to output stream 1 / 0 error is encountered which is cached by catch block prin�ng just
“World”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
Correct Answer:
Detailed Solu�on:
IndexOutOfBoundsExcep�on is raised by TRY block. Observe the order of catching the excep�ons. Always
catch a Subclass excep�on before a Superclass excep�on.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
What is the output of the Java code with FINALLY block and RETURN statement?
a. inside TRY
b. inside TRY
inside FINALLY
c. inside FINALLY
d. Compiler error
Correct Answer:
b. inside TRY
inside FINALLY
Detailed Solu�on:
Even if a RETURN statement is present at the last line of TRY block, the control is not returned to the
calling method. The JVM searches for the suitable FINALLY block and executes it before returning. So, the
FINALLY block has higher priority than a RETURN statement.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
a. To handle an excep�on
b. To catch an excep�on
c. To clean up resources a�er a try block
d. None of the above
Correct Answer:
Detailed Solu�on:
The purpose of the finally block in Java excep�on handling is to clean up resources a�er a try block. The
finally block is executed whether or not an excep�on is thrown in the try block. It is typically used to
release resources such as open files, database connec�ons, or network sockets that were acquired in the
try block. The finally block ensures that these resources are released even if an excep�on is thrown,
which helps prevent resource leaks and other issues. The finally block is op�onal, but it is good prac�ce
to use it when dealing with resources that need to be released.