Java quiz

Classes, objects, collections, exceptions, generics and streams.

82 questionsInstant feedbackFree

Set up your quiz

Difficulty
Number of questions

Tip: press 1–4 to answer and Enter for the next question.

All 82 questions with answers
  1. Who originally developed the Java programming language?

    • Bjarne Stroustrup at AT&T
    • Guido van Rossum at CWI
    • James Gosling at Sun Microsystems (correct)
    • Dennis Ritchie at Bell Labs
  2. Which method is the entry point of a Java application?

    • public void start()
    • public static void main(String[] args) (correct)
    • public static void run(String args)
    • static int main()
  3. Java source code is compiled into what?

    • Machine code for one CPU
    • Assembly language
    • Bytecode (correct)
    • JavaScript
  4. Which program runs Java bytecode?

    • The Java linker
    • The Java compiler (javac)
    • The operating system shell
    • The Java Virtual Machine (JVM) (correct)
  5. What file extension do Java source files use?

    • .class
    • .js
    • .jav
    • .java (correct)
  6. What file extension do compiled Java bytecode files use?

    • .jar
    • .java
    • .class (correct)
    • .exe
  7. Which keyword is used to create an object in Java?

    • make
    • create
    • new (correct)
    • object
  8. Which of these is NOT a primitive type in Java?

    • boolean
    • String (correct)
    • int
    • char
  9. What is the default value of an int field in a Java class?

    • 0 (correct)
    • 1
    • null
    • undefined
  10. What is the default value of an object reference field in Java?

    • 0
    • undefined
    • null (correct)
    • empty
  11. Which keyword is used to inherit from a class in Java?

    • super
    • inherits
    • implements
    • extends (correct)
  12. Which keyword is used by a class to use an interface?

    • implements (correct)
    • uses
    • extends
    • interface
  13. How do you write a single-line comment in Java?

    • # comment
    • // comment (correct)
    • -- comment
    • <!-- comment -->
  14. Which method prints a line of text to the console in Java?

    • System.out.println() (correct)
    • console.log()
    • print()
    • echo()
  15. What is the size of an int in Java?

    • 32 bits (correct)
    • 16 bits
    • It depends on the platform
    • 64 bits
  16. What is the size of a long in Java?

    • 128 bits
    • 16 bits
    • 32 bits
    • 64 bits (correct)
  17. Which type stores a single character in Java?

    • String
    • Character only
    • byte
    • char (correct)
  18. Which operator is used to compare two int values for equality?

    • ===
    • == (correct)
    • equals
    • =
  19. What does the length of an array named arr give you?

    • arr.count
    • arr.size()
    • arr.length()
    • arr.length (correct)
  20. What does the length of a String s give you?

    • s.count()
    • s.length() (correct)
    • s.size()
    • s.length
  21. Which keyword stops a loop immediately in Java?

    • exit
    • break (correct)
    • continue
    • stop
  22. Which keyword makes a variable's value unchangeable after it is assigned?

    • final (correct)
    • static
    • immutable
    • const
  23. What is the index of the first element in a Java array?

    • 1
    • It depends on the array type
    • -1
    • 0 (correct)
  24. Which access modifier makes a member visible everywhere?

    • default
    • public (correct)
    • protected
    • private
  25. Which access modifier restricts a member to its own class?

    • public
    • private (correct)
    • protected
    • package
  26. What is the result of 10 / 4 when both are int in Java?

    • 2.0
    • 3
    • 2 (correct)
    • 2.5
  27. What is the result of 10 % 4 in Java?

    • 0
    • 2 (correct)
    • 4
    • 2.5
  28. Which class is commonly used to read keyboard input in simple programs?

    • Scanner (correct)
    • Keyboard
    • InputStream only
    • Reader
  29. Which data type is used for true/false values?

    • bit
    • bool
    • Boolean only
    • boolean (correct)
  30. Which loop always runs its body at least once?

    • while
    • for-each
    • for
    • do-while (correct)
  31. What is method overloading?

    • Several methods with the same name but different parameter lists (correct)
    • Calling a method too many times
    • A subclass replacing a parent method
    • A method that returns several values
  32. What is method overriding?

    • Hiding a field in a subclass
    • Two methods with the same name in one class
    • Making a method final
    • A subclass providing its own version of a parent class method (correct)
  33. What does the static keyword mean for a method?

    • It runs only once
    • It is private by default
    • It cannot be changed
    • It belongs to the class, not to any object (correct)
  34. What does the super keyword refer to?

    • The main class
    • The outermost class in the file
    • The parent class of the current object (correct)
    • The current object
  35. What does the this keyword refer to?

    • The calling method
    • The class itself
    • The current object (correct)
    • The parent class
  36. Which statement about constructors is true?

    • They are called with the call keyword
    • A class can have only one
    • They must return void
    • They have the same name as the class and no return type (correct)
  37. Which collection does NOT allow duplicate elements?

    • HashSet (correct)
    • Vector
    • LinkedList
    • ArrayList
  38. Which collection stores key-value pairs?

    • HashMap (correct)
    • Stack
    • HashSet
    • ArrayList
  39. What is the main difference between ArrayList and a plain array?

    • An ArrayList can only hold primitives
    • Arrays cannot be indexed
    • An array can grow automatically
    • An ArrayList can grow and shrink in size (correct)
  40. How should you compare the contents of two String objects?

    • s1.compare(s2)
    • s1 === s2
    • s1 == s2
    • s1.equals(s2) (correct)
  41. Why are Java Strings called immutable?

    • Their content cannot be changed after creation (correct)
    • They are stored only on the stack
    • They cannot be compared
    • They cannot be assigned to variables
  42. Which class should you use to build a long string efficiently in a loop?

    • Character
    • StringBuilder (correct)
    • String
    • StringArray
  43. What is autoboxing?

    • Automatic conversion between a primitive and its wrapper class (correct)
    • Converting a class to an interface
    • Boxing objects for serialization
    • Packing classes into a JAR
  44. What happens when an exception is thrown and not caught anywhere?

    • The JVM retries the statement
    • Java ignores it
    • The program pauses until it is fixed
    • The thread ends and the stack trace is printed (correct)
  45. Which block always runs whether or not an exception occurs?

    • catch
    • finally (correct)
    • throw
    • default
  46. What is a checked exception?

    • An exception that cannot be caught
    • An exception the compiler forces you to handle or declare (correct)
    • An exception thrown by the JVM only
    • Any exception that has been caught
  47. Which is a checked exception?

    • ArrayIndexOutOfBoundsException
    • NullPointerException
    • ArithmeticException
    • IOException (correct)
  48. Which exception is thrown when you call a method on a null reference?

    • ClassCastException
    • NullPointerException (correct)
    • IllegalStateException
    • NumberFormatException
  49. What does the throws keyword do in a method signature?

    • Ignores exceptions
    • Declares exceptions the method may pass to its caller (correct)
    • Catches exceptions
    • Throws an exception immediately
  50. What is an abstract class?

    • A class that cannot be instantiated and may have abstract methods (correct)
    • A class with only static methods
    • A class with no fields
    • A class that cannot be extended
  51. Since Java 8, what can interfaces contain besides abstract methods?

    • Instance fields that can change
    • Private constructors
    • Constructors
    • Default and static methods (correct)
  52. Can a Java class extend more than one class?

    • Yes, any number
    • Only if they are abstract
    • Yes, but only two
    • No, Java supports single class inheritance (correct)
  53. Which is the parent of every class in Java?

    • Base
    • Class
    • Object (correct)
    • Main
  54. What does the instanceof operator check?

    • Whether a class exists
    • Whether a variable is initialized
    • Whether two objects are equal
    • Whether an object is an instance of a type (correct)
  55. What is encapsulation?

    • Inheriting from many classes
    • Hiding fields and exposing access through methods (correct)
    • Compiling code into bytecode
    • Running code in parallel
  56. What does the enhanced for loop for (int x : nums) do?

    • Visits only even indexes
    • Counts down through nums
    • Sorts nums first
    • Visits every element of nums in order (correct)
  57. What is the output of System.out.println(1 + 2 + "3");?

    • 15
    • 123
    • 6
    • 33 (correct)
  58. What is the output of System.out.println("1" + 2 + 3);?

    • 15
    • 33
    • 123 (correct)
    • 6
  59. What does a lambda expression like x -> x * 2 represent?

    • A generic type
    • An implementation of a functional interface (correct)
    • A loop
    • A new class declaration
  60. Which interface must a class implement to be used in a for-each loop?

    • Iterable (correct)
    • Comparable
    • Runnable
    • Serializable
  61. Which method of the Comparable interface must you implement?

    • compareTo (correct)
    • equals
    • sortBy
    • compare
  62. What does List.of(1, 2, 3) return?

    • An array
    • A mutable ArrayList
    • An unmodifiable list (correct)
    • A Set
  63. What is the purpose of generics, as in List<String>?

    • Type safety checked at compile time (correct)
    • Allowing primitive types in collections
    • Automatic sorting
    • Faster runtime execution
  64. Which keyword is used to create a thread-safe method that only one thread can run at a time on an object?

    • transient
    • synchronized (correct)
    • volatile
    • atomic
  65. Which method starts a new thread of execution?

    • begin()
    • start() (correct)
    • execute()
    • run()
  66. What happens if you call run() directly on a Thread object instead of start()?

    • A new thread is created anyway
    • An exception is thrown
    • A compile error
    • The code runs on the current thread, not a new one (correct)
  67. If you override equals(), which other method should you also override?

    • compareTo()
    • toString()
    • clone()
    • hashCode() (correct)
  68. What is the output of: Integer a = 127, b = 127; System.out.println(a == b);?

    • Compile error
    • false
    • It throws an exception
    • true (correct)
  69. What is the output of: Integer a = 128, b = 128; System.out.println(a == b);?

    • true
    • false (correct)
    • It throws an exception
    • Compile error
  70. What does the volatile keyword guarantee?

    • The variable is thread-safe for compound operations
    • The variable is not serialized
    • Reads and writes of the variable are visible to all threads (correct)
    • The variable cannot change
  71. What does the transient keyword do on a field?

    • Excludes it from default serialization (correct)
    • Makes it thread-safe
    • Stores it only on the stack
    • Makes it constant
  72. What is type erasure in Java generics?

    • Generic type information is removed at runtime (correct)
    • Type parameters are checked only at runtime
    • Unused types are deleted from the JAR
    • Generics are converted to arrays
  73. Which is true about a HashMap?

    • It keeps keys sorted
    • It allows one null key and many null values (correct)
    • It is synchronized
    • It keeps insertion order
  74. Which Map keeps its keys in sorted order?

    • LinkedHashMap
    • Hashtable
    • HashMap
    • TreeMap (correct)
  75. What does Stream.map() do?

    • Filters out elements
    • Transforms each element and returns a new stream (correct)
    • Sorts the stream
    • Collects the stream into a Map
  76. Which stream operation is terminal?

    • filter()
    • sorted()
    • collect() (correct)
    • map()
  77. What is the purpose of Optional<T>?

    • To make a field optional in a constructor
    • To allow nullable primitives
    • To skip a method call
    • To represent a value that may be absent without using null (correct)
  78. What does try-with-resources do?

    • Catches every exception type
    • Retries a block on failure
    • Runs code in a new thread
    • Automatically closes resources that implement AutoCloseable (correct)
  79. Where are objects created with new stored?

    • In the method area only
    • On the heap (correct)
    • On the stack
    • In CPU registers
  80. What does the garbage collector do?

    • Removes unused imports
    • Frees memory used by objects that are no longer reachable (correct)
    • Closes open files automatically
    • Deletes unused source files
  81. What is a record in modern Java (Java 16+)?

    • A type of collection
    • A mutable struct
    • A database row
    • A concise immutable data class with auto-generated accessors (correct)
  82. What is the difference between == and equals() for objects?

    • They are always identical
    • == compares references; equals() compares content as defined by the class (correct)
    • equals() only works for Strings
    • == compares content; equals() compares references