Java MCQ Output based Questions - Constructor

In this article, we will cover some Java MCQ output based questions which will help extend your Java knowledge. Usually In Interviews nowadays, interviewer are more focused on negative questions rather than conceptual questions, and If you go through below questions, It will surely give you an add on and add value to your experience.

Java MCQ Output based Questions

Constructor in Java

class First
{
public First() { System.out.println("a"); }
}

class Second extends First
{
public Second() { System.out.println("b"); }
}

class Third extends Second
{
public Third() { System.out.println("c"); }
}

public class MainClass
{
public static void main(String[] args)
{
Third c = new Third();
}
}

Output - 
a
b
c

Explanation - While creating a new object of ‘Third’ type, before calling the default constructor of Third class, the default constructor of super class is called i.e, Second class and then again before the default constructor of super class, default constructor of First class is called. And hence gives such output.


class First
{
int i = 10;

public First(int j)
{
System.out.println(i);
this.i = j * 10;
}
}

class Second extends First
{
public Second(int j)
{
super(j);
System.out.println(i);
this.i = j * 20;
}
}

public class MainClass
{
public static void main(String[] args)
{
Second n = new Second(20);
System.out.println(n.i);
}
}

Output 
10
200
400

Explanation - Since in ‘Second’ class it doesn’t have its own ‘i’, the variable is inherited from the super class. Also, the constructor of parent is called when we create an object of Second.

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;
}
}

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.


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.


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();

}

}


A) Writing…

B) Writing book

C) Writing code

D) Compilation fails


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.


class Person

{

private void who()

{

System.out.println("Inside private method Person(who)");

}


public static void whoAmI()

{

System.out.println("Inside static method, Person(whoAmI)");

}


public void whoAreYou()

{

who();

System.out.println("Inside virtual method, Person(whoAreYou)");

}

}


class Kid extends Person

{

private void who()

{

System.out.println("Kid(who)");

}


public static void whoAmI()

{

System.out.println("Kid(whoAmI)");

}


public void whoAreYou()

{

who();

System.out.println("Kid(whoAreYou)");

}

}

public class Gfg

{

public static void main(String args[])

{

Person p = new Kid();

p.whoAmI();

p.whoAreYou();

}

}


output- 

Inside static method, Person(whoAmI)

Kid(who)

Kid(whoAreYou)


Explanation - Static binding (or compile time) happens for static methods. Here p.whoAmI() calls the static method so it is called during compile time hence results in static binding and prints the method in Person class. 

Whereas p.whoAreYou() calls the method in Kid class since by default Java takes it as a virtual method i.e, dynamic binding. In other words, Static methods cannot be overriden.


class Gfg

{

static int num;

static String mystr;


// constructor

Gfg()

{

num = 100;

mystr = "Constructor";

}


// First Static block

static

{

System.out.println("Static Block 1");

num = 68;

mystr = "Block1";

}


// Second static block

static

{

System.out.println("Static Block 2");

num = 98;

mystr = "Block2";

}


public static void main(String args[])

{

Gfg a = new Gfg();

System.out.println("Value of num = " + a.num);

System.out.println("Value of mystr = " + a.mystr);

}

}


Output - 

Static Block 1

Static Block 2

Value of num = 100

Value of mystr = Constructor


Explanation - Static block gets executed when the class is loaded in the memory. A class can have multiple Static blocks, which are executed in the same sequence in which they have been written into the program.

Note: Static Methods can access class variables without using object of the class. Since constructor is called when a new instance is created so firstly the static blocks are called and after that the constructor is called. If we would have run the same program without using object, the constructor would not have been called.


Which of the following are true about constructor declaration?


a) Constructors can be declared final.

b) Constructors can be surrounded by try/catch blocks.

c) Constructor cannot throw exception.

d) Constructors can hold synchronized code(so that each thread can access constructor sequentially). 

class Helper

{

private int data;

private Helper()

{

data = 5;

}

}

public class Test

{

public static void main(String[] args)

{

Helper help = new Helper();

System.out.println(help.data);

}

}


a) Compilation error

b) 5

c) Runtime error

d) None of these


Explanation - A private constructor cannot be used to initialize an object outside the class that it is defined within because it is no longer visible to the external class.


public class Test

{

public Test()

{

System.out.printf("1");

new Test(10);

System.out.printf("5");

}

public Test(int temp)

{

System.out.printf("2");

new Test(10, 20);

System.out.printf("4");

}

public Test(int data, int temp)

{

System.out.printf("3");

}

public static void main(String[] args)

{

Test obj = new Test();

}

}

a) 12345

b) Compilation error

c) 15

d) Runtime error


Explanation - Constructors can be chained and overloaded. When Test() is called, it creates another Test object calling the constructor Test(int temp).


class Base

{

public static String s = " Super Class ";

public Base()

{

System.out.printf("1");

}

}

public class Derived extends Base

{

public Derived()

{

System.out.printf("2");

super();

}

public static void main(String[] args)

{

Derived obj = new Derived();

System.out.printf(s);

}

}


a) 21 Super Class

b) Super Class 21

c) Compilation error

d) 12 Super Class


Explanation - Constructor call to super class must be the first statement in the constructor of the Derived class.


class Test

{

final int MAXIMUM;

final double PI;


public Test(int max)

{

MAXIMUM = max;

}


public Test(double pi)

{

PI = pi;

}


public static void main(String[] args)

{

Test t1 = new Test(1500);

Test t2 = new Test(3.145);


System.out.println("MAXIMUM : " + t1.MAXIMUM + " PI : " + t2.PI);

}

}


a) Compilation error

b) Runtime error

c) 1500 3.145

d) 3.145 1500


Explanation - As we know that we can initialize a blank final variable inside constructor also, but if there are more than one constructors then it is mandatory to initialize all final variables in all of them. This is because we can create an object of class by calling any one of the constructors, but if that constructor is not initializing any one of declared final variable than there is problem.


Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Testing in CI/CD

Azure Tutorials Series - IaaS vs PaaS vs SaaS