In a switch case, the ________ keyword is used to specify the code that should be executed if no case matches.

  • default
  • defaultcase
  • else
  • fallback
In a switch case in Java, the default keyword is used to specify the code that should be executed if none of the case labels match the switch expression. It acts as a fallback or default option when no other case matches the switch expression.

What is the primary purpose of using synchronized methods in Java?

  • To ensure that a method is only accessible within the same package.
  • To make a method private and inaccessible.
  • To optimize the execution speed of a method.
  • To prevent multiple threads from accessing and modifying shared resources simultaneously.
Synchronized methods in Java are used primarily to prevent multiple threads from accessing and modifying shared resources simultaneously, ensuring thread safety. This is crucial in multithreading scenarios where multiple threads may otherwise interfere with each other when accessing shared data.

Which Java method is used to establish a connection to a specified URL?

  • URLConnect()
  • connectToURL()
  • establishConnection()
  • openConnection()
To establish a connection to a specified URL in Java, you use the openConnection() method provided by the URL class. This method returns a URLConnection object, which can be used to interact with the resource located at the URL.

What is the worst-case time complexity of the Quick Sort algorithm?

  • O(log n)
  • O(n log n)
  • O(n)
  • O(n^2)
The worst-case time complexity of the Quick Sort algorithm is O(n^2). However, on average, it has a time complexity of O(n log n), making it a very efficient sorting algorithm for large datasets. The worst-case scenario occurs when the pivot chosen is always the smallest or largest element, resulting in unbalanced partitions.

What will be the output of the following code snippet: System.out.println("Java".concat("Programming"))?

  • Compilation Error
  • Java Programming
  • JavaProgramming
  • ProgrammingJava
The concat() method in Java combines two strings, and in this case, it appends "Programming" to "Java," resulting in "JavaProgramming." Therefore, the correct output is "JavaProgramming."

In JDBC, the ________ provides methods to move to the first record, last record, next record, and previous record in a ResultSet.

  • ResultIterator
  • ResultSet
  • ResultSetMetadata
  • ResultSetNavigation
In JDBC, the ResultSet interface provides methods like first(), last(), next(), and previous() to navigate through the result set obtained from a database query. The other options are not related to ResultSet navigation in JDBC.

In a scenario where you need to add a new method to an interface that is implemented by dozens of classes without breaking existing functionality, how would you achieve this in Java 8 and above?

  • Add a default method to the interface, providing a default implementation for the new method. This way, existing implementations won't be affected, and classes can choose to override the default method if needed.
  • Create a separate utility class with the new method and have implementing classes use this utility class to access the new functionality.
  • It's not possible to add a new method to an existing interface without breaking existing functionality in Java 8 and above.
  • Use a combination of a new interface that extends the existing one with the new method and update all implementing classes to implement the new interface.
In Java 8 and above, you can add a new method to an existing interface by providing a default implementation for the new method. Existing classes that implement the interface won't be affected and can choose to override the new method if needed. This ensures backward compatibility without breaking existing functionality.

The keyword _______ is used to instantiate an object inside its own class definition.

  • constructor
  • instantiate
  • new
  • this
In Java, the keyword new is used to instantiate an object inside its own class definition. When you use new, you create a new instance of the class and allocate memory for it. The other options are not used for this purpose.

Interfaces in Java can have ________ methods from Java 8 onwards.

  • Both Abstract and Static Methods
  • Non-Static Methods
  • Only Abstract Methods
  • Only Static Methods
Starting from Java 8, interfaces in Java can have both abstract and static methods. This enhancement allows interfaces to have default method implementations using the 'default' keyword and static utility methods. However, they still cannot have instance variables.

What is method overloading in Java?

  • It allows creating methods with the same name and parameters but in different classes.
  • It allows multiple methods in a class with the same name but different parameters.
  • It means creating methods with the same name and return type in a class.
  • It refers to changing the method name in a class to make it more descriptive.
Method overloading in Java occurs when there are multiple methods in a class with the same name but different parameters. This is a form of polymorphism and allows you to use the same method name for operations that are logically related but take different inputs.