Object Oriented Programming using Java Questions and Answers – Types of Constructors

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Types of Constructors”.

1. What is the output of the following Java code?

class Constructor 
{
    String name;
    ConstructorName()
    {
        this.name = "A";
    }
    public static void main(String[] args) 
    {
        ConstructorName obj = new ConstructorName();
        System.out.println(obj.name);
    }
}

a) A
b) Null
c) ” ”
d) Compilation error
View Answer

Answer: a
Explanation: Constructor is a block of code that initializes the newly created object. In the above program, the class variable ‘String’ is initialized using the constructor during the creation of object ‘obj’. ‘this’ keyword is used to refer to the currently executing object.
Output:

$ javac Constructor.java
$ java Constructor
A

2. What is the output of the following Java code?

advertisement
advertisement
class ConstructorDisplay
{  
    int id; 
    void display()
    {
        System.out.println(id);
    }  
    public static void main(String args[])
    {  
        ConstructorDisplay obj1=new ConstructorDisplay();  
        obj1.display();   
    }  
}

a) 0
b) ” ”
c) Garbage Value
d) Compilation Error
View Answer

Answer: a
Explanation: Constructor is a special type of method which is used to initialize the object. Default constructor is automatically invoked when an object is created. In this case, an object ‘obj1’ is created and hence the constructor is invoked. The default value of an int variable is 0.
Output:

$ javac ConstructorDisplay.java
$ java ConstructorDisplay
0

3. What is the output of the following Java code?

advertisement
class ConstructorPrint
{  
    float f;
    void Print()
    {
        System.out.println(f);
    }  
    public static void main(String args[])
    {  
        ConstructorPrint obj1=new ConstructorPrint();  
        obj1.Print();   
    }  
}

a) 0
b) 0.0f
c) 0.0
d) Compilation Error
View Answer

Answer: c
Explanation: Constructor is a special type of method which is used to initialize the object. Default constructor is automatically invoked when an object is created. A float class variable ‘f’ is present, as there is no initial value assigned to it, the default value of 0.0 is assigned to it.
Output:

advertisement
$ javac ConstructorPrint.java
$ java ConstructorPrint
0.0

4. What is the output of the following Java code?

class ConstructorString
{  
    String s;
    ConstructorString(String str)
    {
        System.out.println(str);
    }  
    public static void main(String args[])
    {  
        ConstructorString obj1=new ConstructorString();  
    }  
}

a) 0
b) Null
c) Garbage Value
d) Compilation Error
View Answer

Answer: d
Explanation: A constructor initializes an object as soon as it is created. It has the same name as the class and is a special kind of method. In this case, a parameterized constructor is present but in the object creation, the parameter is not present to execute the parameterized constructor.
Output:

$ javac ConstructorString.java
$ java ConstructorString
Uncompilable Source code

5. What is the output of the following Java code?

class Parameterised
{  
    String str="Hello";
    Parameterised(String str)
    {
        System.out.println(this.str);
    }  
    public static void main(String args[])
    {  
        Parameterised obj1=new Parameterised("World");  
    }  
}

a) Hello
b) World
c) Null
d) Compilation Error
View Answer

Answer: a
Explanation: A constructor is a member function of a class which initializes objects of a class. It is called during the object creation. As a parameterized constructor is present in the above program, the value is passed in the object creation and printed using the ‘this’ keyword.
Output:

$ javac Parameterised.java
$ java Parameterised
Hello

6. What is the output of the following Java code?

class Add 
{
    int  a;
    int  b;
    Add(int x, int y)
    {
        a = x;
        a = a + 10;
        b = y;
        System.out.println(x+" "+y);
    }
    public static void main(String args[])
    {
        Add obj1 = new Add(10,20);
    }
}

a) 10 20
b) 20 20
c) Null
d) Compilation
View Answer

Answer: a
Explanation: Constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object.
The values of x and y are given as 10 and 20 at the object creation itself and hence the values are printed successfully.
Output:

$ javac Add.java
$ java Add
10 20

7. What is the output of the following Java code?

class Parameterised2
{  
    int num=8;
    Parameterised2(int num)
    {
        this.num=num;
        System.out.println(this.num);
    }  
    public static void main(String args[])
    {  
        Parameterised2 obj1=new Parameterised2(10);  
    }  
}

a) 8
b) 10
c) 0
d) Compilation Error
View Answer

Answer: b
Explanation: A constructor is a special method that is used to initialize an object. In the above block of code, a parameterized constructor is used to set the value of an instance variable at the object creation in the main() method. ‘this’ keyword is used to refer to the instance variable.
Output:

$ javac Parameterised2.java
$ java Parameterised2
10

8. What is the output of the following Java code?

class Parameterised3
{
    int  num1;
    int  num2;
    Parameterised3(int y, int x)
    {
        num1 = x;
        num2 = y;
        System.out.println(num2);
    }
    public static void main(String args[])
    {
        Parameterised3 obj1 = new Parameterised3(40, 80);
    }
}

