Java_2 Flashcards
(10 cards)
Select the correct statements about rehashing in HashMap
rehashing happens when number of HashMap buckets change
rehashing happens when keys change
rehashing happens when values change
rehashing happens when load factor increases the threshold value
rehashing happens when inner binary search tree needs rebalance
In Java, a HashMap rehashes when its number of entries exceeds the product of its capacity and load factor = threshold
Capacity: The number of buckets (internal array size).
Load factor: A threshold ratio (default is 0.75).
Rehashing: The internal array is resized (usually doubled) and all entries are re-distributed (rehashed) into the new array.
so answer should be??/
rehashing happens when number of HashMap buckets change
Select the correct statements about exceptions
Java developers can create their own exceptions.
Exceptions can not have type parameters
Exceptions should be caught starting from the most specific to the most generic
All Exceptions are children of Error class.
All Exceptions are children of Throwable class
Only children of of Exception class ca be present in catch block?????
Java developers can create their own exceptions.
Exceptions should be caught starting from the most specific to the most generic
All Exceptions are children of Throwable class
Select the correct statements about JVM and JIT
JVM interpretes byte code
JVM interpretes machine code
JIT compiler can compile machine code into byte code
JIT can compile byte code into the machine code
JVM interpretes byte code
JIT can compile byte code into the machine code
Which of the following can be synchronised
Classes
Methods
Variables
Code blocks
Methods
Code blocks
Instance method Locks the instance (this)
Static method Locks the Class object
Synchronized block Locks the specified object
Code inside block Only one thread can execute at a time per lock
Select the correct statements about Stream API
reduce method can be used to limit Stream length to a certain number of elements
filter function takes a predicate and removes element from the Stream if predicate is evaluated as false
flatMap method takes a function which transforms element from one type to another
flatMap method takes a function which transforms element into Stream
filter method takes a function which transforms an element into boolean
✅ filter function takes a predicate and removes element from the Stream if predicate is evaluated as false
✅ flatMap method takes a function which transforms element into a Stream
✅ filter method takes a function which transforms an element into boolean
✖️ Incorrect: reduce is used to combine elements, not to limit their count.
✔️ Use limit(n) to restrict the number of elements in a stream.
✖️ Incorrect: That’s what map() does.
✔️ flatMap() expects a function that returns a Stream, not just a transformed value.
Select the correct statements about Java multithreading
Result of an asynchronous computation can be represented by java.util.concurent.Future class
if object does not have shared mutable state it is thread safe
We can create unlimited number of threads
All standard java collections are thread safe
✅ Result of an asynchronous computation can be represented by java.util.concurrent.Future class
✅ If an object does not have shared mutable state it is thread-safe
✖️ Incorrect: There is a limit based on system resources (memory, OS limits). Creating too many threads will eventually cause OutOfMemoryError or degrade performance.
✖️ Incorrect: Most standard collections in java.util like ArrayList, HashMap, LinkedList are not thread-safe.
✔️ Use collections from java.util.concurrent (e.g., ConcurrentHashMap) or use Collections.synchronizedList(…) to make them thread-safe.
Object[] nestedArray1 = {5,6,7};
Object[] nestedArray2 = {3,4, nestedArray1, 8};
Object[] arr = {1, 2, nestedArray2};
for (Object ob: arr) {
if (ob instanceof Integer)
System.out.println(ob + “ “);
else
System.out.println(ob.size() + “”);
} Object[] nestedArray1 = {5,6,7};
Object[] nestedArray2 = {3,4, nestedArray1, 8};
Object[] arr = {1, 2, nestedArray2};
for (Object ob: arr) {
if (ob instanceof Integer)
System.out.println(ob + “ “);
else
System.out.println(ob.size() + “”);
}
Compilation failed
ob is of type Object, and Object does not have a size() method.
What will be the output:
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
if (i == 3) {
continue;
}
System.out.println(numbers[i] + “ “);
}
1
2
3
5
Select correct statements about collections
Stack is FiFO data structure
Stack is LIFO data structure
HashMap keeps elements unordered
TreeMap keeps elements in an insertion order
TreeMap requires elements to implement Comparable interface or pass Comparator
Stack is LIFO data structure
✔️ True. Stack (in java.util) follows the Last In, First Out principle.
HashMap keeps elements unordered
✔️ True. HashMap does not guarantee any order of keys or values.
TreeMap requires elements to implement Comparable interface or pass Comparator
✔️ True. TreeMap sorts its keys, so the keys must either:Implement the Comparable interface, or be provided with a Comparator at construction.
Which statements will compile
List<Number> ints = new ArrayList<Integer>();
List<Integer> nums = new ArrayList<Number>();
List<object> nums1 = new ArrayList<Integer>();
List<? extends Number> nums2 = new ArrayList<Integer();
List<? super Number> nums3 = new ArrayList<object>();</object></Integer></object></Number></Integer></Integer></Number>
❌ List<Number> ints = new ArrayList<Integer>() ArrayList<Integer> is not a subtype of List<Number> — generics are invariant in Java.
Even though Integer is a subtype of Number, List<Integer> is not a subtype of List<Number>.
❌List<Integer> nums = new ArrayList<Number>();
❌List<object> nums1 = new ArrayList<Integer>();</Integer></object></Number></Integer></Number></Integer></Number></Integer></Integer></Number>
✅List<? extends Number> nums2 = new ArrayList<Integer>();
? extends Number means "a list of some unknown type that is a subtype of Number", and Integer is a subtype of Number.
You can read from nums2, but you cannot add new elements (except null), because the exact type is unknown.</Integer>
✅ List<? super Number> nums3 = new ArrayList<object>();
Object is a supertype of Number, so ArrayList<object> is valid here.You can add Number or any subclass of it to nums3.</object></object>
The abstract class Number is the superclass of platform classes representing numeric values that are convertible to the primitive types byte, double, float, int, long, and shor