OCJP Practice Papers Java Stream API include following topics

  • Create methods with arguments and return values; including overloaded methods
  • Apply the static keyword to methods and fields
  • Create and overload constructors; differentiate between default and user definedconstructors
  • Apply access modifiers
  • Apply encapsulation principles to a class
  • Determine the effect upon object references and primitive values when they arepassed into methods that change the values

See the complete syllabus for OCJP here

  1. Fill in the blanks: The______ access modifier allows access to everything the ______access modifier does and more.
    A. package-private, protected
    B. protected, public
    C. protected, package-private
    D. private, package-private

2. What is the command to call one constructor from another constructor in the same class?
A. super()
B. this()
C. that()
D. construct()

3. What is the output of the following application? package stocks; public class Bond { private static int price = 5; public boolean sell() { if(price10) { price++; return true; } else if(price=10) { return false; } } public static void main(String[] cash) { new Bond().sell(); new Bond().sell(); new Bond().sell(); System.out.print(price); } }
A. 5
B. 6
C. 8
D. The code does not compile.

4. What is true about the following program? package figures; public class Dolls { public void nested() { nested(2,true); } // g1 public int nested(int level, boolean height) { return nested(level); } public int nested(int level) { return level+1; }; // g2 public static void main(String[] outOfTheBox) { System.out.print(new Dolls().nested()); } }
A. It compiles successfully and prints 3 at runtime.
B. It does not compile because of line g1.
C. It does not compile because of line g2.
D. It does not compile for some other reason.

5. Fill in the blank: Java uses ___________ to send data into a method.
A. pass-by-null
B. pass-by-value
C. both pass-by-value and pass-by-reference
D. pass-by-reference

6. Which of the following is a valid JavaBean method signature?
A. public void getArrow()
B. public void setBow()
C. public void setRange(int range)
D. public String addTarget(String target)

7. Which of the following statements about calling this() in a constructor is not true?
A. If this() is used, it must be the first line of the constructor.
B. If super() and this() are both used in the same constructor, super() must appear on the line immediately after this().
C. If arguments are provided to this(), then there must be a constructor in the class able to take those arguments.
D. If the no-argument this() is called, then the class must explicitly implement the

no-argument constructor. 8. Which of the following can fill in the blank to make the class compile? package ai; public class Robot { ____________ compute() { return 10; } }
A. Public int
B. Long
C. void
D. private String

9. Fill in the blank: A ____________variable is always available to all instances of the class.
A. public
B. local
C. static
D. instance

10. Which line of code, inserted at line p1, causes the application to print 5? package games; public class Jump { private int rope = 1; protected boolean outside; public Jump() { // p1 outside = true; } public Jump(int rope) { this.rope = outside ? rope : rope+1; } public static void main(String[] bounce) { System.out.print(new Jump().rope); } }
A. this(4);
B. new Jump(4);
C. this(5);
D. rope = 4;

11. Which of the following statements is not true?
A. An instance of one class may access an instance of another classs attributes if it has a reference to the instance and the attributes are declared public.
B. An instance of one class may access package-private attributes in a parent class, provided the parent class is not in the same package.
C. Two instances of the same class may access each others private attributes.
D. An instance of one class may access an instance of another classs attributes if both

classes are located in the same package and marked protected. 12. Given the following class, what should be inserted into the two blanks to ensure the class data is properly encapsulated? package storage; public class Box { public String stuff; ____________String____________ () { return stuff; } public void setStuff(String stuff) { this.stuff = stuff; } }
A. public and getStuff
B. private and isStuff
C. public and setStuff
D. None of the above

13. Which statement about a no-argument constructor is true?
A. The Java compiler will always insert a default no-argument constructor if you do not define a no-argument constructor in your class.
B. In order for a class to call super() in one of its constructors, its parent class must explicitly implement a no-argument constructor.
C. If a class extends another class that has only one constructor that takes a value, then the child class must explicitly declare at least one constructor.
D. A class may contain more than one no-argument constructor.

14. Which of the following method signatures does not contain a compiler error?
A. public void sing(String key, String... harmonies)
B. public void sing(int note, String... sound, int music)
C. public void sing(String... keys, String... pitches)
D. public void sing(String... notes, String melodies)

15. Given the following application, which diagram best represents the state of the mySkier, mySpeed, and myName variables in the main() method after the call to the slalom() method? package slopes; public class Ski { private int age = 18; private static void slalom(Ski racer, int[] speed, String name) { racer.age = 18; name = "Wendy"; speed = new int[1]; speed[0] = 11; racer = null; } public static void main(String... mountain) { final Ski mySkier = new Ski(); mySkier.age = 16; final int[] mySpeed = new int[1]; final String myName = "Rosie"; slalom(mySkier,mySpeed,myName); } } A. B. C. D. 16. Given the class below, which method signature could be successfully added to the class as an overloaded version of the findAverage() method? public class Calculations { public Integer findAverage(int sum) { return sum; } }
A. public Long findAverage(int sum)
B. public Long findAverage(int sum, int divisor)
C. public Integer average(int sum)
D. private void findAverage(int sum)

17. Which of the following is not a reason to use encapsulation when designing a class?
A. Promote usability by other developers.
B. Maintain class data integrity of data elements.
C. Prevent users from modifying the internal attributes of a class.
D. Increase concurrency and improve performance.

18. Which of the following data types can be modified after they are passed to a method as an argument?
A. int[]
B. String
C. long
D. boolean

19. What is the best way to call the following method from another class in the same package, assuming the class using the method does not have any static imports? package useful; public class MathHelper { public static int roundValue(double d) { // Implementation omitted } }
A. MathHelper:roundValue(5.92)
B. MathHelper.roundValue(3.1)
C. roundValue(4.1)
D. useful.MathHelper.roundValue(65.3)

20. Given a method with one of the following return types, which data type prevents the return statement from being used within the method?
A. byte
B. String
C. void
D. None of the above

21. How many final modifiers would need to be removed for this application to compile? package end; public final class Games { public final static int finish(final int score) { final int win = 3; final int result = score++ 5 ? 2 : win; return result+=win; } public static void main(final String[] v) { System.out.print(finish(Integer.parseInt(v[0]))); } }
A. None
B. One
C. Two
D. The code will not compile regardless of the number of final modifiers that are

removed. 22. Fill in the blanks: ____________is used to call a constructor in the parent class, while ____________is used to reference a member of the parent class.
A. super and this()
B. super and super()
C. super() and this
D. super() and super

23. Given the following method signature, which classes can call it? void run(String government)
A. Classes in other packages
B. Classes in the same package
C. Subclasses in a different package
D. All classes

24. Which statement(s) about the following class would help to properly encapsulate the data in the class? package shield; public class Protect { private String material; protected int strength; public int getStrength() { return strength; } public void setStrength(int strength) { this.strength = strength; } } I. Change the access modifier of strength to private. II. Add a getter method for material. III. Add a setter method for material.
A. I
B. II and III
C. I, II, and III
D. None, the data in the class is already encapsulated.

25. Which of the following is a valid method name in Java?
A. Go_$Outside$2()
B. have-Fun()
C. new()
D. 9enjoyTheWeather()

26. Which of the following lines of code can be inserted in the line below that would allow the class to compile? package farm; public class Coop { public final int static getNumberOfChickens() { // INSERT CODE HERE } }
A. return 3.0;
B. return 5L;
C. return 10;
D. None of the above

27. Which of the following is a true statement about passing data to a method?
A. A change made to a primitive value passed to a method is reflected in the calling method.
B. A change made to the data within an object passed to a method is reflected in the calling method.
C. Reassigning an object reference passed to a method is reflected in the calling method.
D. A change made to a boolean value passed to a method is reflected in the calling

method. 28. What is a possible output of the following application? package wrap; public class Gift { private final Object contents; protected Object getContents() { return contents; } protected void setContents(Object contents) { this.contents = contents; } public void showPresent() { System.out.print("Your gift: "+contents); } public static void main(String[] treats) { Gift gift = new Gift(); gift.setContents(gift); gift.showPresent(); } }
A. Your gift: wrap.Gift@29ca2745
B. Your gift: Your gift:
C. It does not compile.
D. It compiles but throws an exception at runtime.

29. Which of the following is a valid JavaBean method prefix?
A. is
B. gimme
C. request
D. put

30. Given the following two classes, each in a different package, which line inserted below allows the second class to compile? package clothes; public class Store { public static String getClothes() { return "dress"; } } package wardrobe; // INSERT CODE HERE public class Closet { public void borrow() { System.out.print("Borrowing clothes: "+getClothes()); } }
A. static import clothes.Store.getClothes;
B. import clothes.Store.*;
C. import static clothes.Store.getClothes;
D. import static clothes.Store;

31. What access modifier is used to mark class members package-private?
A. private
B. default
C. protected
D. None of the above

32. How many lines of the following program contain compilation errors? package sky; public class Stars { private int inThe = 4; public void Stars() { super(); } public Stars(int inThe) { this.inThe = this.inThe; } public static void main(String[] endless) { System.out.print(new sky.Stars(2).inThe); } }
A. None
B. One
C. Two
D. Three

33. Which of the following statements is true?
A. An instance method is allowed to reference a static variable.
B. A static method is allowed to reference an instance variable.
C. A static initialization block is allowed to reference an instance variable.
D. A final static variable may be set in a constructor.

34. Given the following method declaration, which line can be inserted to make the code compile? public short calculateDistance(double lat1, double lon1, double lat2, double lon2) { // INSERT CODE HERE }
A. return new Integer(3);
B. return new Byte((byte)6);
C. return 5L;
D. return new Short(4).longValue();

35. Which of the following statements about overloaded methods are true? I. Overloaded methods must have the same name. II. Overloaded methods must have the same return type. III. Overloaded methods must have a different list of parameters.
A. I
B. I and II
C. I and III
D. I, II, and III

36. How many lines of code would need to be removed for the following class to compile? package work; public class Week { private static final String monday; String tuesday; final static wednesday = 3; final protected int thursday = 4; }
A. One
B. Two
C. Three
D. The code will not compile regardless of the number of lines removed.

37. What is the output of the following application? package pet; public class Puppy { public static int wag = 5; // q1 public void Puppy(int wag) { // q2 this.wag = wag; } public static void main(String[] tail) { System.out.print(new Puppy(2).wag); // q3 } }
A. 2
B. It does not compile because of line q1.
C. It does not compile because of line q2.
D. It does not compile because of line q3.

38. Fill in the blanks: The ____________access modifier allows access to everything the ____________access modifier does and more.
A. public, private
B. private, package-private
C. package-private, protected
D. private, public

39. What is the output of the following application? package ship; public class Phone { private int size; public Phone(int size) {this.size=size;} public static void sendHome(Phone p, int newSize) { p = new Phone(newSize); p.size = 4; } public static final void main(String... params) { final Phone phone = new Phone(3); sendHome(phone,7); System.out.print(phone.size); } }
A. 3
B. 4
C. 7
D. The code does not compile.

40. Given the following class, which line of code when inserted below would prevent the class from compiling? public class Drink { public static void water() {} public void get() { // INSERT CODE HERE } }
A. water();
B. this.Drink.water();
C. this.water();
D. Drink.water();

41. Given the following method declaration signature, which of the following is a valid call of this method? public void call(int count, String me, String... data)
A. call(9,"me",10,"Al")
B. call(5)
C. call(2,"home","sweet")
D. call("answering","service")

42. Which statement about a static variable is true?
A. The value of a static variable must be set when the variable is declared or in a static initialization block.
B. It is not possible to read static final variables outside the class in which they are defined.
C. It is not possible to reference static methods using static imports.
D. A static variable is always available in all instances of the class.

43. Which of the following is not a true statement?
A. The first line of every constructor is a call to the parent constructor via the super() command.
B. A class does not have to have a constructor explicitly defined.
C. A constructor may pass arguments to the parent constructor.
D. A final instance variable whose value is not set when they are declared or in an

initialization block should be set by the constructor. 44. How many final modifiers would need to be removed for this application to compile? package park; public class Tree { public final static long numberOfTrees; public final double height; static {} { final int initHeight = 2; height = initHeight; } static { numberOfTrees = 100; height = 4; } }
A. None
B. One
C. Two
D. The code will not compile regardless of the number of final modifiers removed.

45. What is the output of the following application? package jungle; public class RainForest extends Forest { public RainForest(long treeCount) { this.treeCount = treeCount+1; } public static void main(String[] birds) { System.out.print(new RainForest(5).treeCount); } }c lass Forest { public long treeCount; public Forest(long treeCount) { this.treeCount = treeCount+2; } }
A. 5
B. 6
C. 8
D. The code does not compile.

46. What is the output of the following application? public class ChooseWisely { public ChooseWisely() { super(); } public int choose(int choice) { return 5; } public int choose(short choice) { return 2; } public int choose(long choice) { return 11; } public static void main(String[] path) { System.out.print(new ChooseWisely().choose((byte)2+1)); } }
A. 5
B. 2
C. 11
D. The code does not compile.

47. What is the output of the following application? package sports; public class Football { public static Long getScore(Long timeRemaining) { return 2*timeRemaining; // m1 } public static void main(String[] refs) { final int startTime = 4; System.out.print(getScore(startTime)); // m2 } }
A. 8
B. The code does not compile because of line m1.
C. The code does not compile because of line m2.
D. The code compiles but throws an exception at runtime.

48. Which of the following is a valid method name in Java?
A. $sprint()
B. \jog13()
C. walk#()
D. %run()

49. Assume there is a class Bouncer with a protected variable. Methods in which class can access this variable?
A. Only subclasses of Bouncer
B. Any subclass of Bouncer or any class in the same package as Bouncer
C. Only classes in the same package as Bouncer
D. Any superclass of Bouncer

50. Given the following two classes, each in a different package, which line inserted below allows the second class to compile? package commerce; public class Bank { public void withdrawal(int amountInCents) {} public void deposit(int amountInCents) {} } package employee; // INSERT CODE HERE public class Teller { public void processAccount(int depositSlip, int withdrawalSlip) { withdrawal(withdrawalSlip); deposit(depositSlip); } }
A. import static commerce.Bank.*;
B. static import commerce.Bank.*;
C. import static commerce.Bank;
D. None of the

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