Object Oriented Programming using Java Questions and Answers – Constructors

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

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

class Constructor 
{
    int x;  
    public Constructor() 
    {
        x = 5;
    }
    public static void main(String[] args) 
    {
        Constructor obj = new Constructor();
        System.out.println(obj.x);
    }
}

a) 5
b) 0
c) Garbage Value
d) -1
View Answer

Answer: a
Explanation: A constructor in Java is a special method that is used to initialize objects. They are used to set initial values for object attributes also. In this case, the variable ‘x’ is initialized in the same manner.
Output:

5

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

advertisement
advertisement
class Constructor 
{
    int x;  
    public void Constructor() 
    {
        x = 5;
    }
    public static void main(String[] args) 
    {
        Constructor obj = new Constructor();
        System.out.println(obj.x);
    }
}

a) 5
b) 0
c) Garbage Value
d) -1
View Answer

Answer: b
Explanation: A constructor in Java is a special method that is used to initialize objects. They have no return type, not even the ‘void’ keyword that is used to indicate that there is no return type. In this case, Constructor() is an ordinary method because it has the ‘void’ keyword. Hence, the compiler assigns a default value of 0 to ‘x’ which is a class variable.
Output:

0

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

class Constructor 
{
    static int x = 1;  
    public  Constructor() 
    {
        x = 5;
    }
    public static void main(String[] args) 
    {
        Constructor obj = new Constructor();
        System.out.println(obj.x);
    }
}

a) 5
b) 0
c) 1
d) -1
View Answer

Answer: a
Explanation: Constructor is a block of code that initializes the newly created object. The constructor has the same name as that of the class. In the above program, the constructor initializes the value of variable ‘x’ to 5.
Output:

advertisement
5

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

advertisement
class Constructor 
{
    static int x = 1;  
    public  Constructor(int n) 
    {
        x = n;
    }
    public static void main(String[] args) 
    {
        Constructor obj = new Constructor(4);
        System.out.println(obj.x);
    }
}

a) 5
b) 0
c) 1
d) 4
View Answer

Answer: d
Explanation: Constructor is a block of code that initializes the newly created object. The constructor has the same name as that of the class. In the above program, a parameterized constructor is used to initialize the value of class variable.
Output:

4

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

class Constructor 
{
    int x=1;  
    public Constructor(int n) 
    {
        x=x+n;
    }
    public static void main(String[] args) 
    {
        Constructor obj = new Constructor(3);
        System.out.println(obj.x);
    }
}

a) 5
b) 0
c) 1
d) 4
View Answer

Answer: d
Explanation: A constructor in Java is a special method that is used to initialize objects. The constructor has the same name as that of the class. Since the variable ‘x’ is of class type, it is possible to modify it inside the constructor.
Output:

4

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

class Constructor 
{
    final int x=1;  
    public Constructor(int n) 
    {
        x=x+n;
    }
    public static void main(String[] args) 
    {
        Constructor obj = new Constructor(3);
        System.out.println(obj.x);
    }
}

a) 5
b) Compilation error
c) Garbage Value
d) 4
View Answer

Answer: b
Explanation: A constructor is similar to a method that is invoked automatically when an object of the class is created. A constructor has the same name as that of class and it does not return any value. Here, variable ‘x’ of type final and hence it is not possible to modify the value of variables.
Output:

Uncompilable source code

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

class Constructor 
{
    private int x;
    private Constructor()
    {
        System.out.println("Constructor Called");
        x = 10;
    }
   public static void main(String[] args)
   {
       Constructor obj = new Constructor();
       System.out.println("Value of x = " + obj.x);
   }
}

a) 5
b) Compilation error
c) 10
d) 4
View Answer

Answer: c
Explanation: A constructor is a member function of a class which initializes objects of a class. A constructor has the same name as that of class and it does not return any value. It doesn’t return any value but can have an access-specifier to control the execution in different classes.
Output:

10

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

class Constructor 
{
    private int x;
    private Constructor()
    {
        System.out.println("Constructor Called");
        x = 15;
    }
}
class Main
{
   public static void main(String[] args)
   {
       Constructor obj = new Constructor();
       System.out.println("Value of x = " + obj.x);
   }
}

a) 5
b) Compilation error
c) 10
d) 4
View Answer

Answer: b
Explanation: The constructor method is a special method for creating and initializing an object created within a class. Since the variable ‘x’ is of type private, it has access only in the particular class. Members of a program present outside a class cannot be accessed.
Output:

Uncompilable source code

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

class Name
{   
    Name(String str)
    {
        System.out.println(str);
    }  
    public static void main(String args[])
    {  
        Name ob=new Name("Dravid");  
    }  
}

a) Dravid
b) Compilation error
c) Garbage value
d) Null
View Answer

Answer: a
Explanation: In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. An object ‘ob’ is created of class type ‘Name’. It is given a parameterized value of “Dravid”.
Output:

Dravid

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

class Name
{   
    String str="abc";
    Name()
    {
        System.out.println(str);
    }  
    public static void main(String args[])
    {  
        Name ob=new Name();  
    }  
}

a) abc
b) Null
c) Compilation error
d) -1
View Answer

Answer: a
Explanation: Constructor is a block of code that initializes the newly created object. It has the same name as that of the class. The constructor is invoked as soon as the object ‘ob’ of class Name is created.
Output:

abc

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

class Main 
{
    int x=5;
    Main()
    {
        this.x = 7;
    }
    public static void main(String[] args)
    {
        Main obj = new Main();
        System.out.println(obj.x);
    }
}

a) 5
b) Compilation error
c) 12
d) 7
View Answer

Answer: d
Explanation: A constructor in Java is a special method that is used to initialize objects. They are used to set initial values for object attributes also. ‘this’ keyword is used to refer to the current object.
Output:

7

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

class Main 
{
    int x=10;
    Main()
    {
        this.x = x;
    }
    public static void main(String[] args)
    {
        Main obj = new Main();
        System.out.println(obj.x);
    }
}

a) 5
b) Compilation error
c) Garbage value
d) 10
View Answer

Answer: d
Explanation: A constructor is a special method that is used to initialize a newly created object. ‘this’ keyword is a reference to the current object, whose method is being called upon.
Output:

10

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

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

a) Garbage value
b) 10
c) -1
d) 15
View Answer

Answer: d
Explanation: ‘this’ is a reference variable that refers to the current object. It is also used to distinguish between parameters and class variables. The constructor is invoked as soon as the object is created and the parameterized constructor is executed while the default constructor is ignored.
Output:

15 15

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

class Main 
{
    int x=7;
    Main()
    {
        System.out.print(x+" ");
    }
    Main(int x)
    {
        this.x = x;
        System.out.print(x+" ");
    }
    public static void main(String[] args)
    {
        Main obj = new Main(5);
        Main obj2= new Main();
    }
}

a) Garbage value
b) 7 5
c) 5 7
d) 5
View Answer

Answer: c
Explanation: The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name. It refers to the current object in a method or constructor. Here, the default constructor is called first and then the parameterized constructor is executed.
Output:

5 7

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

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

a) 10
b) Garbage value
c) Compilation error
d) 10 10
View Answer

Answer: a
Explanation: A constructor is a member function of a class which initializes objects of a class. There is only one constructor in the above program, that too a parameterized constructor. Since only a default constructor execution statement is given in the main() method, compiler assigns the value of 10 automatically and thereby the parameterized constructor is not executed.
Output:

10

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.