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 programming languages
  1. Faster performance

  2. Methods are synchronised

  3. Thread-safety

  4. Multithreading

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology web technology
  1. HttpServletRequest

  2. HttpServletResponse

  3. ServletConfig

  4. ServletContext

Reveal answer Fill a bubble to check yourself
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

Multiple choice technology programming languages
  1. extern

  2. volatile

  3. transient

  4. this

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. The member variable called length

  2. The method length() returns the number of characters.

  3. The member variable called size

  4. The method size() returns the number of characters.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. getParent()

  2. getAncestor()

  3. findAncestor()

  4. findParent()

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. System.gc();

  2. System.free();

  3. System.setGarbageCollection();

  4. System.out.gc();

Reveal answer Fill a bubble to check yourself
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).

Multiple choice technology programming languages
  1. An exception is thrown if you attempt to add an element with a duplicate value

  2. The add method returns false if you attempt to add an element with a duplicate value

  3. A set may contain elements that return duplicate values from a call to the equals method

  4. Duplicate values will cause an error at compile time

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. f1.getBar() ==null

  2. b2.getFoo == null

  3. b1.getFoo == null

  4. none of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology testing
  1. JProbe tracks calls to the methods in your application

  2. Encapsulates data when your application calls the specified native methods

  3. Triggers provide a way to tie JProbe actions to your program’s execution

  4. None

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology testing
  1. Heap View

  2. Instance View

  3. Leak Doctor View

  4. Allocation View

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology platforms and products
  1. Property-Map-Value calls a Rule-Obj-MapValue once, and Property-Map-ValuePair calls it twice

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

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

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

Reveal answer Fill a bubble to check yourself
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.