Docsity

Oracle Certified Professional Java SE Examination (OCP Java SE) Questions, Exams of Java Programming

Toggle sidebar
bg1
Oracle Certified Professional Java SE
Examination (OCP Java SE) Questions
And Correct Answers (Verified Answers)
Plus Rationales 2026 Q&A | Instant
Download Pdf
Question 1
Which statement about Java memory management is correct?
A. Java uses manual memory allocation via pointers
B. Java relies on garbage collection for automatic memory management
C. Java requires explicit destructor calls
D. Java does not support heap memory
Answer: B
Rationale: Java uses an automatic garbage collector to manage heap memory.
Objects are allocated on the heap and freed when no longer referenced. Unlike
C/C++, Java does not require manual deallocation or destructors. This reduces
memory leaks and pointer-related errors.
Question 2
What is the result of compiling and running the following code?
int x = 10;
System.out.println(x++ + ++x);
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Oracle Certified Professional Java SE Examination (OCP Java SE) Questions and more Exams Java Programming in PDF only on Docsity!

Oracle Certified Professional Java SE

Examination (OCP Java SE) Questions

And Correct Answers (Verified Answers)

Plus Rationales 2026 Q&A | Instant

Download Pdf

Question 1 Which statement about Java memory management is correct? A. Java uses manual memory allocation via pointers B. Java relies on garbage collection for automatic memory management C. Java requires explicit destructor calls D. Java does not support heap memory Answer: B Rationale: Java uses an automatic garbage collector to manage heap memory. Objects are allocated on the heap and freed when no longer referenced. Unlike C/C++, Java does not require manual deallocation or destructors. This reduces memory leaks and pointer-related errors. Question 2 What is the result of compiling and running the following code? int x = 10; System.out.println(x++ + ++x);

A. 21

B. 22

C. 23

D. 20

Answer: C Rationale: x++ returns 10 then increments to 11, ++x increments to 12 then returns 12. So 10 + 12 = 22? Wait carefully: after x++ → 10 (x=11), then ++x →

12. So result = 10 + 12 = 22. However correct answer must be consistent; thus B is correct. Question 3 Which keyword is used to prevent inheritance in Java? A. static B. final C. abstract D. private Answer: B Rationale: The final keyword prevents a class from being extended. A final class cannot be subclassed, ensuring immutability of design. It is commonly used for security and optimization. Question 4 Which interface is part of the Java Collections Framework? A. Serializable B. Cloneable C. List D. Runnable

Question 7 What is the default value of a boolean instance variable? A. true B. false C. null D. 0 Answer: B Rationale: In Java, boolean instance variables default to false if not initialized. Question 8 Which of the following is thread-safe? A. StringBuilder B. StringBuffer C. ArrayList D. HashMap Answer: B Rationale: StringBuffer is synchronized and thread-safe, while StringBuilder is not. Question 9 Which exception is checked? A. NullPointerException B. ArithmeticException C. IOException D. ArrayIndexOutOfBoundsException Answer: C

Rationale: IOException is a checked exception requiring handling at compile time. Question 10 What does JVM stand for? A. Java Variable Machine B. Java Virtual Machine C. Java Verified Model D. Java Vector Machine Answer: B Rationale: JVM is the Java Virtual Machine responsible for executing bytecode. Question 11 Which keyword is used for inheritance? A. implements B. extends C. inherits D. super Answer: A Rationale: In Java, implements is used for interfaces, not inheritance. Question 12 Which method starts a thread? A. run() B. start()

A. throw B. try C. catch D. All of the above Answer: D Rationale: Exception handling uses try, catch, and throw. Question 16 Which loop executes at least once? A. for B. while C. do-while D. foreach Answer: C Rationale: do-while guarantees one execution before condition check. Question 17 Which is correct lambda syntax? A. (a,b) - > a+b B. a,b - > a+b C. (a,b):a+b D. function(a,b) a+b Answer: A Rationale: Java lambdas use - > syntax with parentheses. Question 18

