In which version of Java were Lambda expressions introduced?

  • Java 6
  • Java 7
  • Java 8
  • Java 9
Lambda expressions were introduced in Java 8. They are a significant feature that simplifies the syntax for writing anonymous functions and enhances the readability and conciseness of code in Java.

The ________ class represents a Uniform Resource Identifier and is designed to handle the complete URI syntax.

  • URI
  • URL
  • URN
  • UniformResource
The correct answer is "URI." In Java, the URI class is used to represent a Uniform Resource Identifier. It's designed to handle the complete URI syntax, including components like scheme, authority, path, query, and fragment. A URI is a broader concept that includes URLs (Uniform Resource Locators) and URNs (Uniform Resource Names).

The ________ method of the ExecutorService interface is commonly used to submit a Callable task and returns a Future object.

  • execute
  • invokeAll
  • start
  • submit
In Java, the submit method of the ExecutorService interface is used to submit a Callable task and returns a Future object representing the result of the computation. This method is commonly used for asynchronous tasks that return results.

A constructor in Java cannot have a return type and is declared with the same name as the ________.

  • class
  • interface
  • method
  • object
In Java, constructors are special methods used to initialize objects. They have the same name as the class they belong to, making option 1 ("class") the correct choice. Constructors cannot have a return type.

The ______ arithmetic operator divides the left-hand operand by the right-hand operand and returns the remainder.

  • %
  • *
  • +
  • -
The "%" (modulo) operator in Java is used to divide the left-hand operand by the right-hand operand and returns the remainder. For example, "10 % 3" returns 1 because 10 divided by 3 leaves a remainder of 1. The other operators perform different arithmetic operations.

What is the initial capacity of the HashMap when no size is defined, and how is it related to the number of entries it can ideally accommodate without resizing?

  • 16, and it can accommodate 16 entries.
  • 10, and it can accommodate 10 entries.
  • 32, and it can accommodate 32 entries.
  • The initial capacity is not defined, and it dynamically adjusts as entries are added.
In Java, when you create a HashMap without specifying an initial capacity, it defaults to an initial capacity of 16. This means that initially, the HashMap can accommodate 16 key-value pairs. However, as the number of entries increases and reaches a certain threshold (usually 75% of the capacity), the HashMap automatically resizes itself, doubling its capacity. So, option (a) is correct, with the explanation that it can ideally accommodate 16 entries initially but will resize when necessary.

What will be the output of calling a method overridden in the child class?

  • It depends on how the method is called
  • It depends on the method's signature
  • The child class's method will always be called
  • The parent class's method will always be called
When a method is overridden in the child class, the version of the method in the child class is called when the method is invoked on an instance of the child class. This is known as method overriding and is a fundamental concept in object-oriented programming.

Lambda expressions eliminate the need for ________.

  • Anonymous inner classes
  • Arrays
  • Inheritance
  • Interfaces
Lambda expressions eliminate the need for anonymous inner classes. Before lambda expressions were introduced in Java, anonymous inner classes were used to implement single-method interfaces, like Runnable or ActionListener. Lambda expressions provide a more concise and expressive way to define such implementations, reducing the verbosity of code.

How does the compiler resolve the "+" operator when used with different data types (e.g., String and int)?

  • It performs the operation based on the type of the first operand and ignores the second operand's type.
  • It throws a compilation error because the + operator cannot be used with different data types.
  • It throws a runtime error because the + operator is ambiguous with different data types.
  • It uses type conversion to promote one of the operands to the type of the other operand, and then performs the operation.
It uses type conversion to promote one of the operands to the type of the other operand, and then performs the operation. For example, if you add a string and an int, the int is converted to a string, and string concatenation is performed. If you add two integers, normal addition is performed.

Imagine you are developing a text editor that frequently alters strings (like undo, redo, replace, cut, copy, and paste operations). Which class(es) would you utilize for efficient memory and performance management?

  • String and PhantomReference
  • StringBuffer and SoftReference
  • StringBuilder and WeakReference
  • StringJoiner and ReferenceQueue
In a text editor, where efficient memory and performance management are crucial, StringBuilder is used for in-place string manipulation, and WeakReference helps in memory management by allowing objects to be garbage collected when not strongly referenced. StringBuffer is thread-safe but may not provide the best performance. The other options are not suitable for such scenarios.