programming
beginner
10 sample questions
Java MCQ Practice Test
Object-Oriented Programming (OOP) concepts like classes, objects, and inheritance.
Using methods, constructors, and exception handling.
Q1. Consider a Java program utilizing a custom class 'MyClass' implementing the Runnable interface. This class contains a volatile boolean variable 'stopThread' controlling thread termination. Assuming a scenario where the main thread needs to gracefully stop the 'MyClass' thread using the 'stopThread' variable, what is the MOST efficient and robust method to ensure complete thread termination without reliance on thread interrupts, considering potential complexities like thread blocking on I/O or synchronized blocks?
-
A. Employ a 'while (!stopThread) { ... }' loop and regularly poll 'stopThread' within the run() method.
-
B. Utilize a dedicated 'Thread.join()' call within the main thread, continuously checking for thread completion using 'isAlive()'.
-
C. Implement a wait/notify mechanism utilizing synchronized blocks and a dedicated wait condition variable to signal termination. ✓
-
D. Leverage the 'stop()' method (deprecated) for abrupt thread termination, followed by resource cleanup in a 'finally' block.
Explanation: Polling ('while (!stopThread)') is inefficient and can lead to CPU waste. 'Thread.join()' can hang indefinitely if the thread is blocked. The deprecated 'stop()' method is unsafe. A wait/notify mechanism offers the most efficient and reliable way to handle thread termination gracefully, especially when dealing with potential blocking scenarios within the thread's execution path. It allows the thread to release resources and cleanly exit when instructed to do so.
Q2. Consider a Java program utilizing a custom class 'MyThread' extending Thread. This class has a synchronized method 'processData(String data)' that manipulates a shared resource. Multiple instances of 'MyThread' concurrently invoke 'processData'. Which statement BEST describes the potential synchronization behavior concerning reentrancy when a 'MyThread' instance calls 'processData' recursively from within 'processData' itself?
-
A. The recursive call will deadlock as the same thread attempts to acquire the intrinsic lock already held.
-
B. The recursive call will proceed without blocking due to Java's reentrant lock mechanism. ✓
-
C. The recursive call will lead to unpredictable behavior, potentially causing data corruption.
-
D. The program will compile but the recursive call to processData will throw an IllegalMonitorStateException
Explanation: Java's synchronized keyword uses intrinsic locks (monitor locks). These locks are reentrant, meaning that the same thread that holds the lock can acquire it again without blocking. Therefore, the recursive call within the synchronized method will succeed. Options A and C are incorrect because reentrancy prevents deadlocks and unpredictable behavior in this specific scenario. Option D is incorrect because an IllegalMonitorStateException is typically thrown when attempting to unlock a lock not held by the current thread, not in the case of reentrant locking.
Q3. What is the purpose of the java.lang.reflect.Proxy class in Java?
-
A. It is used to create dynamic proxies to intercept method calls ✓
-
B. It is used to create custom serialization mechanisms
-
C. It is used to create custom deserialization mechanisms
-
D. It is used to create custom exception handling mechanisms
Explanation: The java.lang.reflect.Proxy class in Java is used to create dynamic proxies that can intercept method calls. This allows for the implementation of aspects, interceptors, and other design patterns that require method call interception.
Q4. What is the purpose of the \u0008 character in a Java string literal, and how does it affect the string's behavior?
-
A. It is a backspace character that removes the preceding character when printed to the console. ✓
-
B. It is a whitespace character that is ignored in regular expression patterns.
-
C. It is a special character that escapes all subsequent characters in the string.
-
D. It is an invalid character in Java string literals.
Explanation: The \u0008 character is a backspace character. When printed to the console, it moves the cursor back one position, effectively deleting the preceding character. It does not have a special meaning in regular expressions.
Q5. What is the primary function of the `assertThat` method in the AssertJ library?
-
A. To create custom matcher classes.
-
B. To initiate an assertion on a value or object. ✓
-
C. To define custom assertion messages.
-
D. To override default assertion behavior.
Explanation: The `assertThat` method is the entry point for creating assertions in AssertJ. It takes a value or object as input and returns an object that allows you to perform various assertions on that value or object. While it doesn't directly create custom assertion methods, it's the foundation upon which they are built.
Q6. In Java, what is the purpose of the \\ (double backslash) escape sequence in a string literal?
-
A. It is used to escape the forward slash character
-
B. It is used to denote a line continuation
-
C. It is used to represent a single backslash character ✓
-
D. It is used to escape Unicode escape sequences
Explanation: In Java, the \\ (double backslash) escape sequence is used to represent a single backslash character within a string literal. Since a single backslash is used as an escape character, to include a literal backslash, you must escape it with another backslash. For example: String path = "C:\\Users\\Documents";
Q7. Which Java annotation is most directly related to controlling how annotations are stored and whether they are available during compile-time processing?
-
A. @FunctionalInterface
-
B. @Repeatable
-
C. @Retention ✓
-
D. @Deprecated
Explanation: @Retention is used to specify how long annotations are stored. The RetentionPolicy.SOURCE value means the annotation is available only at compile time, allowing for compile-time checks and processing.
Q8. What is the purpose of the 'java.lang.AutoCloseable' interface in Java, introduced in Java 7?
-
A. It provides a way to autobox primitive types
-
B. It enables the use of try-with-resources statement for automatic resource management ✓
-
C. It provides a way to implement custom iterators
-
D. It enables the use of lambda expressions
Explanation: The 'java.lang.AutoCloseable' interface is used to enable the try-with-resources statement, which automatically closes resources such as files, sockets, and database connections, preventing resource leaks and making the code more concise.
Q9. What character does the escape sequence \u0008 represent in a Java string literal?
-
A. A whitespace character
-
B. A newline character
-
C. A backspace character ✓
-
D. A null character
Explanation: In Java, the \u0008 escape sequence represents a backspace character. When printed, it moves the cursor one position to the left, effectively deleting the preceding character.
Q10. Which of the following techniques is commonly used to add context or additional information when re-throwing an exception in Java?
-
A. Chaining exceptions ✓
-
B. Suppressing exceptions
-
C. Swallowing exceptions
-
D. Ignoring exceptions
Explanation: Chaining exceptions involves creating a new exception and setting the original exception as its cause. This allows you to add context to the original exception while preserving the stack trace and original exception information. This is typically done using the constructor of the new exception that accepts a 'cause' parameter.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free