The collect() method in the Stream API is a type of ________ operation.
- Intermediate
- Stateful
- Stateless
- Terminal
The collect() method is a terminal operation in the Stream API. It is used to accumulate the elements of a stream into a collection, such as a List or Set. Terminal operations are those that trigger the processing of the stream and produce a final result.
In a real-world application managing user profiles, how might parameterized constructors be used to quickly initialize user objects with provided details during registration?
- Parameterized constructors are not used for user profile initialization.
- Parameterized constructors are used for data retrieval from the database.
- Parameterized constructors are used to create admin profiles with elevated privileges.
- Parameterized constructors can accept user details as parameters and create user objects with initialized data during registration.
Parameterized constructors can be utilized in a user profile management system to quickly initialize user objects during the registration process. These constructors accept user details (e.g., username, email, password) as parameters, allowing you to create user objects with pre-populated data, making registration more efficient and straightforward.
Lambda expressions are used primarily to define the implementation of ________ interfaces.
- Abstract
- Functional
- Interface
- Marker
Lambda expressions in Java are primarily used to define the implementation of functional interfaces. A functional interface is an interface that has only one abstract method. Lambda expressions provide a concise way to implement the single abstract method of such interfaces.
Which of the following expressions will result in a value of true?
- "Java" == "java"
- (4 + 6) * 2 == 20
- 10 != 10
- 5 > 3
The expression 5 > 3 is true because 5 is indeed greater than 3. The other options are not true: 10 != 10 is false (since 10 is equal to 10), (4 + 6) * 2 == 20 is true (since 10 equals 20), and "Java" == "java" is false because string comparison in Java is case-sensitive.
To access or modify the private fields in a class, you should use ________ methods.
- accessor
- constructor
- mutator
- public
To access or modify private fields in a class, you should use "mutator" methods, also known as setter methods. These methods are designed to set or modify the private field values, maintaining control over access.
The method overriding is also known as ________ time polymorphism.
- compile-time
- dynamic
- runtime
- static
Method overriding is a form of polymorphism that occurs at runtime, making it dynamic polymorphism. It allows the actual method to be determined at runtime based on the object's type, which is why it's also known as runtime polymorphism.
In a class instance, synchronized blocks can be used to avoid locking the entire method and only lock ________.
- the class instance
- the instance itself
- the method
- the thread
In Java, synchronized blocks allow you to lock specific portions of a method rather than locking the entire method. By using synchronized blocks, you can choose what specific data or code you want to protect from concurrent access by specifying the object that should serve as the lock, commonly referred to as "the method's ________."
The method ________ is used to execute SQL for DDL statements using JDBC.
- executeDDL()
- executeQuery()
- executeStatement()
- executeUpdate()
In JDBC, the method executeUpdate() is used to execute SQL statements that perform Data Definition Language (DDL) operations, such as creating, altering, or dropping database objects. It returns an integer representing the number of rows affected by the statement. executeQuery() is used for retrieving data, not DDL statements.
If a class has multiple constructors, it can be said to have constructor ________.
- chaining
- overloading
- overriding
- polymorphism
When a class has multiple constructors with different parameter lists, it is said to have constructor overloading. Constructor overloading allows you to create multiple constructors in the same class, each with a different set of parameters. This is a form of method overloading specific to constructors. Constructor overriding is not a valid term in Java.
How can you access variables in the surrounding scope from a lambda expression?
- They are automatically accessible within the lambda.
- You can access them using the super keyword.
- You cannot access them.
- You need to pass them as parameters to the lambda expression.
To access variables from the surrounding scope within a lambda expression, you typically need to pass them as parameters to the lambda expression. This is known as "capturing" variables. Lambda expressions in Java can access effectively final local variables and instance variables. Attempting to access non-final variables can result in compilation errors.