Computer Knowledge

Java Core Classes and Threads

1,473 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 technology programming languages
  1. itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );

  2. ((Item)itemMap.get(“orange”)).name = “Orange”;

  3. itemMap.put("orange", new Item("Orange") );

  4. None of above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Option B directly mutates the Item object inside the HashMap by accessing it via get() and setting its name field. This changes the value object's content without replacing the reference. Option C creates a new Item but doesn't prevent other references to the old one.

Multiple choice technology programming languages
  1. code attribute cannot be changed as Item class doesn’t have setter method for it.

  2. for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }

  3. int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }

  4. int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Option D uses an Iterator to traverse the list, which avoids repeated get(i) calls and index recalculation. For LinkedList, get(i) is O(n), making B and C O(n²). Iterator provides consistent O(1) traversal regardless of List implementation. Options A, B, C all work but D is most efficient.

Multiple choice technology programming languages
  1. Item im = new Item(null); im = (Item ) itemList.get(0);

  2. Item im = new Item(“Done”); im = (Item ) itemList.get(0);

  3. Item im = null; im = (Item ) itemList.get(0);

  4. All of the above.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Declaring a reference variable as null and then assigning it to an existing object from a list avoids allocating unnecessary memory for a new object that would immediately be discarded and garbage collected.

Multiple choice technology programming languages
  1. Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }

  2. Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }

  3. Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }

  4. All of the above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In option B, the function reassigns the local parameter im to a new Item, but Java passes parameters by value for object references. The reassignment doesn't affect the caller's reference - the original Item object remains unchanged. Options A and C both mutate or return the actual object.

Multiple choice technology programming languages
  1. Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }

  2. Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }

  3. Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }

  4. All of the above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Java is pass-by-value. Reassigning the local parameter im to a new object inside the method does not affect the original reference in the calling function, meaning the original object's name remains unchanged.

Multiple choice technology programming languages
  1. im.name = "Done";

  2. Item otherObject = new Item(“Done”); itemList.set(49, otherObject);

  3. ((Item)itemList.get(49)).name = “Done”;

  4. None of above is correct as they are not changing 50th element.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Option A is the simplest approach if 'im' is already a reference to the 50th element. The question asks for the simplest code snippet to add, and directly setting the name attribute with 'im.name = "Done";' is the most concise. Option C is more explicit but verbose - it retrieves the element and sets the name in one statement, which is self-contained but not the 'simplest' to add if im already exists.

Multiple choice technology programming languages
  1. Item im = new Item(null); im = (Item ) itemList.get(0);

  2. Item im = new Item(“Done”); im = (Item ) itemList.get(0);

  3. Item im = null; im = (Item ) itemList.get(0);

  4. All of the above.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Option C is correct because it avoids unnecessary object creation. Options A and B both create new Item objects ('new Item(null)' and 'new Item("Done")') before immediately overwriting the reference with the retrieved element from itemList. This wastes memory as the newly created objects become unreachable and garbage collectible. Option C simply declares a null reference and then assigns the retrieved object - no wasted allocation.

Multiple choice technology programming languages
  1. itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );

  2. ((Item)itemMap.get(“orange”)).name = “Orange”;

  3. itemMap.put("orange", new Item("Orange") );

  4. None of above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

To modify the value associated with a key in a HashMap, retrieving the object reference and modifying its field directly is the most efficient approach, avoiding the overhead of removing and re-inserting keys.

Multiple choice technology programming languages
  1. code attribute cannot be changed as Item class doesn’t have setter method for it.

  2. for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }

  3. int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }

  4. int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Option D using Iterator is the most performance efficient because it avoids repeated index calculations and bounds checking that occurs with indexed access in a loop. Iterator is optimized for sequential traversal and minimizes overhead. Option B uses indexed access with itemList.get(i) in each iteration, which recalculates position. Option C is similar to B but with while loop - still uses indexed get(). Iterator approach is generally faster for ArrayList traversal.

Multiple choice technology programming languages
  1. itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );

  2. ((Item)itemMap.get(“orange”)).name = “Orange”;

  3. itemMap.put("orange", new Item("Orange") );

  4. None of above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Option B is simplest and correct because it retrieves the existing Item object for key "orange" and modifies its name attribute in-place. This is more efficient than removing and re-adding the entry. Option A unnecessarily removes the key-value pair and creates a new Item object, which is redundant when you can just modify the existing object's attribute. Option C would replace the entire value object with a new one rather than modifying the existing one.

Multiple choice technology programming languages
  1. code attribute cannot be changed as Item class doesn’t have setter method for it.

  2. for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }

  3. int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }

  4. int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Option D using Iterator is the most performance efficient because it avoids repeated index calculations and bounds checking that occurs with indexed access in a loop. Iterator is optimized for sequential traversal and minimizes overhead. Option B uses indexed access with itemList.get(i) in each iteration, which recalculates position. Option C is similar to B but with while loop - still uses indexed get(). Iterator approach is generally faster for ArrayList traversal.

Multiple choice technology programming languages
  1. Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }

  2. Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }

  3. Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }

  4. All of the above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In Java, arguments are passed by value. Reassigning the reference variable im = new Item("Done") inside the method only changes the local parameter copy, leaving the original object in the calling method unchanged.

Multiple choice technology programming languages
  1. im.name = "Done";

  2. Item otherObject = new Item(“Done”); itemList.set(49, otherObject);

  3. ((Item)itemList.get(49)).name = “Done”;

  4. None of above is correct as they are not changing 50th element.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The simplest way to change an attribute of an object inside a list is to retrieve it and directly modify its field. im.name = "Done"; directly updates the reference if im points to the 50th element.

Multiple choice technology programming languages
  1. Item im = new Item(null); im = (Item ) itemList.get(0);

  2. Item im = new Item(“Done”); im = (Item ) itemList.get(0);

  3. Item im = null; im = (Item ) itemList.get(0);

  4. All of the above.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Option C is correct because it avoids unnecessary object creation. Options A and B both create new Item objects ('new Item(null)' and 'new Item("Done")') before immediately overwriting the reference with the retrieved element from itemList. This wastes memory as the newly created objects become unreachable and garbage collectible. Option C simply declares a null reference and then assigns the retrieved object - no wasted allocation.