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 databases
  1. Message

  2. StackTrace

  3. Source

  4. Line number

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

The StackTrace property provides a string representation of the frames on the call stack at the time of the current exception, including line numbers when debugging symbols are available. The Message property only contains the error description text. Source refers to the application name, and there is no built-in 'Line number' property.

Multiple choice technology databases
  1. The running application crashes`

  2. The exception is simply ignored and the object is garbage collected

  3. The exception is simply ignored, but the object is not garbage collected

  4. If a Runtime Exception is thrown in the finalize method

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

In Java, if an uncaught exception (such as a RuntimeException) is thrown during the execution of the finalize method, the exception is ignored, the finalization of the object terminates, and the object remains eligible for garbage collection. The application does not crash.

Multiple choice technology databases
  1. The method must return a type of either IEnumerator or Ienumerable

  2. The method must return a type of IComparable

  3. The method must explicitly contain a collection

  4. A and B

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

In C#, the foreach statement can iterate over any type that implements IEnumerable or returns an IEnumerator. The method does not need to return IComparable, nor does it need to explicitly contain a collection internally, as long as the returned type supports the iteration pattern.

Multiple choice technology databases
  1. By invoking the free method on the object

  2. By calling system.gc() method

  3. By setting all references to the object to new values (say null).

  4. Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object.

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

The correct answer is D. Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object. The JVM will automatically free the memory used by an object when it is no longer referenced by any other object.

Option A is incorrect because there is no free method in Java.

Option B is incorrect because calling system.gc() only requests the JVM to run the garbage collector, but it does not guarantee that the garbage collector will actually free the memory used by the object.

Option C is incorrect because setting all references to the object to new values (say null) will only prevent the JVM from garbage collecting the object, but it will not actually free the memory used by the object.

The memory used by an object will only be freed when the object is no longer referenced by any other object. This is the principle of garbage collection.

Multiple choice technology databases
  1. Writing the object to a Stream

  2. Reading the Object from a Stream

  3. Adding the Object to a collection

  4. All of the above

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

Serialization is the process of converting an object's state into a byte stream so that it can be written to memory, a database, or a file. Reading the object back from a stream to reconstruct its state is called deserialization, not serialization.

Multiple choice technology databases
  1. character streams

  2. byte streams

  3. Both (A) and (B)

  4. None of the Above

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

Java I/O uses two parallel hierarchies: byte streams (InputStream/OutputStream) for binary data and character streams (Reader/Writer) for text. Byte streams handle raw I/O (images, files, network). Character streams handle text with proper encoding (unicode, localization). Both types are fundamental to Java's I/O architecture.

Multiple choice technology databases
  1. Open

  2. BeginTransaction

  3. Execute

  4. CommitTransaction

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

A Connection Object in VB.NET provides methods for managing database connections: Open() to establish a connection, BeginTransaction() to start a transaction, and CommitTransaction() to commit changes. Execute() is not a Connection method - it belongs to Command Objects which are used to execute SQL commands once a connection is established.

Multiple choice technology programming languages
  1. Asynchronous

  2. Synchronous

  3. Synchronous and Asynchronous

  4. None of above

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

In general API design (such as Java Servlet sessions, JMS session methods, or database sessions), session methods are synchronous blocking operations. While asynchronous messaging exists, a session's execution context is synchronous by default unless explicitly specified.

Multiple choice technology testing
  1. Step Over

  2. Step Out

  3. Step Into

  4. Step Till

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

Step Over executes the current line and if it calls a method, runs the entire method without showing internal steps. Step Into would enter the method's code, Step Out returns from the current method to its caller, and Step Till is not a standard debugging command.

Multiple choice technology databases
  1. queue

  2. callback

  3. interval

  4. transport

Reveal answer Fill a bubble to check yourself
A,B,C Correct answer
Explanation

Creating a timer event requires a queue (to manage event dispatching), a callback function (to execute when the timer fires), and an interval (the time between timer events). Transport is not required for timers - they are local timing objects that don't involve network communication.

Multiple choice technology programming languages
  1. Set ThrowOnUnmappableChar = True

  2. Set ThrowOnmappableChar = True

  3. Set ThrowOnmappableChar = False

  4. Set ThrowOnUnmappableChar = False

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

Setting ThrowOnUnmappableChar to true causes marshaling operations to throw an exception when a Unicode character cannot be mapped to an ANSI character. Setting it to false silently replaces unmappable characters with a default '?' character.

Multiple choice technology programming languages
  1. The code does not compile

  2. The code compiles cleanly and shows “Object Version

  3. The code compiles cleanly and shows “String Version”

  4. The code throws an Exception at Runtime

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

When method overloading exists with null argument, Java chooses the most specific method. Since String extends Object, the String version is more specific than Object version. The compiler resolves null to the String overload at compile-time. The code compiles cleanly and prints 'String Version'. This is a classic Java overload resolution rule.

Multiple choice technology programming languages
  1. new

  2. break

  3. default

  4. goto

Reveal answer Fill a bubble to check yourself
A,B,C Correct answer
Explanation

Java has 50 reserved keywords including 'new' (instantiation), 'break' (loop exit), and 'default' (switch/annotation default). 'goto' is a reserved keyword but not used in Java - it's reserved for potential future use but has no functionality. Option D is incorrect because goto is indeed a reserved keyword in Java.

Multiple choice technology programming languages
  1. final

  2. finally

  3. finalize

  4. finalised

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

In Java, 'final' is a keyword for variables/methods/classes that cannot be changed/extended. 'finally' is a keyword used in try-catch blocks for code that always executes. 'finalize()' is a method name (deprecated) for garbage collection cleanup. However, 'finalised' is NOT part of the Java language - it's a common misspelling. The correct answer is D because it's the only option that is not a valid Java keyword or method.