Computer Knowledge
Java Core Classes and Threads
1,935 Questions
Java core classes and threads form the foundation of object oriented programming and are crucial for IT officer and programming exams. This includes concepts like string mutability, collections, and multithreading. Solve these questions to test your practical coding and theoretical knowledge.
String and StringBuffer classesThread execution methodsJava collections frameworkCharacter streams input outputVariable serialization rules
Java Core Classes and Threads Questions
-
copyInto(array : Object[])
-
removeFirst()
-
lastIndexOf(element :e)
-
addLast(object)
-
Set(int index, element e)
A
Correct answer
Explanation
This is the correct answer, as copyInto(array : Object[])Method is present in the Vector class and it copies the elements in this vector to an array.
-
containsKey(object)
-
containsValue(object)
-
Get(key : K)
-
Put(key : K, Value : V)
-
setValue(Value : V)
E
Correct answer
Explanation
This is the correct option because setValue(Value : V) method is not present in the map interface, this method is present in the entry interface. This method replaces the value in this entry with a new specified value.
-
retainAll(Collection<!--?--> c)
-
containsAll(Collection<!--?--> c)
-
toArray(T[] a)
-
firstEntry()
-
equals(Object o)
D
Correct answer
Explanation
This is the correct choice because firstEntry() method is not present in Collection interface. It is used for map interface and its classes. This method returns a key-value mapping associated with the least key in this map, or null if the map is empty. So this is the correct choice.
-
getKey()
-
getValue()
-
setValue(Value : V)
-
keyset()
-
getKeys()
D
Correct answer
Explanation
This is the correct answer, as Keyset() method is present in the map interface and it returns a set consisting of the keys in this map.
D
Correct answer
Explanation
This is the correct choice because there are 5 constructors of ConcurrentHashMap class. These are :- 1) ConcurrentHashMap():- Creates a new, empty map with a default initial capacity (16), load factor (0.75) and concurrencyLevel (16), 2) ConcurrentHashMap(int initialCapacity):- Creates a new, empty map with the specified initial capacity and with default load factor (0.75), and concurrencyLevel (16), 3) ConcurrentHashMap(int initialCapacity, float loadFactor):- Creates a new, empty map with the specified initial capacity and load factor and with the default concurrencyLevel (16), 4) ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel):- Creates a new, empty map with the specified initial capacity, load factor and concurrency level, 5) ConcurrentHashMap(Map m):- Creates a new map with the same mappings as the given map.
B
Correct answer
Explanation
This is the correct answer, as there are 5 constructors of LinkedHashMap class. These are:- LinkedHashMap():-
constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75), LinkedHashMap(int initialCapacity):- constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and a default load factor (0.75), LinkedHashMap(int initialCapacity, float loadFactor):- constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and load factor.
LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder):- constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode, and LinkedHashMap(Map<? extends K,? extends V> m):- constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map.
-
boolean containsKey(object)
-
containValue(object)
-
getKey(object)
-
KeysSet(object)
-
removeKey(object)
A
Correct answer
Explanation
This is the correct answer, as containsKey(object) method is present in the map interface in java collections framework. This method returns true if this map contains a mapping for the specified key.
-
putAll(map : m)
-
Get(key : K)
-
isEmpty()
-
removeLast()
-
Put(key : K, Value : V)
D
Correct answer
Explanation
This is incorrect, as removeLast()method is not present in the map interface, it is present in LinkedList class.This method removes the last element from the list.
-
illegal args
-
illegal argument
-
illegal arguments
-
illegal arg
B
Correct answer
Explanation
IllegalArgumentException is thrown when a method receives an argument that is inappropriate or invalid. The correct exception name uses the singular form 'argument' rather than plural, matching Java's naming convention for this standard exception.
-
join
-
stop
-
enumerate
-
yield
A
Correct answer
Explanation
The join() method causes the current thread to wait until the thread on which it is called completes execution. This is a fundamental thread synchronization mechanism in Java that ensures one thread waits for another to finish before proceeding.
-
Window Activated
-
window Closed
-
Window Opened
-
Window Closing
-
To String ()
-
Get Source ()
-
Get String ()
-
None of the above
B
Correct answer
Explanation
The skip() method in Java's java.io package takes exactly one argument - a long value specifying the number of bytes to skip. This method is used in input/output streams to skip over a specified number of bytes without reading them.
-
IO
-
File Not Found
-
Class Not Found
-
Interrupted
B
Correct answer
Explanation
FileInputStream throws FileNotFoundException (a subclass of IOException) when attempting to open a file that doesn't exist. This is a checked exception that must be handled or declared when working with file input streams.