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

Java Quiz Lesson 3

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)
6 views4 pages

Java Quiz Lesson 3

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
You are on page 1/ 4

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";

str.concat(" World");

System.out.println(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};

System.out.println(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

You might also like