The keyword ________ is used within a constructor to call another constructor in the same class.

  • constructor()
  • extend()
  • super()
  • this()
In Java, the this() keyword is used within a constructor to call another constructor in the same class. This is called constructor chaining and allows you to reuse code logic among constructors in a class. The super() keyword is used to call a constructor of the superclass. The other options are not valid for constructor invocation.

What is the key difference between the poll() and remove() methods when used with a Queue in Java?

  • poll() returns null if the queue is empty, while remove() throws an exception.
  • poll() removes and returns the head of the queue if it's not empty, while remove() removes and returns the head but throws an exception if the queue is empty.
  • poll() removes and returns the head of the queue if it's not empty, while remove() returns null if the queue is empty.
  • poll() and remove() have the same behavior when used with a Queue.
In Java, the poll() method is used to retrieve and remove the head of a queue. If the queue is empty, it returns null. On the other hand, the remove() method also removes the head of the queue but throws a NoSuchElementException if the queue is empty. So, option (b) is the correct answer, with the explanation that the key difference is in the handling of empty queues.

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.

To avoid an infinite loop, the condition within the ________ loop must eventually be false.

  • infinite
  • inner
  • nested
  • outer
To prevent an infinite loop in Java, the condition within the outer loop must eventually evaluate to false. An infinite loop occurs when the loop condition is always true, and the loop keeps executing indefinitely. The other options are not directly related to the prevention of infinite loops.