What is the primary purpose of a constructor in Java?
- To create objects of the class.
- To define the class methods.
- To initialize the class variables.
- To provide a way to destroy objects of the class.
In Java, a constructor's primary purpose is to initialize the class's instance variables when an object is created. Constructors don't define class methods or create/destroy objects; that's not their primary role.
Which loop structure should be used when the number of iterations is known in advance?
- do-while loop
- for loop
- if statement
- while loop
The for loop is ideal when you know the number of iterations in advance. It consists of an initialization, condition, and increment/decrement statement. This loop is suitable for iterating over arrays, collections, or any situation where the number of iterations is predetermined.
The ________ keyword can be used to print multi-dimensional arrays in a deep-to-string manner.
- deepToString
- display
- toString
In Java, the Arrays.deepToString() method can be used to print multi-dimensional arrays in a deep-to-string manner. This method recursively converts the array into a string representation, including nested arrays, making it useful for debugging and displaying complex data structures.
A private constructor prevents the class from being instantiated outside of the class and is commonly used in ________.
- Abstraction
- Inheritance
- Polymorphism
- Singleton Pattern
A private constructor is commonly used in the Singleton design pattern. The Singleton pattern ensures that a class has only one instance, and the private constructor prevents external instantiation. It is not typically used for inheritance, polymorphism, or abstraction.
The ________ class in Java creates an immutable sequence of characters.
- CharArray
- String
- StringBuffer
- StringBuilder
In Java, the String class creates an immutable sequence of characters. This means that once a string is created, its content cannot be changed. The other options, StringBuilder, StringBuffer, and CharArray, are used for mutable character sequences.
How can you cancel a task submitted to ExecutorService using Future?
- future.cancel(true)
- future.interrupt()
- future.shutdown()
- future.stop()
In Java, you can cancel a task submitted to an ExecutorService using the cancel method on a Future object. The argument true passed to cancel(true) means an attempt to interrupt the task, while false means attempting a graceful cancellation. Using stop() and interrupt() is not recommended for canceling tasks, and shutdown() is used to shut down the entire ExecutorService, not to cancel a specific task.
Which method removes the first occurrence of the specified element from a LinkedList?
- delete()
- deleteFirst()
- remove()
- removeFirst()
The remove() method in a LinkedList is used to remove the first occurrence of the specified element. It takes the element as an argument and searches for its first occurrence, then removes it. removeFirst() is not a standard method in LinkedList. deleteFirst() and delete() are not valid methods for removing elements in a LinkedList.
Which of the following is not a valid JDBC transaction isolation level?
- TRANSACTION_COMMITTED
- TRANSACTION_NONE
- TRANSACTION_REPEATABLE_READ
- TRANSACTION_SERIALIZABLE
JDBC defines standard transaction isolation levels such as TRANSACTION_NONE, TRANSACTION_SERIALIZABLE, and TRANSACTION_REPEATABLE_READ. However, TRANSACTION_COMMITTED is not a valid JDBC transaction isolation level. Isolation levels determine how transactions interact with each other and the data.
How can you manipulate request headers when using HttpURLConnection?
- By altering the HTTP request method
- By calling addRequestHeader method
- By modifying the Content-Type header
- By using the setRequestProperty method
To manipulate request headers in HttpURLConnection, you should use the setRequestProperty method. This method allows you to set custom headers for your HTTP request. Modifying the Content-Type header is one specific use case of this method. The other options are not standard ways to manipulate headers using HttpURLConnection.
If a method in an interface is declared without an access modifier, it is implicitly ________.
- package-private (no modifier)
- private
- protected
- public
In Java, if a method in an interface is declared without an access modifier, it is implicitly considered public. This means that the method is accessible from any class that implements the interface, even if it is in a different package. The other access modifiers (private, protected, and package-private) cannot be used for interface methods.
Which of the following is not a part of the JDBC API?
- DriverManager
- ResultSet
- ResultSetMetaData
- SQLException
DriverManager is not a part of the JDBC API. It is a class in the Java standard library used to manage a list of database drivers. The other options are part of the JDBC API for working with databases in Java.
What is the purpose of the flatMap() method in the Stream API?
- To collect the elements of the stream into a list
- To filter elements based on a given predicate
- To flatten a stream of streams into a single stream
- To perform an operation on each element in the stream
The flatMap() method in the Stream API is used to flatten a stream of streams into a single stream. It is particularly useful when you have a stream of elements, and each element is itself a stream (e.g., a stream of lists). flatMap() will merge all these sub-streams into one, providing a single stream of elements. This is different from map(), which produces a stream of streams.