|
SCJP 5.0 [Declaration and Access Control] - 1
by Viral Gala
17/10/2007
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
Question 2:which are invalid declaration?
1. final int x2age = 5;
2. static int _name;
3. protected int abc;
4. abstract int emp;
question 3 : which are invalid keyword?
1. native
2. synchronized
3. goto
4. main
Question 4 : which are invalid keyword?
1. throw
2. throws
3. strictfp
4. struct
5. enum
Question 5 : Both the class are in different file.what will be the output?
package EMPone;
class Employee
{
public int empid;
public String name;
public int age;
static int count=0;
public Employee(int id,String name,int age)
{
this.empid=id;
this.name=name;
this.age=age;
count++;
}
}
package EMPtwo;
import EMPone.Employee;
class JavaBeat15
{
public static void main(String[] args)
{
Employee x1 = new Employee(1,"abc",21);
x1.age = 25;
System.out.println("The age of Employee x1 is "+x1.age);
}
}
1. compile time error.
2. Runtime Error.
3. The age of Employee x1 is 21
4. The age of Employee x1 is 25
Question 6 : Both the class are in different file.what will be the output?
package EMPone;
public class Employee
{
public int empid;
public String name;
public int age;
static int count=0;
public Employee(int id,String name,int age)
{
this.empid=id;
this.name=name;
this.age=age;
count++;
}
}
package EMPtwo;
class JavaBeat16
{
public static void main(String[] args)
{
Employee x1 = new Employee(1,"abc",21);
x1.age = 25;
System.out.println("The age of Employee x1 is "+x1.age);
}
}
1. compile time error.
2. Runtime Error.
3. The age of Employee x1 is 21
4. The age of Employee x1 is 25
Question 7 : what will be the output?
class JavaBeat17
{
public int x=5;
public int y=6;
public static void main(String[] args)
{ private int x=5;
int i=0;
for (i=0;i<5;i++)
x++;
System.out.println(" "+x+i);
}
}
1. Compile time error
2. 55
3. 105
4. 15
Question 8 : Both the class are in different file. what will be the output?
package EMPone;
public class Employee
{
public int empid;
public String name;
protected int age;
public Employee(int id,String name,int age)
{
this.empid=id;
this.name=name;
this.age=age;
}
}
package EMPtwo;
import EMPone.Employee;
class JavaBeat18
{
public static void main(String[] args)
{
Employee x1 = new Employee(1,"abc",21);
x1.age = 25;
System.out.println("The age of Employee x1 is "+x1.age);
}
}
1. compile time error.
2. Runtime Error.
3. The age of Employee x1 is 21
4. The age of Employee x1 is 25
Question 9 : Whether below code will compile or not?
interface Department
{
int Depid =15;
int getDepID();
}
class Manager implements Department
{
String name;
String Location;
public int getDepID()
{
return Depid;
}
public static void main(String[] args)
{
Manager x = new Manager();
x.Depid =16;
System.out.println("The Department is "+x.getDepID());
}
}
Whether above code will compile or not
1. True
2. False
Question 10 : Both the class are in different file.what will be the output?
package EMPone;
public class Employee
{
public int empid;
public String name;
protected int age;
static protected int count=0;
public Employee(int id,String name,int age)
{
this.empid=id;
this.name=name;
this.age=age;
count++;
}
public Employee()
{}
}
package EMPtwo;
import EMPone.Employee;
class JavaBeat20 extends Employee
{
public static void main(String[] args)
{
Employee x1 = new Employee(1,"abc",21);
x1.count = 10;
System.out.println("The count of Employee is "+x1.count);
}
}
1. compile time error.
2. The count of Employee is 1
3. The count of Employee is 10
4. The count of Employee is 0
|