Week 05 Assignment Solution
Week 05 Assignment Solution
PROGRAMMING IN JAVA
Assignment 5
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
a. IntegerOutOfBoundExcep on
b. IntegerFormatExcep on
c. Arithme cExcep on
d. NumberFormatExcep on
Correct Answer:
d. NumberFormatExcep on
parseInt() method parses input into integer. This method will throw NumberFormatExcep on.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
interface P {
String p = "PPPP";
String methodP();
}
interface Q extends P {
String q = "QQQQ";
String methodQ();
}
class R implements P, Q {
public String methodP() {
return q + p;
}
public String methodQ() {
return p + q;
}
}
public class Main{
public static void main(String[] args) {
R r = new R();
System.out.println(r.methodP());
System.out.println(r.methodQ());
}
}
a. QQQQPPPP
PPPPQQQQ
b. PPPPQQQQ
QQQQPPPP
c. PPPPPPPP
QQQQQQQQ
d. Compila on error
Correct Answer:
a. QQQQPPPP
PPPPQQQQ
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
class A implements B {
public int methodB(int i) {
return i = +i * i;
}
}
interface B {
int methodB(int i);
}
System.out.println(b.methodB(2));
}
}
a. 4
b. 6
c. 2
d. 8
Correct Answer:
a. 4
QUESTION 4:
A method that potentially generates a checked exception must include this keyword in its method
signature:
a. throw
b. extend
c. throws
d. extends
Correct Answer:
c. throws
Any Java class that generates a checked excep on and does not handle it internally must use the throws
keyword to alert other methods of its instability.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
a. The finally block is only executed if an excep on is thrown in the try block
b. The finally block is only executed if an excep on is thrown in the catch block
c. The finally block is only executed if an excep on is not thrown in the try or catch block
d. The finally block is executed regardless of whether an excep on is thrown in the try or catch
block
Correct Answer:
d. The finally block is executed regardless of whether an excep on is thrown in the try or catch
block
The finally block always executes, regardless of whether or not Java throws an excep on in the try block.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
a. A try block can have many catch blocks but only one finally block
b. A try block can have many catch blocks and many finally blocks
c. A try block must have one finally block for each catch block
d. A try block must have at least one catch block to have a finally block
Correct Answer:
a. A try block can have many catch blocks but only one finally block
A try block can only have one finally block. However, mul ple catches are allowed. It is even allowable to
have no catch blocks and only a finally block.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
class Output {
public static void main(String args[]) {
try {
int a = 0;
int b = 5;
int c = b / a;
System.out.print("Hello");
} catch (Exception e) {
System.out.print("World");
} finally {
System.out.print("World");
}
}
}
a. Hello
b. World
c. HelloWOrld
d. WorldWorld
Correct Answer:
d. WorldWorld
finally block is always executed a er tryblock, no ma er excep on is found or not. catch block is
executed only when excep on is found. Here divide by zero excep on is found hence both catch and
finally are executed.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
class Output {
public static void main(String args[]) {
try {
int a = 0;
int b = 5;
int c = a / b;
System.out.print("Hello");
} finally {
System.out.print("World");
}
}
}
a. Hello
b. World
c. HelloWOrld
d. Compila on Error
Correct Answer:
c. HelloWOrld
QUESTION 9:
interface calculate {
void cal(int item);
}
class interfaces {
public static void main(String args[]) {
displayA arr1 = new displayA();
displayB arr2 = new displayB();
arr1.x = 0;
arr2.x = 0;
arr1.cal(2);
arr2.cal(2);
System.out.print(arr1.x + " " + arr2.x);
}
}
a. 0 0
b. 2 2
c. 4 1
d. 1 4
Correct Answer:
c. 4 1
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
class displayA implements the interface calculate by doubling the value of item, where as class
displayB implements the interface by dividing item by item, therefore variable x of class displayA stores 4
and variable x of class displayB stores 1.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
a. NullPointerExcep on
b. ArrayIndexOutOfBoundsExcep on
c. IOExcep on
d. Arithme cExcep on
Correct Answer:
c. IOExcep on
IOExcep on is not a subclass of the Run meExcep on class. It is a checked excep on in Java.