When a thread tries to access a synchronized block of code in an object, it must first obtain the ________ on the object.
- Lock
- Monitor
- Semaphore
- Semaphore
When a thread tries to access a synchronized block in Java, it must first obtain the "Lock" on the object. This ensures that only one thread can execute the synchronized code block at a time, preventing race conditions.
The InputStream and OutputStream classes are part of the ________ package.
- java.io
- java.lang
- java.streams
- java.util
The InputStream and OutputStream classes are part of the java.io package in Java. These classes are used for reading and writing data in a byte-oriented manner, making them essential for I/O operations.
The operator ______ is used to invert the value of a boolean expression.
- !
- &&
- +
- ==
In Java, the ! (logical NOT) operator is used to invert or negate the value of a boolean expression. It changes true to false and vice versa, making it a fundamental operator for boolean logic in Java.
How does the wait() method differ from the sleep() method when working with threads?
- wait() and sleep() are interchangeable; there is no difference.
- wait() and sleep() have no impact on thread execution.
- wait() is used for inter-thread communication and releases the lock, while sleep() pauses the thread and retains the lock.
- wait() is used for pausing the thread and retains the lock, while sleep() is used for inter-thread communication and releases the lock.
In Java, the wait() method is used for inter-thread communication and is typically used with synchronization mechanisms like synchronized blocks. It releases the lock and allows other threads to execute, whereas sleep() pauses the thread but retains the lock, making it unsuitable for inter-thread communication.
How can one ensure that a particular section of code does not have concurrent access by multiple threads?
- Use ReentrantLock from java.util.concurrent package.
- Use Thread.sleep() to create delays.
- Use synchronized keyword on the method.
- Use volatile keyword for variables.
To ensure that a particular section of code does not have concurrent access by multiple threads, you can use the ReentrantLock class from the java.util.concurrent package. This allows you to create a lock that multiple threads can use to synchronize access to a critical section of code. Unlike synchronized methods, it provides more fine-grained control over locking.
What will be the output of the following code: int x = 10; x *= 3;?
- x is assigned the value 0
- x is assigned the value 10
- x is assigned the value 13
- x is assigned the value 30
In this code, x is first assigned the value 10, and then the compound assignment operator *= multiplies it by 3. So, x will be assigned the value 30. The other options are incorrect as they don't represent the correct result of the code.
Which of the following sorting algorithms is most efficient in terms of average-case time complexity?
- Bubble Sort
- Insertion Sort
- Quick Sort
- Selection Sort
Quick Sort is known for its efficiency in terms of average-case time complexity. It has an average-case time complexity of O(n log n) and is often faster than other sorting algorithms like Bubble Sort, Selection Sort, and Insertion Sort, which have worse average-case time complexities. Quick Sort's efficiency is achieved through a divide-and-conquer approach.
Which operators are overloaded for the String class in Java?
- * (repetition)
- + (concatenation)
- - (subtraction)
- / (division)
In Java, the + operator is overloaded for the String class, allowing you to concatenate strings using the + operator. Other operators like *, -, and / are not overloaded for String and would result in compilation errors if used inappropriately.
A thread enters the ________ state once its run() method has completed execution.
- Blocked
- Runnable
- Terminated
- Waiting
A thread enters the "Terminated" state in Java once its "run()" method has completed execution. In this state, the thread has finished its task and is no longer actively executing code.
Consider a scenario where multiple threads are executing tasks, but the main thread needs to wait until all tasks are complete before proceeding. How can this be achieved using Future and ExecutorService?
- Use ExecutorService's awaitTermination() method to block the main thread until all tasks are completed.
- Use ExecutorService's invokeAny() method to submit tasks and block the main thread until any one of the tasks completes.
- Use ExecutorService's shutdown() method to ensure that all tasks are complete before proceeding with the main thread.
- Use invokeAll() method of ExecutorService to submit tasks and obtain a list of Future objects, then call get() method on each Future object to block the main thread until tasks are complete.
In this scenario, you can use the invokeAll() method to submit tasks and obtain a list of Future objects representing each task. Calling the get() method on each Future object will block the main thread until all tasks are complete. This allows synchronization between the main thread and the worker threads.
Can an abstract class have a constructor in Java?
- It depends on the class
- No
- Yes, always
- Yes, but with restrictions
Yes, an abstract class can have a constructor in Java. However, there are some restrictions. The constructor of an abstract class is typically used to initialize the fields of the abstract class, and it can be called from subclasses using the super keyword.
What is the purpose of the start() method in a JavaFX application?
- It defines the application's GUI components.
- It handles user input events.
- It initializes the application's resources.
- It is the entry point for launching the app.
The start() method is the entry point for a JavaFX application. It is automatically called by the JavaFX framework when the application starts. Within this method, you set up the initial state of your application, create the main user interface, and configure event handling. This method serves as the starting point for building your JavaFX application.