Test Series - java

Test Number 52/64

Q: 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
Solution: If you write a constructor with arguments, the default constructor is not added by the compiler. You should add it explicitly.
Q: A constructor can call another overloaded constructor using the ___ keyword in Java.
A. super
B. local
C. con
D. this
Solution: no solution
Q: What is the output of the below Java program with overloaded constructors?
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
Solution: You can pass parameters to another constructor.
Q: In Java, you can pass __ variables from one constructor to another overloaded constructor.
A.  local variables
B. static variables
C. non-static variables
D.  local and static variables
Solution: no solution
Q: 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
Solution: Constructor overloading allows constructors with different arguments at the same time.
Q: 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
Solution: 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.
Q: What is the output of the below Java program with overloaded constructors?
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
Solution: Java supports using the varargs in constructors.
Q: Choosing a suitable overloaded constructor happens at ___ time in Java.
A. Compile-time
B. Run time
C. -
D. -
Solution: no solution
Q: Java constructor overloading follows ___ principle in Object-Oriented programming.
A. Inheritance
B. Encapsulation
C. Polymorphism
D. None
Solution: Overloading of constructors requires you to specify the same name to all constructors. So, it satisfies the polymorphism principle of Oops.
Q: Java allows calling or invoking a method from a constructor. State TRUE or FALSE.
A. TRUE
B. FALSE
C. -
D. -
Solution: no solution
Q: 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
Solution: Invoking a method from within a constructor is allowed.

You Have Score    /11