Skip to content
VJ Classes

One place to learn science, commerce, arts, computers and life skills

10 questions. Think of your answer first, then open “Show answer”.

  1. Which tool of the JDK turns Hello.java into Hello.class?

    1. java
    2. javac
    3. javadoc
    4. jar
    Show answer

    Answer: B · javac

    javac is the compiler. The java command starts the JVM to run the compiled class, and jar packs classes into an archive.

  2. Which of these is NOT a primitive type in Java?

    1. boolean
    2. char
    3. String
    4. double
    Show answer

    Answer: C · String

    String is a class, so its values are objects. The other three are primitive types.

  3. What is the index of the first element of a Java array?

    1. 0
    2. 1
    3. -1
    4. It depends on the length of the array
    Show answer

    Answer: A · 0

    Java arrays are zero-based: the first element has index 0 and the last has index length - 1.

  4. A program calls sc.nextInt() and then sc.nextLine(), but nextLine() returns an empty string. Why?

    1. nextLine() only works with text files
    2. nextInt() closes the Scanner
    3. The user typed the number wrongly
    4. nextInt() leaves the end-of-line character in the input, and nextLine() reads up to it
    Show answer

    Answer: D · nextInt() leaves the end-of-line character in the input, and nextLine() reads up to it

    The number is read but the Enter key press stays in the input. The next call to nextLine() consumes that leftover and returns an empty string.

  5. What is the value of x? int x = 5; x += 3; x *= 2;

    1. 13
    2. 11
    3. 16
    4. 10
    Show answer

    Answer: C · 16

    First x becomes 5 + 3 = 8, and then 8 * 2 = 16.

  6. Which pair of methods in the same class is an example of overloading?

    1. int add(int a, int b) and double add(double a, double b)
    2. int add(int a, int b) and int add(int x, int y)
    3. int add(int a, int b) in a parent class and the same method in a child class
    4. int add(int a, int b) and void add(int a, int b)
    Show answer

    Answer: A · int add(int a, int b) and double add(double a, double b)

    Overloading means the same name with different parameter lists. A different return type alone is not enough, and a child class repeating a parent method is overriding.

  7. Which keyword makes a field or method belong to the class itself instead of to each object?

    1. final
    2. abstract
    3. private
    4. static
    Show answer

    Answer: D · static

    A static member is shared by the whole class and can be used through the class name without creating an object.

  8. Which statement about inheritance in Java is true?

    1. A class can extend two classes at the same time
    2. A class can extend one class and implement several interfaces
    3. An interface can be instantiated with new
    4. An abstract class must have no methods with bodies
    Show answer

    Answer: B · A class can extend one class and implement several interfaces

    Java allows a single parent class but any number of interfaces. Interfaces cannot be instantiated, and abstract classes may contain ordinary methods.

  9. What does this print? ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.remove(1); System.out.println(list);

    1. [10, 20, 30]
    2. [20, 30]
    3. [10, 30]
    4. [10, 20]
    Show answer

    Answer: C · [10, 30]

    Calling remove with an int argument removes the element at that index. Index 1 holds 20, so the list becomes [10, 30].

  10. What does this print? HashMap<String, Integer> m = new HashMap<>(); m.put("a", 1); m.put("b", 2); m.put("a", 3); System.out.println(m.size() + " " + m.get("a"));

    1. 3 1
    2. 2 3
    3. 3 3
    4. 2 1
    Show answer

    Answer: B · 2 3

    A key can appear only once. The second put with the key "a" replaces the value 1 by 3, so the map has 2 entries and "a" maps to 3.

Want to test yourself? Take the same questions as a timed practice test and see your score. Start the practice test