Which exception type must be explicitly handled or declared to be thrown in a method?
- Checked exceptions
- Errors
- None of the above
- Unchecked exceptions (Runtime)
In Java, checked exceptions (which extend the Exception class but not RuntimeException) must be explicitly handled or declared to be thrown in a method. This requirement ensures that the code handles potentially problematic situations.
In which case(s) does Binary Search perform in O(1) time complexity?
- When the array is empty
- When the target element is at the first index of the array
- When the target element is at the middle of the array
- When the target element is not present in the array
Binary Search performs in O(1) time complexity when the target element is at the first index of the sorted array. In this case, it can directly access the element and return it. When the array is empty, it still performs in O(1) time complexity as there are no elements to search. In other cases, it performs in O(log n) time complexity, where 'n' is the number of elements in the array.
Which of the following is a valid synchronized method declaration?
- public synchronized void myMethod() {}
- public void myMethod() { synchronized() {} }
- public void synchronized myMethod() {}
- synchronized void myMethod() {}
The correct syntax for a synchronized method declaration in Java is public synchronized void myMethod() {}, where the synchronized keyword comes before the access modifier (public) and the return type (void). This ensures that only one thread can execute the myMethod at a time.
In Java, which of the following statements is true regarding the fall-through behavior in the switch case?
- In Java, when a case statement is matched, execution continues with the next case statement unless terminated by a break or other control flow statement.
- The fall-through behavior is controlled by the continue keyword.
- The fall-through behavior is controlled by the fallthrough keyword.
- The fall-through behavior is not supported in Java switch cases.
In Java, the switch statement supports fall-through behavior, meaning that when a case label is matched, the code will continue to execute the subsequent case labels unless interrupted by a break statement. This behavior allows multiple cases to be executed for a single condition match, making it important to use break to terminate the flow when needed.
Which of the following statements about constructor overloading in Java is correct?
- Constructor overloading allows a class to have multiple constructors with the same name but different parameters.
- Constructor overloading is not supported in Java.
- Constructor overloading requires that all constructors have the same number of parameters.
- Constructor overloading can only be used in abstract classes.
a) is correct. Constructor overloading in Java enables a class to have multiple constructors with the same name but different parameters. This allows for creating objects with various initializations. b) and d) are incorrect statements. c) is also incorrect because the number of parameters can vary among overloaded constructors.
Which of the following states is not a valid thread state in Java?
- Blocked
- Running
- Sleeping
- Terminated
In Java, the valid thread states are Running, Blocked, Terminated, and Sleeping. "Running" is not a valid thread state because it's not used to describe the state of a thread in Java. A running thread is simply a thread that is currently executing its code.
Which method in the ExecutorService interface waits for all tasks to complete after a shutdown request?
- awaitShutdown()
- awaitTermination()
- isTerminated()
- shutdownNow()
The awaitTermination() method in the ExecutorService interface is used to wait for all tasks to complete after a shutdown request has been made using the shutdown() method. It blocks until all tasks have completed or the specified timeout has passed. The isTerminated() method checks if the ExecutorService has terminated but doesn't wait.
A method or a data member which is declared as ________ is accessible to all classes in the Java program.
- default (package-private)
- private
- protected
- public
In Java, when a method or data member is declared as "public," it is accessible to all classes, regardless of their location in the program. This is the highest level of visibility, and it allows unrestricted access from any part of the program.
The switch case in Java can be used with data types like _______, _______, and _______.
- byte, short, int
- char, long, byte
- double, string
- int, float, boolean
In Java, the switch case can be used with data types like char, long, and byte. While other data types like int, float, boolean, double, and String are commonly used in Java, they are not directly compatible with the switch case. This limitation is important to consider when using switch statements.
When using a PreparedStatement, how is a value assigned to a placeholder?
- By calling a separate method named "setValue"
- Placeholder values are automatically assigned
- Using the "=" operator
- setXXX() methods (e.g., setString(), setInt())
In a PreparedStatement, values are assigned to placeholders using the setXXX() methods, where "XXX" represents the data type (e.g., setString() for strings, setInt() for integers). This ensures proper handling of data types and helps prevent SQL injection. The other options are not valid ways to assign values to placeholders.