GA4 Solutions
GA4 Solutions
Week 4
1. Consider the code given below. [MCQ:2 points]
Page 2
2. Consider the code given below. [MCQ:2 points]
Page 3
Solution: Since the static type of s is Shape, and print() method does not belong
to Shape, the call s.print() is illegal.
Page 4
3. Consider the code given below. [MSQ:2 points]
Identify the appropriate option(s) to fill in the blank at LINE1, such the output is:
norm is : 5.0
print(c.norm());
c.print(c.norm());
√
NumberType.print(c.norm());
ComplexNum.print(c.norm());
Solution: A static method belongs to an interface, must be invoked with the same
interface name.
Page 5
4. Consider the code given below. [MSQ:2 points]
Identify the appropriate option to fill in blank at LINE1 such that the output becomes
Execute: fetch students
√
public DBIF connectDB(String u, String p)
√
public ConnectionObj connectDB(String u, String p)
public Database connectDB(String u, String p)
public static ConnectionObj connectDB(String u, String p)
Page 6
5. Consider the code given below. [MCQ:2 points]
}
}
Identify the appropriate option to fill in the blank at Line 1 to print Hello India as
the output.
g.greet();
√
g.checkCountry().greet();
Page 7
g.IndiaGreetings.greet();
g.Inter.IndiaGreetings.greet();
Solution: To print Hello India greet() must be called. IndiaGreetings class not
visible outside of Greetings class, but the interface Inter is visible which has greet()
abstract method. checkCountry() is called which returns India Greetings object.
And the greet() is called using that object.
Page 8
6. Consider the code given below.
Identify the appropriate option to fill in the blank at Line 1 to print In Programming
class as the output.
Language.Programming.show();
new Language().Programming.show();
√
new Language().new Programming().show();
Language.new Programming().show();
Page 9
7. Consider the Java program given below and predict the output.
[ MCQ : 2 points]
This code generates a compilation error because an interface can’t have fully
implemented methods.
Cat meows
Dog barks
√
Cat meows
Cat eats every day
Dog barks
Dog eats every day
Cat eats every day
Dog eats every day
Page 10
Solution: Option 3
From JDK 1.8 onwards, the interface supports default methods. Program executes
successfully and all methods called.
Page 11
8. Consider the Java program given below and select the correct option from among the
given choices. [ MCQ : 2 points]
Solution: class Three implements interface Two, interface Two extends from inter-
face One.
class Three should override both print() and display().
Page 12
9. Choose all the correct option(s) regarding the code given below.
}
public class FClass{
public static void main(String[] args) {
University uni = new University();
(uni.getReference()).getName() ;
}
}
[MSQ:2 points]
This code compiles successfully and generates output:
IITMadras
This code generates a compilation error because college name is a private
variable.
This code generates a compilation error because getReference() is actually
not returning a valid object of College type.
√
This code generates a compilation error because the method getName() is not
overridden and hence not accessible.
Page 13
Solution:
Method getName is a member function of the nested class College which is private,
hence the only way we can access this method is by applying dynamic dispatch using
method overriding. Therefore we have to use either an abstract class or interface
which the inner class would inherit/implement and then we can access the getName
method as an overridden implementation.
So correct choice would be option 4.
Page 14
The Java code below models a certain functionality of a ropeway system, carrying
tourists, that allows 6 travelers in each cabin. The travelers are let in one after the
other into a cabin until the maximum capacity is reached (class CabinCounter). Then,
the cabin is allowed to move on, and the next cabin is readied for boarding. A counter
(class MasterCounter) is used to track the total number of travelers using the ropeway
in a day. Based on the above information and the following code, answer questions 10
and 11.
public MasterCounter(){
cc = new CabinCounter(this); //A new cabin arrives
cc.performCount();
}
public void inform(){
this.incrementDailyCounter();
cc = new CabinCounter(this); //A new cabin arrives
.................... //Blank Line 2
}
public void incrementDailyCounter(){
Page 15
dayCount = dayCount + 6;
}
10. Which should be the line at Blank Line 1 so that the MasterCounter is notified of a
cabin becoming full?
[ MCQ: 2points]
mc.inform();
√
master.inform();
cc.incrementDailyCounter();
MasterCounter cannot be notified because master is a private object inside
CabinCounter.
11. Which should be the line at Blank Line 2 so that the CabinCounter will start checking
for the new cabin becoming full?
[ MCQ: 2points]
There is no need to add any line because the constructor of CabinCounter will
initiate the counting for the new cabin.
√
cc.performCount();
master.incrementDailyCounter();
cc.inform();
Page 16