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.
What is the default value of an int field in a Java class?
easy
- null
- 0
- undefined
- 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.
Which keyword prevents a class from being subclassed?
easy
- static
- final
- abstract
- 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.
What does this print?
easy
String s = "Java"; System.out.println(s.length() + s.charAt(0));- 4J
- 78
- Java4
- 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.
Which collection does not allow duplicate elements?
easy
- ArrayList
- LinkedList
- HashSet
- Vector
Answer: C. HashSet
Explanation: Set implementations (HashSet, TreeSet, LinkedHashSet) keep unique elements. Lists allow duplicates.
What does this print?
medium
String a = "hi"; String b = new String("hi"); System.out.println((a == b) + " " + a.equals(b));- true true
- false true
- false false
- true false
Answer: B. false true
Explanation: == compares references: new String creates a separate object. equals() compares the characters.
What is the difference between an abstract class and an interface in modern Java (8+)?
medium
- Interfaces cannot have any method bodies
- A class can implement many interfaces but extend only one class; abstract classes can hold state (instance fields)
- Abstract classes cannot have constructors
- 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.
What does this print?
medium
int[] a = {1, 2, 3}; int[] b = a; b[0] = 9; System.out.println(a[0]);- 1
- 9
- 0
- An error
Answer: B. 9
Explanation: Arrays are objects: b = a copies the reference, so both names point to the same array.
Which exception types must be caught or declared with throws?
medium
- Checked exceptions such as IOException
- Unchecked exceptions such as NullPointerException
- Errors such as OutOfMemoryError
- 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.
What does HashMap use to find the bucket for a key, and what must stay consistent?
hard
- toString(); it must be unique
- hashCode() to pick the bucket and equals() to match the key; equal objects must have equal hash codes
- compareTo(); keys must be sorted
- 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.
What does this print?
hard
Integer x = 127, y = 127; Integer p = 128, q = 128; System.out.println((x == y) + " " + (p == q));- true true
- true false
- false false
- 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().
What does the volatile keyword guarantee?
hard
- Atomic increments (x++)
- Visibility: a write by one thread is seen by other threads reading the variable
- Mutual exclusion like synchronized
- 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.
Which statements about Java strings are true? (Select all that apply)
hard
- String objects are immutable
- StringBuilder is mutable and faster for building strings in a loop
- String literals with the same text can share one object from the string pool
- 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.