Constructor Overloading in Java tutorial, Interview MCQ
Table of Contents
Constructor Overloading in Java Interview MCQ Questions and Answers
Study and learn Interview MCQ Questions and Answers on Java Constructor Overloading. Attend job interviews easily with these Multiple Choice Questions
Constructor Overloading in Java
All Java classes contain a Constructor. Even if we do not create a constructor, the compiler creates a default constructor for us. A Constructor’s name is the same as the Class name. Let us know more about Java Constructor Overloading and Constructor usage in this Java tutorial.
What is Constructor Overloading in Java
A Java Constructor is like a Method without Return Type in which initializations happen.
Java Constructors are of two types.
- Default Constructor
- User-Defined Constructor
1. Default Constructor
A Default Constructor in Java is without any parameters (Zero). If you do not write, Java compiler adds a default constructor before compiling for you. An example is given below.
class Piano { int keys; Piano() { //commented code //we can write some code like keys=20; } }
In the above example code, all new objects of the Piano type created will be having a default value Zero for the property “keys“.
2. User-Defined Constructor
A User-Defined constructor in Java is a constructor created by the user with a certain number of Parameters. An example is given below.
class Piano { long keys; Piano(long numKeys) { keys = numKeys; } }
In the above example, we have created a constructor that accepts a number of piano keys as a parameter. Piano’s Property “keys” has been initialized with the received argument or parameter. So, developers will have more comfort using this user-defined constructor.
Note: Constructors support implicit typecasting of parameters if those are of convertible or compatible data types. Here, upcasting of parameters happen automatically. Java Runtime automatically searches for a matching Constructor and invokes it. So, calling the above with Piano(int) parameter automatically calls the Piano(long) constructor if Piano(int) does not exist. Check the example below with integers and double values.
public Class Forest { double animalCount; Forest(double count) //accepts int, long, float and double values { animalCount = count; } public static void main(String[] args) { int animals = 10; Forest forest = new Forest(10); //passing int value System.out.println("Animals = " + forest.animalCount); } }
It is time to introduce Overloading of Java Constructors now.
Constructor Overloading
Java Constructor Overloading is nothing but writing more than one Constructor in a Class with distinct parameter list. That means Java allows you to use the same Constructor name any number of times as long as you write unique constructors. To maintain uniqueness, you can simply change the Data Type or Class Type while accepting parameters.
Java Constructor Signature is nothing but a combination of Constructor’s name and Parameter List. To overload a constructor, you should maintain a unique Constructor Signature.
Below is a good example that explains Constructor Overloading.
public class Piano { int keys; String name; float price; Piano(int keys2) { keys = keys2; } Piano(float price2) { price = price2; } Piano(String name2) { name = name; } Piano(String name2, int keys2, float price2) { //some code } Piano(int keys2, float price2, String name2) { //some code } Piano(float price2, int keys2, String name2) { //some code } public static void main(String[] args) { Piano p1 = new Piano("ONE"); Piano p2 = new Piano(32); Piano p3 = new Piano(50.5f); Piano p4 = new Piano("TWO", 24, 45.5f); Piano p5 = new Piano(40, 25.5f, "THREE"); Piano p6 = new Piano(94.5f, 50, "FOUR"); } }
If you do not Overload a Constructor properly, you will get an error. Let us check another example that shows wrong way of defining constructors that fail to overload.
class Piano2 { int keys; int price; Piano2() { System.out.println("NEW Piano Created."); } Piano2(int keysNum) //Piano2(int) { keys = keysNum; } Piano2(int price2) //Piano2(int) //Error: Duplicate Constructor Signature { price = price2; } }
You should do Constructor Overloading in such a way that the Java Runtime can identify correct which constructor to choose depend on the data type of parameters in the list. Otherwise, the compiler throws an error in the format “Duplicate method CONSTRUCTORNAME(datatype)“.
This is about Constructor Overloading in Java and its usage.
[WpProQuiz 159]
Constructor overloading in JAVA MCQ
1) A Java constructor is like a method without ___.
A) statements
B) return type
C) argument list
D) None
Answer [=] B
2) The name of a constructor and the name of a class are ___.
A) Same
B) Different
Answer [=] A
3) The placement of a constructor inside a class should be ___.
A) Always at the beginning of class
B) Always at the end of class
C) Anywhere in the class
D) None
Answer [=] C
4) The purpose of a Java constructor is ___.
A) Initialization of variables with passed data
B) Writing custom code
C) Accepting other objects as inputs
D) All the above
Answer [=] D
5) Memory is allocated to an object once the execution of ___ is over in Java language.
A) main method
B) constructor
C) destructor
D) None
Answer [=] B
6) What is the output of the below Java program?
public class TestingConstructor { void TestingConstructor() { System.out.println("Amsterdam"); } TestingConstructor() { System.out.println("Antarctica"); } public static void main(String[] args) { TestingConstructor tc = new TestingConstructor(); } }
A) Antarctica
B) Amsterdam
C) No output
D) Compiler error
Answer [=] A
Explanation:
Here the constructor is TestingConstructor() without return type.
7) In Java, a constructor with no parameters or no arguments is called ___ constructor.
A) Default constructor
B) User-defined constructor
Answer [=] A
8) In Java, a constructor with one or more arguments or parameters is called a ___ constructor.
A) Default constructor
B) User-defined constructor or Non-default constructor
Answer [=] B
9) The compiler adds a default no-argument constructor to a class if it ___.
A) does not define a constructor at all.
B) defines at least one constructor with arguments
Answer [=] A
10) Constructor Overloading in Java means adding more than ___ constructors with the different argument list.
A) 1
B) 2
C) 3
D) 8
Answer [=] A
11) What is the output of the below Java program with constructors?
public class Constructor2 { int count=10; Constructor2(int count) { System.out.println("Count=" + count); } public static void main(String[] args) { Constructor2 con = new Constructor2(); } }
A) Count=0
B) Count=10
C) Compiler error
D) None of the above
Answer [=] C
Explanation:
If you write a constructor with arguments, the default constructor is not added by the compiler. You should add it explicitly.
12) A constructor can call another overloaded constructor using the ___ keyword in Java.
A) super
B) local
C) con
D) this
Answer [=] D
13) What is the output of the below Java program with overloaded constructors? (Constructor Overloading)
public class Constructor3 { int birds=10; Constructor3() { this(20); } Constructor3(int birds) { System.out.println("Birds=" + birds); } public static void main(String[] args) { Constructor3 con = new Constructor3(); } }
A) Birds=0
B) Birds=10
C) Birds=20
D) Compiler error
Answer [=] C
Explanation:
You can pass parameters to another constructor.
14) In Java, you can pass __ variables from one constructor to another overloaded constructor. (Constructor Overloading)
A) local variables
B) static variables
C) non-static variables
D) local and static variables
Answer [=] D
15) Choose the correct way of calling the second constructor from the first constructor in the below code options.
A)
Constructor5() { int a=30; this('A'); } Constructor5(char c) { // }
B)
Constructor5() { int a=30; this('A'); System.out.println("Success"); } Constructor5(char c) { // }
C)
Constructor5() { this('A'); System.out.println("Success"); } Constructor5(char c) { // }
D) All the above
Answer [=] C
Explanation:
Only the first statement should call another constructor.
16) What is the output of the below Java program with many constructors?
public class Constructor7 { Constructor7(int a) { System.out.println("Book=" + a); } Constructor7(float a) { System.out.println("Pen="+ a ); } public static void main(String[] args) { Constructor7 con = new Constructor7(50.5f); } }
A) Book=50
B) Pen=50.5
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
Constructor overloading allows constructors with different arguments at the same time.
17) What is the output of the below Java program with many constructors?
public class Constructor8 { Constructor8(boolean a) { System.out.println("MODEM="+ a ); } Constructor8(float a) { System.out.println("ROUTER=" + a); } public static void main(String[] args) { Constructor8 con1 = new Constructor8(50); Constructor8 con2 = new Constructor8(false); } }
A)
ROUTER=50.0 MODEM=false
B)
ROUTER=50 MODEM=false
C) Compiler error
D) None
Answer [=] A
Explanation:
Java is capable of typecasting an int variable to a float when necessary. Because there isn’t a function Object() { [native code] } above that will take an int argument, the number 50 is passed to one that will.
18) What is the output of the below Java program with overloaded constructors? (Constructor Overloading)
public class Jiraffe { Jiraffe(int sugarcanes) { System.out.println("Eats "+ sugarcanes + " Sugarcanes"); } Jiraffe(int age, int...sugarcanes) { System.out.println("Eats "+ sugarcanes[0] + " Sugarcanes"); } public static void main(String[] args) { Jiraffe jiff2 = new Jiraffe(40); Jiraffe jiff = new Jiraffe(5,10); } }
A)
2.Eats 40 Sugarcanes 2.Eats 10 Sugarcanes
B)
1.Eats 40 Sugarcanes 2.Eats 10 Sugarcanes
C) Compiler error
D) None
Answer [=] B
Explanation:
Java supports using the varargs in constructors.
19) Choosing a suitable overloaded constructor happens at ___ time in Java.
A) Compile-time
B) Run time
Answer [=] B
20) Java constructor overloading follows ___ principle in Object-Oriented programming.
A) Inheritance
B) Encapsulation
C) Polymorphism
D) None
Answer [=] C
Explanation:
Constructor Overloading requires you to specify the same name to all constructors. So, it satisfies the polymorphism principle of Oops.
21) Java allows calling or invoking a method from a constructor. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
22) What is the output of the below Java program?
public class Constructor9 { Constructor9() { show(); } void show() { System.out.println("JAM JAM"); } public static void main(String[] args) { Constructor9 con = new Constructor9(); } }
A) JAM JAM
B) No output
C) Compiler error
D) None
Answer [=] A