Assignment Solution 5
Assignment Solution 5
PROGRAMMING IN JAVA
Assignment 05
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
a. I and II
b. II and III
c. I, II and III
Correct Answer:
Detailed Solution:
The finally block always executes except when the JVM exits using System.exit(). It can exist
without a catch block and may contain a return statement, though this is not recommended.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
interface A {
int x = 10;
void display();
}
class B implements A {
public void display() {
System.out.println("Value of x: " + x);
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
obj.display();
}
}
a. Value of x: 10
b. Value of x: 0
c. Compilation Error
d. Runtime Error
Correct Answer:
a. Value of x: 10
Detailed Solution:
Variables in interfaces are public, static, and final by default. Hence, the value of x is accessible
in the display method.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
class NPTEL {
public static void main(String[] args) {
try {
int a = 5;
int b = 0;
System.out.println(a / b);
} catch (ArithmeticException e) {
System.out.print("Error ");
} finally {
System.out.print("Complete");
}
}
}
a. 5 Complete
b. Error Complete
c. Runtime Error
d. Compilation Error
Correct Answer:
b. Error Complete
Detailed Solution:
An ArithmeticException is caught in the catch block, which prints “Error”. The finally block
executes afterward, printing “Complete”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
Which of the following is TRUE regarding abstract class and an interface in Java?
a. I, II and III
b. II only
c. I and II only
Correct Answer:
a. I, II and III
Detailed Solution:
Abstract classes can have constructors and concrete methods. Interfaces support multiple inheritance.
Before Java 8, interfaces could only contain abstract methods, but now they can include default and
static methods.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
a. NullPointerException
b. ArrayIndexOutOfBoundsException
c. IOException
d. ArithmeticException
Correct Answer:
c. IOException
Detailed Solution:
IOException is a checked exception, meaning it must be either caught or declared in the throws clause
of a method. The others are unchecked exceptions, which do not require explicit handling.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
a. try
b. catch
c. final
d. finally
Correct Answer:
c. final
Detailed Solution:
In Java, exceptions are handled using the try, catch, and finally blocks. The try block contains code
that might throw an exception, the catch block handles specific exceptions, and the finally block
executes regardless of whether an exception occurs.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
c. To catch an exception
Correct Answer:
Detailed Solution:
The throws keyword is used in a method signature to declare the exceptions that the method might
throw, alerting callers to handle or propagate these exceptions.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
Correct Answer:
Detailed Solution:
In Java, an interface can extend multiple interfaces. Interfaces can also contain public, static,
and final variables, but they cannot be instantiated directly.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
interface Demo {
void display();
}
a. Hello, NPTEL!
b. Compilation Error
c. Runtime Error
d. No Output
Correct Answer:
a. Hello, NPTEL!
Detailed Solution:
The Test class implements the Demo interface and provides a definition for the display method.
When display() is called, it prints “Hello, NPTEL!”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
interface Calculator {
void calculate(int value);
}
a. Square: 9 Cube: 9
b. Cube: 27 Square: 9
Correct Answer:
a. Square: 9 Cube: 9
Detailed Solution:
The Cube class overrides the calculate method of the Square class. In the Cube class’s calculate
method, super.calculate(value) is called, which executes the calculate method of the Square
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
class. First, "Square: 9" is printed by the superclass method. Then, the overridden method in Cube prints
"Cube: 9".