Output of Java Program | Set 7
Last Updated :
28 Jun, 2021
Difficulty level : Intermediate
Predict the output of following Java Programs.
Program 1 :
Java
public class Calculator
{
int num = 100;
public void calc(int num) { this.num = num * 10; }
public void printNum() { System.out.println(num); }
public static void main(String[] args)
{
Calculator obj = new Calculator();
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 :
Java
public class MyStuff
{
String name;
MyStuff(String n) { name = n; }
public static void main(String[] args)
{
MyStuff m1 = new MyStuff("guitar");
MyStuff m2 = new MyStuff("tv");
System.out.println(m2.equals(m1));
}
@Override
public boolean equals(Object obj)
{
MyStuff m = (MyStuff) obj;
if (m.name != null) { return true; }
return false;
}
}
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 :
Java
class Alpha
{
public String type = "a ";
public Alpha() { System.out.print("alpha "); }
}
public class Beta extends Alpha
{
public Beta() { System.out.print("beta "); }
void go()
{
type = "b ";
System.out.print(this.type + super.type);
}
public static void main(String[] args)
{
new Beta().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 :
Java
public class Test
{
public static void main(String[] args)
{
StringBuilder s1 = new StringBuilder("Java");
String s2 = "Love";
s1.append(s2);
s1.substring(4);
int foundAt = 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 :
Java
class Writer
{
public static void write()
{
System.out.println("Writing...");
}
}
class Author extends Writer
{
public static void write()
{
System.out.println("Writing book");
}
}
public class Programmer extends Author
{
public static void write()
{
System.out.println("Writing code");
}
public static void main(String[] args)
{
Author a = new Programmer();
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.