GA3 Solutions
GA3 Solutions
public class A {
public void display( ){
System.out.print("Hii ");
}
}
public class B extends A{
public void display(String s){
display();
System.out.println(s);
}
public static void main(String[] args) {
A a = new B( );
// Line 1
}
}
What is the correct instruction to be written in Line 1 in order to print Hii Ram as
output?
a.display(“Ram”);
a.display(“Hii Ram” );
((A)a).display(“Hii Ram”)
√
((B)a).display(“Ram”);
Solution:
Solution:
Page 2
3. Which method/s is/are new methods of the subclass?
[Nalini: MSQ: 2points]
perimeter
area
√
sides
√
angleSum
Page 3
4. Consider the following code: [Nalini: MSQ: 2points]
Solution:
• A class cannot inherit two classes, multiple inherits is not allowed in java.
• An object of type parent class cannot refer subclass. Because the parent class
may not have all the functionalities of subclass
Page 4
5. Consider the Java code given below. [ARUP: MCQ: 2 points]
Solution:
In function call doIt(d);, d is of Duck type and implicitly type casted to the
parent type Bird. However, b refers to a subclass object, i.e. object of Duck.
Thus, b.fly() prints it can fly. The condition b instanceof Duck returns
true and ((Duck) b).swim() prints it can swim.
Page 5
6. Consider the Java code given below. [ARUP: MCQ: 2 points]
Page 6
√
area of Rectangle
volume is unknown
area of Cube
volume of Cube
It generates compiler error
Solution: For the call compute(r);, although the static type of s is Shape, the
dynamic type is Rectangle (since it refers to object of Rectangle). Thus, it prints
area of Rectangle
volume is unknown
For the call compute(c);, although the static type of s is Shape, the dynamic type
is Cube (since it refers to object of Cube). Thus, it prints
area of Cube
volume of Cube
Page 7
7. Consider the following abstract types.
Identify the correct subtype and inheritance relationships between the classes.
[ARUP: MSQ: 2 points]
Solution: Since Copier has more functionality than Scanner and Printer, Copier
is a subtype of Scanner and Printer.
Since we can suppress one of the functions in Copier and use it as a Scanner or
Printer, both Scanner and Printer inherit Copier.
Page 8
8. Consider the Java code given below. [ARUP: MCQ: 2 points]
Identify the appropriate option to fill in the blank at line:1 such that output of the
code will be:
101 : Nutan : HR
Employee(empid , name );
this(empid , name );
√
super(empid , name );
empid = empid ; name = name ;
Page 9
Solution: Since the instance variables empid and name are declared as private in
class Employee, they are invisible in class Manager. Thus, option-4 is incorrect.
The only way to initialize the private instance variables in class Employee by
calling it’s constructor, which can be done by the subclass Manager using super
keyword.
Page 10
9. Consider the following code. [Bhaskar:MSQ:2points]
Choose all the correct option/s which mention/s the required modification (if any) in
the code for successful compilation.
Page 11
√
Define an appropriate no argument constructor of Student class.
√
Call the Student class parameterized constructor explicitly inside
Topper class’s constructor.
No modification required, code compiles successfully.
Page 12
10. Consider the following code. [Bhaskar:MCQ:2ppoints]
Page 13
Choose the correct option regarding the given code.
This code produces a compilation error on line 37
This code produces a compilation error on line 36
This code will generate the output:
endanger status of Tiger is false
Endanger status
√
This code will generate the output:
endanger status of Tiger is false
Mammal details
This code will generate the output:
endanger status of null is false
Mammal details
Page 14