What is the average-case time complexity of Binary Search?
- O(1)
- O(log n)
- O(n log n)
- O(n)
The average-case time complexity of Binary Search is O(log n), where 'n' is the number of elements in the array. This is because, on average, Binary Search eliminates half of the remaining elements with each comparison, resulting in a logarithmic growth rate. In the worst-case, it is still O(log n), but in the best-case, it can be O(1) as mentioned in the first question.
What will be the result of the expression 7 ^ 3 in Java?
- 0
- 10
- 3
- 4
In Java, the ^ operator is the bitwise XOR operator. It performs a bitwise XOR operation on the binary representations of the two operands. In this case, 7 in binary is 0111, and 3 in binary is 0011. Performing XOR, we get 0100, which is 4 in decimal. So, the result is 4.
Can method overloading be achieved by changing only the return type of methods?
- No, method overloading can only be achieved by changing both the method name and return type.
- No, method overloading is not possible by changing only the return type; the method name and/or parameters must also differ.
- Yes, as long as the method name and parameters are the same, changing the return type is sufficient for method overloading.
- Yes, method overloading can be achieved solely by changing the return type, even if the method name and parameters are the same.
Method overloading is based on the method name and parameters, not the return type. Therefore, simply changing the return type of methods with the same name and parameters does not constitute method overloading in Java. Different parameters are required to overload methods.
The ______ class in JavaFX is used to create a simple timeline animation.
- AnimationTimer
- KeyFrame
- Timeline
- TranslateTransition
In JavaFX, the Timeline class is used to create a simple timeline animation. It allows you to define keyframes and specify changes in properties over time, making it suitable for animations. The other options are also related to animations but serve different purposes.
Consider a scenario where you need to write a log file and ensure that each log entry is written to disk immediately for audit compliance. Which classes and/or methods would you use to implement this?
- FileWriter and BufferedWriter classes
- FileOutputStream class and flush() method
- PrintWriter class and sync() method
- RandomAccessFile class and write() method
In this scenario, to ensure that each log entry is written to disk immediately, you can use the PrintWriter class with the sync() method. This combination ensures that data is flushed and written to disk immediately. The other options do not provide the same level of immediate disk writing.
The ________ Interface extends Collection and declares the behavior of containers
- Iterable
- List
- Map
- Queue
The List interface extends the Collection interface in Java. It is used to represent ordered collections of elements, allowing duplicates and providing various methods to manipulate the list. The other options do not extend Collection.
In a situation where you are developing a caching solution that needs fast retrieval and insertion of key/value pairs but also needs to maintain insertion order for iteration, which Map implementation would be most suitable?
- HashMap
- Hashtable
- LinkedHashMap
- TreeMap
For a caching solution requiring fast retrieval, insertion, and maintaining insertion order, LinkedHashMap is the most suitable choice. It combines the features of a hash table and a linked list, allowing for constant-time retrieval and insertion while also preserving the order of insertion. HashMap offers fast retrieval but doesn't guarantee order. TreeMap orders elements but has a more complex structure. Hashtable is outdated and should be avoided.
What will happen if the overriding method is static in the superclass?
- It will lead to a compile-time error.
- The static method in the subclass will hide the static method in the superclass.
- The static method in the subclass will override the static method in the superclass.
- The static method in the superclass will hide the static method in the subclass.
When a method is declared as static in both the superclass and the subclass, it does not represent method overriding but method hiding. In such cases, the static method in the subclass will hide (not override) the static method in the superclass. The choice of which method to invoke depends on the reference type. If you call the method on the superclass reference, the superclass method is invoked; if you call it on the subclass reference, the subclass method is invoked.
The ________ statement can be used to prematurely exit a loop based on a particular condition.
- Break Statement
- Continue Statement
- Exit Statement
- Return Statement
In Java, the "break" statement is used to prematurely exit a loop based on a particular condition. It is commonly used in "for" and "while" loops to exit the loop when a specific condition is met. The other options (2 to 4) have different purposes and are not used for exiting loops.
The ______ method of the Future interface is used to check if the task is done or not.
- checkDone()
- hasCompleted()
- isDone()
- taskStatus()
In Java, the isDone() method of the Future interface is used to check if a task submitted to a ExecutorService is completed or not. It returns true if the task is done; otherwise, it returns false.