DECLARATIONS,INITIALISATIONS,SCOPES

QUESTIONS

Question 1 : what will be the output?
class Employee
{
  private int empid;
  String name;
  Employee(int id,String name)
  {
  this.empid=id;
  this.name=name;
  }

  public int getEmpId()
  {
          return empid;
  }


  public String getString()
  {
          return name;
  }

}


class  JavaBeat11
{
 public static void main(String[] args)
 {
  Employee x1 = new Employee(1,"abc");
  Employee x2 = new Employee(2,"def");
  Employee x3 = new Employee(3,"ghi");
  
  x1.empid=11;

  System.out.println("Employee id of x1 is :  "+ x1.getEmpId());
  System.out.println("Employee id of x2 is :  "+ x2.getEmpId());
  System.out.println("Employee id of x3 is :  "+ x3.getEmpId());

 }
}

1. compile time error.
2. Runtime Error.
3. Employee id of x1 is : 11
Employee id of x2 is : 2
Employee id of x3 is : 3
4. Employee id of x1 is : 1
Employee id of x2 is : 2
Employee id of x3 is : 3

1) Ans : 1->compile time error
Instance variable empid has private access in class Employee
It can be access only within the class Employee and using its member function
since class javabeat11 is trying to access instance variable empid we will get compile time error

Question 2:which are invalid declaration?
1. final int x2age = 5;
2. static int _name;
3. protected int abc;
4. abstract int emp;
2)ans : 4
abstract keyword can be applied to class,interface or method
abstract keyword can't be applied to instance variable
                                                                                                                                                                    MORE

110MB Free Hosting Network