0% found this document useful (0 votes)
21 views10 pages

24S - W03 - Final

This document contains 10 multiple choice questions about Java inheritance and polymorphism. The questions cover topics like constructor calling order, overriding methods and variables, using the super keyword, static methods, and final vs abstract classes.

Uploaded by

Different Codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views10 pages

24S - W03 - Final

This document contains 10 multiple choice questions about Java inheritance and polymorphism. The questions cover topics like constructor calling order, overriding methods and variables, using the super keyword, static methods, and final vs abstract classes.

Uploaded by

Different Codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur

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

QUESTION 1:

Which of the following statement is true regarding the order of execution of constructors in an
inheritance hierarchy?

a. Base class constructor will be called followed by the derived class constructor.
b. Derived class constructor will be called followed by the base class constructor.
c. Only Base class constructor will be called.
d. Only derived class constructor will be called.

Correct Answer:

a. Base class constructor will be called followed by the derived class constructor.

Detailed Solu�on:

On object crea�on of derived class, first base class constructor and then the derived class constructor
will be called.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 2:

The super() method is used to:

a. Call constructor of friend class


b. Is a declared method
c. Call constructor of the parent class
d. Call constructor

Correct Answer:

c. Call constructor of the parent class

Detailed Solu�on:

In Java programming language, the super() is a reference variable that is used to refer parent class
constructors. The super can be used to call parent class's variables and methods. The super() can be used
to call parent class' constructors only.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 3:

What will be the output of the following Java program?

class A {

int i;

void display() {
System.out.println(i);
}
}

class B extends A {

int j;

void display() {
System.out.println(j);
}
}

class inheritance_demo {

public static void main(String args[]) {


B obj = new B();
obj.i = 1;
obj.j = 2;
obj.display();
}
}

a. 0
b. 1
c. 2
d. Compila�on Error

Correct Answer:

c. 2

Detailed Solu�on:

Class A & class B both contain display() method, class B inherits class A, when display() method is called
by object of class B, display() method of class B is executed rather than that of Class A.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 4:

In Java, is it possible to override a static method?

a. Yes, we can override a sta�c method just like we do with instance methods.
b. No, sta�c methods cannot be overridden because they belong to the class, not the object.
c. It depends on whether the sta�c method is declared as final or not.
d. It depends on the access modifier of the sta�c method.

Correct Answer:

b. No, sta�c methods cannot be overridden because they belong to the class, not the object.

Detailed Solu�on:

In Java, a sta�c method is bound to the class and not to the instance. Hence, it is not part of the state of
the object and doesn't par�cipate in polymorphism and dynamic dispatch, which are necessary for
method overriding.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 5:

What is the output of the following Java program?

public class Vehicle {


public void move() {
System.out.println("The vehicle moves");
}
}

public class Car extends Vehicle {


public void move() {
System.out.println("The car moves");
}
}

public class Main {


public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.move();
}
}

a. "The vehicle moves"


b. "The car moves"
c. The code does not compile
d. None of the above

Correct Answer:

b. "The car moves"

Detailed Solu�on:

In Java, a subclass can override methods from its superclass. In this example, the Car class is overriding
the move method of the Vehicle class. Since the object is instan�ated as a Car, the overridden move
method in the Car class is called, producing the output "The car moves"
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 6:

What is the output of the below Java program with inheritance?

class Sweet {

void price() {
System.out.print("Sweet=$10 ");
}
}

class Sugar extends Sweet {

void price() {
super.price();
System.out.print("Sugar=$20");
}
}

public class JavaInheritance1 {

public static void main(String[] args) {


Sugar su = new Sugar();
su.price();
}
}

a. Sweet=$10 Sugar=$20
b. Sweet=$10 Sugar=$10
c. Sweet=$20 Sugar=$20
d. Compiler error

Correct Answer:

a. Sweet=$10 Sugar=$20

Detailed Solu�on:

No�ce the use of the keyword "super". Using this keyword, you can call superclass's methods and
variables.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 7:

What is the purpose of method hiding in Java inheritance?

a. To prevent a subclass from inheri�ng methods


b. To override superclass methods with new implementa�ons
c. To expose private methods of the superclass
d. To define methods with the same name in both the superclass and subclass

Correct Answer:

d. To define methods with the same name in both the superclass and subclass

Detailed Solu�on:

Method hiding occurs when a subclass defines a sta�c method with the same name as a sta�c method in
the superclass.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 8:

What is the output of the following Java program?

class Parent {

String name = "parent";

String message() {
return "from parent";
}
}

class Child extends Parent {

String name = "child";

String message() {
return "from child";
}
}

public class Main {

public static void main(String[] args) {


Parent p = new Child();
System.out.println(p.name + " " + p.message());
}
}

a. "parent from parent"


b. "child from child"
c. "parent from child"
d. "child from parent"

Correct Answer:

c. "parent from child"

Detailed Solu�on:

In Java, while methods are overridden (dynamic binding), variables are not overridden (sta�c binding).
Therefore, p.name refers to the Parent class variable, and p.message() refers to the Child class method.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 9:

Can a class be marked as both “final” and “abstract” in Java?

a. Yes, but only if it has no methods.


b. Yes, a class can be marked as both “final” and “abstract.”
c. No, a class cannot be both “final” and “abstract.”
d. Yes, but only if it is marked as “protected.”

Correct Answer:

c. No, a class cannot be both “final” and “abstract.”

Detailed Solu�on:

A class marked as “final” cannot be extended (subclassed), while an abstract class is meant to be
extended. Therefore, they are contradictory modifiers.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 10:

In Java, is it possible to override a static method?

a. Yes, we can override a sta�c method just like we do with instance methods.
b. No, sta�c methods cannot be overridden because they belong to the class, not the object.
c. It depends on whether the sta�c method is declared as final or not.
d. It depends on the access modifier of the sta�c method.

Correct Answer:

b. No, sta�c methods cannot be overridden because they belong to the class, not the object.

Detailed Solu�on:

In Java, a sta�c method is bound to the class and not to the instance. Hence, it is not part of the state of
the object and doesn't par�cipate in polymorphism and dynamic dispatch, which are necessary for
method overriding.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy