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.

How can a developer prevent a field from being serialized?

  • Mark the field as final.
  • Mark the field as private.
  • Mark the field as static.
  • Mark the field as transient.
In Java, to prevent a field from being serialized, you can mark it as transient. When a field is marked as transient, it will not be included in the serialization process. The other options do not directly prevent serialization. Marking a field as final has no impact on serialization. Marking it as static means the field will be serialized. Marking it as private affects only access, not serialization.

What is the purpose of the serialVersionUID field and how does it play a role during deserialization?

  • It determines whether an object can be serialized or not
  • It helps prevent security vulnerabilities in deserialization
  • It is a randomly generated unique identifier
  • It specifies the version of the class, ensuring compatibility during deserialization
The serialVersionUID field is used to specify the version of a class during serialization. It plays a crucial role during deserialization by ensuring compatibility. If the version of the class that was serialized differs from the version during deserialization, it can lead to InvalidClassException. This field helps maintain compatibility and avoid issues. The other options are not the primary purpose of serialVersionUID.

If a program encounters an issue while writing to a file and you haven’t handled exceptions, the program will ________.

  • continue execution
  • display an error message
  • pause execution
  • terminate without any error message
If a program encounters an issue while writing to a file and exceptions are not properly handled, the program will terminate abruptly without any error message. This can lead to unexpected behavior and data loss. It's crucial to handle exceptions when working with file I/O to gracefully manage errors and provide meaningful feedback.