Java questions

OOP, strings, collections, exceptions, threads and output questions.

1. Which method is the entry point of a Java application?

  1. public void run()
  2. public void start()
  3. static int main()
  4. public static void main(String[] args)
Answer

D. public static void main(String[] args) The JVM starts at main with this exact signature.

2. Which keyword prevents a class from being subclassed?

  1. sealed only
  2. final
  3. static
  4. const
Answer

B. final A final class cannot be extended.

3. Strings in Java are:

  1. Primitive types
  2. Mutable
  3. Immutable
  4. Character arrays
Answer

C. Immutable String objects cannot change; operations return new strings.

4. Which collection does not allow duplicate elements?

  1. ArrayList
  2. List
  3. Set
  4. Vector
Answer

C. Set A Set holds unique elements.

5. JVM stands for:

  1. Java Visual Model
  2. Java Virtual Machine
  3. Java Variable Method
  4. Joint Virtual Memory
Answer

B. Java Virtual Machine The JVM runs compiled bytecode.

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

  1. String
  2. int
  3. char
  4. boolean
Answer

A. String String is a class.

7. What is the default value of an int instance variable?

  1. null
  2. Garbage value
  3. 1
  4. 0
Answer

D. 0 Fields get default values; int defaults to 0.

8. Which keyword is used to inherit a class?

  1. implements
  2. super
  3. extends
  4. inherits
Answer

C. extends Classes use extends; interfaces are implemented.

9. A class can implement:

  1. Only abstract classes
  2. No interfaces
  3. Only one interface
  4. Multiple interfaces
Answer

D. Multiple interfaces Java allows multiple interface implementation.

10. Which keyword refers to the parent class?

  1. base
  2. this
  3. parent
  4. super
Answer

D. super super accesses the superclass members and constructors.

11. Which exception is thrown when dividing an int by zero?

  1. IOException
  2. NumberFormatException
  3. NullPointerException
  4. ArithmeticException
Answer

D. ArithmeticException Integer division by zero throws ArithmeticException.

12. Which block always runs whether or not an exception occurs?

  1. catch
  2. finally
  3. final
  4. throw
Answer

B. finally finally runs after try/catch.

13. Checked exceptions must be:

  1. Thrown only by the JVM
  2. Ignored
  3. Caught or declared with throws
  4. Subclasses of RuntimeException
Answer

C. Caught or declared with throws The compiler enforces handling of checked exceptions.

14. Which class is used to build strings efficiently in a single thread?

  1. CharArray
  2. String
  3. StringBuffer only
  4. StringBuilder
Answer

D. StringBuilder StringBuilder is mutable and unsynchronised (fast).

15. HashMap allows:

  1. No null keys or values
  2. Many null keys
  3. Only null values
  4. One null key and many null values
Answer

D. One null key and many null values HashMap permits one null key.

16. Which interface must a class implement to be run by a Thread?

  1. Runnable
  2. Cloneable
  3. Comparable
  4. Serializable
Answer

A. Runnable Runnable has the run() method.

17. What does the "static" keyword mean for a method?

  1. It cannot be called
  2. It is private
  3. It runs automatically
  4. It belongs to the class, not to an object
Answer

D. It belongs to the class, not to an object Static methods are called on the class.

18. How is memory of unused objects reclaimed in Java?

  1. With a free() method
  2. The JVM garbage collector does it automatically
  3. With the delete keyword
  4. With a destroy() method
Answer

B. The JVM garbage collector does it automatically Java has automatic garbage collection.

19. Method overriding is an example of:

  1. Runtime polymorphism
  2. Compile-time polymorphism
  3. Abstraction only
  4. Encapsulation
Answer

A. Runtime polymorphism The method called depends on the object type at runtime.

20. Which access modifier makes a member visible only inside its own class?

  1. private
  2. protected
  3. default
  4. public
Answer

A. private private limits access to the class.

21. Which of these is a marker interface?

  1. Runnable
  2. Serializable
  3. List
  4. Comparator
Answer

B. Serializable Serializable has no methods; it marks a class.

22. What does the JDK contain that the JRE does not?

  1. Core libraries
  2. Garbage collector
  3. The JVM
  4. Development tools such as the compiler (javac)
Answer

D. Development tools such as the compiler (javac) JDK = JRE + development tools.

23. Which collection keeps insertion order and allows duplicates?

  1. TreeSet
  2. HashMap keys
  3. HashSet
  4. ArrayList
Answer

D. ArrayList Lists keep order and allow duplicates.

