What will be the output of following code ?<br /> class BuggyBread2 {       <br />    private static int counter = 0;     <br />    void BuggyBread2() {        <br />       counter = 5;    <br />    }     <br />    <br />    BuggyBread2(int x){<br />       counter = x;    <br />    }        <br /> <br />    public static void main(String[] args) {        <br />       BuggyBread2 bg = new BuggyBread2();        <br />       System.out.println(counter);    <br />    } <br /> }
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers

 Q1. What will be the output of following code ?
class BuggyBread2 {       
   private static int counter = 0;     
   void BuggyBread2() {        
      counter = 5;    
   }     
   
   BuggyBread2(int x){
      counter = x;    
   }        

   public static void main(String[] args) {        
      BuggyBread2 bg = new BuggyBread2();        
      System.out.println(counter);    
   } 
}
Core Java
Ans.  Compile time error as it won't find the constructor matching BuggyBread2(). 
Compiler won't provide default no argument constructor as programmer has already defined one constructor. 
Compiler will treat user defined BuggyBread2() as a method, as return type ( void ) has been specified for that. 

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   code   coding   tricky questions   interesting questions   default constructor   constructor

Try 3 Question(s) Test


Related Questions

  Why Java provides default constructor ?
  What is a Default Constructor ?
  Will Compiler creates a default no argument constructor if we specify only multi argument constructor ?
  Difference between Class#getInstance() and new operator ?
  Can we create an object if a Class doesn't have any constructor ( not even the default provided by constructor ) ?
  Does every class needs to have one non parameterized constructor ?
  Variable of the boolean type is automatically initialized as?
  In a case where there are no instance variables what does the default constructor initialize?
 Is it mandatory to have a default constructor ?
 Why Java doesn't provide a default constructor if we define an argument constructor ?



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: