Create New Post

Java MCQs - 8

  1. What does the this keyword refer to in Java?

A) It refers to the previous object.
B) It refers to the next object.
C) It refers to the current object.
D) It refers to the superclass object.

Answer: C) It refers to the current object.

  1. What is the output of the following code?
int[] arr = new int[5];
System.out.println(arr.length);

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

Answer: B) 5

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

A) Constructors can return a value.
B) Constructors can be inherited.
C) Constructors can be private.
D) Constructors can be marked as final.

Answer: C) Constructors can be private.

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

A) He
B) Hel
C) ll
D) llo

Answer: C) ll

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

A) Interfaces can have constructors.
B) A class can implement multiple interfaces using the extends keyword.
C) Interface methods are final by default.
D) Interfaces can have instance variables.

Answer: B) A class can implement multiple interfaces using the extends keyword.

  1. What is the output of the following code?
System.out.println(Math.pow(2, 3)); 

A) 5
B) 8
C) 6
D) 2

Answer: B) 8

  1. Which of the following is NOT a valid Java keyword?

A) constant
B) synchronized
C) transient
D) volatile

Answer: A) constant

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

A) H
B) e
C) l
D) o

Answer: A) H

  1. Which of the following is true about Java method parameters?

A) Method parameters cannot have default values.
B) Method parameters can be marked as final.
C) Method parameters are always passed by value.
D) Method parameters can only be primitive data types.

Answer: B) Method parameters can be marked as final.

  1. What is the output of the following code?
int x = 10;
int y = 20;
System.out.println(x + y + "Result");

A) Result1020
B) 30Result
C) 30Result1020
D) Compiler error

Answer: B) 30Result

Comments

Leave a Reply

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

58154