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 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.

Multiple choice technology programming languages
  1. assert

  2. interface

  3. extend

  4. transient

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

Of the options, 'extend' is not a Java keyword. 'assert' (A), 'interface' (B), and 'transient' (D) are all valid Java keywords. 'extend' might be confused with 'extends', which IS a keyword for inheritance, but 'extend' by itself is not a Java reserved word.

Multiple choice technology databases
  1. a) Trap it with a Handler

  2. b) Propagate it to the Calling Environment

  3. c) a & then b

  4. d) b & then a

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

In PL/SQL, you handle exceptions by either trapping them locally using an exception handler block, or by allowing them to propagate unhandled to the calling environment to be managed at a higher level.

Multiple choice technology testing
  1. GetCellData (Row,Col);

  2. GetRowValue (Rowid,Colname);

  3. GetData (Row,Col);

  4. GetCellValue (Row,Col);

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

In QTP (QuickTest Professional), the GetCellData(row, col) method is used to retrieve data from a specific cell in an HTML Table object. The method takes row and column indices as parameters and returns the cell's content.

Multiple choice technology testing
  1. GetCellData (Row,Col);

  2. GetRowValue (Rowid,Colname);

  3. GetData (Row,Col);

  4. GetCellValue (Row,Col);

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

In QTP (QuickTest Pro), the GetCellData(Row, Col) method retrieves data from an HTML table cell by specifying row and column indices. The other options (GetRowValue, GetData, GetCellValue) are not standard QTP methods for table data retrieval. Understanding the correct method is essential for web table testing and validation.

Multiple choice technology programming languages
  1. O (a) By invoking the method get (..., String type) on the ResultSet, where type is the database

  2. O (b) By invoking the method get (..., Type type) on the ResultSet, where Type is an object

  3. O (c) By invoking the method getValue (...), and cast the result to the desired java type.

  4. O (d) By invoking the special getter methods on the ResultSet: getString (...), get Boolean (...),

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

To retrieve information from a ResultSet, the user needs to know the methods that can be used to access the data. A ResultSet is a table of data that represents the results of a database query.

Now let's go through each option and explain why it is right or wrong:

A. O (a) By invoking the method get (..., String type) on the ResultSet, where type is the database

This option is incorrect because there is no method get (..., String type) in ResultSet. The method get() can be used to retrieve data from the ResultSet, but it requires the index or the name of the column. The String type parameter is not valid.

B. O (b) By invoking the method get (..., Type type) on the ResultSet, where Type is an object

This option is incorrect because there is no method get (..., Type type) in ResultSet. The method get() can be used to retrieve data from the ResultSet, but it requires the index or the name of the column. The Type type parameter is not valid.

C. O (c) By invoking the method getValue (...), and cast the result to the desired java type.

This option is incorrect because there is no method getValue() in ResultSet. The method get() can be used to retrieve data from the ResultSet, but it requires the index or the name of the column. Additionally, the method get() returns an object that needs to be cast to the desired data type.

D. O (d) By invoking the special getter methods on the ResultSet: getString (...), get Boolean (...),

This option is correct. ResultSet provides special getter methods such as getString(), getInt(), getBoolean(), etc. that can be used to retrieve data from the ResultSet. These methods take the index or the name of the column as a parameter and return the value of the column as the desired data type.

The Answer is: D