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

  2. False

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

Java has robust built-in support for multithreaded programming through the Thread class, Runnable interface, and synchronized keyword. The language was designed from the ground up to support concurrent programming, making it one of the core strengths of the Java platform.

Multiple choice technology packaged enterprise solutions
  1. lazy=false;

  2. lazy=true;

  3. lazy=yes;

  4. lazy=no;

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

The default value of lazy loading in Hibernate is 'true'. This means associations are loaded on-demand when first accessed, not eagerly when the parent object is loaded. This lazy loading improves performance by avoiding unnecessary database queries for data that might never be used.

Multiple choice technology programming languages
  1. a) session.contains() method to determine if an instance belongs to the session cache.

  2. b) session.contains() method to determine if an instance belongs to the data base .

  3. c) Both are correct

  4. d) none of the above

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

session.contains() checks if an instance exists in the session cache (first-level cache), not whether it's in the database. An entity can be in session cache without being persisted, or exist in database without being in cache.

Multiple choice technology programming languages
  1. a) select() and poll() have no differences

  2. b) poll() is event driven while select() is not

  3. c) select() is event driven while poll() is not

  4. d) None

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

While both monitor multiple file descriptors, poll() is event-driven and scales better because it uses a structure array instead of bitmasks, whereas select() requires rebuilding the descriptor sets every time.

Multiple choice technology programming languages
  1. a) By mapping the property with access="onlyfield" in Hibernate metadata

  2. b) By mapping the property with access="variable" in Hibernate metadata

  3. c) By mapping the property with access="field" in Hibernate metadata

  4. d) none of the above

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

In Hibernate, mapping a property with access="field" tells Hibernate to bypass the setter method and access the instance variable directly using reflection. This is useful when you have non-trivial setter logic that shouldn't execute during object loading. The 'variable' and 'onlyfield' values are not valid access strategies.

Multiple choice technology programming languages
  1. a) On session close session.load() Emp Object unavailable to access

  2. b) On session close session.createCriteria() Emp unavailable to access

  3. c) None

  4. Both a & b

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

To understand the difference between session.load(Emp.class) and session.createCriteria(Emp.class), the user needs to know about Hibernate, an ORM (Object-Relational Mapping) framework that provides a way to map Java objects to database tables.

session.load(Emp.class) and session.createCriteria(Emp.class) are two different ways to retrieve data from the database using Hibernate.

session.load(Emp.class) is used to load an object from the database using its identifier. It returns a proxy object of the requested entity and the actual database query is executed only when the proxy object is accessed for the first time. If the identifier is not found in the database, then it throws the ObjectNotFoundException exception.

On the other hand, session.createCriteria(Emp.class) is used to create a Criteria object that can be used to retrieve data from the database. It provides a way to construct a query using a set of criteria such as restrictions, projections, and ordering. It returns a list of objects that match the criteria specified in the query.

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

A. a) On session close session.load() Emp Object unavailable to access: This option is partially correct. When the session is closed before accessing the Emp object loaded using session.load(), then the ObjectNotFoundException exception is thrown. However, the Emp object is still available to access until the session is closed.

B. b) On session close session.createCriteria() Emp unavailable to access: This option is incorrect. When the session is closed before accessing the Emp object retrieved using session.createCriteria(), then the LazyInitializationException exception is thrown. However, the Emp object is still available to access until the session is closed.

C. c) None: This option is incorrect. There is a difference between session.load() and session.createCriteria() as explained above.

D. Both a & b: This option is incorrect. Option A is partially correct and option B is incorrect.

Therefore, the correct answer is:

The Answer is: A

Multiple choice technology programming languages
  1. a) remove the object and its collections from the first level cache

  2. b) remove the object and its collections from the second level cache

  3. c) remove the object and its collections from the data base

  4. d) None of the above

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

In Hibernate, evict() removes an object and its associated collections from the first-level cache (session cache). This detaches the object from the session, making changes no longer tracked. It does NOT affect second-level cache or the database.

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
Multiple choice technology programming languages
  1. a) refresh();

  2. b) flush();

  3. c) fetch();

  4. d) load()

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

The refresh() method re-loads an object and all its collections from the database, overwriting the current state in the Session. This is particularly useful when database triggers initialize or modify properties after the initial save - refresh() retrieves those trigger-generated values. Option A correctly identifies refresh(). Option B (flush) synchronizes the Session state to the database (writes to DB, not reads from it). Options C and D are incorrect because there's no standard fetch() method, and load() retrieves only if already in Session or throws exception.

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.