Java - Online Test - Constructor - javasearch.buggybread.com


Share

Search Java Test Questions


 223 test questions in repository.




Java - Test Questions on 'Constructor' - 15 questions found


Which of the following is false about Constructors ?
Constructor can be overloaded
A no argument constructor is provided by the compiler if we declare only constructors with arguments.
Constructors shouldn't have any return types , not even void.
If super is not explicitly called, still super() is intrinsically added by the compiler.

  java  oops  constructor

What is a default constructor ?Core Java
Constructor without parameters declared by user
Constructor provided by Java if no constructor is declared
Constructor with empty body
All of the above

  Constructor

How can we create objects if we make the constructor private ?Core Java
We can't create objects if constructor is private
We can only create objects if we follow singleton pattern
We can only create one object
We can create new object through static method or static block

  constructor   private constructor

With the following code, Which is a valid way to initialize ?
public class BuggyBread {

   private String element1;

   private String element2;

   private BuggyBread(String element1, String element2){
      this.element1 = element1;
      this.element2 = element2;
   }

   public static class Builder {
   
      private String element1;

      private String element2;

      Builder(BuggyBread buggybread){
         element1 = buggybread.element1;
         element2 = buggybread.element2;
      }

      Builder withElement1(String element1){
         this.element1 = element1;
         return this;
      }

      Builder withElement2(String element2){
         this.element2 = element2;
         return this;
      }

      BuggyBread build(){
         BuggyBread buggybread = new BuggyBread(element1,element2);
         return buggybread;
      }
   }
}
Core Java
BuggyBread buggybread = new BuggyBread();
BuggyBread buggybread = new BuggyBread("element1","element2");
BuggyBread.Builder builder = new BuggyBread.Builder();
BuggyBread.Builder builder = new BuggyBread.Builder("element1","element2");

  builder pattern

What will be the output of following code ?

public class BuggyBread {
   
   private int x;
   private int y;
   
   BuggyBread(int x,int y){};
   
   public static void main(String[] args){
      BuggyBread buggybread = new BuggyBread();
      System.out.println(buggybread.x);
   }
}
Core Java
0
null
compilation error due to uninitialized element
compilation error due to constructor

  default constructor

What will be the output of following code ?

public class BuggyBread {

   private int x;
   private Integer y;

   private BuggyBread(int x,int y){};

   public static void main(String[] args){
      BuggyBread buggybread = new BuggyBread(1,2);
      System.out.println(buggybread.x);
   }
}
Core Java
compilation error due to private constructor
compilation error due to uninitialized elements
0 null
0 0

  constructor

What is the problem with this code ?

public class Car extends Vehicle{   
   int x;
   
   Car(int y){
      x = 5;
   }
   
   Car(){
      this(5);
      super();
   }
}
Core Java
We cannot overload constructors
Constructors should have type
we should have called super() before this(5)
We cannot have both super() and this() in a constructor

  Constructor  this  super

What is the problem with this code ?

public class Car extends Vehicle{
   int x;

   Car(int y){
      x = 5;
   }

   Car(){
      super();
      this.x = 5;
   }
}
Core Java
We cannot overload constructors
We cannot call super in overloaded constructor
we cannot have this and super in constructor
There is no problem

  constructor  this  super

The following code is an example of

public class Car extends Vehicle{
   int x;

   Car(int y){
      x = 5;
   }

   Car(){
      this(5);
   }
}
Core Java
Constructor Overloading
Constructor Chaining
Both Constructor Overloading and Chaining
None of above

  constructor  constructor overloading  constructor chaining

What is the problem with following code ?

public class Car extends Vehicle{
   int x;

   void Car(int y){
      x = 5;
   }

   void Car(){
      this(5);
   }
}
Core Java
We cannot use this() within normal method
We cannot chain methods
We cannot overload constructors
We cannot use return type void with overloaded methods

  constructor

Default Constructor is provided by Java , if ..Core Java
No constructor is provided by coder
No argument constructor is not provided by Coder
No Constructor is provided by coder initializing fields to null and primitive default values
No argument constructor is provided by coder

  Constructor

Default Constructor is provided by Java ... Core Java
To Reserve Memory
To provide at least one instance method
To Make it look good
To initialize the object state

  Constructor

Which of the following is not the use of this keyword ?Core Java
Passing itself to another method
To call the static method
Referring to the instance variable when local variable has the same name
Calling another constructor in constructor chaining

  this  this keyword  java

What will be the output of executing following class ?

public class BuggyBread {

static {
System.out.println("Static Block");
}

{
System.out.println("Initialization Block");
}

BuggyBread(){
System.out.println("Constructor");
}

public static void main(String[] args){
System.out.println("Main Method");
}
}
Core Java
Static Block
Main Method
Static Block
Instance Initialization Block
Main Method
Static Block
Constructor
Main Method
Static Block
Instance Initialization Block
Constructor
Main Method

  static block  Instance Initialization Block

What will be the output of following code ?

public class BuggyBread {

   private int x;

   private BuggyBread(int x){
      x = x;
   };

   public static void main(String[] args){
      BuggyBread buggybread = new BuggyBread(5);
      System.out.println(buggybread.x);
   }
}
Core Java
compilation error
undefined
0
5

  this keyword  coding  code







comments powered by Disqus