OCJP Practice Papers - Selected Classes From Java API

OCJP Practice Papers Selected Classes from Java API include following topics

  • Manipulate data using the StringBuilder class and its methods
  • Create and manipulate Strings
  • Create and manipulate calendar data using classes from java.time.LocalDateTime,java.time.LocalDate, java.time.LocalTime, java.time.format.DateTimeFormatter,java.time.Period
  • Declare and use an ArrayList of a given type
  • Write a simple Lambda expression that consumes a Lambda Predicate expression

See the complete syllabus for OCJP here

  1. What is the best reason for using StringBuilder instead of String?
    A. StringBuilder adds support for multiple threads.
    B. StringBuilder can use == to compare values.
    C. StringBuilder saves memory by reducing the number of objects created.
    D. StringBuilder supports different languages and encodings.

2. What is not true about a String?
A. It can be created without coding a call to a constructor.
B. It can be reused via the string pool.
C. It is final.
D. It is mutable.

3. Which of the following creates a StringBuilder with a different value than the other options?
A. new StringBuilder().append("clown")
B. new StringBuilder("clown")
C. new StringBuilder("cl").insert(2, "own")
D. All of them create the same value.

4. What is the output of the following? StringBuilder teams = new StringBuilder("333"); teams.append(" 806"); teams.append(" 1601"); System.out.print(teams);
A. 333
B. 333 806 1601
C. The code compiles but outputs something else.
D. The code does not compile.

5. How many of the types ArrayList, List, and Object can fill in the blank to produce code that compiles? List frisbees = new ____________();
A. None
B. One
C. Two
D. Three

6. What is the output of the following? ListString tools = new ArrayList(); tools.add("hammer"); tools.add("nail"); tools.add("hex key"); System.out.println(tools.get(1));
A. hammer
B. hex key
C. nail
D. None of the above

7. What is the result of the following code? StringBuilder sb = new StringBuilder("radical") .insert(sb.length(), "robots"); System.out.println(sb);
A. radicarobots
B. radicalrobots
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

8. What is the output of the following? ListString museums = new ArrayList(1); museums.add("Natural History"); museums.add("Science"); museums.add("Art"); museums.remove(2); System.out.println(museums);
A. [Natural History, Science]
B. [Natural History, Art, Science]
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

9. What is the output of the following? 12: StringBuilder b = new StringBuilder("12"); 13: b = b.append("3"); 14: b.reverse(); 15: System.out.println(b.toString());
A. 12
B. 123
C. 321
D. The code does not compile.

10. What is the main benefit of a lambda expression?
A. It allows you to convert a primitive to a wrapper class.
B. It allows you to change the bytecode while the application is running.
C. It allows you to inherit from multiple classes.
D. It allows you to write code that has the execution deferred.

11. What is the output of the following? 5: StringBuilder line = new StringBuilder("-"); 6: StringBuilder anotherLine = line.append("-"); 7: System.out.print(line == anotherLine); 8: System.out.print(" "); 9: System.out.print(line.length());
A. false 1
B. false 2
C. true 1
D. true 2

12. The author of this method forgot to include the data type. Which of the following reference types can fill in the blank to complete this method? public static void secret( ____________mystery) { mystery.add("metal"); String str = mystery.get(0); int num = mystery.length(); }
A. ArrayList
B. ArrayListString
C. StringBuilder
D. None of the above

13. Which portion of code can be removed so that this line of code continues to compile? PredicateStringBuilder p = (StringBuilder b) {return true;};
A. Remove StringBuilder b
B. Remove -
C. Remove { and ;}
D. Remove { return and ;}

14. What is the output of the following? 20: ListCharacter chars = new ArrayList(); 21: chars.add('a'); 22: chars.add('b'); 23: chars.set(1, 'c'); 24: chars.remove(0); 25: System.out.print(chars.size() + " " + chars.contains('b'));
A. 1 false
B. 1 true
C. 2 false
D. 2 true

15. What is the output of the following? 12: String b = "12"; 13: b += "3"; 14: b.reverse(); 15: System.out.println(b.toString());
A. 12
B. 123
C. 321
D. The code does not compile.

16. How many of these lines fail to compile? PredicateString pred1 = s false; PredicateString pred2 = (s) false; PredicateString pred3 = String s false; PredicateString pred4 = (String s) false;
A. One
B. Two
C. Three
D. Four

