The ________ method of Connection interface sets the changes made since the previous commit/rollback permanent.
- commit()
- persistChanges()
- saveChanges()
- updateChanges()
The commit() method of the Connection interface in JDBC is used to make the changes made since the previous commit or rollback permanent. It effectively saves the changes to the database. Other options are not valid methods for this purpose.
________ is the class in Java that provides methods to get details of a URL and manipulate them.
- URIDetails
- URL
- URLDetails
- URLManipulator
The correct answer is "URL." In Java, the URL class provides methods to get details of a URL and manipulate them. You can use URL class methods to retrieve various components of a URL, such as the protocol, host, port, path, and more. It is a fundamental class for working with URLs in Java.
What is the primary advantage of using an ExecutorService to manage threads?
- Automatic garbage collection
- Better control over thread creation and reuse
- Greater parallelism and multi-threading control
- Simplicity in managing threads
The primary advantage of using an ExecutorService to manage threads is better control over thread creation and reuse. It provides a higher-level abstraction for managing thread execution, which can lead to more efficient and scalable applications. The other options do not accurately describe the primary advantage of using an ExecutorService.
Which of the following keywords is used to manually throw an exception in Java?
- catch
- throw
- throws
- try
In Java, the throw keyword is used to manually throw an exception. It allows you to create custom exceptions or raise predefined exceptions when certain conditions are met. This is a fundamental part of Java's exception handling mechanism.
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.
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.
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.
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.
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.
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 is the use of the transient keyword in the context of serialization?
- It forces immediate serialization of the field.
- It indicates that the field should be ignored during deserialization.
- It prevents the field from being accessed in any context.
- It specifies that the field should be stored permanently in the serialized object.
In the context of Java serialization, the transient keyword is used to indicate that a field should be ignored during the deserialization process. It ensures that the field's value is not restored from the serialized data. The other options are incorrect; transient doesn't prevent field access, store the field permanently, or force immediate serialization.
When creating a custom exception, extending which class will make it a checked exception?
- Error
- Exception
- RuntimeException
- Throwable
When you extend the Exception class (or its subclasses), your custom exception becomes a checked exception in Java. Checked exceptions must be either caught or declared in the method signature where they are thrown.