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

Multiple choice
  1. copyInto(array : Object[])

  2. removeFirst()

  3. lastIndexOf(element :e)

  4. addLast(object)

  5. Set(int index, element e)

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. containsKey(object)

  2. containsValue(object)

  3. Get(key : K)

  4. Put(key : K, Value : V)

  5. setValue(Value : V)

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. retainAll(Collection<!--?--> c)

  2. containsAll(Collection<!--?--> c)

  3. toArray(T[] a)

  4. firstEntry()

  5. equals(Object o)

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. 8

  2. 7

  3. 6

  4. 5

  5. 4

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. 6

  2. 5

  3. 4

  4. 3

  5. 2

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. putAll(map : m)

  2. Get(key : K)

  3. isEmpty()

  4. removeLast()

  5. Put(key : K, Value : V)

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. illegal args

  2. illegal argument

  3. illegal arguments

  4. illegal arg

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. join

  2. stop

  3. enumerate

  4. yield

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. two

  2. one

  3. four

  4. three

Reveal answer Fill a bubble to check yourself
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.

Multiple choice
  1. IO

  2. File Not Found

  3. Class Not Found

  4. Interrupted

Reveal answer Fill a bubble to check yourself
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.