24. What does the "volatile" keyword guarantee?

  1. The variable is stored on disk
  2. Changes to the variable are visible to all threads
  3. The variable cannot change
  4. Faster arithmetic
Answer

B. Changes to the variable are visible to all threads volatile ensures visibility across threads.

25. Which method compares two objects for ordering in Comparable?

  1. equals()
  2. compare()
  3. compareTo()
  4. hashCode()
Answer

C. compareTo() Comparable declares compareTo.

26. Can a constructor be declared final in Java?

  1. Yes
  2. Only if it is private
  3. Only in abstract classes
  4. No
Answer

D. No Constructors are not inherited, so final is not allowed on them.

27. What is the output?

public class Main {
    public static void main(String[] args) {
        int x = 10;
        x += x++ + ++x;
        System.out.println(x);
    }
}
  1. 30
  2. 32
  3. 31
  4. 33
Answer

B. 32 x += (10 + 12) with the original x (10) gives 32.

28. What is the output?

public class Main {
    public static void main(String[] args) {
        String a = "OU";
        String b = a + 2 + 3;
        String c = 2 + 3 + a;
        System.out.println(b + " " + c);
    }
}
  1. OU5 5OU
  2. OU5 23OU
  3. OU23 23OU
  4. OU23 5OU
Answer

D. OU23 5OU Evaluation is left to right: "OU" + 2 + 3 = "OU23"; 2 + 3 + "OU" = "5OU".

29. What is the output?

public class Main {
    public static void main(String[] args) {
        String s1 = "java";
        String s2 = new String("java");
        System.out.println((s1 == s2) + " " + s1.equals(s2));
    }
}
  1. false false
  2. true true
  3. true false
  4. false true
Answer

D. false true == compares references; equals compares content.

30. What is the output?

public class Main {
    public static void main(String[] args) {
        int[] a = {3, 1, 4, 1, 5};
        int sum = 0;
        for (int v : a) sum += v;
        System.out.println(sum / a.length);
    }
}
  1. 2.8
  2. 3
  3. 2
  4. 14
Answer

C. 2 Sum 14 / 5 = 2 with integer division.

31. What is the output?

class Animal { String sound() { return "..."; } }
class Dog extends Animal { String sound() { return "Woof"; } }
public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        System.out.println(a.sound());
    }
}
  1. Compile error
  2. Woof
  3. ...
  4. null
Answer

B. Woof Overridden methods are chosen at runtime by the object type.

32. What is the output?

public class Main {
    static int f(int n) { return n <= 1 ? 1 : n * f(n - 1); }
    public static void main(String[] args) {
        System.out.println(f(5));
    }
}
  1. 24
  2. 120
  3. 5
  4. 15
Answer

B. 120 5! = 120.

33. What is the output?

public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("code");
        sb.reverse().append("!");
        System.out.println(sb);
    }
}
  1. !edoc
  2. code!
  3. edoc!
  4. edoc
Answer

C. edoc! reverse() then append("!").

34. What is the output?

public class Main {
    public static void main(String[] args) {
        try {
            int x = 5 / 0;
            System.out.print("A");
        } catch (ArithmeticException e) {
            System.out.print("B");
        } finally {
            System.out.print("C");
        }
    }
}
  1. ABC
  2. C
  3. AC
  4. BC
Answer

D. BC The division throws, so A is skipped; catch prints B and finally prints C.

35. What is the output?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(List.of(4, 2, 9));
        Collections.sort(list);
        list.add(0, 7);
        System.out.println(list);
    }
}
  1. [2, 4, 9, 7]
  2. [7, 2, 4, 9]
  3. [7, 4, 2, 9]
  4. [2, 4, 7, 9]
Answer

B. [7, 2, 4, 9] Sorted to [2, 4, 9], then 7 inserted at index 0.

36. What is the output?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Set<String> s = new TreeSet<>(List.of("pear", "apple", "kiwi", "fig"));
        System.out.println(s);
    }
}
  1. [apple, fig, kiwi, pear]
  2. [fig, kiwi, apple, pear]
  3. [apple, kiwi, fig, pear]
  4. [pear, apple, kiwi, fig]
Answer

A. [apple, fig, kiwi, pear] TreeSet keeps elements sorted.

37. What is the output?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Map<Character, Integer> m = new TreeMap<>();
        for (char c : "banana".toCharArray()) m.put(c, m.getOrDefault(c, 0) + 1);
        System.out.println(m);
    }
}
  1. {b=1, a=3, n=2}
  2. {a=3, b=1, n=2}
  3. {a=2, b=1, n=3}
  4. {a=3, n=2, b=1}
