What is the outcome of calling the get() method on Future if the task is canceled?
- It returns the result of the task.
- It throws a CancellationException.
- It throws an ExecutionException.
- It throws an InterruptedException.
When you call the get() method on a Future and the associated task is canceled, it throws a CancellationException. This exception indicates that the task was canceled before it could complete. It is important to catch this exception when working with Future objects to handle canceled tasks gracefully.
What does the setScene() method do in JavaFX?
- Sets the background.
- Sets the primary stage.
- Sets the scene for the stage.
- Sets the title.
In JavaFX, the setScene() method is used to set the scene for a Stage. The scene contains the graphical content that you want to display within the stage. By calling setScene(), you associate a specific scene with a stage, allowing you to display different content. The other options are not the purpose of this method.
Which operator is used in Java to compare two string objects for equality?
- !=
- .equals()
- ==
- compare()
In Java, you should use the .equals() method to compare the contents of two string objects for equality. The == operator, on the other hand, checks whether the two string objects are the same object in memory, which is not the same as comparing their content. The other options are not the recommended way to compare strings for equality.
Imagine a scenario in a multi-threaded application where certain resources are being accessed concurrently, leading to data inconsistency. How would you solve this issue using Locks and Conditions?
- Implement resource locking using the volatile keyword to ensure data consistency and use Thread.sleep() for thread synchronization.
- Use ExecutorService to schedule resource access tasks concurrently, as the use of Locks and Conditions is not necessary in this scenario.
- Use ExecutorService to schedule resource access tasks sequentially, ensuring that only one thread accesses the resources at a time.
- Use synchronized blocks to protect access to the shared resources and notify/wait mechanisms from within those blocks to coordinate thread access.
In a multi-threaded scenario where data inconsistency is a concern, you can use Locks and Conditions. Synchronized blocks can be used to protect access to shared resources, and notify/wait mechanisms can be used to coordinate thread access. This ensures that only one thread accesses the resource at a time, preventing data inconsistency.
Which of the following is the correct way to declare an integer variable in Java?
- float a;
- int a = 5;
- int[] a;
- integer a = 5;
In Java, the int keyword is used to declare an integer variable. The syntax is: int variableName = value;. The other options are not the correct ways to declare a single integer variable.
The method ________ of ServerSocket class is used to listen for incoming client requests.
- accept()
- connect()
- listen()
- open()
In Java, the accept() method of the ServerSocket class is used to listen for incoming client requests. When a client attempts to connect, this method accepts the connection and returns a Socket object for further communication.
Can a subclass constructor directly access the private variables of its superclass?
- No
- Only if they are static
- Only if they have the same name
- Yes
No, a subclass constructor cannot directly access the private variables of its superclass. Private variables are not visible to subclasses, so they cannot be accessed or modified directly. Instead, you can use setter and getter methods or make the superclass variables protected or package-private (default) if you need subclass access.
How can you prematurely exit a loop in Java?
- break statement
- continue statement
- goto statement (not recommended)
- return statement
You can use the break statement to prematurely exit a loop in Java. When break is encountered within a loop, it immediately terminates the loop's execution, allowing you to exit the loop based on a certain condition or event. It is a commonly used control flow statement for loop termination.
You are working on a class that must not be instantiated and only serves to provide utility static methods. How would you utilize constructors to enforce this non-instantiability?
- Define a constructor with a boolean parameter to control instantiation.
- Define a private constructor to prevent instantiation and make the class final to prevent subclassing.
- Make the class abstract with a default constructor for utility methods.
- Utilize a public constructor with a warning message to discourage instantiation.
To enforce non-instantiability of a class meant for utility static methods, you should define a private constructor, preventing external instantiation. Additionally, making the class final ensures that it cannot be subclassed, further enforcing its intended usage as a utility class with only static methods.
Using the ________ method, you can run multiple SQL statements using a single Statement object, separated by semicolons.
- execute()
- executeBatch()
- executeQuery()
- executeUpdate()
The executeBatch() method in JDBC allows you to execute multiple SQL statements using a single Statement object, with statements separated by semicolons. This is particularly useful for batch processing. Other options are valid for executing single SQL statements.