Hack4Hack

Java practice questions with answers

Practise Java questions at your own pace: strings, collections, OOP, exceptions, memory, autoboxing and concurrency. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.

  1. What is the default value of an int field in a Java class?

    easy

    1. null
    2. 0
    3. undefined
    4. It must be initialised

    Answer: B. 0

    Explanation: Fields get default values: 0 for numbers, false for boolean, null for objects. Local variables have no default and must be assigned before use.

  2. Which keyword prevents a class from being subclassed?

    easy

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

    Answer: B. final

    Explanation: A final class cannot be extended (String is one). A final method cannot be overridden, and a final variable cannot be reassigned.

  3. What does this print?

    easy

    String s = "Java";
    System.out.println(s.length() + s.charAt(0));
    1. 4J
    2. 78
    3. Java4
    4. An error

    Answer: B. 78

    Explanation: length() is the int 4 and charAt(0) is the char 'J' (code 74). int + char is numeric addition: 4 + 74 = 78.

  4. Which collection does not allow duplicate elements?

    easy

    1. ArrayList
    2. LinkedList
    3. HashSet
    4. Vector

    Answer: C. HashSet

    Explanation: Set implementations (HashSet, TreeSet, LinkedHashSet) keep unique elements. Lists allow duplicates.

  5. What does this print?

    medium

    String a = "hi";
    String b = new String("hi");
    System.out.println((a == b) + " " + a.equals(b));
    1. true true
    2. false true
    3. false false
    4. true false

    Answer: B. false true

    Explanation: == compares references: new String creates a separate object. equals() compares the characters.

  6. What is the difference between an abstract class and an interface in modern Java (8+)?

    medium

    1. Interfaces cannot have any method bodies
    2. A class can implement many interfaces but extend only one class; abstract classes can hold state (instance fields)
    3. Abstract classes cannot have constructors
    4. There is no difference

    Answer: B. A class can implement many interfaces but extend only one class; abstract classes can hold state (instance fields)

    Explanation: Since Java 8 interfaces can have default and static methods, but they still cannot hold instance state, and a class can extend only one class.

  7. What does this print?

    medium

    int[] a = {1, 2, 3};
    int[] b = a;
    b[0] = 9;
    System.out.println(a[0]);
    1. 1
    2. 9
    3. 0
    4. An error

    Answer: B. 9

    Explanation: Arrays are objects: b = a copies the reference, so both names point to the same array.

  8. Which exception types must be caught or declared with throws?

    medium

    1. Checked exceptions such as IOException
    2. Unchecked exceptions such as NullPointerException
    3. Errors such as OutOfMemoryError
    4. All exceptions

    Answer: A. Checked exceptions such as IOException

    Explanation: Checked exceptions (subclasses of Exception but not RuntimeException) are enforced by the compiler. RuntimeExceptions and Errors are unchecked.

  9. What does HashMap use to find the bucket for a key, and what must stay consistent?

    hard

    1. toString(); it must be unique
    2. hashCode() to pick the bucket and equals() to match the key; equal objects must have equal hash codes
    3. compareTo(); keys must be sorted
    4. The key's memory address only

    Answer: B. hashCode() to pick the bucket and equals() to match the key; equal objects must have equal hash codes

    Explanation: If you override equals() you must override hashCode() too, or equal keys can land in different buckets and lookups fail.

  10. What does this print?

    hard

    Integer x = 127, y = 127;
    Integer p = 128, q = 128;
    System.out.println((x == y) + " " + (p == q));
    1. true true
    2. true false
    3. false false
    4. false true

    Answer: B. true false

    Explanation: Autoboxing caches Integer objects from -128 to 127, so x and y are the same object. 128 creates two separate objects. Compare boxed numbers with equals().

  11. What does the volatile keyword guarantee?

    hard

    1. Atomic increments (x++)
    2. Visibility: a write by one thread is seen by other threads reading the variable
    3. Mutual exclusion like synchronized
    4. That the variable is never cached on disk

    Answer: B. Visibility: a write by one thread is seen by other threads reading the variable

    Explanation: volatile gives visibility and ordering, not atomicity: x++ on a volatile int can still lose updates. Use AtomicInteger or synchronized for that.

  12. Which statements about Java strings are true? (Select all that apply)

    hard

    1. String objects are immutable
    2. StringBuilder is mutable and faster for building strings in a loop
    3. String literals with the same text can share one object from the string pool
    4. String is a primitive type

    Answer: A. String objects are immutable; B. StringBuilder is mutable and faster for building strings in a loop; C. String literals with the same text can share one object from the string pool

    Explanation: String is a final, immutable class, not a primitive. Repeated += in a loop creates many objects; StringBuilder appends in place.

Play the Daily Quiz at 4 PM IST