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 java
  1. notify()

  2. wait()

  3. InputStream access

  4. sleep()

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

The notify() method is used to wake up a thread that is waiting on an object's monitor, but it does not cause the current thread to stop or terminate. In contrast, wait() blocks the thread, sleep() pauses it, and blocking I/O operations like InputStream access can also stop thread progress until data is available.

Multiple choice java
  1. Object

  2. Thread

  3. Runnable

  4. Class

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

The wait(), notify(), and notifyAll() methods are defined in the java.lang.Object class. Since these methods deal with the monitor/lock status of a specific object, they are made available to all objects through the root Object class rather than being specific to Thread or Runnable.

Multiple choice java
  1. Double precision floating-point (double)

  2. Character (char)

  3. Byte integer (byte)

  4. Boolean (boolean)

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

The char primitive type in Java is designed to store Unicode characters using 16-bit UTF-16 encoding. This allows Java to support international characters. Double is for floating-point numbers, byte for small integers, and boolean for true/false values.

Multiple choice java
  1. Short integer to character

  2. Integer to long integer

  3. Floating-point to double precision floating-point

  4. Character to floating-point

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

Widening conversions occur when a value is moved to a type that can accommodate its range. Short to char is not a widening conversion because short is signed (-32768 to 32767) and char is unsigned (0 to 65535); thus, negative short values cannot be represented in char.

Multiple choice java
  1. Iterator

  2. Map

  3. Enumeration

  4. Iterable

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

The enhanced for-loop (for-each) works with arrays and any object implementing the Iterable interface. The Iterable interface requires the implementation of an iterator() method. While Maps and Iterators are related, the expression itself must implement Iterable (or be an array).

Multiple choice java
  1. Compilation fails

  2. one two three four

  3. four three two one

  4. four one three two

  5. one two three four one

  6. one four three two one

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

TreeSet stores elements in a sorted, ascending order (natural ordering for Strings). It also rejects duplicates. The strings 'four', 'one', 'three', 'two' are added, and the second 'one' is ignored. The natural alphabetical order is 'four', 'one', 'three', 'two'.

Multiple choice java
  1. _jspService()

  2. jspInit()

  3. jspDestroy()

  4. getParameter()

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

In the JSP lifecycle, the _jspService() method is generated by the container and corresponds to the body of the JSP page. The JSP specification forbids the user from overriding this method, whereas jspInit() and jspDestroy() can be overridden.

Multiple choice java
  1. Float

  2. None of the above

  3. byte

  4. String

  5. integer

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

In Java, 'byte' is a primitive type. 'String' is a class (reference type). 'integer' is not a valid keyword (it's 'Integer' wrapper or 'int' primitive). 'Float' is a wrapper class (primitive is 'float'). Therefore, 'byte' is the only primitive type listed.

Multiple choice java
  1. Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type

  2. The size of a string can be retrieved using the length property.

  3. Strings are a primitive type in Java that overloads the + operator for concatenation

  4. The String class is implemented as a char array, elements are addressed using the stringname[] convention

Reveal answer Fill a bubble to check yourself
C Correct answer
Multiple choice java
  1. True

  2. False

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

The statement is True.

The "synchronized" keyword in Java is used to achieve thread synchronization. When a method or a block of code is declared as synchronized, it means that only one thread can access that method or block at a time.

When a thread encounters a synchronized method or block, it first checks if the lock associated with the object is available. If the lock is available, the thread grabs the lock and proceeds with executing the code inside the synchronized method or block. After the execution is complete, the lock is released, allowing other threads to access the synchronized code.

So, the "synchronized" keyword ensures that a thread grabs an object lock before continuing execution, making the statement True.

The answer is: A. True

Multiple choice java core java
  1. 1.java

  2. 2.javaw

  3. 3.javapath

  4. 4.javadoc

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

javapath is not a standard Java tool. java (executes programs), javaw (windowless java), and javadoc (generates documentation) are all valid JDK command-line tools. javapath does not exist in the standard Java toolkit.

Multiple choice java core java
  1. 1.add(Object x)

  2. 2.remove(Object x)

  3. 3.contains(Object x)

  4. 4.insert(int i,Object x)

  5. 5.set(int I,Object x)

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

ArrayList has add(), remove(), contains(), and set() methods, but does not have an insert() method - the equivalent functionality is provided by add(int index, E element). Option D correctly identifies the non-existent method.