Interview questions on Java coding 3 - output for below code

1)what is the output for below code.

class Counter{  
static int count=0;//will get memory when instance is created  
  
Counter(){  
count++;  
System.out.println(count);  
 
public static void main(String args[]){  
  
Counter c1=new Counter();  
Counter c2=new Counter();  
Counter c3=new Counter();  
  
 }  

} 

Ans: 1
2
3

2)what is the output for below code.
 static class  Emp {
        int eno=10;
        public int getEmp(){
                return eno;
        }
}
 public class TestEmp
 {
         public static void main(String[] args) {
                Emp obj=new Emp();
                System.out.println(obj.getEmp());
        }

 }

Ans: we can't write the static class out side the class

Share This

0 comments: