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 web technology
  1. Due to its nature as a reference type, StringBuilder has a smaller footprint than string,thereby allowing more efficent memory management.

  2. StringBuilder is inherently more efficent due to its nature as a reference type comparedto string, which is a value type

  3. StringBuilder allows for dynamic appending of types; string creates a new string every time it is appended to.

  4. StringBuilder has the ability to accept objects other than string in order to build a string by automatically calling the ToString method on the object.

  5. Regular expressions are built into StringBuilder, allowing for much more efficent text processing than what could usually be achieved by using a string.

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

StringBuilder is mutable and efficient for multiple string modifications. Each string append creates a new string object in memory (strings are immutable), while StringBuilder modifies the same buffer internally, reducing memory allocations and garbage collection pressure. Options A and B incorrectly claim string is a value type (it's a reference type).

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

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

  3. Both are correct

  4. None of the above

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

Session.contains() checks if a specific entity instance is currently managed within the persistence context (session cache). It does not query the database to determine if the record exists there. Therefore, the first statement is correct, while the second is incorrect because it misinterprets the method's scope.

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

  2. Remove the object and its collections from the second level cache

  3. Remove the object and its collections from the data base

  4. None of the above

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

The session.evict() method removes an object and its associated collections from the first-level cache (session cache). This detaches the object from the session, meaning subsequent changes won't be tracked. It does not affect the second-level cache or delete from the database.

Multiple choice technology programming languages
  1. ListResultSet

  2. ResultSet

  3. ScrollableResultSet

  4. ScrollableResult

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

The scroll() method on Hibernate's Query interface returns an instance of ScrollableResults, which acts as a cursor to scroll through the query results. The option ScrollableResult contains a typographical error (missing the trailing 's'), but represents the intended interface type, while the other choices are incorrect.

Multiple choice technology programming languages
  1. refresh();

  2. flush();

  3. fetch();

  4. load()

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

The refresh() method re-loads the object and all its collections from the database, overwriting any changes made to the persistent object. This is particularly useful when database triggers modify properties after the initial load. Unlike flush() which writes changes to the database, refresh() synchronizes the in-memory object with the database state.

Multiple choice technology web technology
  1. str.Substring(str.Length);

  2. str.Substring(str.Length - 1);

  3. str.LastChar();

  4. str.RightChar();

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

To get the rightmost character of a string, you use Substring(str.Length - 1, 1) or Substring(str.Length - 1). This starts from the last character position. Option A with str.Length would cause an ArgumentOutOfRangeException, and options C and D are not valid string methods.

Multiple choice technology web technology
  1. DateTime.Now.PreviousMonth;

  2. DateTime.Now.CurrentMonth - 1;

  3. DateTime.Now.AddMonths(-1);

  4. DateTime.Now.AddMonths(1);

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

The AddMonths method is the correct way to perform date arithmetic in C#. Using a negative parameter (-1) subtracts months from the current date. DateTime.Now.AddMonths(1) would give next month, and there are no PreviousMonth or CurrentMonth properties on DateTime.

Multiple choice technology programming languages
  1. java.io

  2. java.util

  3. javax.xml.stream

  4. java.io.*

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

The RandomAccess interface is part of the java.util package in Java Collections Framework. It's a marker interface used by List implementations to indicate that they support fast (generally constant time) random access to elements, like ArrayList.

Multiple choice technology programming languages
  1. request

  2. ServletConfig

  3. ServletContext

  4. application

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

The init method in a servlet receives a ServletConfig object as its parameter. This object contains servlet-specific initialization parameters and configuration information. ServletContext represents the entire web application context, not individual servlet configuration.

Multiple choice technology
  1. String str =”cocoa”;

  2. NSString *str =@”cocoa”;

  3. NSString str= “cocoa”;

  4. NSString* str=@”cocoa”;

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

In Objective-C, strings are declared using NSString class with the @ symbol before the string literal to indicate it's an Objective-C string object (not a C string). The correct syntax is NSString *str = @"cocoa"; where the asterisk denotes a pointer to the object, and the @ symbol creates an NSString object from the literal.

Multiple choice technology
  1. -(void) mouseUp: (NSEvent *) theEvent;

  2. -(void) mouseDown: (NSEvent *) theEvent;

  3. -(void) mouseDragged: (NSEvent *) theEvent;

  4. -(void) mouseClick: (NSEvent *) theEvent;

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

The standard mouse event methods in Cocoa are mouseUp, mouseDown, and mouseDragged. There is no 'mouseClick' method in the standard mouse event protocol - click events are typically derived from up/down combinations.

Multiple choice technology
    • (void)webViewDidStartLoad:(UIWebView *)webView;
    • (void)webViewDidFinishLoad:(UIWebView *)webView;
    • (void)webView:(UIWebView *)webViewdidFailLoadWithError:(NSError *)error;
  1. None

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

The UIWebViewDelegate protocol's webViewDidStartLoad: is the first method called when a web view begins loading content. This is followed by either webViewDidFinishLoad: or webView:didFailLoadWithError: depending on the outcome. Option D (None) is incorrect since the delegate protocol defines specific lifecycle methods.