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.
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.
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.
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.
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.
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.
A two-dimensional array int[][] arr is essentially an array of ________ in Java.
- arrays
- arrays of arrays
- arrays of integers
- integers
In Java, a two-dimensional array int[][] arr is essentially an array of arrays. It means that each element of arr is itself an array, which can hold integers or other data types. This concept allows you to create tables or matrices of data.
The _______ block in a Java class is executed before constructors.
- finalize
- instance
- main
- static
In Java, the static block is executed before constructors. Static blocks are used for performing class-level initialization tasks. They run when the class is loaded, and they are executed only once. Constructors, on the other hand, are used to initialize instance variables and are called when an object is created.
How is operator overloading achieved for the String class in Java?
- By defining custom methods for concatenation
- By using the '+' operator for String concatenation
- By using the '+' operator with custom type conversion
- Java does not support operator overloading for the String class
Operator overloading is not supported for the String class in Java. Instead, Java provides a convenient way to concatenate strings using the '+' operator, but it doesn't involve operator overloading. You can use custom methods for string manipulation, but it's not true operator overloading.
How does the invokeAll() method of ExecutorService behave?
- It executes all submitted tasks concurrently and returns a list of Future objects representing their results.
- It executes all submitted tasks concurrently and returns the result of the first task that completes successfully.
- It submits all tasks to the ExecutorService but returns only the first completed task's result.
- It waits indefinitely until all tasks are completed, returning the results in the order they were submitted.
The invokeAll() method of ExecutorService takes a collection of Callable tasks, executes them concurrently, and returns a list of Future objects that represent the results. The order of the results matches the order of the tasks in the input collection. It doesn't return results as they complete; it waits for all tasks to finish and then returns the results.