OCJP Certification Dumps


1: Employee[] department = new Employee[10];
What exception occurs when you try adding an eleventh employee to the department?

  1. IOException
  2. NullPointerException
  3. ArithmeticException
  4. ArrayIndexOutOfBoundsException
Answer: 4 – ArrayIndexOutOfBoundsException

2: How would you loop through the Clothing array items to calculate the total price of all items after tax? The Clothing class has a public double field price. Assume the double variables total and tax are already declared

A. forEach(Clothing c in items){
total += c.price*(1+tax);
}

B. for(Clothing c: items){
total = c.price*(1+tax);
}

C. for(Clothing c: items){
total = total + c.price*(1+tax);
}

D. for(Clothing c: items[]){
total = total + c.price*tax;
}

Answer: B

3. How would you create a Employee class with a String field name?

A) public class Employee {
    name String;
}

B) public class Employee {
    String = name;
}

C) public class Employee {
    String name;
}

D) public class Employee {
    String = "name";
}
Answer: C

4. You’ve encapsulated the Employee class’s name field. The related setName method requires an String argument. You instantiate a Employee e1 in the main method, which is written in another class. How should you set the name of Employee e1 from the main method?

  1. e1.setName(“Vijay”)
  2. e1.name = “Vijay”
  3. e1.setEmployee(new Employee())
  4. e1.name = new Employee()
Answer: 1

5. If you override the toString method in Employee class which extended User then from which class does this method originate?

  1. Comparable
  2. Object
  3. Employee
  4. User
Answer: 2- Object

6.How would you declare the Employee constructor? Employee emp= new Employee (“Vijay”, 26, “M”);

  1. public Employee(String name, int age, String gender){…}
  2. public Employee (String name, double age{…}
  3. public Employee (String name, String age, String gender){…}
  4. public void Employee (String name, int age, String gender){…}
  5. public void Employee name, age{…}
Answer: 1- public Employee(String name, int age, String gender){…}

7.You have an array of Employee objects called employees. The Employee class has public fields for gender and age. You create a loop to get the same age of employees. How would you add an if statement to ensure an employee is only added to the total if it’s the same age as the employee e1? Assume the Employee class has a public int field age.

A) for(Employee e: employees){
     if(e.age == e1.age)){
//add to list of same age
}
}
B) for(Employee e: employees){
    if(e == e1)){
        //add to list of same age 
    }
}
C) for(Employee e: employees){
    if(employees[e].age == e1.age){
     //add to list of same age
    }
}
D) for(Employee e: employees){
    if(e.age == e1)){
    //add to list of same age
    }
}
Answer: A

8. You’re encapsulating the Employee class’s department field by writing a setDepartment method. The department field must always be set to default “CSE” setDepartment receives null value. How would you implement this method?

A. public void setDepartment(String  departmentName) {
    if(departmentName!=null){
        this.departmentName = departmentName;
    }
    else{
         this.departmentName = "CSE";
    }
}
B. public void setDepartment(String  departmentName) {
   if(departmentName == null){
     this.departmentName = "CSE";
    }
   this.departmentName = departmentName;
    
}
C. public void setDepartment() {
    if(departmentName!=null){
        this.departmentName = departmentName;
    }
    else{
         this.departmentName = "CSE";
    }
}
D. public void setDepartment(String  departmentName) {
    this.departmentName = departmentName;
}
Answer: A

9. You have a Employee class with a public String field name. What would you write in a main method to create a Employee instance and set its name field to Arjun?

A) Employee emp = new Employee ();
emp  = "Arjun";

B) Employee emp ;
emp .name = "Arjun";

C) Employee emp  = "Arjun";

D) Employee emp  = new Employee ();
emp .name = "Arjun";
Answer: D

10.Which is a valid print statement?

  1. System.println(“Hellow World!!!”);
  2. System.out.println(Hellow World!!!)
  3. System.out.println(“Hellow World!!!”);
  4. System.out(Hellow World!!!).println;
Answer: 3