How does type inference work with Lambda expressions in Java?

  • Type inference always uses the Object type for Lambda parameters.
  • Type inference automatically infers parameter types from the context and Lambda body.
  • Type inference is not applicable to Lambda expressions in Java.
  • Type inference relies on explicit type declarations for Lambda parameters.
In Java, Lambda expressions benefit from type inference, which allows the compiler to automatically infer the parameter types from the context and the Lambda body. This feature makes Lambda expressions concise and expressive. It helps reduce the verbosity of code by eliminating the need for explicit type declarations in most cases. Understanding how type inference works with Lambdas is essential for writing clean and readable code when using functional programming techniques in Java.

When reading from a file using a FileReader, it's often wrapped with a ________ to increase efficiency.

  • BufferedReader
  • FileBufferReader
  • FileWriter
  • InputStreamReader
When reading from a file, wrapping a FileReader with a BufferedReader is a common practice in Java. BufferedReader buffers the input, reducing the number of reads from the file and thus increasing efficiency.

Which of the following data structures does not allow duplicate elements?

  • ArrayList
  • HashSet
  • LinkedList
  • TreeMap
The HashSet data structure in Java does not allow duplicate elements. It is implemented using a hash table and ensures that all elements are unique by using the hashing mechanism. ArrayList and LinkedList can contain duplicate elements, while TreeMap allows duplicates based on keys.

What will be the output of the executeQuery() method in JDBC?

  • A boolean indicating success or failure.
  • Nothing; it doesn't return any value.
  • The number of rows affected.
  • The result of the SQL query execution.
The executeQuery() method in JDBC returns a ResultSet object, which contains the result of the SQL query execution. This ResultSet can be used to retrieve and manipulate the query results. The other options are incorrect as they do not accurately describe the output of this method.

The ________ class provides a tunable, flexible thread pool.

  • ExecutorService
  • Runnable
  • Thread
  • ThreadPoolExecutor
The ThreadPoolExecutor class in Java provides a tunable, flexible thread pool. It allows you to customize various aspects of thread pool behavior, such as the number of threads and task scheduling.

How does the BufferedOutputStream class work in Java when connected to a file output byte stream?

  • It compresses the data before writing it to the file stream to save storage space.
  • It encrypts the data to provide security during the write operation.
  • It improves I/O performance by reducing the number of write operations to the underlying stream.
  • It reads data from the file output stream and stores it in a buffer for later retrieval.
BufferedOutputStream in Java enhances performance by buffering the output data before writing it to the underlying file stream. This reduces the number of write operations and improves efficiency. It doesn't read or compress data; its primary purpose is to optimize data output.

How can we gracefully shutdown an ExecutorService?

  • Call shutdown()
  • Call shutdown() and then shutdownNow()
  • Call shutdownNow() and then shutdown()
  • Call stop()
To gracefully shut down an ExecutorService, you should first call the shutdown() method, which initiates an orderly shutdown, allowing previously submitted tasks to execute before terminating. You should not call shutdownNow() before shutdown() because it forcefully attempts to stop all executing tasks. The other options are incorrect for a graceful shutdown.

Imagine you are implementing a system that performs various mathematical operations. How would you use Lambda expressions to define various operations in a clean and efficient way?

  • By creating a separate class for each mathematical operation and using the Lambda keyword to instantiate objects of these classes.
  • By defining functional interfaces with abstract methods corresponding to each mathematical operation and implementing them using Lambda expressions.
  • By using nested Lambda expressions within each other to perform mathematical operations.
  • By using traditional method declarations and avoiding Lambda expressions for mathematical operations.
In Java, you can use Lambda expressions to implement functional interfaces with a single abstract method. For mathematical operations, you can define functional interfaces, such as BinaryOperator, and use Lambda expressions to implement these interfaces concisely and efficiently. This approach enhances code readability and allows you to pass behavior as data, making your code more modular.

Can we override a method in the same class?

  • No, it is not allowed
  • Yes, but it has no practical purpose
  • Yes, it is allowed
  • Yes, with different method names
In Java, method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. Therefore, it is not possible to override a method in the same class because there is no superclass-subclass relationship. However, you can overload methods within the same class by defining methods with the same name but different parameter lists.

The ________ parameter allows Lambda expressions to be passed around as if it was a type.

  • Consumer
  • Functional
  • Predicate
  • Target
The term you're looking for is "Functional." In Java, Lambda expressions can be assigned to functional interfaces, which act as a type for lambda expressions. These interfaces typically define a single abstract method that the lambda expression implements. This allows lambda expressions to be treated as if they were a type, and they can be passed as parameters to methods, returned from methods, or stored in variables.