JAVA Quizzes

 Here's a Java quiz for you. Each question is followed by multiple-choice answers. Choose the correct option for each question.

(You will find the answers to the questions at the end of the quiz)

1. What is the correct way to declare a constant in Java?

   a. const int x;

   b. constant x;

   c. final int x;

   d. int final x;


2. How do you declare a method in Java that does not return any value?

   a. void methodName() { }

   b. int methodName() { }

   c. method methodName() { }

   d. String methodName() { }


3. What is the keyword used to define a class in Java?

   a. class

   b. type

   c. struct

   d. define


4. How do you create an instance of a class in Java?

   a. MyClass obj = new MyClass;

   b. MyClass obj = create MyClass();

   c. MyClass obj = new MyClass();

   d. create MyClass obj;


5. In Java, what is the purpose of the `super` keyword?

   a. Calls the superclass constructor

   b. Refers to the current instance of the class

   c. Calls a static method

   d. Allocates memory for an object


6. What is the output of the following code snippet?

   
    int x = 5;
    System.out.println(x++);
    System.out.println(++x);
   

   a. 5, 6

   b. 6, 7

   c. 5, 7

   d. 6, 6


7. How do you catch exceptions in Java using a `try-catch` block?

   a. catch (Exception e) { }

   b. catch (ExceptionType e) { }

   c. catch (Throwable t) { }

   d. All of the above


8. What is the purpose of the `static` keyword in Java?

   a. Declares a static variable

   b. Declares a constant

   c. Defines a member function

   d. Allows access to the class-level variable/method without creating an instance


9. How do you define a constructor in Java?

   a. constructor MyClass() { }

   b. void MyClass() { }

   c. MyClass() { }

   d. new MyClass() { }


10. What is the difference between `==` and `.equals()` in Java when comparing objects?

    a. `==` compares object references, and `.equals()` compares object content.

    b. `.equals()` compares object references, and `==` compares object content.

    c. They are interchangeable and can be used for both reference and content comparison.

    d. Both `==` and `.equals()` are used for reference comparison.


11. What is the keyword used for inheritance in Java?

    a. inherit

    b. extends

    c. implements

    d. inheritance


12. What is the output of the following code snippet?


    String str = "Hello";
    System.out.println(str.charAt(0));
   

    a. H

    b. e

    c. Hello

    d. 72


13. How do you open a file named "example.txt" for reading in Java?

    a. File file = new File("example.txt", "r");

    b. FileReader file = new FileReader("example.txt");

    c. InputStream file = new InputStream("example.txt");

    d. BufferedReader file = new BufferedReader("example.txt");


14. What is the purpose of the `final` keyword in Java?

    a. Declares a constant

    b. Prevents inheritance

    c. Makes a variable/method unchangeable

    d. All of the above


15. What is the Java keyword used to implement multiple inheritance through interfaces?

    a. extends

    b. interface

    c. implements

    d. extends and implements


Answers:

1. c,

2. a, 

3. a, 

4. c, 

5. a, 

6. b, 

7. a, 

8. d, 

9. c, 

10. a, 

11. b, 

12. a, 

13. b, 

14. d, 

15. c

Post a Comment

You're welcome to share your ideas with us in comments.