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
  1. int

  2. byte

  3. long

  4. double

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

However A, B and C are all wrong. Each of these would result in a narrowing conversion. Whereas we want a widening conversion, therefore the only correct answer is D. Don't be put off by the long cast, this applies only to the variable x and not the rest of the expression. It is the variable y (of type double) that forces the widening conversion todouble. Java's widening conversions are:

  • From a byte to a short, an int, a long, a float, or a double.
  • From a short, an int, a long, a float, or a double.
  • From a char to an int, a long, a float, or a double.
  • From an int to a long, a float, or a double.
  • From a long to a float, or a double.
  • From a float to a double.
Multiple choice
  1. String str = “Welcome to Java Programming”

  2. String str = new String( “Welcome to Java Programming” )

  3. String str; str = “Welcome to Java Programming”

  4. String str; str = new String (“Welcome to Java Programming” )

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

In Java, string literals are preferred and most commonly used to create string objects directly, utilizing string pooling efficiently. While the new keyword creates a new object in the heap explicitly, string literal assignment is the standard idiomatic practice.

Multiple choice
  1. After line 9

  2. After line 10

  3. After line 11

  4. Garbage collector never invoked in methodA()

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

Option D is correct. Garbage collection takes place after the method has returned its reference to the object. The method returns to line 6, there is no reference to store the return value. so garbage collection takes place after line 6. Option A is wrong. Because the reference to obj1 is stored in obj2[0]. The Object obj1still exists on the heap and can be accessed by an active thread through the reference stored in obj2[0]. Option B is wrong. Because it is only one of the references to the object obj1, the other reference is maintained in obj2[0]. Option C is wrong. The garbage collector will not be called here because a reference to the object is being maintained and returned in obj2[0].

Multiple choice
  1. An empty memory space is returned so that the developers can utilize it.

  2. void returns no data type.

  3. void is not supported in Java

  4. None of the above

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

The void keyword in Java (and C/C++) indicates that a method returns no value. It specifies the absence of a return type, meaning the method performs its action but doesn't produce any result that can be assigned to a variable.

Multiple choice
  1. main()

  2. destroy()

  3. init()

  4. repaint()

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

The init() method is the first lifecycle method called when an applet is loaded. It initializes the applet's state, sets up components, and prepares the applet for execution. The start() method comes after init() and is called to begin the applet's active execution.

Multiple choice
  1. init(), start() and destroy()

  2. init(), start() and paint()

  3. init(), start(), paint(). stop() and destroy()

  4. init(), start() and stop()

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

The applet lifecycle has four main methods: init() (called once when loaded), start() (called to begin execution), stop() (called when the applet should pause), and destroy() (called when the applet is being removed). The paint() method is for graphics, not a lifecycle method. Option D correctly lists the three core lifecycle methods.

Multiple choice
  1. StringReader

  2. PipeReader

  3. InputReader

  4. Both 1 and 2

  5. Both 1 and 3

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

There is no subclass of Reader class named InputReader. The correct name is InputStreamReader used for reading a character stream.So this is the correct answer according to the question.