Create New Post

Java MCQs - 9

  1. What is the output of the following code?
String str = "Java"; str.concat(" Programming"); System.out.println(str); 

A) Java Programming
B) Java
C) Programming
D) Compiler error

Answer: B) Java

Explanation: The concat() method returns a new string and does not modify the original string.

  1. Which of the following is true about the ArrayList class in Java?

A) It is a synchronized collection.
B) It can store only primitive data types.
C) It allows duplicate elements.
D) It is a resizable array.

Answer: D) It is a resizable array.

  1. What is the output of the following code?
System.out.println(10 / 3); 

A) 3
B) 3.33
C) 3.0
D) 3.333

Answer: C) 3.0

  1. In Java, which of the following is NOT a valid access modifier?

A) private
B) package-private
C) internal
D) protected

Answer: C) internal

  1. What is the output of the following code?
 String str = "Hello";
System.out.println(str.indexOf("l"));

A) 0
B) 2
C) 3
D) Compiler error

Answer: B) 2

  1. Which of the following statements about Java arrays is true?

A) Arrays can store elements of different data types.
B) The size of an array can be changed dynamically.
C) Arrays in Java are objects.
D) Arrays can be initialized using a loop.

Answer: C) Arrays in Java are objects.

  1. What is the output of the following code?
System.out.println("Java".length());
 

A) 5
B) 4
C) 1
D) Compiler error

Answer: A) 4

  1. Which of the following statements about Java threads is true?

A) Threads are independent of each other.
B) A thread can be restarted after it is stopped.
C) Threads always have the same priority.
D) Threads cannot communicate with each other.

Answer: B) A thread can be restarted after it is stopped.

  1. What is the output of the following code?
String str = "Hello";
System.out.println(str.toLowerCase());

A) HELLO
B) Hello
C) hello
D) Compiler error

Answer: C) hello

  1. Which of the following is true about Java HashSet?

A) It allows duplicate elements.
B) Elements in a HashSet are ordered.
C) It is synchronized.
D) It does not allow null elements.

Answer: D) It does not allow null elements.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

98521