NPTEL Programming In Java Week 5 Assignment Answers 2024
Q1. Which of the following is an incorrect statement about interfaces?
a. Interfaces specifies what class must do but not how it does.
b. Interfaces are specified public if they are to be accessed by any code in the program.
c. All variables in interface are implicitly final and static.
d. All variables are static and methods are public if interface is defined public.
Answer:- d
Explanation: While it’s true that all variables in an interface are implicitly public
, static
, and final
, and methods are public
and abstract
, the interface’s visibility (public or package-private) does not impact the implicit public
nature of methods or static
nature of variables. Option d wrongly implies this dependency.
Q2. How do you access a static method of an interface?
a. Using the interface name
b. Using the method name directly
c. Through an object of the interface
d. Through an implementation class
Answer:- a
Explanation: Static methods in an interface are accessed using the interface name, just like static methods in a class.
Q3. What is the output of the below Java program with an Interface?
a. 1000
b. 2000
c. Compiler error
d. None of the above
Answer:- c
Explanation: Interfaces cannot contain implemented code (except static and default methods). If the code violates rules (like accessing undefined members), it leads to a compilation error.
Q4. What happens when we access the same variable defined in two interfaces implemented by the same class?
a. Compilation failure
b. Runtime Exception
c. The JVM is not able to identify the correct variable
d. The interfaceName.variableName needs to be defined
Answer:- d
Explanation: If two interfaces contain variables with the same name, ambiguity arises. To resolve it, you must specify the variable with its interface name.
Q5. Predict the output of following Java program
a. Got the Test Exception
Inside finally block
b. Got the Test Exception
c. Inside finally block
d. Compiler Error
Answer:- a
Explanation: If an exception is caught, the catch block runs and the finally block also runs if present. Hence, both messages are printed.
Q6. What happens if an exception is not caught in the catch block?
a. The finally block handles it
b. The exception is ignored
c. The exception is thrown to the caller method
d. The program terminates immediately
Answer:- c
Explanation: If an exception is not caught, it propagates back to the method that called the current method, unless handled there.
Q7. What will be the output of the following Java program?
a. Hello
b. World
c. HelloWorld
d. Hello World
Answer:- b
Explanation: The logic in the program likely leads to printing “World” only. Without code it’s assumed from output pattern.
Q8. What is the output of the below Java code with Exceptions?
a. Index 4 out of bounds for length 3
b. Index 4 out of bounds for length 3 Some exception
c. Some exception
d. No exception occurs
Answer:- a
Explanation: Accessing index 4 in an array of length 3 throws an ArrayIndexOutOfBoundsException
. So the exception message appears.
Q9. 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
Answer:- b
Explanation: Even if a return
statement is executed inside a try block, the finally
block still executes before the method exits.
Q10. What is the purpose of the finally block in Java exception handling?
a. To handle an exception
b. To catch an exception
c. To clean up resources after a try block
d. None of the above
Answer:- c
Explanation: The finally
block ensures that resource cleanup (like closing file streams or releasing connections) happens, regardless of whether an exception occurred or not.