Week 06 Assignment Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur


NOC24-CS105 (July-2024 24A)

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

QUESTION 1:

What is the output of the following program?

public class Question {


public static void main(String[] args) {
try {
int a = 5 / 0;
}
catch (Exception e) {

catch (ArithmeticException a) {
System.out.println("Cannot divide by 0");
}
}
System.out.println("Hello World");
}
}

a. “Hello World”

b. “Cannot divide by 0”

c. Compila on Error

d. Run me Error (the code compiles successfully)

Correct Answer:

c. Compila on Error

Detailed Solu on:

This first handler catches excep ons of type Exception; therefore, it catches any excep on, including
ArithmeticException. The second handler could never be reached. This code will not compile.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 2:

What will be the output of the following program?

class Question {
int i;
public Question(int i) {
this.i = i--;
}
}
class Question1 extends Question {
public Question1(int i) {
super(++i);
System.out.println(i);
}
}
public class Check {
public static void main(String[] args) {
Question1 n = new Question1(20);
}
}

a. 20

b. 21

c. 19

d. 22

Correct Answer:

b. 21

Detailed Solu on:

The program creates an instance of Question1 with an ini al value of 20. Inside the Question1
constructor, i is pre‐incremented to 21 before being passed to the Question superclass constructor.
In the Question constructor, this.i is assigned the value 21 (from i-- which uses i before
decremen ng it). Returning to the Question1 constructor, i is s ll 21, and this value is printed. Thus,
the output is 21.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 3:

What is the output of the following program?

class Question {
static int x;
static {
x++;
}

{
++x;
}
}
class Question1 extends Question {
static {
--x;
}

{
x--;
}
}
public class Check {
public static void main(String[] args) {
System.out.println(new Question1().x);
}
}

a. 1

b. 2

c. 0

d. Compila on Error

Correct Answer:

c. 0

Detailed Solu on:

When the Question class is loaded, its sta c block increments x to 1. Then, the Question1 class is loaded,
and its sta c block decrements x back to 0. When an instance of Question1 is created, the instance ini alizer
blocks run: Question's block increments x to 1, and Question1's block decrements x back to 0.
Therefore, prin ng x in the main method outputs 0.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 4:

Which exception is thrown when an array element is accessed beyond the array size?

a. ArrayElementOutOfBounds

b. ArrayIndexOutOfBoundsExcep on

c. ArrayIndexOutOfBounds

d. None of these

Correct Answer:

b. ArrayIndexOutOfBoundsExcep on

Detailed Solu on:

The excep on that is thrown when an array element is accessed beyond the array size in Java is:

ArrayIndexOutOfBoundsException

This is the specific excep on in Java that indicates that an array has been accessed with an illegal index,
either nega ve or greater than or equal to the size of the array. The other op ons listed do not represent
valid excep ons in Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 5:

What is the output of the following program?

class Q {
public void disp() {
System.out.println("java");
}
}
class P extends Q {
public void disp() {
System.out.println("nptel");
}
}
class C extends P {
public void disp(){
super.super.disp();
System.out.println("course");
}
}
public class Question {
public static void main(String[] args) {
C c = new C();
c.disp();
}
}

a. java

b. java
course

c. nptel
course

d. Compila on Error

Correct Answer:

d. Compila on Error

Detailed Solu on:

The code a empts to use super.super.disp() in the C class, which is invalid in Java. Java does not
support accessing a grandparent class's method directly using super.super. This syntax results in a
compila on error because super can only be used to refer to the immediate superclass.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 6:

Fill in the blank in the program so that the output is “Java”.

interface X {
void display();
}
class Y implements X {
___________ display() { //MISSING_CODE
System.out.println("Java");
}
}
public class MainClass {
public static void main(String[] args) {
Y r = new Y();
r.display();
}
}

a. public void

b. void

c. private void

d. sta c void

Correct Answer:

a. public void

Detailed Solu on:

Interface methods must be implemented as public. Because, interface methods are public by
default and you should not reduce the visibility of any methods while overriding.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 7:

How many times will “Java” be printed if the following code is executed?

class X {
static {
Y.display();
}
}
class Y extends X {
static void display() {
System.out.println("Java");
}
}
public class MainClass {
public static void main(String[] args) {
Y.display();
}
}

a. 0

b. 1

c. 2

d. 3

Correct Answer:

c. 2

Detailed Solu on:

When the code is executed, the sta c block in class X is executed first, which calls Y.display(). This
prints "Java" once. Then, in the main method of MainClass, Y.display() is called again, resul ng
in "Java" being printed for the second me. Thus, "Java" is printed twice in total.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 8:

The following is a simple program using the concept of thread. What is the output of the following
program?

public class Question extends Thread {


public void run() {
for (int i = 1; i < 8; i++) {
System.out.print(++i + " ");
}
}
public static void main(String args[]) {
Question t1 = new Question();
t1.run();
}
}

a. 1 3 5 7

b. 2 4 6 8

c. 1 3 5 7 9

d. 2 4 6

Correct Answer:

b. 2 4 6 8

Detailed Solu on:

The increment operators increase the value of i to 2 in the first run. A erwards, two increments are
happening ll i < 8 condi on is not sa sfied.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 9:

For the program given below, what will be the output after its execution?

public class Main {


public static void main(String[] args) {
Thread thread = Thread.currentThread();
thread.run();
System.out.print(Thread.activeCount());
}
}

a. 1

b. 2

c. 0

d. 01

Correct Answer:

a. 1

Detailed Solu on:

java.lang.Thread.activeCount() : Returns an es mate of the number of ac ve threads in the


current thread’s thread group and its subgroups.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 10:

Which of the following method returns a reference to the currently executing thread object?

a. public sta c boolean interrupted();

b. public sta c Thread currentThread();

c. public final boolean isAlive();

d. public final void suspend();

Correct Answer:

b. public sta c Thread currentThread();

Detailed Solu on:

Only public static Thread currentThread() method returns a reference to the currently
execu ng thread object among the op ons.

You might also like