The ________ method of DatagramSocket class is used to send a packet to a server.

  • send()
  • sendPacket()
  • sendToServer()
  • transmitPacket()
In Java, the send() method of the DatagramSocket class is used to send a packet to a server. It is a crucial method for working with UDP (User Datagram Protocol) sockets.

What method is commonly used to execute SQL queries in Java?

  • execute()
  • executeQuery()
  • executeStatement()
  • executeUpdate()
In Java, the executeQuery() method is commonly used to execute SQL queries using JDBC. This method is specifically designed for executing SELECT queries and returns a ResultSet containing the query results. The other options (executeStatement(), executeUpdate(), and execute()) are not typically used for executing queries or have different purposes.

Imagine a situation where you are dealing with very large files that need to be read and processed, but you want to ensure that the system remains responsive and does not run out of memory. How would you implement file reading in this case?

  • Read the entire file into memory using FileInputStream.
  • Use a BufferedReader with a small buffer size.
  • Implement a memory-mapped file using FileChannel and MappedByteBuffer.
  • Use Scanner with a large buffer size.
To ensure responsiveness and prevent memory exhaustion when dealing with large files, it's best to use a BufferedReader with a reasonably small buffer size. This allows for efficient reading of the file without loading the entire content into memory. The other options are less efficient and may lead to memory issues.

The expression a != b returns true if a is ______ b.

  • equal to
  • greater than
  • less than
  • not equal to
The expression "a != b" returns true if "a" is not equal to "b." It checks if the values of "a" and "b" are different. The "!=" operator is used for inequality comparisons. The other options represent different comparison operations.

What will be the output of the following code snippet: public int add(int a, long b) { return a + b; } and public long add(int a, int b) { return a + b; }?

  • Compilation Error: Ambiguous method call.
  • Compilation Error: Duplicate method add with the same parameter types.
  • Compilation Error: Mismatched return types.
  • The code will run without errors, and the output will be the sum of a and b.
The code will result in a compilation error because both methods have the same name and the same parameter types (int and long). Java does not allow you to overload methods based solely on the return type. Overloaded methods must have different parameter lists. Overloading based on return types would lead to ambiguity.

What will be the initial capacity of a HashSet when no size is defined during its creation?

  • 0
  • 10
  • 16
  • It will result in an error
When you create a HashSet in Java without specifying its initial capacity, it defaults to an initial capacity of 16. The capacity dynamically increases as needed when elements are added to the HashSet.

If we have a 2D array int[][] arr, the expression arr[i] refers to the __________.

  • an error
  • entire array
  • ith column of the array
  • ith row of the array
In a 2D array in Java, arr[i] refers to the ith row of the array. This is because a 2D array is essentially an array of arrays, where each element arr[i] is itself an array representing a row. Accessing arr[i] gives you the entire row at index i.

How does Binary Search perform when there are multiple occurrences of the search key in the data?

  • It returns an error due to ambiguity.
  • It returns the index of the first occurrence.
  • It returns the index of the last occurrence.
  • It returns the index of the middle occurrence.
Binary Search is designed to find the index of the last occurrence of the search key when there are multiple occurrences. This is because, in binary search, if a match is found, the algorithm continues searching in the right subarray to ensure it returns the last occurrence. This behavior is efficient for tasks like counting occurrences. The other options do not accurately describe the behavior of Binary Search in this context.

The setLength method, which allows altering the length of the character sequence stored in the object, is a method of ________.

  • CharArray
  • String
  • StringBuffer
  • StringBuilder
The setLength method is a method of the StringBuffer class in Java. It allows you to alter the length of the character sequence stored in the StringBuffer object. The other options are not related to the setLength method.

Which operator can be used to invert the sign of a numeric expression?

  • !
  • +
  • -
  • ~
The - (minus) operator can be used to invert the sign of a numeric expression. For example, if you have a variable int x = 5;, then -x would give you -5. The other options are not used for inverting the sign of numeric values.