Java Multiple Choice Question

Question : 1 Which keyword is used to declare a class in Java?

  • A

    class

  • B

    void

  • C

    public

  • D

    new

Answer:

Question : 2 Which data type is used to store whole numbers in Java?

  • A

    int

  • B

    String

  • C

    double

  • D

    char

Answer:

Question : 3 What is the output of the following code snippet? ```java System.out.println(5 == 5); ```

  • A

    true

  • B

    false

  • C

    error

  • D

    0

Answer:

Question : 4 Which method is called automatically when an object of a class is created?

  • A

    run()

  • B

    start()

  • C

    main()

  • D

    constructor

Answer:

Question : 5 Which keyword is used to refer to the current instance of a class in Java?

  • A

    this

  • B

    super

  • C

    self

  • D

    current

Answer:

Question : 6 What is the output of the following code snippet? ```java int x = 5; System.out.println(x++); ```

  • A

    5

  • B

    6

  • C

    error

  • D

    0

Answer:

Question : 7 Which keyword is used to create a subclass in Java?

  • A

    this

  • B

    super

  • C

    extends

  • D

    class

Answer:

Question : 8 Which of the following is NOT a valid Java identifier?

  • A

    myVariable

  • B

    2ndVariable

  • C

    _myVariable

  • D

    MyVariable123

Answer:

Question : 9 Which operator is used to compare two values for equality in Java?

  • A

    ==

  • B

    =

  • C

    ===

  • D

    equals()

Answer:

Question : 10 Which of the following statements is used to exit from a loop in Java?

  • A

    break

  • B

    continue

  • C

    exit

  • D

    return

Answer:

Question : 11 Which keyword is used to define a constant value in Java?

  • A

    static

  • B

    final

  • C

    constant

  • D

    const

Answer:

Question : 12 Which of the following access modifiers restricts access the least in Java?

  • A

    private

  • B

    protected

  • C

    public

  • D

    default

Answer:

Question : 13 Which of the following is a type of loop in Java that is executed at least once?

  • A

    for

  • B

    while

  • C

    do-while

  • D

    foreach

Answer:

Question : 14 What does the "static" keyword mean in Java?

  • A

    Dynamic binding

  • B

    Class level

  • C

    Instance level

  • D

    Shared among all instances of the class

Answer:

Question : 15 Which of the following is not a valid type of variable in Java?

  • A

    int

  • B

    string

  • C

    double

  • D

    boolean

Answer:

Question : 16 What is the result of 5 / 2 in Java?

  • A

    2

  • B

    2.5

  • C

    2.0

  • D

    2

Answer:

Question : 17 Which method is called when an object is garbage collected in Java?

  • A

    finalize()

  • B

    destroy()

  • C

    garbageCollector()

  • D

    delete()

Answer:

Question : 18 What is the purpose of the "toString()" method in Java?

  • A

    Converts a string to uppercase

  • B

    Converts a string to lowercase

  • C

    Returns a string representation of an object

  • D

    Concatenates two strings

Answer:

Question : 19 What is the output of the following code snippet? ```java int[] numbers = {1, 2, 3, 4, 5}; System.out.println(numbers[3]); ```

  • A

    1

  • B

    2

  • C

    3

  • D

    4

Answer:

Question : 20 Which keyword is used to prevent a method from being overridden in Java?

  • A

    override

  • B

    final

  • C

    static

  • D

    private

Answer:

Question : 21 Which operator is used to concatenate strings in Java?

  • A

    +

  • B

    -

  • C

    *

  • D

    concat()

Answer:

Question : 22 Which class is used to read input from the user in Java?

  • A

    System.in

  • B

    Scanner

  • C

    Reader

  • D

    InputStream

Answer:

Question : 23 What is the result of the expression (5 > 3) && (8 < 5) in Java?

  • A

    true

  • B

    false

  • C

    error

  • D

    0

Answer:

Question : 24 What is the output of the following code snippet? ```java String str = "hello"; System.out.println(str.charAt(2)); ```

  • A

    e

  • B

    l

  • C

    h

  • D

    o

Answer:

Question : 25 Which statement is used to manually throw an exception in Java?

  • A

    throw

  • B

    throws

  • C

    try

  • D

    catch

Answer: