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. If the start() method is invoked twice on the same Thread object, an exception is thrown at runtime.

  2. The order in which threads were started might differ from the order in which they actually run.

  3. If the run() method is directly invoked on a Thread object, an exception is thrown at runtime.

  4. If the sleep() method is invoked on a thread while executing synchronized code, the lock is not released.

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

To answer this question one needs to know about threads in programming. Threads are the smallest unit of execution that can be scheduled by an operating system. Each thread represents a separate flow of control. Threads are lightweight compared to processes, and they share memory with other threads in the same process.

Now let's go through each option and explain why it is true or false:

A. If the start() method is invoked twice on the same Thread object, an exception is thrown at runtime.

This statement is true. If the start() method is invoked twice on the same Thread object, an exception (IllegalThreadStateException) is thrown at runtime.

B. The order in which threads were started might differ from the order in which they actually run.

This statement is true. The operating system schedules threads based on its own algorithm, which might differ from the order in which the threads were started.

C. If the run() method is directly invoked on a Thread object, an exception is thrown at runtime.

This statement is false. If the run() method is directly invoked on a Thread object, the run() method will execute just like any other method. However, it will not start a new thread of execution, and any other code that is waiting for the thread to complete will not be notified.

D. If the sleep() method is invoked on a thread while executing synchronized code, the lock is not released.

This statement is true. When a thread invokes the sleep() method while executing synchronized code, the lock associated with the synchronized code is not released. This means that other threads that require the same lock will have to wait until the sleeping thread releases the lock.

Therefore, the correct answer is: C. If the run() method is directly invoked on a Thread object, an exception is thrown at runtime.

Multiple choice technology programming languages
  1. abstract

  2. assert

  3. synchronized

  4. strictfp

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

The assert keyword was introduced in Java 1.4 as a mechanism for implementing assertions - boolean expressions that test assumptions during development. The other keywords (abstract, synchronized, strictfp) were present in earlier Java versions. Assertions are typically disabled in production but can be enabled at runtime with the -ea or -enableassertions flag.

Multiple choice technology programming languages
  1. 0

  2. 1

  3. 2

  4. Compilation fails

  5. It is not possible to know

  6. An exception is thrown at runtime

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

At // doStuff, two CardBoard objects are eligible for garbage collection: c1 and the object originally referenced by c1 before go() was called. The go() method sets its local parameter cb to null, but this does not affect the actual c2 object. After c1 = null, the first CardBoard object becomes eligible. c2 remains referenced.

Multiple choice technology programming languages
  1. The invocation of an object's finalize() method is always the last thing that happens before an object is garbage collected (GCed).

  2. When a stack variable goes out of scope it is eligible for GC.

  3. Some reference variables live on the stack, and some live on the heap.

  4. Only objects that have no reference variables referring to them can be eligible for GC.

  5. It's possible to request the GC via methods in either java. lang. Runtime or java.lang.System classes.

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

Reference variables can exist on the stack as local variables or on the heap as instance variables. The garbage collector can be requested using System.gc() or Runtime.getRuntime().gc(). Distractors are incorrect because local variables themselves are not garbage collected, finalize is not guaranteed to run immediately before GC, and islands of isolated objects can be GCed.

Multiple choice technology programming languages
  1. Faster f = Faster.Higher;

  2. Faster f = Better.Faster.Higher;

  3. Better.Faster f = Better.Faster.Higher;

  4. Bigger.Faster f = Bigger.Faster.Higher;

  5. Better. Faster f2; f2 = Better.Faster.Longer;

  6. Better b; b.Faster = f3; f3 = Better.Faster.Longer;

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

Faster is an enum defined inside class Better. To reference it outside, you need the fully qualified name Better.Faster. Options C and E use correct syntax: Better.Faster is the type, and values are Better.Faster.Higher and Better.Faster.Longer. Options A, B, D are missing the outer class name.

Multiple choice technology programming languages
  1. b = (Number instanceof s);

  2. b = (s instanceof Short);

  3. b = s.instanceof(Short);

  4. b = (s instanceof Number);

  5. b = s.instanceof(Object);

  6. b = (s instanceof String);

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

Short is a wrapper class and inherits from Number. Options B and D are correct: s instanceof Short checks if s is a Short instance (true), and s instanceof Number checks if s is a Number instance (also true, since Short extends Number). Option A has reversed instanceof syntax. Options C, E use dot notation instead of instanceof keyword.

Multiple choice technology programming languages
  1. Compilation succeeds.

  2. Compilation fails due only to an error on line 3

  3. Compilation fails due only to an error on line 4

  4. Compilation fails due only to an error on line 5

  5. Compilation fails due to errors on lines 3 and 5

  6. Compilation fails due to errors on lines 3, 4, and 5

Reveal answer Fill a bubble to check yourself
A Correct answer
Multiple choice technology programming languages
  1. After line 8 runs

  2. After line 9 runs.

  3. After line 10 runs.

  4. After line 11 runs.

  5. An exception is thrown at runtime.

  6. Never in this program

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

Line 10 attempts e2.e = el, but e2 was set to null on line 8. Accessing e2.e throws NullPointerException. Note: el is a typo (should be e1), but the null dereference happens first.

Multiple choice technology programming languages
  1. Compilation fails.

  2. An error occurs at runtime.

  3. foobarhi

  4. barhi

  5. hi

  6. foohi

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

new Foo() prints 'foo'. makeBar() instantiates an anonymous subclass of inner class Bar using new Bar() {}, which executes Bar's constructor and prints 'bar'. It then calls go() which prints 'hi', resulting in 'foobarhi'.

Multiple choice technology programming languages
  1. Map m = Collection.synchronizeMap(hashMap);

  2. Map m = Collections.synchronizeMap(hashMap);

  3. Map m = hashMap.synchronizeMap();

  4. none

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

HashMap is not thread-safe, but can be wrapped using Collections.synchronizedMap() to obtain a synchronized wrapper. Option B correctly shows the syntax: Map m = Collections.synchronizeMap(hashMap); - note the class is Collections (plural), not Collection. Option A is incorrect because Collection.synchronizeMap doesn't exist.

Multiple choice technology web 2.0
  1. The statement(s) it executes run(s) only once.

  2. It pauses the script in which it is called.

  3. clearTimeOut() won't stop its execution.

  4. The delay is measured in hundredths of a second.

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

To understand setTimeOut(), the user needs to know how JavaScript functions and the concept of asynchronous programming.

setTimeOut() is a built-in function in JavaScript that allows you to execute a block of code after a specified delay.

Now, let's go through each option and explain why it is right or wrong:

A. The statement(s) it executes run(s) only once. - This option is correct. The block of code passed as the first parameter to setTimeOut() is executed only once after the specified delay. If you want to execute it repeatedly, you can use setInterval().

B. It pauses the script in which it is called. - This option is incorrect. setTimeOut() is an asynchronous function, which means that it does not pause the script. Instead, it schedules the block of code to run after the specified delay and continues executing the rest of the code.

C. clearTimeOut() won't stop its execution. - This option is incorrect. If you want to stop the execution of setTimeOut() before it runs the scheduled block of code, you can use clearTimeout(). This function takes the ID returned by setTimeOut() as a parameter and cancels the execution.

D. The delay is measured in hundredths of a second. - This option is incorrect. The delay is measured in milliseconds, not hundredths of a second. For example, if you want to delay the execution of a block of code for 1 second, you can pass 1000 (1000 milliseconds = 1 second) as the delay parameter to setTimeOut().

Therefore, the correct answer is:

The Answer is: A

Multiple choice technology architecture
  1. Looper pattern

  2. Visitor pattern

  3. DataAccess pattern

  4. Iterator pattern

  5. None of the above

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

The Iterator pattern provides a way to access elements of a collection sequentially without exposing its underlying representation. It defines a uniform interface for traversal, whether the collection is an array, list, tree, or any other structure. Looper isn't a standard pattern, Visitor adds operations without changing classes, and DataAccess isn't a GoF pattern.

Multiple choice technology programming languages
  1. A method without code

  2. A method which is called when an event is triggered.

  3. A method which doesnot contain code.

  4. A reference to a method which itself doesnot contain code.

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

A delegate is a reference to a method - it's a type-safe function pointer that holds a reference to a method with a matching signature. The delegate itself doesn't contain implementation code, it merely references a method that does. Option D correctly describes a delegate as a reference to a method, which itself contains the actual code. Options A, B, and C are incorrect descriptions of what delegates represent.

Multiple choice technology programming languages
  1. String start = new String("Welcome to Java Programming");

  2. String start[] = "Welcome to Java Programming";

  3. String start = "Welcome to Java Programming";

  4. none

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

String literals in Java are automatically interned and shared from the string pool. Using new String() creates a separate object. The array syntax in B is incorrect for a single String.