Create New Post

Java MCQs - 2

  1. Which keyword is used to specify that a method does not return any value in Java?
    A) void
    B) null
    C) none
    D) empty

    Answer: A) void

  2. What is the correct way to declare a constant variable in Java?
    A) const int MAX_VALUE = 100;
    B) final int MAX_VALUE = 100;
    C) static int MAX_VALUE = 100;
    D) final constant int MAX_VALUE = 100;

    Answer: B) final int MAX_VALUE = 100;

  3. Which of the following is used to read input from the user in Java?
    A) System.readInput()
    B) Console.readLine()
    C) Scanner
    D) BufferedReader

    Answer: C) Scanner

  4. What does the 'super' keyword refer to in Java?
    A) It refers to the superclass of the current class.
    B) It refers to the subclass of the current class.
    C) It refers to the base class of the current class.
    D) It refers to the derived class of the current class.

    Answer: A) It refers to the superclass of the current class.

  5. Which of the following is true about the 'break' statement in Java?
    A) It is used to exit a loop or switch statement.
    B) It is used to skip the remaining code in a loop and continue to the next iteration.
    C) It is used to terminate the program abruptly.
    D) It is used to define a label in Java.

    Answer: A) It is used to exit a loop or switch statement.

  6. What is the correct way to declare and initialize an array in Java?
    A) int[] arr = {1, 2, 3, 4, 5};
    B) array arr = {1, 2, 3, 4, 5};
    C) int arr[] = [1, 2, 3, 4, 5];
    D) int arr[] = new int[5] {1, 2, 3, 4, 5};

    Answer: A) int[] arr = {1, 2, 3, 4, 5};

  7. Which method is called when an object is created in Java?
    A) finalize()
    B) constructor()
    C) new()
    D) initialize()

    Answer: B) constructor()

  8. What is the correct way to compare two strings in Java for equality?
    A) str1 == str2
    B) str1.equals(str2)
    C) str1.compareTo(str2)
    D) str1.equal(str2)

    Answer: B) str1.equals(str2)

  9. What is the purpose of the 'try', 'catch', and 'finally' blocks in Java?
    A) To handle exceptions
    B) To define loops
    C) To declare variables
    D) To execute code asynchronously

    Answer: A) To handle exceptions

  10. Which of the following is true about Java Virtual Machine (JVM)?
    A) It is hardware-specific.
    B) It directly executes Java source code.
    C) It ensures platform independence.
    D) It is responsible for compiling Java code.

    Answer: A) It is hardware-specific.

Comments

Leave a Reply

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

12033