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
-
1 and 2
-
2 and 3
-
3 and 4
-
2 and 4
B
Correct answer
Explanation
(2) - This is a Java keyword
(3) - This is a Java keyword
(1) - Is incorrect because although it is a method of Thread/Runnable it is not a keyword
(4) - This is not a Java keyword the keyword is implements
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.
-
Thread t = new Thread(X);
-
Thread t = new Thread(X); t.start();
-
X run = new X(); Thread t = new Thread(run); t.start();
-
Thread t = new Thread(); x.run();
C
Correct answer
Explanation
Option C is suitable to start a thread.
-
String str = “Welcome to Java Programming”
-
String str = new String( “Welcome to Java Programming” )
-
String str; str = “Welcome to Java Programming”
-
String str; str = new String (“Welcome to Java Programming” )
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.
-
After line 9
-
After line 10
-
After line 11
-
Garbage collector never invoked in methodA()
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].
-
An empty memory space is returned so that the developers can utilize it.
-
void returns no data type.
-
void is not supported in Java
-
None of the above
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.
-
main()
-
destroy()
-
init()
-
repaint()
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.
-
init(), start() and destroy()
-
init(), start() and paint()
-
init(), start(), paint(). stop() and destroy()
-
init(), start() and stop()
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.
-
read(byte[ ] b)
-
mark(int limit)
-
read()
-
both 1 and 3
-
all of these
E
Correct answer
Explanation
As explained, all of the methods are defined in the Input Stream class.So this is the correct choice.
-
flush()
-
write(byte[ ] b)
-
write(byte[ ] b, int offset, int len)
-
both 1 and 3
-
all of these
E
Correct answer
Explanation
This is correct answer because as I explained all of the given methods are defined in the OutputStream class.
-
skip(long)
-
mark(int limit)
-
reset()
-
both 1 and 3
-
all of these
E
Correct answer
Explanation
As I explained all of the methods are defined in the Reader class.So this is the correct choice.
-
CharWriter
-
StringWriter
-
BufferedWriter
-
Both 1 and 2
-
Both 1 and 3
A
Correct answer
Explanation
This is the correct answer. There is no Subclass named charWriter. The correct name is CharArrayWriter which is used for writing to a char array.So this is the correct answer according to the question.
-
StringReader
-
PipeReader
-
InputReader
-
Both 1 and 2
-
Both 1 and 3
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.
-
getPath()
-
getName()
-
length()
-
both 1 and 2
-
all of these
E
Correct answer
Explanation
As I explained that all of the given methods are defined in the File class.So this is the correct answer.
-
StringBufferedInputStream
-
FilterInputStream
-
SequenceInputStream
-
Both 2 and 3
-
All of these
D
Correct answer
Explanation
As I explained both the classes in option 2 & 3 are the only subclasses of InputStream class.So this is the correct answer.