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.
The ________ block can be used to handle different types of exceptions in a single block.
- catch
- finally
- throw
- try
The try block is used to enclose a section of code that may throw exceptions. You can have multiple catch blocks after a single try block to handle different types of exceptions, making it possible to handle various exceptions in a unified way.
You need to perform different actions on each element of a heterogeneous list (i.e., containing different types of objects). How would you implement the loop to handle different types and perform type-specific actions?
- Use a custom iterator with type-specific handlers.
- Use a for-each loop with instanceof checks.
- Use a series of if statements to check each element's type.
- Use a switch statement inside a for loop.
To handle different types in a heterogeneous list, you can use a for-each loop and check each element's type using the instanceof operator. This approach allows you to perform type-specific actions. The other options may not be as flexible or efficient for this task.