Which class is commonly used to create a simple animation that moves a node along a path in JavaFX?

  • Animation
  • NodeMoveAnimation
  • PathTransition
  • Scene
In JavaFX, the PathTransition class is commonly used to create animations that move a node along a path. This class allows you to specify the path, duration, and other animation properties. The other options, like Scene, are used for different aspects of JavaFX applications.

Which of the following is synchronized?

  • ArrayList
  • Hashtable
  • LinkedList
  • StringBuilder
The Hashtable class in Java is synchronized, which means it is thread-safe. This ensures that multiple threads can safely access and modify the Hashtable concurrently without causing data corruption. ArrayList and LinkedList are not synchronized, making them suitable for single-threaded operations by default. StringBuilder is also not synchronized and is used for efficient string manipulation in a single thread.

Which of the following is true regarding the flow of control in a try-catch-finally statement?

  • The catch block is executed before finally.
  • The catch block is optional.
  • The code inside try is optional.
  • The finally block is executed before try.
In a try-catch-finally statement in Java, the flow of control is such that the try block is executed first. If an exception occurs within the try block, control is transferred to the appropriate catch block (if matching). Finally block is executed after try (whether an exception occurred or not), making it suitable for cleanup code.

In a switch case, the ________ keyword is used to specify the code that should be executed if no case matches.

  • default
  • defaultcase
  • else
  • fallback
In a switch case in Java, the default keyword is used to specify the code that should be executed if none of the case labels match the switch expression. It acts as a fallback or default option when no other case matches the switch expression.

What is the primary purpose of using synchronized methods in Java?

  • To ensure that a method is only accessible within the same package.
  • To make a method private and inaccessible.
  • To optimize the execution speed of a method.
  • To prevent multiple threads from accessing and modifying shared resources simultaneously.
Synchronized methods in Java are used primarily to prevent multiple threads from accessing and modifying shared resources simultaneously, ensuring thread safety. This is crucial in multithreading scenarios where multiple threads may otherwise interfere with each other when accessing shared data.

Which Java method is used to establish a connection to a specified URL?

  • URLConnect()
  • connectToURL()
  • establishConnection()
  • openConnection()
To establish a connection to a specified URL in Java, you use the openConnection() method provided by the URL class. This method returns a URLConnection object, which can be used to interact with the resource located at the URL.

If a superclass has a protected field, will subclasses in different packages have direct access to it?

  • Access to protected fields depends on the specific package-level access rules defined in the project.
  • No, subclasses in different packages cannot access the protected field directly; they must use getter and setter methods.
  • Subclasses can access protected fields, but only if they are in the same package as the superclass.
  • Yes, subclasses in different packages can access the protected field directly without any restrictions.
Subclasses in different packages cannot access the protected field directly. Protected members are accessible to subclasses, but only within the same package or through inheritance. Access control rules apply to protect the encapsulation of classes across packages, ensuring proper access control and encapsulation.

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.