24S - W03 - Final
24S - W03 - Final
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:
Correct Answer:
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:
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 {
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:
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:
Correct Answer:
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:
class Sweet {
void price() {
System.out.print("Sweet=$10 ");
}
}
void price() {
super.price();
System.out.print("Sugar=$20");
}
}
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:
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:
class Parent {
String message() {
return "from parent";
}
}
String message() {
return "from child";
}
}
Correct Answer:
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:
Correct Answer:
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:
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.