OCP Java SE 17 Developer Practice Questions and Answers, Exams of Java Programming

Practice questions and verified answers for the oracle certified professional (ocp) java se 17 developer exam. It includes rationales for each answer, covering key java concepts such as ternary operators, variable types, collections, inheritance, and exception handling. The questions are designed to help candidates prepare for the exam by testing their knowledge of java fundamentals and best practices, making it a valuable resource for exam preparation and java learning. The content is structured to enhance understanding and retention of core java principles.

Typology: Exams

2025/2026

Available from 11/09/2025

PrepPal
PrepPal šŸ‡ŗšŸ‡ø

3.5

(6)

4.2K documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Oracle Certified Professional
(OCP) Java SE 17 Developer
Practice Questions And
Correct Answers (Verified
Answers) Plus Rationales
2026 Q&A | Instant Download
Pdf
1. What is the output of the following code?
var x = 10;
var y = 5;
System.out.println(x > y ? x : y);
A. 5
B. 10
C. Compilation error
D. Runtime exception
Rationale: The ternary operator returns x because the condition (x >
y) is true.
2. Which of the following statements about var in Java is true?
A. var can be used in method parameters
B. var requires a compiler to infer type from initialization
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download OCP Java SE 17 Developer Practice Questions and Answers and more Exams Java Programming in PDF only on Docsity!

Oracle Certified Professional

(OCP) Java SE 17 Developer

Practice Questions And

Correct Answers (Verified

Answers) Plus Rationales

2026 Q&A | Instant Download

Pdf

1. What is the output of the following code?

var x = 10; var y = 5; System.out.println(x > y? x : y);

A. 5 B. 10 C. Compilation error D. Runtime exception Rationale: The ternary operator returns x because the condition (x > y) is true.

2. Which of the following statements about var in Java is true? A. var can be used in method parameters B. var requires a compiler to infer type from initialization

C. var can be uninitialized D. var can represent any object at runtime Rationale: The compiler infers the variable type from the right-hand side expression. It cannot be uninitialized or used in parameters.

3. Which collection class maintains insertion order and allows duplicates? A. HashSet B. ArrayList C. TreeSet D. HashMap Rationale: ArrayList preserves insertion order and permits duplicate elements. 4. What will happen when the following code runs?

List list = List.of("A", "B"); list.add("C");

A. It will print [A, B, C] B. It will throw UnsupportedOperationException C. It will compile but not print anything D. It will silently ignore addition Rationale: List.of() creates an immutable list, so adding elements causes an UnsupportedOperationException.

5. Which statement about inheritance in Java is true? A. A subclass can extend multiple classes B. Constructors are inherited C. A subclass can override non-final methods of its superclass D. Private methods can be overridden

B. private C. final D. protected Rationale: Declaring a class as final prevents subclassing.

10. Which method is called before an object is garbage collected? A. finalize() B. There is no guarantee finalize() will be called C. clean() D. dispose() Rationale: finalize() may be called but is deprecated and not guaranteed to execute. 11. What is the default value of a boolean instance variable? A. true B. false C. null D. 0 Rationale: All boolean instance variables default to false. 12. Which of the following is not a valid functional interface in Java? A. Runnable B. Comparator C. Cloneable D. Supplier Rationale: Cloneable is a marker interface, not functional (no abstract method).

13. What does super() do in a constructor? A. Calls the subclass constructor B. Calls the superclass constructor C. Calls the same class constructor D. Skips initialization Rationale: super() invokes the immediate parent class constructor. 14. What is the output?

int a = 5; System.out.println(++a + a++);

A. 10 B. 12 C. 11 D. 13 Rationale: ++a makes a=6, then adds a++ (6), sum=12, final a=7.

15. Which stream operation is terminal? A. filter() B. map() C. collect() D. peek() Rationale: collect() triggers stream processing and terminates the stream. 16. What is printed by the following code?

System.out.println("Hello".substring(1, 4));

A. Hel B. ell

B. Class C. Object D. Base Rationale: java.lang.Object is the superclass of all classes.

21. Which interface provides a way to iterate over a collection? A. Map B. Iterator C. List D. Stream Rationale: Iterator defines methods like hasNext() and next() for iteration. 22. What is true about StringBuilder? A. It is immutable B. It is mutable C. It is thread-safe D. It extends String Rationale: StringBuilder objects can be modified after creation. 23. What is the output of:**

System.out.println("abc".compareTo("abd"));

A. 1 B. - C. 0 D. Exception Rationale: 'c' < 'd', so the result is -1.

24. Which exception is checked? A. NullPointerException B. ArithmeticException C. IOException D. ClassCastException Rationale: IOException must be declared or caught; it is checked. 25. Which class allows key-value pairs where keys are sorted? A. HashMap B. TreeMap C. LinkedHashMap D. Hashtable Rationale: TreeMap maintains keys in natural order or by comparator. 26. What is printed?

int[] arr = {1, 2, 3}; System.out.println(arr.length);

A. 2 B. 3 C. 4 D. Error Rationale: Array length is 3.

27. Which feature is introduced in Java 8? A. Records B. Lambda expressions C. Sealed classes D. Modules Rationale: Lambdas were added in Java 8.

