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
-
Due to its nature as a reference type, StringBuilder has a smaller footprint than string,thereby allowing more efficent memory management.
-
StringBuilder is inherently more efficent due to its nature as a reference type comparedto string, which is a value type
-
StringBuilder allows for dynamic appending of types; string creates a new string every time it is appended to.
-
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.
-
Regular expressions are built into StringBuilder, allowing for much more efficent text processing than what could usually be achieved by using a string.
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).
-
Session.contains() method to determine if an instance belongs to the session cache
-
Session.contains() method to determine if an instance belongs to the data base
-
Both are correct
-
None of the above
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.
-
Remove the object and its collections from the first level cache
-
Remove the object and its collections from the second level cache
-
Remove the object and its collections from the data base
-
None of the above
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.
-
ListResultSet
-
ResultSet
-
ScrollableResultSet
-
ScrollableResult
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.
-
refresh();
-
flush();
-
fetch();
-
load()
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.
-
str.Substring(str.Length);
-
str.Substring(str.Length - 1);
-
str.LastChar();
-
str.RightChar();
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.
-
DateTime.Now.PreviousMonth;
-
DateTime.Now.CurrentMonth - 1;
-
DateTime.Now.AddMonths(-1);
-
DateTime.Now.AddMonths(1);
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.
-
java.io
-
java.util
-
javax.xml.stream
-
java.io.*
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.
-
request
-
ServletConfig
-
ServletContext
-
application
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.
-
Bit-oriented
-
Byte-oriented
-
Character Oriented
-
1+2
-
2+3
-
All of these
-
String str =”cocoa”;
-
NSString *str =@”cocoa”;
-
NSString str= “cocoa”;
-
NSString* str=@”cocoa”;
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.
-
-(void) mouseUp: (NSEvent *) theEvent;
-
-(void) mouseDown: (NSEvent *) theEvent;
-
-(void) mouseDragged: (NSEvent *) theEvent;
-
-(void) mouseClick: (NSEvent *) theEvent;
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.
D
Correct answer
Explanation
In Objective-C naming conventions, methods containing 'copy', 'alloc', or 'new' return objects with a retain count of 1 (the caller owns them). Methods with 'init' (like init, initWithSomething:) operate on already-allocated instances and don't return retained objects.
-
- (void)webViewDidStartLoad:(UIWebView *)webView;
-
- (void)webViewDidFinishLoad:(UIWebView *)webView;
-
- (void)webView:(UIWebView *)webViewdidFailLoadWithError:(NSError *)error;
-
None
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.
-
true
-
false
-
Depends on situation
-
none of the above