Difficulty level : Intermediate
Predict the output of following Java Programs.
Program 1 :
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 :
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 :
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 :
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 :
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 GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks 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.
- Output of Java Program | Set 1
- Output of Java Program | Set 2
- Output of Java Program | Set 3
- Output of Java program | Set 28
- Output of Java program | Set 5
- Output of Java Program | Set 11
- Output of Java Program | Set 19
- Output of Java program | Set 17
- Output of Java Program | Set 8
- Output of Java program | Set 26
- Output of Java program | Set 27
- Output of Java Program | Set 4
- Output of Java Program | Set 9
- Output of Java Program | Set 6
- Output of Java program | Set 15 (Inner Classes)
Improved By : jacobjohn2016