To prevent fall-through in a switch case, the ________ keyword is used after each case block.
- break
- continue
- exit
- return
To prevent fall-through in a switch case in Java, you use the break keyword after each case block. This ensures that once a case is matched and executed, the control exits the switch statement and doesn't fall through to subsequent cases.
When a static synchronized method is executed, the thread holds a lock for that method's ________.
- class
- instance
- subclass
- superclass
In Java, when a static synchronized method is executed, it holds a lock for the entire class rather than an instance of the class. This means that other threads attempting to access any synchronized static method of the same class will be blocked until the lock is released, ensuring exclusive access to the class-level resource, which is "the method's ________."
What will be the result of the expression !!true?
- Compilation Error
- FALSE
- Runtime Error
- TRUE
The expression !!true is a double negation of the boolean value true. It will result in true. The first ! operator negates true to false, and the second ! operator negates false back to true. This is a common way to ensure a boolean value is truly true.
Which method is used to retrieve the InputStream of a Socket object?
- getInputStream()
- getOutputStream()
- getSocketInputStream()
- openInputStream()
In Java, you can retrieve the InputStream of a Socket object using the getInputStream() method. This input stream allows you to read data from the socket. The other options are not used for retrieving the input stream of a socket.
Which of the following statements is/are true regarding the final keyword when used with variables?
- A final variable can only be initialized once and cannot be changed afterward.
- A final variable must be initialized at the time of declaration.
- Final variables are automatically initialized with the default value for their data type.
- Final variables can be changed in any method of the class.
When the "final" keyword is used with variables in Java, it indicates that the variable's value can only be assigned once, and after that, it cannot be changed. This enforces immutability and is often used for constants. The other options are incorrect. A final variable must be initialized either at the time of declaration or in the constructor of the class. Final variables are not automatically initialized.
Imagine you are developing a system that requires scheduling of tasks, like sending notifications, at fixed-rate intervals. How would you implement this using concurrency utilities in Java?
- Use CachedThreadPoolExecutor: Dynamically adjust thread pool size to handle scheduling tasks efficiently.
- Use FixedThreadPoolExecutor: Allocate a fixed number of threads for scheduling tasks.
- Use ScheduledThreadPoolExecutor: Schedule tasks with fixed-rate intervals using scheduleAtFixedRate method.
- Use SingleThreadPoolExecutor: Execute tasks one by one with fixed-rate intervals to ensure sequential processing.
For scheduling tasks at fixed-rate intervals, the ScheduledThreadPoolExecutor is the most appropriate choice. It provides methods like scheduleAtFixedRate to achieve this functionality. CachedThreadPoolExecutor and SingleThreadPoolExecutor do not specialize in scheduling tasks, and using FixedThreadPoolExecutor is not optimal for scheduling.
In Java, when a subclass has the same method as declared in the parent class, it is known as method ________.
- hiding
- inheriting
- overloading
- overriding
When a subclass has the same method name, return type, and parameters as declared in the parent class, it is known as method overriding. The subclass provides a specific implementation for that method.
Consider a scenario where two threads are trying to access synchronized methods in a single class instance. Describe how the synchronized methods will manage the access of these two threads.
- Both threads can execute the synchronized methods concurrently, as synchronization allows multiple threads to access synchronized methods simultaneously.
- The first thread will execute the synchronized method, and the second thread will wait until the first thread completes its execution.
- The second thread will execute the synchronized method and then the first thread, as the order of execution is undefined.
- The second thread will execute the synchronized method if it has a higher priority than the first thread.
Synchronized methods in a single class instance are mutually exclusive. Only one thread can execute a synchronized method at a time. The first thread to enter the synchronized method will execute it, and the second thread will wait until the first one completes its execution. The order of execution is not defined, and thread priority does not affect synchronization.
The ______ method of the Animation class is used to play the animation in reverse order from the end position.
- playBackward()
- reverse()
- reverseOrder()
- rewind()
To play an animation in reverse order from the end position in JavaFX, you can use the reverse() method of the Animation class. This method reverses the animation, making it appear as if it's playing backward. The other options do not specifically address reversing animations.
To handle a transaction in JDBC, first, you must set the auto-commit mode to ________ before starting the transaction.
- FALSE
- TRUE
- commit
- rollback
In JDBC, to handle a transaction, you must set the auto-commit mode to false using the setAutoCommit(false) method before starting the transaction. This ensures that multiple SQL statements can be treated as a single transaction until explicitly committed. Setting it to true would mean auto-commit mode is enabled, where each SQL statement is treated as a separate transaction.