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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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
Java MCQ Practice Test | Mind Hustle