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.

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.

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 ________ method of URL class provides the port number of the URL.

  • fetchPortNumber()
  • getPort()
  • obtainPort()
  • retrievePort()
The correct answer is getPort(). In Java, the URL class provides the getPort() method, which allows you to obtain the port number of a URL. This is useful when working with network-related tasks where you need to know the port associated with a particular URL.

Which class is commonly used for reading characters from a file in Java?

  • FileInputReader
  • FileOutputReader
  • FileReader
  • FileWriter
In Java, the commonly used class for reading characters from a file is FileReader. It is part of the java.io package and is specifically designed for character input. Other options like FileWriter, FileInputReader, and FileOutputReader are not standard classes in Java for file reading.

To obtain a string representation of a primitive data type, you can use the static valueOf method of the ________ class.

  • Double
  • Float
  • Integer
  • String
In Java, you can use the static valueOf method of the Integer class to obtain a string representation of a primitive data type. For example, Integer.valueOf(42) will return the string "42". The other options do not provide this specific functionality.

How does the Merge Sort algorithm behave in terms of space complexity?

  • It has a space complexity of O(1) as it sorts the array in place without using additional memory.
  • It has a space complexity of O(log n) because it divides the array into smaller subarrays, reducing memory usage.
  • It has a space complexity of O(n) because it creates a temporary array to hold the merged subarrays.
  • It has a space complexity of O(n^2) due to the need for additional memory for merging subarrays.
Merge Sort has a space complexity of O(n) because it creates a temporary array to hold the merged subarrays during the sorting process. Unlike some other sorting algorithms like Quick Sort, Merge Sort does not use a constant amount of extra memory (O(1)) or divide the memory usage logarithmically (O(log n)).

What does the Serializable interface contain?

  • A set of methods for creating new objects
  • A set of methods for mathematical calculations
  • A set of methods for serializing objects
  • A set of methods for sorting objects
The Serializable interface in Java does not contain any methods. Instead, it serves as a marker interface, indicating that the class implementing it can be serialized. Serialization allows objects to be converted into a byte stream for storage or transmission. It's used for objects that need to be saved to a file or sent over a network.

The process of defining a new exception by extending an existing exception class is known as ________.

  • Customization of exception
  • Exception chaining
  • Exception creation
  • Exception handling
In Java, when you define a new exception by extending an existing exception class, it's called "Customization of exception." This process allows you to create your own exception classes that suit your application's specific needs while maintaining compatibility with Java's exception hierarchy.

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.