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
-
a) <session-factory>
-
b) <hibernate>
-
c) <hibernate-configuration>
-
d) None
C
Correct answer
Explanation
is the root element in hibernate.cfg.xml (Hibernate's main configuration file). Individual mapping files use , but is a child element, not the root.
-
itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );
-
((Item)itemMap.get(“orange”)).name = “Orange”;
-
itemMap.put("orange", new Item("Orange") );
-
None of above
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.
-
code attribute cannot be changed as Item class doesn’t have setter method for it.
-
for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }
-
int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }
-
int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }
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.
-
Item im = new Item(null); im = (Item ) itemList.get(0);
-
Item im = new Item(“Done”); im = (Item ) itemList.get(0);
-
Item im = null; im = (Item ) itemList.get(0);
-
All of the above.
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.
-
Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }
-
Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }
-
Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }
-
All of the above
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.
-
Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }
-
Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }
-
Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }
-
All of the above
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.
-
im.name = "Done";
-
Item otherObject = new Item(“Done”); itemList.set(49, otherObject);
-
((Item)itemList.get(49)).name = “Done”;
-
None of above is correct as they are not changing 50th element.
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.
-
Item im = new Item(null); im = (Item ) itemList.get(0);
-
Item im = new Item(“Done”); im = (Item ) itemList.get(0);
-
Item im = null; im = (Item ) itemList.get(0);
-
All of the above.
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.
-
itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );
-
((Item)itemMap.get(“orange”)).name = “Orange”;
-
itemMap.put("orange", new Item("Orange") );
-
None of above
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.
-
code attribute cannot be changed as Item class doesn’t have setter method for it.
-
for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }
-
int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }
-
int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }
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.
-
itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );
-
((Item)itemMap.get(“orange”)).name = “Orange”;
-
itemMap.put("orange", new Item("Orange") );
-
None of above
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.
-
code attribute cannot be changed as Item class doesn’t have setter method for it.
-
for (int i = 0; i < itemList.size(); i++) { if(itemList.get(i) != null) { ((Item)itemList.get(i)).code = 50+i; } }
-
int i = 0; Item im = null; while (i < itemList.size()) { im = (Item)itemList.get(i); if(im != null) { im.code = 50+i; } i++; }
-
int i = 0; Item im = null; Iterator itr = itemList.iterator(); while (itr.hasNext()) { im =(Item) itr.next(); im.code = 50+i; i++; }
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.
-
Function call:- changeName(im); Function implementation :- { im.name = "Done"; return null; }
-
Function call:- changeName(im); Function implementation :- { im = new Item("Done"); return null; }
-
Function call :- im = changeName(im); function implementation :- { Item otherObject = new Item("Done"); return otherObject; }
-
All of the above
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.
-
im.name = "Done";
-
Item otherObject = new Item(“Done”); itemList.set(49, otherObject);
-
((Item)itemList.get(49)).name = “Done”;
-
None of above is correct as they are not changing 50th element.
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.
-
Item im = new Item(null); im = (Item ) itemList.get(0);
-
Item im = new Item(“Done”); im = (Item ) itemList.get(0);
-
Item im = null; im = (Item ) itemList.get(0);
-
All of the above.
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.