Constructor
Constructor
---------------
What is the output of the below program?
class T
{
int pCode= 99;
}
public class Main
{
public static void main(String args[])
{
T t1 = new T();
System.out.println(t1.pCode);
}
}
a)99
b)Runtime Error
c)Compile time error
d)Blank
Answer:a)99
Question-2
---------------
What is the output of the below program?
class Employee{
private String name;
public Employee(String name) {
this.name=name;
}
}
public class Test16 {
public static void main(String[] Java) {
Employee e = new Employee("JavaDeveloper");
System.out.println("Emp Name : "+e.name);
}
}
Question: 3
--------------
Can we declare constructor as private ?
a)Yes
b)No
Answer:a)Yes
Question : 4
---------------
a)private
b)protected
c)public
d)final
Answer:d)final
Question :5
---------------
What happens if we add return type to constructor ?
Question-6
--------------
What is the default accessibility modifier of a default constructor?
a)private
b)protected
c)default
d)same as class accessibility modifier
Question-7
--------------
What is the output of the below program?
public class Test7 {
private Test7 (int x) { //line 1
System.out.print(x);
}
private static Test7() { //line 2
System.out.print(25);
}
public static void main(String[] args){
Test7 t = new Test7(35);
}
}
a)35
b)25
c)Error at line no 1
d)Runtime Error
e)Error at line no 2
Question-8
---------------
The implicit return type of a constructor is ?
a)void
b)There is no return type
c)a class object in which it is defined
d)None of these
Question-9
--------------
What is the output of the below program?
class Person{
Person(){
System.out.println(“Hello constructor!”);
return;
}
}
public class Test17 {
public static void main(String[] var) {
Person p = new Person();
}
}
a)Hello constructor!
b)Blank Screen
c)Compiler time Error
d)Runtime Error
Answer:a)Hello constructor!
Question-10
----------------
Choose correct option for the following code
public class ConstructorTest {
public int a = 10;
public ConstructorTest() {
}
public ConstructorTest(int a) {
this.a = a;
System.out.println(a);
}
Answer:b)Run successfully
Question-11
----------------
public class Test
{ }
What is the prototype of the default constructor ?
a)public Test(void)
b)Test()
c)Test(void)
d)public Test()
Answer:d)public Test()
Question-12
----------------
What is the output of following code?
class Test
{
int a=10;
int b=20;
public static void main(String[] args)
{
Test e1=new Test();
Test e2=new Test();
System.out.println(e1.a+". .."+e1.b); //10 20
System.out.println(e2.a+". .."+e2.b); //10 20
e1.a=50;
e2.b=60;
System.out.println(e1.a+". .."+e1.b); //50 20
System.out.println(e2.a+". .."+e2.b); //10 60
}
}
a)
10 20
10 20
10 20
10. ..20
b)
..20
10 20
50 60
50 60
c)
10 20
10 20
50 10
20. ..60
d)
10 20
10 20
50 20
10 60
Answer:d)
10 20
10 20
50 20
10 60
Question-13
----------------
In constructor overloading, what must differ between the constructors?
a) Return type
b) Access modifier
c) Parameter list
d) Method body
Question-14
----------------
Which of the following statements is true about overloaded constructors?
Question-15
----------------
Which keyword is used for constructor chaining in Java?
a) super
b) this
c) new
d) self
e)both a & b
Answer:e)both a & b
Question-16
----------------
In a constructor chaining scenario, where should the call to another constructor be
placed?
Question-17
---------------
Consider the following code snippet :
class A {
A() {
this(10);
}
A(int x) {
System.out.println(x);
}
}
a) No output
b) 10
c) Compilation error
d) Runtime error
Answer:b) 10
Question-18
---------------
Given the following class, what will happen if an object is instantiated with new
Example(5)?
class Example {
Example() {
this(0);
System.out.println("Default Constructor");
}
Example(int x) {
System.out.println("Parameterized Constructor: " + x);
}
}
a) "Default Constructor"
b) "Parameterized Constructor: 0"
c) "Parameterized Constructor: 5"
d) Compilation error
Question-19
----------------
In constructor chaining, if this() is used, what must be ensured?
Question-20
---------------
In the following code, which constructor will be called when an object of Demo is
created with new Demo(10)?
class Demo {
Demo() {
this(0);
System.out.println("Default");
}
Demo(int x) {
this("Demo");
System.out.println("Integer: " + x);
}
Demo(String name) {
System.out.println("String: " + name);
}
}
a) Demo()
b) Demo(int x)
c) Demo(String name)
d) Compilation error
Question-21
----------------
Analyze the following code snippet:
class Example {
Example() {
System.out.println("No-argument constructor");
}
Example(int x) {
this();
System.out.println("Parameterized constructor: " + x);
}
}
What will be the output when an object is created with new Example(5);?
a) No-argument constructor
b) Parameterized constructor: 5
c) No-argument constructor followed by Parameterized constructor: 5
d) Compilation error
Question-22
----------------
public class Batman {
int squares = 81;
public static void main(String[] args) {
new Batman().go(); //new Batman() is an anonymous object and it is
calling go method
}
void go() {
incr(++squares);
System.out.println(squares);
}
void incr(int squares) {
squares += 10;
}
}
11. What is the result?
A. 81
B. 82
C. 91
D. 92
E. An exception is thrown at runtime.
Answer:B. 82
Question-23
----------------
public class Think {
int iq = 7;
Think() {
System.out.print("iq :" + (iq = 8));
this();
}
Think() {
System.out.println("iq :" + (iq = 9));
}
a)iq : 8
iq :9
b)iq : 8 iq :9
c)Recursive Constructor call....Runtime Error
d)Compile time error
Answer:The given code will result in a compile-time error due Duplicate Constructor
Definitions
Question-24
---------------
public class UseYourBrain{
int i=5;
private UseYourBrain(){
this(i);
System.out.println("NPC... ");
}
}
protected UseYourBrain(String message){
super();
System.out.println("Message :"+message);
}
public static void main(String [] args ){
new UseYourBrain();
}
}
a)Message :hi
PC :5
NPC...
b)NPC...
PC :5
Message :hi
d)Runtime Error
Constructor chaining and overloading.txt
Displaying Constructor chaining and overloading.txt.
Answer:a)Message :hi
PC :5
NPC...