The keyword ________ is used to apply restrictions on class, method, and variable.

  • final
  • private
  • protected
  • static
The keyword "private" is used to apply restrictions on class members. It restricts access to only within the same class, making it the most restrictive access modifier in Java.

Which of the following methods can be used to create a stream from a collection?

  • filter()
  • forEach()
  • mapToDouble()
  • stream()
You can create a stream from a collection in Java using the stream() method. This method is available on all collections and provides a way to obtain a stream that allows you to perform various stream operations on the elements of the collection. Options 2 to 4 are not used to create streams but for other stream operations.

What is the difference between a synchronized block and a synchronized method?

  • Synchronized blocks are used for asynchronous programming.
  • Synchronized blocks can be used to synchronize multiple sections of code within the same method.
  • Synchronized methods are more efficient than synchronized blocks.
  • Synchronized methods can only be used to synchronize the entire method.
The key difference between a synchronized block and a synchronized method is that synchronized blocks can be used to synchronize multiple sections of code within the same method, providing more fine-grained control over synchronization. In contrast, synchronized methods synchronize the entire method. Synchronized methods are not necessarily more efficient than synchronized blocks; their efficiency depends on the specific use case.

You are developing a payroll system. How would you design a class for storing employee details and ensuring that some sensitive information (like salary) cannot be accessed directly from the object?

  • Define private instance variables for employee details, including salary, and provide public get and set methods for accessing and modifying the salary.
  • Define public instance variables for employee details and make the salary variable final.
  • Define public static variables for employee details, including salary, and use the 'protected' access modifier.
  • Use the 'volatile' keyword for sensitive information like salary.
To ensure that sensitive information like salary cannot be accessed directly from the object, you should define private instance variables for employee details and provide public get and set methods for salary. This follows the principle of encapsulation, which restricts direct access to sensitive data. The other options do not provide adequate security for sensitive information.

Consider a case where you have to model a "Book" in a library system. What properties and methods would you define in the Book class and why?

  • Properties: name, genre, pages; Methods: play, pause, stop.
  • Properties: title, author, ISBN, publication date; Methods: get and set methods for properties, toString for representation.
  • Properties: title, author, ISBN; Methods: checkAvailability, reserve, extendDueDate.
  • Properties: title, author, publication date; Methods: borrow, return, calculateFine.
In a Book class for a library system, you would define properties like title, author, ISBN, and publication date as they represent essential book attributes. Get and set methods are used for encapsulation and data integrity, and the toString method helps in displaying the book's details. The other options include irrelevant properties and methods.

Which of the following is a key characteristic of a lambda expression in Java?

  • It can capture local variables.
  • It can have multiple methods.
  • It is a data type.
  • It is an anonymous inner class.
A key characteristic of a lambda expression in Java is that it can capture local variables from its enclosing scope. This allows lambda expressions to use values from the surrounding context, making them useful for creating concise and expressive code.

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.

Which of the following functional interfaces in Java utilizes Lambda expressions?

  • AbstractClass
  • FunctionalInterface
  • Runnable
  • Serializable
The Runnable interface in Java can be implemented using lambda expressions. It's a functional interface with a single abstract method run(). This allows you to use lambda expressions for tasks that can be executed concurrently. Lambda expressions simplify the creation of anonymous classes for such tasks.

What will happen if the superclass method does not exist in the subclass while trying to override it?

  • It will automatically create a new method in the subclass with the same name.
  • It will lead to a compile-time error.
  • It will result in a runtime error.
  • It will use the superclass method without any issue.
In Java, when you try to override a method from a superclass in a subclass, the method in the superclass must exist; otherwise, it will lead to a compile-time error. Java enforces method signature matching during compile-time, so if the method doesn't exist in the superclass, the compiler will not find a method to override in the subclass, resulting in an error.