1. Which of the following keywords is used to create a thread in Java?
A) new
B) Thread
C) Runnable
D) extend
Answer: B) Thread
2. What is the default value of an object reference in Java?
A) 0
B) null
C) false
D) "" (empty string)
Answer: B) null
3. Which of the following is used to force garbage collection in Java?
A) System.gc()
B) gc.collect()
C) Runtime.collect()
D) System.cleanup()
Answer: A) System.gc()
4. Which of the following interfaces is used by Java to support the collection
of objects?
A) List
B) Set
C) Collection
D) Queue
Answer: C) Collection
5. Which of the following is the correct way to handle checked exceptions in
Java?
A) Use a try-catch block or declare the exception using the throws keyword.
B) Always use a try-catch block to handle checked exceptions.
C) Throw the exception explicitly and do not handle it.
D) You cannot handle checked exceptions in Java.
Answer: A) Use a try-catch block or declare the exception using the throws
keyword.
6. Which of the following methods can be used to check the length of a string
in Java?
A) length()
B) size()
C) count()
D) getSize()
Answer: A) length()
7. Which of the following is true about the StringBuilder class in Java?
A) It is immutable, meaning its contents cannot be modified once created.
B) It is mutable and used to create strings that can be modified without
creating new objects.
C) It can only be used for string concatenation in multithreaded applications.
D) It is not thread-safe and should not be used in single-threaded
applications.
Answer: B) It is mutable and used to create strings that can be modified
without creating new objects.
8. Which of the following collections does not allow null elements in Java?
A) HashSet
B) TreeSet
C) ArrayList
D) LinkedList
Answer: B) TreeSet
9. What is the purpose of the transient keyword in Java?
A) It makes a variable accessible across different threads.
B) It indicates that a variable should not be serialized.
C) It indicates that a variable should be included in the final output.
D) It makes a variable constant during execution.
Answer: B) It indicates that a variable should not be serialized.
10. Which of the following is the superclass of all exceptions in Java?
A) Throwable
B) Error
C) Exception
D) RuntimeException
Answer: A) Throwable