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
-
String
-
StringBuffer
-
StringBuilder
-
none
B
Correct answer
Explanation
StringBuffer is a mutable string class with synchronized methods, making it thread-safe. StringBuilder is also mutable but not thread-safe. String itself is immutable.
-
Faster performance
-
Methods are synchronised
-
Thread-safety
-
Multithreading
A
Correct answer
Explanation
StringBuilder is not thread-safe - its methods are NOT synchronized, which eliminates synchronization overhead. StringBuffer is thread-safe with synchronized methods, making it slower. The main advantage of StringBuilder is faster performance in single-threaded environments.
-
HttpServletRequest
-
HttpServletResponse
-
ServletConfig
-
ServletContext
B
Correct answer
Explanation
To solve this question, the user needs to know that the getWriter() method returns an object of type PrintWriter, which can be used to generate output. The user must also know which class defines this method.
The getWriter() method is used to return an object of type PrintWriter, which can be used to generate output. This method is defined in the HttpServletResponse class.
Therefore, the correct answer is:
The Answer is: B. HttpServletResponse
-
extern
-
volatile
-
transient
-
this
B,C,D
Correct answer
Explanation
Java keywords are reserved words with special meaning. 'volatile' is used for variables in multithreading, 'transient' marks fields not to be serialized, and 'this' refers to the current instance. 'extern' is a C/C++ keyword, not used in Java.
-
The member variable called length
-
The method length() returns the number of characters.
-
The member variable called size
-
The method size() returns the number of characters.
B
Correct answer
Explanation
In Java, String objects use the length() method to return the character count, not a member variable. This is because String is a class - not a primitive or struct - so it encapsulates its data and exposes methods like length() rather than public fields. Option D's size() method doesn't exist for String.
A
Correct answer
Explanation
Vector is synchronized, making it thread-safe for multi-threaded environments, whereas ArrayList is not synchronized and therefore not thread-safe. This is a fundamental difference in Java collections, where ArrayList offers better performance when synchronization is not required.
A
Correct answer
Explanation
Java's garbage collector automatically reclaims memory from objects that are no longer reachable. An object becomes eligible for garbage collection when there are no live references to it in the running program. This is a fundamental memory management feature in Java.
-
getParent()
-
getAncestor()
-
findAncestor()
-
findParent()
A
Correct answer
Explanation
In JSP custom tag handlers, getParent() returns the closest enclosing tag handler. This is part of the Tag and BodyTag interfaces. getAncestor(), findAncestor(), and findParent() are not standard methods in the JSP tag handler API. The getParent() method allows a nested tag to communicate with its parent tag, which is essential for cooperative tag handler patterns.
-
System.gc();
-
System.free();
-
System.setGarbageCollection();
-
System.out.gc();
A
Correct answer
Explanation
System.gc() suggests to the JVM that garbage collection should run. Note that this is only a suggestion - the JVM may ignore it. System.free(), System.setGarbageCollection(), and System.out.gc() are not valid Java methods. The gc() method is in the System class, not in PrintStream (which System.out is).
-
An exception is thrown if you attempt to add an element with a duplicate value
-
The add method returns false if you attempt to add an element with a duplicate value
-
A set may contain elements that return duplicate values from a call to the equals method
-
Duplicate values will cause an error at compile time
B
Correct answer
Explanation
When adding a duplicate element to a Set, the add() method returns false but does not throw an exception. Sets are designed to contain only unique elements - duplicates are simply ignored. The add() method's return value indicates whether the element was actually added (true if it was a new element, false if it was a duplicate). No compile-time or runtime error occurs.
-
f1.getBar() ==null
-
b2.getFoo == null
-
b1.getFoo == null
-
none of the above
A,B
Correct answer
Explanation
In a unidirectional relationship Foo→Bar, only Foo knows about Bar (Foo has a getBar() method, Bar has NO getFoo() method). After f2.setBar(f1.getBar()), f2 now points to b1. Since the relationship is unidirectional from Foo to Bar, b1 has NO reference to any Foo. b1.getFoo() doesn't exist (not that it's null - the method doesn't exist). For the unidirectional case, b2.getFoo() also doesn't exist - but the question implies b2 now has no incoming reference from f2.
-
JProbe tracks calls to the methods in your application
-
Encapsulates data when your application calls the specified native methods
-
Triggers provide a way to tie JProbe actions to your program’s execution
-
None
C
Correct answer
Explanation
Triggers in JProbe allow developers to control profiling actions dynamically by tying them to specific events in the program's execution, such as entering or exiting a method. The distractors describe general profiling behavior or native method encapsulation rather than the specific automation purpose of triggers.
-
Heap View
-
Instance View
-
Leak Doctor View
-
Allocation View
C
Correct answer
Explanation
Leak Doctor View is specifically designed in JProbe to help identify memory leaks by tracking object allocations, garbage collection cycles, and objects that persist longer than expected. It analyzes reference chains and helps pinpoint why objects are not being garbage collected. Heap View shows overall memory distribution, Instance View lists individual objects, and Allocation View tracks where objects were created - none specifically target leak detection like Leak Doctor.
A
Correct answer
Explanation
Overriding methods in Java can throw any unchecked (runtime) exceptions regardless of what the overridden method declares. This includes RuntimeException, Error, and their subclasses. Checked exceptions are more restricted - overriding methods cannot throw broader checked exceptions than the overridden method.
-
Property-Map-Value calls a Rule-Obj-MapValue once, and Property-Map-ValuePair calls it twice
-
Property-Map-Value can call a Rule-Obj-MapValue and pass a single row input, and Property-Map-Value-Pair can call a Rule-Obj-Map Value and pass two row inputs
-
Property-Map-Value can call a Rule-Obj-MapValue and pass a single row input, and Property-Map-Value-Pair can call a Rule-Obj-Map Value and pass a single row input and single column input
-
Property-Map-Value can call a Rule-Obj-MapValue and pass a single row input, and Property-Map-Value-Pair can call a Rule-Obj-Map Value and pass multiple row inputs
C
Correct answer
Explanation
Property-Map-Value passes a single row input to a Rule-Obj-MapValue. Property-Map-ValuePair passes a single row input AND a single column input to the Rule-Obj-MapValue, allowing lookup based on two dimensions. This is the key difference between the two methods.