Create New Post

Java MCQs - 7

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

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

Answer: A) 4

  1. In Java, which keyword is used to define a constant?

A) static
B) final
C) const
D) constant

Answer: B) final

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

A) true
B) false
C) Compiler error
D) Runtime error

Answer: A) true

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

A) Enums cannot have constructors.
B) Enums cannot implement interfaces.
C) Enums can have methods and fields.
D) Enums cannot have constants.

Answer: C) Enums can have methods and fields.

  1. What is the output of the following code?
int x = 5;
System.out.println(x > 2 ? "Yes" : "No");

A) Yes
B) No
C) true
D) false

Answer: A) Yes

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

A) Generics allow you to create classes that can work with any data type.
B) Generics are only applicable to primitive data types.
C) Generics are a way to achieve multiple inheritance in Java.
D) Generics provide type safety by allowing you to specify the data types of objects that a class can work with.

Answer: D) Generics provide type safety by allowing you to specify the data types of objects that a class can work with.

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

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

Answer: B) HELLO

  1. Which of the following is NOT a valid method defined in the Object class in Java?

A) equals()
B) hashCode()
C) compareTo()
D) toString()

Answer: C) compareTo()

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

A) Hellow
B) Hello
C) Hewwo
D) Compiler error

Answer: B) Hello

Explanation: The replace() method returns a new string with the specified characters replaced, but it does not modify the original string.

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

A) Java IO streams are only used for network communication.
B) Java IO streams support only character-based input/output.
C) Java IO streams can be classified into two types: byte streams and character streams.
D) Java IO streams are only used for reading data and not for writing.

Answer: C) Java IO streams can be classified into two types: byte streams and character streams.

Comments

Leave a Reply

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

37074