a) 40
b) 80
c) Null
d) Compilation Error
View Answer

Answer: a
Explanation: A constructor is a member function of a class which initializes objects of a class. Constructor is automatically called when object is created. A parameterized constructor is executed in the above block of code.
Output:

$ javac Parameterised3.java
$ java Parameterised3
40

9. What is the output of the following Java code?

class Parameter 
{
    int num=10;
    Parameter()
    {
        System.out.print(num+" ");
    }
    Parameter(int num)
    {
        this.num = num;
        System.out.print(num+" ");
    }
    public static void main(String[] args)
    {
        Parameter obj = new Parameter();
        System.out.println(obj.num);
    }
}

a) 10
b) Null
c) -1
d) Compilation Error
View Answer

Answer: a
Explanation: Constructor is a block of code that initializes the newly created object. It is a special method for creating and initializing an object created within a class. In this case, the parameterized constructor is not called as there is no value given in the main() method.
Output:

$ javac Parameter.java
$ java Parameter
10

10. What is the output of the following Java code?

class ConstructorCall 
{
    int x, y; 
    public ConstructorCall(int x, int y) 
    {    
        this.x = x;    
        this.y = y; 
        System.out.println(x);
    }
    public static void main(String args[]) 
    {
        ConstructorCall p = new ConstructorCall();
    }
}

a) Null
b) Compilation Error
c) 0
d) Garbage Value
View Answer

Answer: b
Explanation: The main function calls the paramater-less constructor, but there is only one constructor defined in class which takes two parameters. Hence, the parameterized constructor is not executed and since there is no default constructor, the compiler throws an error.
Output:

$ javac ConstructorCall.java
$ java ConstructorCall
Uncompilable source code

11. What is the output of the following Java code?

class NoArgs 
{ 
    private int data; 
    private NoArgs() 
    { 
        data = 5; 
    } 
} 
public class NoArgsCall 
{ 
    public static void main(String[] args) 
    { 
        NoArgs obj = new NoArgs(); 
        System.out.println(obj.data); 
    } 
}

a) 5
b) 0
c) Garbage Value
d) Compilation Error
View Answer

Answer: d
Explanation: Default constructor is a constructor that is automatically generated by the compiler when the object is created. It is a constructor which can be called with no arguments. Since NoArgs() Constructor has private access, it cannot be accessed outside the NoArgs class.
Output:

$ javac NoArgsCall.java
$ java NoArgsCall
Uncompilable source code

12. What is the output of the following Java code?

class DefaultConstructor 
{
    boolean ch;
    public static void main(String[] args) 
    {
        DefaultConstructor obj = new DefaultConstructor();
        System.out.println(obj.ch);
 
    }
}

a) false
b) true
c) 0
d) 1
View Answer

Answer: a
Explanation: Since there is no constructor in the above code, the Java compiler will automatically create a no-argument constructor during run-time. This constructor is known as the default constructor. The default constructor initializes all instance variables with default values. Boolean datatype has a default value of ‘false’.
Output:

$ javac DefaultConstructor.java
$ java DefaultConstructor
false

13. What is the output of the following Java code?

class Derived extends Base
{
    Derived()
    {
        System.out.print("D");
    }
}
class Base
{  
    Base()
    {
        System.out.print("B");
    }
}
class MainMethod
{
    public static void main(String[] args)
    {
        Derived obj1=new Derived();
    }
}

a) BD
b) DB
c) Compilation Error
d) BDB
View Answer

Answer: a
Explanation: When a subclass constructor is called, default constructors of the superclass are called first and then the subclass constructor. Here when Derived is called, the superclass constructor, that is Base will be called first and then Derived will be called.
Output:

$ javac MainMethod.java
$ java MainMethod
BD

14. What is the output of the following Java code?

class Derived extends Base
{
    Derived()
    {
        System.out.print("D");
    }
}
class Base
{  
    Base()
    {
        System.out.print("B");
    }
}
class FunctionCall
{
    public static void main(String[] args)
    {
        Base obj1=new Base();
    }
}

a) B
b) D
c) BD
d) Compilation Error
View Answer

Answer: a
Explanation: A programmer-defined constructor that takes no parameters is called a default constructor. In the above block of code, subclass constructor Base() is called using the Base object and hence only the subclass constructor would be executed.
Output:

$ javac FunctionCall.java
$ java FunctionCall
B

15. What is the output of the following Java code?

class Default 
{ 
    Default d; 
    float f; 
} 
class InstanceVariables 
{ 
    public static void main(String args[]) 
    { 
        Default obj = new Default(); 
        System.out.print(obj.d+" "); 
        System.out.println(obj.f); 
    } 
}

a) Null 0f
b) Null 0.0
c) -1 0.0f
d) -1 0.0
View Answer

Answer: b
Explanation: The default constructor initializes all instance variables with default values. Float datatype has a default value of 0.0 while all objects of parent class have a value of ‘null’ unless they are initialized.
Output:

$ javac InstanceVariables.java
$ java InstanceVariables
null 0.0

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.