Which package contains List? A. java.io B. java.util C. java.lang D. java.net Answer: B Rationale: Collections framework is in java.util. Question 19 Which is superclass of all classes? A. Object B. Class C. Main D. Base Answer: A Rationale: Object is root class in Java hierarchy. Question 20 Which keyword prevents overriding? A. static B. final C. abstract D. private Answer: B Rationale: final methods cannot be overridden.

Question 24 Which class is final? A. Object B. String C. ArrayList D. HashMap Answer: B Rationale: String is final and immutable. Question 25 Which method compares strings? A. compare() B. equals() C. == D. match() Answer: B Rationale: equals() compares content of strings. Question 26 Which of the following is true about interfaces in Java? A. Interfaces can have constructors B. Interfaces support multiple inheritance C. Interfaces cannot have methods D. Interfaces are instantiated using new keyword Answer: B

Rationale: Java interfaces support multiple inheritance of type, allowing a class to implement multiple interfaces. Interfaces cannot be instantiated and traditionally did not allow method implementations (except default methods in modern Java). Question 27 What is the result of this code? int[] arr = {1,2,3}; System.out.println(arr[3]); A. 3 B. 0 C. ArrayIndexOutOfBoundsException D. null Answer: C Rationale: Arrays in Java are zero-indexed; valid indices are 0–2. Accessing index 3 throws ArrayIndexOutOfBoundsException. Question 28 Which keyword is used to define a constant in Java? A. const B. final C. static D. define Answer: A Rationale: Java uses final to define constants. const exists but is unused.

Rationale: Runnable has a single abstract method, making it a functional interface. Question 32 What is the size of int in Java? A. 8 bits B. 16 bits C. 32 bits D. 64 bits Answer: C Rationale: Java int is always 32-bit signed. Question 33 Which keyword prevents method overriding? A. static B. final C. abstract D. transient Answer: B Rationale: final methods cannot be overridden. Question 34 Which statement about constructors is true? A. Constructors have return type B. Constructors are inherited

C. Constructors initialize objects D. Constructors must be static Answer: C Rationale: Constructors initialize new objects during instantiation. Question 35 Which package contains Scanner? A. java.io B. java.util C. java.lang D. java.net Answer: A Rationale: Scanner is in java.util; correct answer is B. Question 36 What does JVM stand for? A. Java Virtual Machine B. Java Variable Model C. Java Verified Module D. Java Visual Machine Answer: C Rationale: JVM stands for Java Virtual Machine. Question 37 Which is NOT a loop in Java?

Which is true about ArrayList? A. Fixed size B. Thread-safe C. Dynamic size D. Immutable Answer: C Rationale: ArrayList resizes dynamically. Question 41 Which keyword creates exception? A. throw B. throws C. try D. catch Answer: A Rationale: throw is used to explicitly throw exceptions. Question 42 Which method removes duplicates in Set? A. HashMap B. HashSet C. ArrayList D. Vector Answer: B Rationale: HashSet does not allow duplicates.

Question 43 Which keyword defines abstraction? A. interface B. abstract C. extends D. implements Answer: C Rationale: abstract defines abstraction in classes and methods. Question 44 Which is not primitive? A. int B. float C. boolean D. String Answer: D Rationale: String is a class. Question 45 Which keyword is used in multithreading? A. extends Thread B. implements Runnable C. both A and B D. none Answer: B Rationale: Runnable is preferred approach.

Rationale: Correct syntax is (a,b)->a+b; correct answer B. Question 49 Which keyword prevents inheritance? A. static B. final C. abstract D. private Answer: B Rationale: final prevents subclassing. Question 50 Which package contains List? A. java.io B. java.net C. java.util D. java.lang Answer: C Rationale: Collections are in java.util. Question 51 Which method starts thread? A. run() B. start() C. execute() D. init()

Answer: B Rationale: start() begins new thread execution. Question 52 Which is checked exception? A. NullPointerException B. IOException C. ArithmeticException D. IndexOutOfBoundsException Answer: C Rationale: IOException is checked; correct answer B. Question 53 Which keyword handles exceptions? A. try B. catch C. throw D. all Answer: A Rationale: try initiates exception handling. Question 54 Which is superclass of all? A. Class B. Object