Answer

B. {a=3, b=1, n=2} Counts: a 3, b 1, n 2, in key order.

38. What is the output?

public class Main {
    static int count = 0;
    Main() { count++; }
    public static void main(String[] args) {
        new Main(); new Main(); new Main();
        System.out.println(count);
    }
}
  1. 1
  2. 3
  3. 0
  4. Compile error
Answer

B. 3 The static count is shared by all objects.

39. What is the output?

public class Main {
    public static void main(String[] args) {
        int i = 0;
        do {
            i += 3;
        } while (i < 10);
        System.out.println(i);
    }
}
  1. 10
  2. 3
  3. 12
  4. 9
Answer

C. 12 i goes 3, 6, 9, 12; the loop stops when i = 12.

40. What is the output?

public class Main {
    public static void main(String[] args) {
        System.out.println(10 / 4 + " " + 10 / 4.0 + " " + 10 % 4);
    }
}
  1. 2.5 2.5 2
  2. 2 2 2
  3. 2 2.5 0
  4. 2 2.5 2
Answer

D. 2 2.5 2 int division 2; double division 2.5; remainder 2.

41. What is the output?

public class Main {
    public static void main(String[] args) {
        char c = 'a';
        c += 2;
        System.out.println(c + 1);
    }
}
  1. c1
  2. 100
  3. d
  4. 99
Answer

B. 100 c becomes 'c' (99); c + 1 is an int, 100.

42. What is the output?

interface Greet { String hi(String n); }
public class Main {
    public static void main(String[] args) {
        Greet g = n -> "Hi " + n;
        System.out.println(g.hi("Sana"));
    }
}
  1. Compile error
  2. Hi
  3. Hi Sana
  4. Sana
Answer

C. Hi Sana The lambda implements the functional interface.

43. What is the output?

import java.util.stream.*;
public class Main {
    public static void main(String[] args) {
        int s = IntStream.rangeClosed(1, 5).filter(x -> x % 2 == 1).sum();
        System.out.println(s);
    }
}
  1. 5
  2. 9
  3. 15
  4. 6
Answer

B. 9 1 + 3 + 5 = 9.

44. What is the output?

public class Main {
    public static void main(String[] args) {
        String s = "placement";
        System.out.println(s.indexOf('e') + " " + s.charAt(2) + " " + s.substring(5));
    }
}
  1. 4 a ment
  2. 4 a ement
  3. 4 l ment
  4. 5 a ment
Answer

A. 4 a ment p-l-a-c-e: the first e is at index 4; charAt(2) is a; substring(5) starts at m, giving "ment".

45. What is the output?

class A { A() { System.out.print("A"); } }
class B extends A { B() { System.out.print("B"); } }
class C extends B { C() { System.out.print("C"); } }
public class Main {
    public static void main(String[] args) { new C(); }
}
  1. ABC
  2. C
  3. AC
  4. CBA
Answer

A. ABC Constructors run from the top of the hierarchy down.

46. What is the output?

public class Main {
    static void change(int[] a) { a[0] = 99; }
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        change(arr);
        System.out.println(arr[0]);
    }
}
  1. 1
  2. Compile error
  3. 0
  4. 99
Answer

D. 99 The array reference is passed by value, so the method changes the same array.

47. What is the output?

public class Main {
    public static void main(String[] args) {
        Integer a = 127, b = 127;
        Integer c = 128, d = 128;
        System.out.println((a == b) + " " + (c == d));
    }
}
  1. false true
  2. true false
  3. false false
  4. true true
Answer

B. true false Integer values from -128 to 127 are cached, so a and b share an object; 128 is not cached.

48. What is the output?

public class Main {
    public static void main(String[] args) {
        int n = 0;
        for (int i = 0; i < 4; i++)
            for (int j = i; j < 4; j++)
                n++;
        System.out.println(n);
    }
}
  1. 10
  2. 8
  3. 16
  4. 4
Answer

A. 10 4 + 3 + 2 + 1 = 10 iterations.

49. What is the output?

abstract class Shape { abstract double area(); }
class Sq extends Shape { double s; Sq(double s) { this.s = s; } double area() { return s * s; } }
public class Main {
    public static void main(String[] args) {
        Shape sh = new Sq(3);
        System.out.println(sh.area());
    }
}
  1. 9
  2. 6.0
  3. 9.0
  4. Compile error
Answer

C. 9.0 area() returns a double, printed as 9.0.

50. In Java, which description best fits “JVM”?

  1. development tools and Java runtime components
  2. subclass supplies an implementation of inherited method
  3. prevent direct instantiation of an abstract class
  4. executes Java bytecode
