Constructors


1
Question 1
Predict the output?
package main;
class T {
  int t = 20;
}
class Main {
   public static void main(String args[]) {
      T t1 = new T();
      System.out.println(t1.t);
   }
}
A
20
B
0
C
Compiler Error
Constructors    
Discuss it


Question 1 Explanation: 
In Java, member variables can assigned a value with declaration. In C++, only static const variables can be assigned like this.
Question 2
Predict the output of following Java program
class T {
  int t = 20;
  T() {
    t = 40;
  }
}
class Main {
   public static void main(String args[]) {
      T t1 = new T();
      System.out.println(t1.t);
   }
}
A
20
B
40
C
Compiler Error
Constructors    
Discuss it


Question 2 Explanation: 
The values assigned inside constructor overwrite the values initialized with declaration.
Question 3
Which of the following is/are true about constructors in Java?

1) Constructor name should be same as class name.
2) If you don't define a constructor for a class, 
    a default parameterless constructor is automatically
    created by the compiler. 
3) The default constructor calls super() and initializes all 
   instance variables to default value like 0, null.
4) If we want to parent class constructor, it must be called in 
   first line of constructor.
A
1
B
1, 2
C
1, 2 and 3
D
1, 2, 3 and 4
Constructors    
Discuss it


Question 4
Is there any compiler error in the below Java program?
class Point {
    int m_x, m_y; 
    public Point(int x, int y) {    m_x = x;    m_y = y;  }
    public static void main(String args[]) 
    {
      Point p = new Point();
    }
}

A
Yes
B
No
Constructors    
Discuss it


Question 4 Explanation: 
The main function calls paramaterless constructor, but there is only one constructor defined in class which takes two parameters. Note that if we write our own constructor, then compiler doesn't create default constructor in Java. This behavior is same as C++.
Question 5
Output of following Java program
class Point {
  int m_x, m_y;
  
  public Point(int x, int y) { m_x = x; m_y = y; }
  public Point() { this(10, 10); }
  public int getX() { return m_x; }
  public int getY() { return m_y; }
  
  public static void main(String args[]) {
    Point p = new Point();
    System.out.println(p.getX());
  }
} 
A
10
B
0
C
compiler error
Constructors    
Discuss it


Question 5 Explanation: 
it's a simple program where constructor is called with parameters and values are initialized.
Question 6
final class Complex {
    private  double re,  im;
    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }
    Complex(Complex c)
    {
      System.out.println("Copy constructor called");
      re = c.re;
      im = c.im;
    }            
    public String toString() {
        return "(" + re + " + " + im + "i)";
    }            
}
class Main {
    public static void main(String[] args) {
        Complex c1 = new Complex(10, 15);
        Complex c2 = new Complex(c1);    
        Complex c3 = c1;  
        System.out.println(c2);
    }
}
A
Copy constructor called
(10.0 + 15.0i)
B
Copy constructor called
(0.0 + 0.0i)
C
(10.0 + 15.0i)
D
(0.0 + 0.0i)
Constructors    
Discuss it


Question 7
class Test
{
    static int a;
    
    static
    {
        a = 4;
        System.out.println ("inside static block\n");
        System.out.println ("a = " + a);
    }
    
    Test()
    {
        System.out.println ("\ninside constructor\n");
        a = 10;
    }
    
    public static void func()
    {
        a = a + 1;
        System.out.println ("a = " + a);
    }
    
    public static void main(String[] args)
    {

        Test obj = new Test();
        obj.func();

    }
}
 
A
inside static block
a = 4
inside constructor
a = 11
B
Compiler Error
C
Run Time Error
D
inside static block
a = 4
inside constructor
a = 5
E
inside static block
a = 10
inside constructor
a = 11
Constructors    
Discuss it


Question 7 Explanation: 
Static blocks are called before constructors. Therefore, on object creation of class Test, static block is called. So, static variable a = 4. Then constructor Test() is called which assigns a = 10. Finally, function func() increments its value.
There are 7 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice