Object Oriented Programming using Java Questions and Answers – Constructor Overloading

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

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

class StudentData
{
   int ID;
   String Name;
   int Age;
   StudentData()
   {
       ID = 100;
       Name = "New";
       Age = 18;
   }
   StudentData(int num1, String str, int num2)
   {
       ID = num1;
       Name = str;
       Age = num2;
       System.out.println(ID);
   }
   public static void main(String args[])
   {
       StudentData obj = new StudentData();
       StudentData obj2 = new StudentData(14, "Dravid", 25);
  }
}

a) 14
b) 100
c) 1400
d) 14100
View Answer

Answer: a
Explanation: StudentData is a classs having two constructors- one of them is of default nature and other is of type parameterized. There are two objects ‘obj’ and ‘obj2’ created for that sake. The parameterized constructor having the print statement displays the output using the values given during the object creation.
Output:

$ javac StudentData.java
$ java StudentData
14

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

advertisement
advertisement
class PrintNumber
{
    float num;
    public PrintNumber(int num)
    {
        this.num=num;
    }
    public PrintNumber(float num)
    {
        this.num=num;
    }
    void Display()
    {
        System.out.println(num);
    }
    public static void main(String[] args) 
    {
        PrintNumber obj=new PrintNumber(2);
        PrintNumber obj1=new PrintNumber(2.0f);
        obj1.Display();
    }
}

a) 2.0
b) 2
c) 2.0f
d) 2.0d
View Answer

Answer: a
Explanation: ‘this’ keyword is used to refer to the currently executing object. In the above program, there is an instance variable named ‘num’ which is initialized using two constructors. There are two objects created for passing int and float values. Since the Display() method is called using obj1 object, the float value is printed.
Output:

$ javac PrintNumber.java
$ java PrintNumber
2.0

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

advertisement
class Overload
{
    float num;
    int x;
    public Overload(int num)
    {
        x=num;
    }
    public Overload(float num)
    {
        this.num=num;
    }
    void Display()
    {
        System.out.println(num);
    }
    public static void main(String[] args) 
    {
        Overload obj=new Overload(2);
        Overload obj1=new Overload(2.0);
        obj1.Display();
    }
}

a) 2.0
b) Compilation Error
c) 2.0f
d) 2.0d
View Answer

Answer: b
Explanation: One instance integer variable ‘x’ and the other float instance variable ‘num’ are initialized in separate constructors. Objects ‘obj’ and ‘obj1’ are created for the same purpose. As obj1 passes a float variable but calls the constructor used to initialize an integer variable, the compiler throws an error.
Output:

advertisement
$ javac Overload.java
$ java Overload
Uncompilable source code

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

class Sum
{
    private int num;
    Sum(int rn)
    {
        num = num + rn;
    }
    public static void main(String args[])
    {
        Sum obj = new Sum();
    }
}

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

Answer: c
Explanation: In the above program, the constructor Sum() takes an integer parameter and adds it with the instance variable ‘num’. But the object ‘obj’ created for calling the constructor doesn’t have any parameter, hence the compiler throws an error.
Output:

$ javac Sum.java
$ java Sum
Uncompilable source code

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

class Division
{
    int num;
    Division(int div)
    {
        num = div/num;
    }
    public static void main(String args[])
    {
        Division obj = new Division(50);
    }
}

a) 50
b) 0
c) Compilation Error
d) Null
View Answer

Answer: c
Explanation: In the above class Division, there is a parameterized constructor which takes the dividend as an input for the division operation to be performed inside the block. Since the instance variable ‘num’ is not initialized, it is assigned a default value of 0. Since division by 0 is not valid, the compiler throws an error during run-time.
Output:

$ javac Division.java
$ java Division
Uncompilable source code

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

class Cricket
{
    int runs,balls;
    Cricket(char x)
    {
        x=5;
    }
    Cricket(int runs, int balls)
    {
        int strikerate = runs/balls * 100;
    }
 
    public static void main(String args[])
    {
        Cricket obj = new Cricket(50);
    }
}

a) 50
b) Infinite
c) Compilation Error
d) 0
View Answer

Answer: c
Explanation: In the above program, the class Cricket has one parameterized constructor and one default constructor, used to calculate Strike Rate and other to assign a value of 5 respectively. As ‘runs’ and ‘balls’ are parameters, they need to be present in the function call in the main() method. But in this case, there is only one value given in the function call. Therefore, an error is thrown.
Output:

$ javac Cricket.java
$ java Cricket
Uncompilable Source Code

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

class Cricket2
{
    int runs,balls;
    Cricket2()
    {
        runs=0;
    }
    Cricket2(int runs, int balls)
    {
        int strikerate = runs/balls * 100;
        System.out.println(strikerate);
    }
    public static void main(String args[])
    {
        Cricket2 obj = new Cricket2(50, 50);
    }
}

a) 1
b) 100
c) Compilation Error
d) 50
View Answer

Answer: b
Explanation: The above program uses the concept of constructor overloading to perform an arithmetic operation. There are two constructors in the class Cricket2. One is a default constructor while the other is a parameterized one. Since the parameterized constructor is called in the main() method, it is executed while the initialization is ignored.
Output:

