PROGRAMMING IN JAVA Assignment 5 Inheritance MCQs | NPTEL

PROGRAMMING IN JAVA Assignment 5 | NPTEL | Answer with Explanation

Test Your Java Fundamentals with a Challenging MCQ Assignment on " Inheritance " Sharpen your core Java skills with this engaging multiple-choice quiz (MCQ). Designed for beginners, this Assignment 5 assesses your understanding of fundamental Java concepts. Whether you're new to programming or refreshing your knowledge, this MCQ [Java Inheritance] challenge is a perfect way to gauge your progress and identify areas for improvement.

PROGRAMMING IN JAVA Assignment 5 Inheritance MCQs  NPTEL

Consider the following program.

class Question{
int i;
}
class Test extends Question{
int j;
void display(){
super.i = j + 1;
System.out.println(j + " " + i);
}}
public class Query{
public static void main(String args[]){
Test obj = new Test();
obj.i=1;
obj.j=2;
obj.display();
}}

If the program is executed, then what will be the output?

Which of the following source files cannot be included in a package?


a. 2 2

b. 3 3

c. 2 3

d. 3 2
Answer

c. 2 3

The output can be checked by execution.

Consider the following piece of code.

package java.util;
public interface EventListener
{
}

Which of the following statement(s) is/are true for the above code?

a. It is an empty interface.
b. It is a tag interface.
c. It is a marker interface.
d. It is a nested interface.
Answer

a. It is an empty interface.,b. It is a tag interface.,c. It is a marker interface.

What is the output of the following code?

interface A {
int x=10;
void m1();
}
class B implements A {
int x = 20;
public void m1(){
System.out.println("java");
}
}
public class Test {
public static void main(String[] args){
A a = new B();
a.m1();
System.out.println(a.x);
}
}

a. java
10
b. java
20
c. 10
java
d. 20
java
Answer


a. java
10

The output can be checked by execution.

Which of the following statement(s) is/are true?

a. All abstract methods defined in an interface must be implemented.
b. The variables defined inside an interface are static and final by default.
c. An interface is used to achieve full abstraction.
d. Inside an interface, a constructor can be called using the super keyword with hierarchy.
Answer

a. All abstract methods defined in an interface must be implemented., b. The variables defined inside an interface are static and final by default., c. An interface is used to achieve full abstraction.

A constructor cannot be called inside an interface using the super keyword with hierarchy.

Which of the following statement(s) is/are true?

1. A class can extend more than one class.

2. A class can extend only one class but many interfaces.

3. An interface can extend many interfaces.

4. An interface can implement many interfaces.

5. A class can extend one class and implement many interfaces.

a. 1 and 2
b. 2 and 4
c. 3 and 5
d. 3 and 4
Answer

c. 3 and 5

An interface can extend many interfaces. A class can extend one class and implement many interfaces.

Which of the following statement(s) is/are true?

a. Abstract class can have abstract and non-abstract methods.
b. Abstract class can have final, non-final, static and non-static variables.
c. Interface has only static and final variables.
d. Interface can provide the implementation of an abstract class.
Answer

a. Abstract class can have abstract and non-abstract methods.,b. Abstract class can have final, non-final, static and non-static variables.,c. Interface has only static and final variables.

Interface can't provide the implementation of an abstract class.

Consider the following piece of code.

public class Question {
public static void main(String args[]) {
try {
int a, b;
b = 0;
a = 25 / b;
System.out.print("A"); }
catch(ArithmeticException e) {
System.out.print("B");
}
finally
{
System.out.print("C");
} } }

What is the output of the above code?

a. A
b. B
c. BC
d. AC
Answer

c. BC

Test by execution.

The class at the top of exception class hierarchy is ..................

a. Object
b. Throwable
c. Exception
d. ArithmeticException
Answer

b. Throwable

The class at the top of the exception class hierarchy is the Throwable class, which is a direct subclass of the Object class.

Which of these class is superclass of every class in Java?

a. Object class
b. Abstract class
c. String class
d. ArrayList class
Answer

a. Object class

All classes in java are inherited from Object class.

interface calculate{
int VAR = 0;
void cal(int item);
}
class display implements calculate{
int x;
public void cal(int item){
if (item<2)
x = VAR;
else
x = item * item;
}
}
public class Question {
public static void main(String args[]){
display[] arr=new display[3];
for(int i=0;i<3;i++)
arr[i]=new display();
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
System.out.print(arr[0].x+"  " + arr[1].x
arr[2].x);
}
} 

If the program is executed, then what will be the output?

a. 0 1 2
b. 0 0 4
c. 0 2 4
d. 0 4 4
Answer

b. 0 0 4

The output can be checked by execution.

PYQ | The Joy of Computing using Python - Course | NPTEL

Learn Java Programming for free with Computer Course - CompEduBox Android app. Share with your Friends

Post a Comment

0 Comments