OCJP Practice Papers-A Advanced Java Class Design

OCJP Practice Papers Advanced Java Class Design Include following topics

  • Develop code that uses abstract classes and methods
  • Develop code that uses the final keyword
  • Create inner classes including static inner class, local class, nested class, andanonymous inner class
  • Use enumerated types including methods, and constructors in an enum type
  • Develop code that declares, implements and/or extends interfaces and use the@Override annotation
  • Create and use Lambda expressions

See the complete syllabus for OCJP here

Part 1 - OCJP Practice Papers Advance Java Class Design

1. Which of the following is required for all valid lambda expressions?
A. ()
B. -
C. {}
D. Parameter data type(s)

2. What is the output of the following application? package holiday; enum DaysOff { Thanksgiving, PresidentsDay, ValentinesDay }public class Vacation { public static void main(String... unused) { final DaysOff input = DaysOff.Thanksgiving; switch(input) { default: case DaysOff.ValentinesDay: System.out.print("1"); case DaysOff.PresidentsDay: System.out.print("2"); } } }
A. 1
B. 2
C. 12
D. None of the above

3. Fill in the blanks: A functional interface must contain or inherit ____________andmay optionally include____________ .
A. at least one abstract method, the @Override annotation
B. exactly one method, static methods
C. exactly one abstract method, the @FunctionalInterface annotation
D. at least one static method, at most one default method

4. Which of the following class types cannot be marked final or abstract?
A. Static nested class
B. Local inner class
C. Anonymous inner class
D. Member inner class

5. Which of the following is a valid lambda expression?
A. r - {return 1==2}
B. (q) - true
C. (x,y) - {int test; return test0;}
D. a,b - true

6. Which of the following properties of an enum can be marked abstract?
A. The enum class definition
B. An enum method
C. An enum value
D. None of the above

7. What is the output of the following application? package world; public class Matrix { private int level = 1; class Deep { private int level = 2; class Deeper { private int level = 5; public void printReality() { System.out.print(level); System.out.print(" "+Matrix.Deep.this.level); System.out.print(" "+Deep.this.level); } } } public static void main(String[] bots) { Matrix.Deep.Deeper simulation = new Matrix().new Deep().new Deeper(); simulation.printReality(); } }
A. 1 1 2
B. 5 2 2
C. 5 2 1
D. The code does not compile.

8. A local inner class can access which type of local variables? I. final II. private III. effectively final
A. I only
B. I and II
C. III only
D. I and III

9. What is the output of the following application? package finance; enum Currency { DOLLAR, YEN, EURO }a bstract class Provider { protected Currency c = Currency.EURO; }p ublic class Bank extends Provider { protected Currency c = Currency.DOLLAR; public static void main(String[] pennies) { int value = 0; switch(new Bank().c) { case 0: value--; break; case 1: value++; break; } System.out.print(value); } }
A. 0
B. 1
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

10. What statement best describes the notion of effectively final in Java?
A. A local variable that is marked final
B. A static variable that is marked final
C. A local variable that is not marked final but whose primitive value or object reference does not change after it is initialized
D. A local variable that is not marked final but whose primitive value or object

reference does not change after a certain point in the method 11. What is the output of the following application? package race; interface Drive { int SPEED = 5; default int getSpeed() { return SPEED; } }interface Hover { int MAX_SPEED = 5; default int getSpeed() { return MAX_SPEED; } }p ublic class Car implements Drive, Hover { public static void main(String[] gears) { class RaceCar extends Car { @Override public int getSpeed() { return 10; } }; System.out.print(new RaceCar().getSpeed()); } }
A. 5
B. 10
C. The code does not compile due to the definition of Racecar.
D. The code does not compile for some other reason.

12. Fill in the blanks: It is possible to extend an ____________but not an____________.
A. interface, abstract class
B. abstract class, enum
C. enum, interface
D. abstract class, interface

13. Which of the following results is not a possible output of this program? package sea; enum Direction { NORTH, SOUTH, EAST, WEST; }; public class Ship { public static void main(String[] compass) { System.out.print(Direction.valueOf(compass[0])); } }
A. WEST is printed.
B. south is printed.
C. An ArrayIndexOutOfBoundsException is thrown at runtime.
D. An IllegalArgumentException is thrown at runtime.

14. Which of the following is not an advantage of using enumerated types in Java?
A. Ensure consistency of data across an application.
B. Offer ability to create new enumerated values at runtime.
C. Provide access to fixed constants whose value does not change during the course of the application.
D. Support cases where a value can only take one of a limited number of options.

15. Given the following enum declaration, how many lines contain compilation errors? package rainbow; enum Light {} public enum Color extends Light { RED, BLUE, ORANGE, GREEN protected Color() {} }
A. None, the code compiles as is.
B. One
C. Two
D. Three

16. Which of the following cannot include a static method in its definition?
A. Abstract class
B. Static nested class
C. Interface
D. Local inner class

17. What is the output of the following application? package ai; interface Pump { void pump(double psi); }i nterface Bend extends Pump { void bend(double tensileStrength); }public class Robot { public static final void apply(Bend instruction, double input) { // r1 instruction.bend(input); } public static void main(String... future) { final Robot r = new Robot(); r.apply(x - System.out.print(x+" bent!"), 5); } }
A. 5.0 bent!
B. The code does not compile because Bend is not a functional interface.
C. The code does not compile because of line r1.
D. None of the above.

18. What is the best reason for applying the @Override annotation to a method?
A. It is required to implement an interface method.
B. It is required to override a method.
C. The method will fail to compile if it is not actually overriding another method.
D. There are no good reasons other than as a form of documentation.

19. What is the output of the following application? package space; public class Bottle { public static class Ship { private enum Sail { // w1 TALL {protected int getHeight() {return 100;}}, SHORT {protected int getHeight() {return 2;}}; protected abstract int getHeight(); } public Sail getSail() { return Sail.TALL; } } public static void main(String[] stars) { Bottle bottle = new Bottle(); Ship q = bottle.new Ship(); // w2 System.out.print(q.getSail()); } }
A. TALL
B. The code does not compile because of line w1.
C. The code does not compile because of line w2.
D. The code compiles but the application does not produce any output at runtime.

20. Which of the following is not a valid lambda expression?
A. (Integer j, k) - 5
B. (p,q) - p+q
C. (Integer x, Integer y) - x*y
D. (left,right) - {return "null";}

OCJP Practice Test, Certification Path, Tips/Tricks

  1. Oracle (OCJP) Java Certification Exam
  2. Java Certification Tips And Tricks
  3. OCJP Practice Papers - Java Basics
  4. OCJP Practice Papers - Operators And Decision Constructs
  5. OCJP Practice Papers-A Advanced Java Class Design
  6. OCJP Practice Papers Creating And Using Arrays
  7. OCJP Practice Papers Using Loop Constructs
  8. OCJP Practice Papers - Generics And Collections
  9. OCJP Practice Papers - Lambda Built-In Functional Interfaces
  10. OCJP Practice Papers Java Class Design
  11. OCJP Practice Papers - Java Stream API
  12. OCJP Practice Papers - Exceptions And Assertions
  13. OCJP Practice Papers - Date/Time API
  14. OCJP Practice Papers - Java I/O Fundamentals
  15. OCJP Practice Papers - Working With Methods And Encapsulation
  16. OCJP Practice Papers - Working With Inheritance
  17. OCJP Practice Papers - Handling Exceptions
  18. OCJP Practice Papers - Selected Classes From Java API
  19. OCJP Practice Papers - Java File I/O Nio.2
  20. OCJP Practice Papers - Java Concurrency