17. What does the following do? public class Shoot { interface Target { boolean needToAim(double angle); } static void prepare(double angle, Target t) { boolean ready = t.needToAim(angle); // k1 System.out.println(ready); } public static void main(String[] args) { prepare(45, d - d 5 || d -5); // k2 } }
A. It prints true.
B. It prints false.
C. It doesnt compile due to line k1.
D. It doesnt compile due to line k2.

18. What is the output of the following? String teams = new String("694"); teams.concat(" 1155"); teams.concat(" 2265"); teams.concat(" 2869"); System.out.println(teams);
A. 694
B. 694 1155 2265 2869
C. The code compiles but outputs something else.
D. The code does not compile.

19. Which of these classes are in the java.util package? I. ArrayList II. LocalDate III. String
A. I only
B. II only
C. I and II
D. I, II, and III

20. Which of the answer choices results in a different value being output than the other three choices? StringBuilder sb = new StringBuilder("radical "); sb = ________________________; System.out.print(sb);
A. new StringBuilder("radical ") .append("robots")
B. new StringBuilder("radical ") .delete(1, 100) .append("obots") .insert(1, "adical r")
C. new StringBuilder("radical ") .insert(7, "robots")
D. new StringBuilder("radical ")

.insert(sb.length(), "robots") 21. What is the output of the following? String[] array = {"Natural History", "Science"}; ListString museums = Arrays.asList(array); museums.set(0, "Art"); System.out.println(museums.contains("Art"));
A. true
B. false
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

22. Which is a true statement?
A. If s.contains("abc") is true, then s.equals("abc") is also true.
B. If s.contains("abc") is true, then s.startsWith("abc") is also true.
C. If s.startsWith("abc") is true, then s.equals("abc") is also true.
D. If s.startsWith("abc") is true, then s.contains("abc") is also true.

23. What is the output of the following? 20: ListCharacter chars = new ArrayList(); 21: chars.add('a'); 22: chars.add('b'); 23: chars.set(1, 'c'); 24: chars.remove(0); 25: System.out.print(chars.length());
A. 0
B. 1
C. 2
D. None of the above

24. The author of this method forgot to include the data type. Which of the following reference types can fill in the blank to complete this method? public static void secret(_____________ mystery) { mystery = mystery.replace("1", "8"); mystery.startsWith("paper"); String s = mystery.toString(); }
A. ArrayList
B. String
C. StringBuilder
D. None of the above

25. Which statement is true about the following figure while ensuring the code continues to compile?
A. can be inserted at position P without making any other changes.
B. can be inserted at position Q without making any other changes.
C. can be inserted at both positions P and Q.
D. None of the above

26. Which of the following can fill in the blank to make the code compile? import java.util.function.*; public class Card { public static void main(String[] s) { PredicateString pred =____________ true; } }
A. (Integer i)
B. (Object o)
C. (String s)
D. None of the above

27. What is the output of the following? 5: String line = new String("-"); 6: String anotherLine = line.concat("-"); 7: System.out.print(line == anotherLine); 8: System.out.print(" "); 9: System.out.print(line.length());
A. false 1
B. false 2
C. true 1
D. true 2

28. What does the following output? Predicate dash = c - c.startsWith(" "); System.out.println(dash.test(""));
A. true
B. false
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

29. Of the classes LocalDate, LocalDateTime, LocalTime, and LocalTimeStamp, how many include hours, minutes, and seconds?
A. One
B. Two
C. Three
D. Four

30. What is the output of the following class? 1: package rocket; 2: public class Countdown { 3: public static void main(String[] args) { 4: String builder = "54321"; 5: builder = builder.substring(4); 6: System.out.println(builder.charAt(2)); 7: } 8: }
A. 2
B. 3
C. 4
D. None of the above

31. Which equivalent code can replace i - i != 0 in the following line? PredicateInteger ip = i i != 0;
A. i - { i != 0 }
B. i - { i != 0; }
C. i - { return i != 0 }
D. i - { return i != 0; }

32. What is the output of the following? LocalDate xmas = LocalDate.of(2016, 12, 25); xmas.plusDays(-1); System.out.println(xmas.getDayOfMonth());
A. 24
B. 25
C. 26
D. None of the above

33. What is the output of the following? 1: public class Legos { 2: public static void main(String[] args) { 3: StringBuilder sb = new StringBuilder(); 4: sb.append("red"); 5: sb.deleteCharAt(0); 6: sb.delete(1, 2); 7: System.out.println(sb); 8: } 9: }
A. e
B. d
C. ed
D. None of the above

34. What does the following output? Predicate clear = c - c.equals("clear"); System.out.println(clear.test("pink"));
A. true
B. false
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

35. Which starts counting from one rather than zero?
A. Array indexes
B. The index used by charAt in a String
C. The months in a LocalDateTime
D. The months in a LocalTime

36. Which statement is not true of Predicate?
A. A boolean is returned from the method it declares.
B. It is an interface.
C. The method it declares accepts two parameters.
D. The method it declares is named test.

37. Which of these periods represents a larger amount of time? Period period1 = Period.ofWeeks(1).ofDays(3); Period period2 = Period.ofDays(10);
A. period1
B. period2
C. They represent the same length of time.
D. None of the above. This code does not compile.

38. What is the result of the following? import java.time.*; import java.time.format.*; public class HowLong { public static void main(String[] args) { LocalDate newYears = LocalDate.of(2017, 1, 1); Period period = Period.ofDays(1); DateTimeFormatter format = DateTimeFormatter.ofPattern("MM-dd-yyyy"); System.out.print(format.format(newYears.minus(period))); } }
A. 01-01-2017
B. 12-31-2016
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

39. Which of the following can fill in the blank so the following code prints true? String happy = " :) - (: "; String really = happy.trim(); String question = ______________________; System.out.println(really.equals(question));
A. happy.substring(0, happy.length() - 1)
B. happy.substring(0, happy.length())
C. happy.substring(1, happy.length() - 1)
D. happy.substring(1, happy.length())

40. Which is not a true statement about the Period class?
A. A Period is immutable.
B. A Period is typically used for adding or subtracting time from dates.
C. You can create a Period representing 2 minutes.
D. You can create a Period representing 5 years.

41. What is the output of the following class? 1: package rocket; 2: public class Countdown { 3: public static void main(String[] args) { 4: StringBuilder builder = new StringBuilder("54321"); 5: builder.substring(2); 6: System.out.println(builder.charAt(1)); 7: } 8: }
A. 1
B. 2
C. 3
D. 4

42. What does the following output? ListInteger pennies = new ArrayList(); pennies.add(3); pennies.add(2); pennies.add(1); pennies.remove(2); System.out.println(pennies);
A. [3, 1]
B. [3, 2]
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

43. The author of this method forgot to include the data type. Which of the following reference types can best fill in the blank to complete this method? public static void secret(_____________ mystery) { char ch = mystery.charAt(3); mystery = mystery.insert(1, "more"); int num = mystery.length(); }
A. ArrayList
B. String
C. StringBuilder
D. None of the above

44. What is the smallest unit you can add to a LocalTime object?
A. Second
B. Millisecond
C. Nanosecond
D. Picosecond

45. What is the result of the following? import java.time.*; import java.time.format.*; public class HowLong { public static void main(String[] args) { LocalDate newYears = LocalDate.of(2017, 1, 1); Period period = Period.ofDays(1); DateTimeFormatter format = DateTimeFormatter.ofPattern("mm-dd-yyyy"); System.out.print(format.format(newYears.minus(period))); } }
A. 01-01-2017
B. 12-31-2016
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

46. Which of the following types can you pass as a parameter to the replace() method on the String class? I. char II. String III. StringBuilder
A. I
B. I and II
C. II and III
D. I, II, and III

47. How many lines does this code output? import java.util.*; import java.util.function.*; public class PrintNegative { public static void main(String[] args) { ListString list = new ArrayList(); list.add("-5"); list.add("0"); list.add("5"); print(list, e - e 0); } public static void print(ListString list, PredicateInteger p) { for (String num : list) if (p.test(num)) System.out.println(num); } }
A. One
B. Two
C. None. The code does not compile.
D. None. The code throws an exception at runtime.

48. What is the output of the following? 12: ListString magazines = new ArrayList(); 13: magazines.add("Readers Digest"); 14: magazines.add("People"); 15: magazines.clear(); 16: magazines.add("The Economist"); 17: magazines.remove(1); 18: System.out.println(magazines.size());
A. 0
B. 1
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

49. What is the output of the following? public class Costume { public static void main(String[] black) { String witch = 'b'; String tail = "lack"; witch = witch.concat(tail); System.out.println(witch); } }
A. b
B. black
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

50. What is the result of the following? LocalDate xmas = LocalDate.of(2016, 12, 25); xmas.setYear(2017); System.out.println(xmas.getYear());
A. 2016
B. 2017
C. The code does not compile.
D. The code compiles but throws an exception at runtime

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