$ javac Cricket2.java
$ java Cricket2
100

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

class Data
{
    int num;
    Data()
    {
        num=0;
    }
    Data(int num)
    {
        System.out.println(num);
    }
    public static void main(String args[])
    {
        Data obj = new Data();
    }
}

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

Answer: d
Explanation: The above program is uses two constructors of the same name ‘Data’, which is also called as constructor overloading in object oriented programming. The parameterized constructor prints the input received but there is no input given during the object creation which indicates that only the default constructor is executed and hence no output.
Output:

$ javac Data.java
$ java Data
"Build Successful"

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

class Display
{
    long num;
    Display()
    {
        num=0;
        System.out.println(num);
    }
    Display(long num)
    {
        System.out.println(num);
    }
    public static void main(String args[])
    {
        Display obj = new Display(75);
    }
}

a) 75
b) 0
c) Null
d) Compilation Error
View Answer

Answer: a
Explanation: Constructor overloading is a concept of having more than one constructor with different parameters or with different datatypes. In the above program, class Display has two constructors-one default constructor and other of type parameterized. Only the parameterized constructor is executed by the compiler due to the object creation statement in the main() method.
Output:

$ javac Display.java
$ java Display
75

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

class DefaultConstructor
{
    float num;
    public DefaultConstructor(int num)
    {
        this.num=num;
    }
    public DefaultConstructor(float num)
    {
        this.num=num;
    }
    void Display()
    {
        System.out.println(num);
    }
    public static void main(String[] args) 
    {
        DefaultConstructor obj2=new DefaultConstructor(3);
        obj2.Display();
    }
}

a) 3
b) 0
c) Null
d) Compilation Error
View Answer

Answer: a
Explanation: In the above program, a class named DefaultConstructor has two default constructors used to initial the float variable ‘num’. The object created in the main() method is given an int value, hence the constructor with int parameter is executed.
Output:

$ javac DefaultConstructor.java
$ java DefaultConstructor
3

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

class Cube
{
    float num;
    public Cube(int num)
    {
        this.num=num*num*num;
    }
    public Cube(char num)
    {
        this.num=num*num*num;
    }
    void Display()
    {
        System.out.println(num);
    }
    public static void main(String[] args) 
    {
        Cube obj=new Cube(3.0);
        obj.Display();
    }
}

a) 27.0
b) 27
c) Compilation error
d) 0
View Answer

Answer: c
Explanation: In the above program, the class Cube has two constructors. While one of them has a parameter of type int, the other one has a parameter of type char. In the main() method, the object is created for a float value even though none of the constructors accept that value. Hence an error is thrown due to contrasting datatypes.
Output:

$ javac Cube.java
$ java Cube
Uncompilable Source Code

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

class Triangle extends Square
{
    Triangle(int b,int h)
    {
        double area=(b*h)/2;
        System.out.println(area); 
    }
    public static void main(String args[]) 
    {   
        Square obj=new Square(5);
    }
}
class Square
{
    Square(int l)
    {
        System.out.println(l*l);
    }
}

a) 25
b) 12.5
c) 12
d) Compilation Error
View Answer

Answer: a
Explanation: Constructor overloading is a concept in programming in which a class may have any number of constructors that differ in parameter list. It can be combined with inheritance as shown in the above program. The object created is of class Sqaure and hence the Square constructor is executed.
Output:

$ javac Triangle.java
$ java Triangle
25

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

 class Even
{
    long num;
    Even()
    {
        if(num%2 == 0)
            System.out.println(num--);
    }
    Even(long num)
    {
        System.out.println(num);
    }
    public static void main(String args[])
    {
        Even obj = new Even(6);
    }
}

a) 5
b) 6
c) 0
d) Compilation error
View Answer

Answer: b
Explanation: In the above program, class Even consists of two constructors. The parameterized constructor prints the number while the no-arguement constructor checks whether the number is even. In the object creation, only the parameterized constructor is executed as one value is given.
Output:

$ javac Even.java
$ java Even
6

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

class Number
{
    long num;
    Number(float num)
    {
        System.out.println(num--);
    }
    Number(char num)
    {
        System.out.println(num);
    }
    public static void main(String args[])
    {
        Number obj = new Number(6.0);
    }
}

a) 5.99
b) 6.0
c) Compilation Error
d) Garbage Value
View Answer

Answer: c
Explanation: The above program is an illustration of constructor overloading. Both the constructors are parameterized and take in float and char type parameters respectively. In the main method, the value passed during the object creation is of type float, hence an error is thrown.
Output:

$ javac Number.java
$ java Number
Uncompilable Source Code

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

class Unary
{
    protected int num;
    Unary(float num)
    {
        System.out.println(num--);
    }
    Unary(double num)
    {
        System.out.println(++num);
    }
    public static void main(String args[])
    {
        Unary obj = new Unary(6.0);
    }
}

a) 7.0
b) 7
c) 6.0
d) 5.0
View Answer

Answer: a
Explanation: The above program is an example of constructor overloading. There are two constructors with the same name but different parameters(data types). During the object creation, a double value of 6.0 is given, therefore the constructor with a double datatype is executed while the other constructor is ignored.
Output:

$ javac Unary.java
$ java Unary
7.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.

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.