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.
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.
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.
Consider a scenario where you need to build a Java application that periodically checks a set of URLs to ensure they are accessible. How would you manage the connections and which classes/methods might be useful to achieve this efficiently?
- Employ the java.util.TimerTask class to create a periodic task. Handle connections with java.io.InputStream and java.io.OutputStream.
- Use the java.io.BufferedWriter and java.io.BufferedReader classes for URL handling. Implement periodic checks using Thread.sleep() and custom thread management.
- Use the java.net.URL class to represent URLs and java.net.HttpURLConnection class to open connections. Implement a periodic task using ScheduledExecutorService to make HTTP requests.
- Utilize the java.net.Socket class for low-level socket operations. Employ Timer class to schedule periodic checks.
To efficiently manage connections to URLs in a Java application, you can use the java.net.URL class to represent the URLs and the java.net.HttpURLConnection class to open connections to these URLs. To perform periodic checks, you can utilize ScheduledExecutorService to create a scheduled task that makes HTTP requests at specified intervals. This approach allows for efficient URL checking and connection management.
What is the average-case time complexity of Binary Search?
- O(1)
- O(log n)
- O(n log n)
- O(n)
The average-case time complexity of Binary Search is O(log n), where 'n' is the number of elements in the array. This is because, on average, Binary Search eliminates half of the remaining elements with each comparison, resulting in a logarithmic growth rate. In the worst-case, it is still O(log n), but in the best-case, it can be O(1) as mentioned in the first question.