Create New Post

Java MCQs - 11

  1. What is the output of the following code?
String str = "Hello,World"; System.out.println(str.split(",")[1]); 

A) Hello
B) World
C) Compiler error
D) Runtime error

Answer: B) World

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

A) A package can contain other packages but not classes.
B) A package can only be imported once in a Java file.
C) Package names must always start with a capital letter.
D) Package-private members are accessible only within the same package.

Answer: D) Package-private members are accessible only within the same package.

  1. What is the output of the following code?
System.out.println(Math.abs(-5)); 

A) 5
B) -5
C) 0
D) Compiler error

Answer: A) 5

  1. In Java, which keyword is used to prevent method overriding?

A) override
B) final
C) sealed
D) static

Answer: B) final

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

A) J
B) a
C) v
D) Compiler error

Answer: A) J

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

A) Interfaces can contain implementation of methods.
B) An interface can extend multiple interfaces using the extends keyword.
C) Interfaces can have constructors.
D) Interface methods must be marked as static.

Answer: B) An interface can extend multiple interfaces using the extends keyword.

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

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

Answer: A) true

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

A) Strings in Java are mutable.
B) Strings can be concatenated using the + operator only.
C) Strings in Java are instances of the String class.
D) Strings in Java are indexed starting from 1.

Answer: C) Strings in Java are instances of the String class.

  1. What is the output of the following code?
System.out.println(Math.floor(5.6)); 

A) 5
B) 6
C) 5.6
D) 6.0

Answer: A) 5

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

A) Arrays can have a fixed size that cannot be changed.
B) Arrays can contain elements of different data types.
C) Arrays are always initialized with default values.
D) Arrays can only store primitive data types.

Answer: A) Arrays can have a fixed size that cannot be changed.

Comments

Leave a Reply

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

11940