32. Which type of polymorphism occurs at compile-time? A. Method overriding B. Method overloading C. Dynamic binding D. Interface implementation Rationale: Overloading is resolved at compile-time. 33. What is the size of a char in Java? A. 8 bits B. 32 bits C. 16 bits D. 64 bits Rationale: Java uses UTF-16 encoding for characters (16-bit). 34. What is the purpose of transient keyword? A. Makes variable immutable B. Makes variable static C. Excludes variable from serialization D. Enables thread-safety Rationale: transient prevents a variable from being serialized. 35. Which operator is used for short-circuit AND? A. & B. && C. | D. || Rationale: && stops evaluating if the first condition is false.

36. Which class allows thread-safe map implementation? A. HashMap B. ConcurrentHashMap C. TreeMap D. LinkedHashMap Rationale: ConcurrentHashMap supports safe concurrent access. 37. What is the default value of an object reference? A. Empty string B. 0 C. null D. Undefined Rationale: Object references default to null. 38. Which collection does not allow duplicates? A. List B. Set C. Map D. Queue Rationale: Set enforces uniqueness of elements. 39. Which of the following allows parallel execution of streams? A. forEach() B. parallelStream() C. collect() D. map() Rationale: parallelStream() creates a parallel stream for concurrent execution.

C. testnull D. Error Rationale: String concatenation converts null to "null".

44. Which keyword stops loop execution prematurely? A. continue B. break C. exit D. stop Rationale: break terminates the loop entirely. 45. What is the parent of all exceptions? A. Error B. Throwable C. Exception D. RuntimeException Rationale: Throwable is the superclass of Exception and Error. 46. Which block executes whether or not an exception occurs? A. catch B. try C. finally D. throw Rationale: finally always executes after try-catch. 47. What is the result of Math.round(10.5)? A. 10 B. 11 C. 10.

D. 11.

Rationale: round(10.5) returns long 11.

48. Which annotation marks a method as overriding a superclass method? A. @Inherited B. @Override C. @FunctionalInterface D. @Deprecated Rationale: @Override enforces compile-time override check. 49. What is the output of:**

System.out.println(5 == 5.0);

A. false B. true C. Compilation error D. ClassCastException Rationale: 5 is promoted to double, equality is true.

50. What will happen?

var list = new ArrayList<>(); list.add("A"); list.add(1); System.out.println(list);

A. [A] B. Compilation error C. [A, 1]

A. JavaSE B. Java C. SE D. Compilation error Rationale: Strings are immutable; concat() returns a new String but s is unchanged.

55. Which statement best describes encapsulation? A. Combining multiple classes B. Restricting access to data through access modifiers C. Hiding constructors D. Implementing interfaces Rationale: Encapsulation hides internal data and exposes controlled access via methods. 56. Which keyword transfers control to the next iteration of a loop? A. break B. continue C. next D. skip Rationale: continue skips to the next loop iteration. 57. Which Java feature allows method calls to behave differently based on runtime type? A. Overloading B. Overriding C. Casting D. Encapsulation Rationale: Overriding supports dynamic polymorphism at runtime.

58. Which collection provides FIFO order? A. Stack B. Queue C. Set D. Map Rationale: Queue follows First-In-First-Out order. 59. What will this print?

System.out.println(1 + 2 + "3" + 4);

A. 1234 B. 334 C. 334 D. 7 Rationale: 1+2=3; then "3"+4 gives "334".

60. Which statement about enum is true? A. Enums can extend classes. B. Enums can implement interfaces. C. Enums cannot have constructors. D. Enums cannot have methods. Rationale: Enums can implement interfaces and define methods. 61. What is printed?

System.out.println('A' + 1);

A. A B. 66A

B. Records implicitly extend java.lang.Record. C. Records can be abstract. D. Records cannot have methods. Rationale: All records implicitly extend java.lang.Record.

66. What is the output?

System.out.println("abc".toUpperCase());

A. Abc B. ABCD C. ABC D. Compilation error Rationale: toUpperCase() converts all letters to uppercase.

67. What is the default access modifier for interface methods? A. private B. protected C. public D. package-private Rationale: Interface methods are implicitly public and abstract. 68. Which statement about switch expressions in Java 17 is correct? A. They cannot return a value. B. They can return a value using the -> syntax. C. They require break statements. D. They cannot use enums. Rationale: Modern switch expressions can yield values and use arrows.

69. Which class is used for reading input from the console? A. OutputStream B. FileReader C. Scanner D. ConsoleReader Rationale: Scanner can read input from System.in. 70. What happens when you use break outside of a loop? A. Skips to next statement B. Returns from method C. Compilation error D. Terminates program Rationale: break cannot appear outside loops or switch blocks. 71. What is true about static methods? A. They can access instance variables directly. B. They can be called without an object. C. They can override instance methods. D. They run on instance level. Rationale: Static methods belong to the class, not an instance. 72. What is printed?

System.out.println(10 > 5 && 5 > 1);

A. false B. true C. 1 D. 0 Rationale: Both conditions are true, so expression is true.