What is the correct syntax for the switch statement in Java?

  • select(expr) { }
  • switch { case: ... break; }
  • switch(expr) { case: ... }
  • switch(expression) { }
In Java, the correct syntax for a switch statement is: switch (expression) { case value1: // Code for value1 break; case value2: // Code for value2 break; // Add more cases as needed default: // Code to execute if no case matches } The switch statement is used for multi-way branching based on the value of the expression.

BufferedReader uses a default buffer size of ________ characters unless specified otherwise.

  • 1024
  • 4096
  • 512
  • 8192
BufferedReader uses a default buffer size of 1024 characters unless you specify a different size during its initialization. Choosing an appropriate buffer size can optimize input operations.

What is the return type of the map() function in Java Streams API?

  • List
  • Stream
  • int
  • void
The map() function in Java Streams API transforms the elements of a stream and returns a new stream of the transformed elements. Therefore, the return type of map() is Stream. It allows you to apply a function to each element and map them to a new value or type.

In Java, if an if statement does not have any braces {}, only the ________ line after the if condition is considered part of the if block.

  • first
  • last
  • middle
  • next
In Java, when an if statement does not have braces {}, only the first line of code immediately following the if condition is considered part of the if block. Any subsequent lines of code are executed outside of the if block. This can lead to unexpected behavior if not used carefully.

Which exception might be thrown when trying to create a new Socket instance?

  • IOException
  • NetworkException
  • SocketCreationException
  • SocketException
When attempting to create a new Socket instance, you may encounter an IOException if there's an issue with the network connection or if the host is unreachable. It's a common exception in socket programming. The other options are not standard exceptions related to socket creation.

How does Java determine which overloaded method to call?

  • Java calls the method randomly, as it cannot determine which one to call.
  • Java calls the method that exactly matches the arguments provided during the method call.
  • Java calls the method with the fewest number of parameters.
  • Java calls the method with the most number of parameters.
Java determines which overloaded method to call by examining the number and types of arguments provided during the method call. It looks for the method that exactly matches the provided arguments. If no exact match is found, it results in a compilation error.

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.