Which method is used to obtain the result from a Future object?

  • fetchResult()
  • get()
  • getResult()
  • obtainResult()
The get() method is used to obtain the result from a Future object in Java. This method is called on a Future instance, and it blocks until the result is available if it's not already computed. It returns the result of the computation or throws an exception if the computation encountered an error.

Imagine you are developing a multi-module application where some modules will be developed by third-party vendors. How would you ensure that the third-party modules adhere to a certain API but do not inherit default method implementations?

  • Provide a detailed API documentation to third-party vendors, leaving it to them to ensure API adherence without enforcing a specific coding approach.
  • Use a combination of interfaces and abstract classes, allowing third-party vendors to choose between them based on their needs.
  • Use abstract classes for the API, providing method stubs without default implementations, and have third-party vendors extend these classes to implement the API.
  • Use interfaces with default methods for the API, allowing third-party vendors to implement the interface while overriding the default methods as needed.
In this scenario, using abstract classes for the API is the preferred choice. Abstract classes provide method stubs without default implementations, ensuring that third-party vendors must implement the required methods while giving them flexibility in their approach. Interfaces with default methods could lead to unwanted method inheritance.

Can we overload Java main method?

  • No, main method overloading is not allowed in Java.
  • Yes, by changing the access modifiers.
  • Yes, by changing the number or type of parameters.
  • Yes, by changing the return type.
In Java, you can overload the main method by changing the number or type of parameters. However, only the standard public static void main(String[] args) method is recognized as the entry point of a Java program. Overloading main with different parameter types won't be recognized as the program's entry point. The JVM expects the standard main method signature.

The FXMLLoader class is utilized to load ________ files.

  • FXML
  • JavaFX
  • UI
  • XML
The FXMLLoader class in JavaFX is used to load FXML (FXML Markup Language) files. FXML files are typically used for defining the user interface of JavaFX applications in a declarative manner. The other options, JavaFX, UI, and XML, are not the files loaded by the FXMLLoader.

In a scenario where you need to add a new method to an interface that is implemented by dozens of classes without breaking existing functionality, how would you achieve this in Java 8 and above?

  • Add a default method to the interface, providing a default implementation for the new method. This way, existing implementations won't be affected, and classes can choose to override the default method if needed.
  • Create a separate utility class with the new method and have implementing classes use this utility class to access the new functionality.
  • It's not possible to add a new method to an existing interface without breaking existing functionality in Java 8 and above.
  • Use a combination of a new interface that extends the existing one with the new method and update all implementing classes to implement the new interface.
In Java 8 and above, you can add a new method to an existing interface by providing a default implementation for the new method. Existing classes that implement the interface won't be affected and can choose to override the new method if needed. This ensures backward compatibility without breaking existing functionality.

The keyword _______ is used to instantiate an object inside its own class definition.

  • constructor
  • instantiate
  • new
  • this
In Java, the keyword new is used to instantiate an object inside its own class definition. When you use new, you create a new instance of the class and allocate memory for it. The other options are not used for this purpose.

Interfaces in Java can have ________ methods from Java 8 onwards.

  • Both Abstract and Static Methods
  • Non-Static Methods
  • Only Abstract Methods
  • Only Static Methods
Starting from Java 8, interfaces in Java can have both abstract and static methods. This enhancement allows interfaces to have default method implementations using the 'default' keyword and static utility methods. However, they still cannot have instance variables.

What is method overloading in Java?

  • It allows creating methods with the same name and parameters but in different classes.
  • It allows multiple methods in a class with the same name but different parameters.
  • It means creating methods with the same name and return type in a class.
  • It refers to changing the method name in a class to make it more descriptive.
Method overloading in Java occurs when there are multiple methods in a class with the same name but different parameters. This is a form of polymorphism and allows you to use the same method name for operations that are logically related but take different inputs.

Consider the code: while(false) { System.out.println("Hello"); }. How many times will "Hello" be printed?

  • 0
  • 1
  • 5
  • It will not be printed at all.
"Hello" will not be printed at all because the condition in the while loop is false from the start. In a while loop, the code block inside the loop will only execute if the condition is true. Since the condition is false, the code block is never executed.

The statement that is used to create an exception object and hand it off to the runtime system is called ________.

  • catch
  • finally
  • throw
  • try
In Java, the throw statement is used to create an exception object and hand it off to the runtime system. This allows you to manually throw exceptions when specific conditions are met in your code.