0% found this document useful (0 votes)
16 views

Week 05 Assignment Solution

Uploaded by

sannutha24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Week 05 Assignment Solution

Uploaded by

sannutha24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur


NOC24-CS105 (July-2024 24A)

PROGRAMMING IN JAVA
Assignment 5
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10

QUESTION 1:

Which exception will be thrown by parseInt() method in Java?

a. IntegerOutOfBoundExcep on

b. IntegerFormatExcep on

c. Arithme cExcep on

d. NumberFormatExcep on

Correct Answer:

d. NumberFormatExcep on

Detailed Solu on:

parseInt() method parses input into integer. This method will throw NumberFormatExcep on.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 2:

What will be the output of the following program?

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

Detailed Solu on:

methodP() returns the concatena on of q and p, resul ng in "QQQQPPPP".


methodQ() returns the concatena on of p and q, resul ng in "PPPPQQQQ".
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 3:

What will be the output of the following code?

class A implements B {
public int methodB(int i) {
return i = +i * i;
}
}

interface B {
int methodB(int i);
}

public class MainClass {


public static void main(String[] args) {
B b = new A();

System.out.println(b.methodB(2));
}
}

a. 4

b. 6

c. 2

d. 8

Correct Answer:

a. 4

Detailed Solu on:

methodB(2) calculates 2 * 2, which equals 4. This value is then returned.


NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

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

Detailed Solu on:

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:

Which of the following statements is true about Java's finally block?

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

Detailed Solu on:

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:

Which of the following statements is true about exception handling in Java:

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

Detailed Solu on:

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:

What will be the output of the following program?

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

Detailed Solu on:

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:

What will be the output of the following Java code?

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

Detailed Solu on:

finally block is always executed a er try block, no ma er excep on is found or not.


NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 9:

What will be the output of the following Java program?

interface calculate {
void cal(int item);
}

class displayA implements calculate {


int x;

public void cal(int item) {


x = item * item;
}
}

class displayB implements calculate {


int x;

public void cal(int item) {


x = item / 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

Detailed Solu on:

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:

Which of the following exceptions is not a subclass of the RuntimeException class?

a. NullPointerExcep on

b. ArrayIndexOutOfBoundsExcep on

c. IOExcep on

d. Arithme cExcep on

Correct Answer:

c. IOExcep on

Detailed Solu on:

IOExcep on is not a subclass of the Run meExcep on class. It is a checked excep on in Java.

You might also like