0% found this document useful (0 votes)
9 views4 pages

Java Threading and Exception Handling Quiz

The document contains a series of multiple-choice questions and answers related to Java programming concepts, including thread management, constructors, string immutability, file reading, exception handling, immutable classes, boolean defaults, constant declaration, array indexing, and collection classes. Key answers include that threads are started with 'start()', constructors are called upon object creation, and 'LinkedList' maintains element order. The document serves as a quiz or review material for Java programming knowledge.

Uploaded by

rose oriana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Java Threading and Exception Handling Quiz

The document contains a series of multiple-choice questions and answers related to Java programming concepts, including thread management, constructors, string immutability, file reading, exception handling, immutable classes, boolean defaults, constant declaration, array indexing, and collection classes. Key answers include that threads are started with 'start()', constructors are called upon object creation, and 'LinkedList' maintains element order. The document serves as a quiz or review material for Java programming knowledge.

Uploaded by

rose oriana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Which of the following methods is used to start a thread in Java?

A) startThread()

B) run()

C) execute()

D) start()

Answer: D) start()

2. Which of the following is true about Java constructors?

A) A constructor can return a value.

B) A constructor is called when an object is created.

C) A constructor must have a return statement.

D) A constructor is only used to initialize static fields.

Answer: B) A constructor is called when an object is created.

3. What is the output of the following code?

String str = "Hello";

[Link](" World");

[Link](str);

A) Hello World

B) World

C) Hello

D) Compilation error

Answer: C) Hello
Explanation: Strings in Java are immutable, meaning that the concat()
method does not modify the original string but returns a new string. Since
the result of concat() is not assigned to the str variable, the original string
"Hello" is printed.

4. Which of the following classes is used to read characters from a file in


Java?

A) FileReader

B) BufferedReader

C) FileInputStream

D) PrintWriter

Answer: A) FileReader

5. What does the finally block do in Java exception handling?

A) It executes only if no exception occurs.

B) It executes only if an exception occurs.

C) It executes regardless of whether an exception occurs or not.

D) It is used to catch multiple exceptions.

Answer: C) It executes regardless of whether an exception occurs or not.

6. Which of the following can be used to create an immutable class in Java?

A) Declaring all fields as final and providing no setter methods.

B) Declaring all fields as private and providing setter methods for them.

C) Declaring the class as abstract.

D) Declaring the class as static.


Answer: A) Declaring all fields as final and providing no setter methods.

7. What is the default value of a boolean variable in Java?

A) true

B) false

C) null

D) 0

Answer: B) false

8. Which of the following is a valid statement to declare a constant in Java?

A) const int MAX = 100;

B) final int MAX = 100;

C) constant int MAX = 100;

D) static final int MAX = 100;

Answer: B) final int MAX = 100;

9. What is the output of the following code?

int[] arr = {1, 2, 3};

[Link](arr[3]);

A) 0

B) 1

C) ArrayIndexOutOfBoundsException
D) Compilation error

Answer: C) ArrayIndexOutOfBoundsException

Explanation: Arrays in Java are 0-indexed, so an array of length 3 (with


indexes 0, 1, and 2) will throw an ArrayIndexOutOfBoundsException when
trying to access index 3.

10. Which of the following collection classes in Java maintains the order of
elements?

A) HashSet

B) HashMap

C) TreeSet

D) LinkedList

Answer: D) LinkedList

Common questions

Powered by AI

Java defines constants using 'final' keyword to prevent modification post-initialization, ensuring data consistency. When declaring constant fields, considerations include visibility (public or private access) and initialization scope (immediate assignment on declaration or through a constructor), influencing encapsulation and flexibility .

Java Strings are immutable, meaning their state cannot be changed once created. The 'concat()' method returns a new String object with the combined contents, leaving the original string unchanged. This is demonstrated when the code `str.concat(' World');` does not modify 'str', but creating a new string that needs assignment to be used .

'FileReader' is used to read character files directly, while 'BufferedReader' provides buffering to 'FileReader' enhancing efficiency through reduced I/O operations. 'BufferedReader' is generally preferred for large data as it reads larger chunks at once and provides additional methods like 'readLine()', improving performance and convenience .

Java constructors cannot return a value because they are not called using a method call but are invoked by the JVM when an object is created. Their primary purpose is to initialize the new object's state by setting the initial values of its fields .

'start()' is used to create a new thread in Java, allowing the JVM to call the 'run()' method to initiate the thread's activity concurrently with the existing ones. Using 'run()' directly doesn't initiate a new thread; it runs in the current thread as a normal method call, therefore lacking the benefits of parallel execution .

The 'finally' block is used to execute code that must run after a try-catch block regardless of exceptions, such as closing resources or cleaning up tasks. By ensuring execution, even during an exception, 'finally' enhances the program's robustness by preventing resource leaks and performing necessary actions .

Declaring fields as 'final' ensures their values cannot change after the object's construction, a key practice for immutability. Immutability prevents unintended changes, making programs less error-prone and more thread-safe. Mutable objects can lead to unpredictable state changes, especially in multi-threaded environments .

LinkedList in Java maintains the insertion order of elements which is crucial for applications requiring sequence integrity, such as in order-dependent computations. Choosing LinkedList impacts algorithm design by necessitating trade-offs between access time and ordering, as it offers linear time complexity for access and constant time for insertions and deletions .

ArrayIndexOutOfBoundsException signals attempts to access indices outside an array's bounds, a common programming error. Developers can prevent it by ensuring index variables remain within bounds and employing exception handling techniques to catch and manage these exceptions effectively, thus maintaining program stability .

The boolean default of 'false' represents Java's conservative initialization approach to prevent accidental true values which typically have more significant semantic action. Using 'false' as default reduces unintentional actions and errors during logical operations .

You might also like