Study and learn Interview MCQ Questions and Answers on Java Constructor Overloading. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Constructor Overloading before reading these objective questions.
public class TestingConstructor { void TestingConstructor() { System.out.println("Amsterdam"); } TestingConstructor() { System.out.println("Antarctica"); } public static void main(String[] args) { TestingConstructor tc = new TestingConstructor(); } }
Here the constructor is TestingConstructor() without return type.
public class Constructor2 { int count=10; Constructor2(int count) { System.out.println("Count=" + count); } public static void main(String[] args) { Constructor2 con = new Constructor2(); } }
If you write a constructor with arguments, the default constructor is not added by the compiler. You should add it explicitly.
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(); } }
You can pass parameters to another constructor.
Constructor5() { int a=30; this('A'); } Constructor5(char c) { // }
Constructor5() { int a=30; this('A'); System.out.println("Success"); } Constructor5(char c) { // }
Constructor5() { this('A'); System.out.println("Success"); } Constructor5(char c) { // }
Only the first statement should call another constructor.
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); } }
Constructor overloading allows constructors with different arguments at the same time.
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); } }
ROUTER=50.0 MODEM=false
ROUTER=50 MODEM=false
Java knows when to typecast a variable to a higher type like a float from int. So the number 50 is passed to a constructor accepting a float argument as there is no constructor accepting int argument above.
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); } }
2.Eats 40 Sugarcanes 2.Eats 10 Sugarcanes
1.Eats 40 Sugarcanes 2.Eats 10 Sugarcanes
Java supports using the varargs in constructors.
Overloading of constructors requires you to specify the same name to all constructors. So, it satisfies the polymorphism principle of Oops.
public class Constructor9 { Constructor9() { show(); } void show() { System.out.println("JAM JAM"); } public static void main(String[] args) { Constructor9 con = new Constructor9(); } }
Invoking a method from within a constructor is allowed.