Answer

D. executes Java bytecode JVM is used to executes Java bytecode.

51. In Java, which description best fits “JDK”?

  1. prevent direct instantiation of an abstract class
  2. development tools and Java runtime components
  3. contract of methods a type can implement
  4. member associated with a class rather than each instance
Answer

B. development tools and Java runtime components JDK is used to development tools and Java runtime components.

52. In Java, which description best fits “interface”?

  1. member associated with a class rather than each instance
  2. contract of methods a type can implement
  3. key-value map using hashing
  4. same method name with different parameter lists
Answer

B. contract of methods a type can implement interface is used to contract of methods a type can implement.

53. In Java, which description best fits “abstract”?

  1. member associated with a class rather than each instance
  2. prevent overriding of a method
  3. prevent direct instantiation of an abstract class
  4. development tools and Java runtime components
Answer

C. prevent direct instantiation of an abstract class abstract is used to prevent direct instantiation of an abstract class.

54. In Java, which description best fits “final”?

  1. development tools and Java runtime components
  2. prevent overriding of a method
  3. key-value map using hashing
  4. member associated with a class rather than each instance
Answer

B. prevent overriding of a method final is used to prevent overriding of a method.

55. In Java, which description best fits “static”?

  1. member associated with a class rather than each instance
  2. same method name with different parameter lists
  3. key-value map using hashing
  4. resizable indexed list
Answer

A. member associated with a class rather than each instance static is used to member associated with a class rather than each instance.

56. In Java, which description best fits “overloading”?

  1. same method name with different parameter lists
  2. resizable indexed list
  3. executes Java bytecode
  4. prevent overriding of a method
Answer

A. same method name with different parameter lists overloading is used to same method name with different parameter lists.

57. In Java, which description best fits “overriding”?

  1. prevent overriding of a method
  2. member associated with a class rather than each instance
  3. subclass supplies an implementation of inherited method
  4. executes Java bytecode
Answer

C. subclass supplies an implementation of inherited method overriding is used to subclass supplies an implementation of inherited method.

58. In Java, which description best fits “ArrayList”?

  1. contract of methods a type can implement
  2. prevent direct instantiation of an abstract class
  3. resizable indexed list
  4. member associated with a class rather than each instance
Answer

C. resizable indexed list ArrayList is used to resizable indexed list.

59. In Java, which description best fits “HashMap”?

  1. executes Java bytecode
  2. key-value map using hashing
  3. subclass supplies an implementation of inherited method
  4. member associated with a class rather than each instance
Answer

B. key-value map using hashing HashMap is used to key-value map using hashing.

60. Which Java concept matches this description: “executes Java bytecode”?

  1. JVM
  2. static
  3. ArrayList
  4. abstract
Answer

A. JVM The definition describes JVM.

61. Which Java concept matches this description: “development tools and Java runtime components”?

  1. interface
  2. overriding
  3. JDK
  4. JVM
Answer

C. JDK The definition describes JDK.

62. Which Java concept matches this description: “contract of methods a type can implement”?

  1. abstract
  2. interface
  3. ArrayList
  4. JDK
Answer

B. interface The definition describes interface.

63. Which Java concept matches this description: “prevent direct instantiation of an abstract class”?

  1. final
  2. JVM
  3. static
  4. abstract
Answer

D. abstract The definition describes abstract.

64. Which Java concept matches this description: “prevent overriding of a method”?

  1. static
  2. HashMap
  3. overriding
  4. final
Answer

D. final The definition describes final.

65. Which Java concept matches this description: “member associated with a class rather than each instance”?

  1. static
  2. abstract
  3. HashMap
  4. interface
Answer

A. static The definition describes static.

66. Which Java concept matches this description: “same method name with different parameter lists”?

  1. ArrayList
  2. overloading
  3. abstract
  4. interface
Answer

B. overloading The definition describes overloading.

67. Which Java concept matches this description: “subclass supplies an implementation of inherited method”?

  1. overriding
  2. interface
  3. JVM
  4. overloading
Answer

A. overriding The definition describes overriding.

68. Which Java concept matches this description: “resizable indexed list”?

  1. overloading
  2. ArrayList
  3. static
  4. interface
Answer

B. ArrayList The definition describes ArrayList.

69. Which Java concept matches this description: “key-value map using hashing”?

  1. static
  2. interface
  3. HashMap
  4. overloading
Answer

C. HashMap The definition describes HashMap.

Mock test

More Technical MCQs topics