Difficulty level : Intermediate
Predict the output of following Java Programs.
Program 1 :
| publicclassCalculator{    intnum = 100;    publicvoidcalc(intnum)  { this.num = num * 10;  }    publicvoidprintNum()     { System.out.println(num); }     publicstaticvoidmain(String[] args)    {        Calculator obj = newCalculator();        obj.calc(2);        obj.printNum();    }} | 
Options :
A) 20
B) 100
C) 1000
D) 2
Answer : A) 20
Explanation : Here the class instance variable name(num) is same as calc() method local variable name(num). So for referencing class instance variable from calc() method, this keyword is used. So in statement this.num = num * 10, num represents local variable of the method whose value is 2 and this.num represents class instance variable whose initial value is 100. Now in printNum() method, as it has no local variable whose name is same as class instance variable, so we can directly use num to reference instance variable, although this.num can be used.
Program 2 :
| publicclassMyStuff{    String name;     MyStuff(String n) {  name = n;  }     publicstaticvoidmain(String[] args)    {        MyStuff m1 = newMyStuff("guitar");        MyStuff m2 = newMyStuff("tv");        System.out.println(m2.equals(m1));    }     @Override    publicbooleanequals(Object obj)    {        MyStuff m = (MyStuff) obj;        if(m.name != null)  { returntrue;  }        returnfalse;    }} | 
Options :
A) The output is true and MyStuff fulfills the Object.equals() contract.
B) The output is false and MyStuff fulfills the Object.equals() contract.
C) The output is true and MyStuff does NOT fulfill the Object.equals() contract.
D) The output is false and MyStuff does NOT fulfill the Object.equals() contract.
Answer : C) The output is true and MyStuff does NOT fulfill the Object.equals() contract.
Explanation : As equals(Object obj) method in Object class, compares two objects on the basis of equivalence relation. But here we are just confirming that the object is null or not, So it doesn’t fulfill Object.equals() contract. As m1 is not null, true will be printed.
Program 3 :
| classAlpha{    publicString type = "a ";    publicAlpha() {  System.out.print("alpha "); }} publicclassBeta extendsAlpha{    publicBeta()  {  System.out.print("beta ");  }     voidgo()    {        type = "b ";        System.out.print(this.type + super.type);    }     publicstaticvoidmain(String[] args)    {        newBeta().go();    }} | 
Options :
A) alpha beta b b
B) alpha beta a b
C) beta alpha b b
D) beta alpha a b
Answer : A) alpha beta b b
Explanation : The statement new Beta().go() executes in two phases. In first phase Beta class constructor is called. There is no instance member present in Beta class. So now Beta class constructor is executed. As Beta class extends Alpha class, so call goes to Alpha class constructor as first statement by default(Put by the compiler) is super() in the Beta class constructor. Now as one instance variable(type) is present in Alpha class, so it will get memory and now Alpha class constructor is executed, then call return to Beta class constructor next statement. So alpha beta is printed.
In second phase go() method is called on this object. As there is only one variable(type) in the object whose value is a. So it will be changed to b and printed two times. The super keyword here is of no use.
Program 4 :
| publicclassTest{    publicstaticvoidmain(String[] args)    {        StringBuilder s1 = newStringBuilder("Java");        String s2 = "Love";        s1.append(s2);        s1.substring(4);        intfoundAt = s1.indexOf(s2);        System.out.println(foundAt);    }} | 
Options :
A) -1
B) 3
C) 4
D) A StringIndexOutOfBoundsException is thrown at runtime.
Answer : C) 4
Explanation : append(String str) method,concatenate the str to s1. The substring(int index) method return the String from the given index to the end. But as there is no any String variable to store the returned string,so it will be destroyed.Now indexOf(String s2) method return the index of first occurrence of s2. So 4 is printed as s1=”JavaLove”.
Program 5 :
| classWriter{    publicstaticvoidwrite()    {        System.out.println("Writing...");    }}classAuthor extendsWriter{    publicstaticvoidwrite()    {        System.out.println("Writing book");    }} publicclassProgrammer extendsAuthor{    publicstaticvoidwrite()    {        System.out.println("Writing code");    }     publicstaticvoidmain(String[] args)    {        Author a = newProgrammer();        a.write();    }} | 
Options :
A) Writing…
B) Writing book
C) Writing code
D) Compilation fails
Answer : B) Writing book
Explanation : Since static methods can’t be overridden, it doesn’t matter which class object is created. As a is a Author referenced type, so always Author class method is called. If we remove write() method from Author class then Writer class method is called, as Author class extends Writer class.
This article is contributed by Gaurav Miglani. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


 
                                    







