Who originally developed the Java programming language?
- A. Bjarne Stroustrup at AT&T
- B. Guido van Rossum at CWI
- C. James Gosling at Sun Microsystems (correct)
- D. Dennis Ritchie at Bell Labs
Which method is the entry point of a Java application?
- A. public void start()
- B. public static void main(String[] args) (correct)
- C. public static void run(String args)
- D. static int main()
Java source code is compiled into what?
- A. Machine code for one CPU
- B. Assembly language
- C. Bytecode (correct)
- D. JavaScript
Which program runs Java bytecode?
- A. The Java linker
- B. The Java compiler (javac)
- C. The operating system shell
- D. The Java Virtual Machine (JVM) (correct)
What file extension do Java source files use?
- A. .class
- B. .js
- C. .jav
- D. .java (correct)
What file extension do compiled Java bytecode files use?
- A. .jar
- B. .java
- C. .class (correct)
- D. .exe
Which keyword is used to create an object in Java?
- A. make
- B. create
- C. new (correct)
- D. object
Which of these is NOT a primitive type in Java?
- A. boolean
- B. String (correct)
- C. int
- D. char
What is the default value of an int field in a Java class?
- A. 0 (correct)
- B. 1
- C. null
- D. undefined
What is the default value of an object reference field in Java?
- A. 0
- B. undefined
- C. null (correct)
- D. empty
Which keyword is used to inherit from a class in Java?
- A. super
- B. inherits
- C. implements
- D. extends (correct)
Which keyword is used by a class to use an interface?
- A. implements (correct)
- B. uses
- C. extends
- D. interface
How do you write a single-line comment in Java?
- A. # comment
- B. // comment (correct)
- C. -- comment
- D. <!-- comment -->
Which method prints a line of text to the console in Java?
- A. System.out.println() (correct)
- B. console.log()
- C. print()
- D. echo()
What is the size of an int in Java?
- A. 32 bits (correct)
- B. 16 bits
- C. It depends on the platform
- D. 64 bits
What is the size of a long in Java?
- A. 128 bits
- B. 16 bits
- C. 32 bits
- D. 64 bits (correct)
Which type stores a single character in Java?
- A. String
- B. Character only
- C. byte
- D. char (correct)
Which operator is used to compare two int values for equality?
- A. ===
- B. == (correct)
- C. equals
- D. =
What does the length of an array named arr give you?
- A. arr.count
- B. arr.size()
- C. arr.length()
- D. arr.length (correct)
What does the length of a String s give you?
- A. s.count()
- B. s.length() (correct)
- C. s.size()
- D. s.length
Which keyword stops a loop immediately in Java?
- A. exit
- B. break (correct)
- C. continue
- D. stop
Which keyword makes a variable's value unchangeable after it is assigned?
- A. final (correct)
- B. static
- C. immutable
- D. const
What is the index of the first element in a Java array?
- A. 1
- B. It depends on the array type
- C. -1
- D. 0 (correct)
Which access modifier makes a member visible everywhere?
- A. default
- B. public (correct)
- C. protected
- D. private
Which access modifier restricts a member to its own class?
- A. public
- B. private (correct)
- C. protected
- D. package
What is the result of 10 / 4 when both are int in Java?
- A. 2.0
- B. 3
- C. 2 (correct)
- D. 2.5
What is the result of 10 % 4 in Java?
- A. 0
- B. 2 (correct)
- C. 4
- D. 2.5
Which class is commonly used to read keyboard input in simple programs?
- A. Scanner (correct)
- B. Keyboard
- C. InputStream only
- D. Reader
Which data type is used for true/false values?
- A. bit
- B. bool
- C. Boolean only
- D. boolean (correct)
Which loop always runs its body at least once?
- A. while
- B. for-each
- C. for
- D. do-while (correct)
What is method overloading?
- A. Several methods with the same name but different parameter lists (correct)
- B. Calling a method too many times
- C. A subclass replacing a parent method
- D. A method that returns several values
What is method overriding?
- A. Hiding a field in a subclass
- B. Two methods with the same name in one class
- C. Making a method final
- D. A subclass providing its own version of a parent class method (correct)
What does the static keyword mean for a method?
- A. It runs only once
- B. It is private by default
- C. It cannot be changed
- D. It belongs to the class, not to any object (correct)
What does the super keyword refer to?
- A. The main class
- B. The outermost class in the file
- C. The parent class of the current object (correct)
- D. The current object
What does the this keyword refer to?
- A. The calling method
- B. The class itself
- C. The current object (correct)
- D. The parent class
Which statement about constructors is true?
- A. They are called with the call keyword
- B. A class can have only one
- C. They must return void
- D. They have the same name as the class and no return type (correct)
Which collection does NOT allow duplicate elements?
- A. HashSet (correct)
- B. Vector
- C. LinkedList
- D. ArrayList
Which collection stores key-value pairs?
- A. HashMap (correct)
- B. Stack
- C. HashSet
- D. ArrayList
What is the main difference between ArrayList and a plain array?
- A. An ArrayList can only hold primitives
- B. Arrays cannot be indexed
- C. An array can grow automatically
- D. An ArrayList can grow and shrink in size (correct)
How should you compare the contents of two String objects?
- A. s1.compare(s2)
- B. s1 === s2
- C. s1 == s2
- D. s1.equals(s2) (correct)
Why are Java Strings called immutable?
- A. Their content cannot be changed after creation (correct)
- B. They are stored only on the stack
- C. They cannot be compared
- D. They cannot be assigned to variables
Which class should you use to build a long string efficiently in a loop?
- A. Character
- B. StringBuilder (correct)
- C. String
- D. StringArray
What is autoboxing?
- A. Automatic conversion between a primitive and its wrapper class (correct)
- B. Converting a class to an interface
- C. Boxing objects for serialization
- D. Packing classes into a JAR
What happens when an exception is thrown and not caught anywhere?
- A. The JVM retries the statement
- B. Java ignores it
- C. The program pauses until it is fixed
- D. The thread ends and the stack trace is printed (correct)
Which block always runs whether or not an exception occurs?
- A. catch
- B. finally (correct)
- C. throw
- D. default
What is a checked exception?
- A. An exception that cannot be caught
- B. An exception the compiler forces you to handle or declare (correct)
- C. An exception thrown by the JVM only
- D. Any exception that has been caught
Which is a checked exception?
- A. ArrayIndexOutOfBoundsException
- B. NullPointerException
- C. ArithmeticException
- D. IOException (correct)
Which exception is thrown when you call a method on a null reference?
- A. ClassCastException
- B. NullPointerException (correct)
- C. IllegalStateException
- D. NumberFormatException
What does the throws keyword do in a method signature?
- A. Ignores exceptions
- B. Declares exceptions the method may pass to its caller (correct)
- C. Catches exceptions
- D. Throws an exception immediately
What is an abstract class?
- A. A class that cannot be instantiated and may have abstract methods (correct)
- B. A class with only static methods
- C. A class with no fields
- D. A class that cannot be extended
Since Java 8, what can interfaces contain besides abstract methods?
- A. Instance fields that can change
- B. Private constructors
- C. Constructors
- D. Default and static methods (correct)
Can a Java class extend more than one class?
- A. Yes, any number
- B. Only if they are abstract
- C. Yes, but only two
- D. No, Java supports single class inheritance (correct)
Which is the parent of every class in Java?
- A. Base
- B. Class
- C. Object (correct)
- D. Main
What does the instanceof operator check?
- A. Whether a class exists
- B. Whether a variable is initialized
- C. Whether two objects are equal
- D. Whether an object is an instance of a type (correct)
What is encapsulation?
- A. Inheriting from many classes
- B. Hiding fields and exposing access through methods (correct)
- C. Compiling code into bytecode
- D. Running code in parallel
What does the enhanced for loop for (int x : nums) do?
- A. Visits only even indexes
- B. Counts down through nums
- C. Sorts nums first
- D. Visits every element of nums in order (correct)
What is the output of System.out.println(1 + 2 + "3");?
- A. 15
- B. 123
- C. 6
- D. 33 (correct)
What is the output of System.out.println("1" + 2 + 3);?
- A. 15
- B. 33
- C. 123 (correct)
- D. 6
What does a lambda expression like x -> x * 2 represent?
- A. A generic type
- B. An implementation of a functional interface (correct)
- C. A loop
- D. A new class declaration
Which interface must a class implement to be used in a for-each loop?
- A. Iterable (correct)
- B. Comparable
- C. Runnable
- D. Serializable
Which method of the Comparable interface must you implement?
- A. compareTo (correct)
- B. equals
- C. sortBy
- D. compare
What does List.of(1, 2, 3) return?
- A. An array
- B. A mutable ArrayList
- C. An unmodifiable list (correct)
- D. A Set
What is the purpose of generics, as in List<String>?
- A. Type safety checked at compile time (correct)
- B. Allowing primitive types in collections
- C. Automatic sorting
- D. Faster runtime execution
Which keyword is used to create a thread-safe method that only one thread can run at a time on an object?
- A. transient
- B. synchronized (correct)
- C. volatile
- D. atomic
Which method starts a new thread of execution?
- A. begin()
- B. start() (correct)
- C. execute()
- D. run()
What happens if you call run() directly on a Thread object instead of start()?
- A. A new thread is created anyway
- B. An exception is thrown
- C. A compile error
- D. The code runs on the current thread, not a new one (correct)
If you override equals(), which other method should you also override?
- A. compareTo()
- B. toString()
- C. clone()
- D. hashCode() (correct)
What is the output of: Integer a = 127, b = 127; System.out.println(a == b);?
- A. Compile error
- B. false
- C. It throws an exception
- D. true (correct)
What is the output of: Integer a = 128, b = 128; System.out.println(a == b);?
- A. true
- B. false (correct)
- C. It throws an exception
- D. Compile error
What does the volatile keyword guarantee?
- A. The variable is thread-safe for compound operations
- B. The variable is not serialized
- C. Reads and writes of the variable are visible to all threads (correct)
- D. The variable cannot change
What does the transient keyword do on a field?
- A. Excludes it from default serialization (correct)
- B. Makes it thread-safe
- C. Stores it only on the stack
- D. Makes it constant
What is type erasure in Java generics?
- A. Generic type information is removed at runtime (correct)
- B. Type parameters are checked only at runtime
- C. Unused types are deleted from the JAR
- D. Generics are converted to arrays
Which is true about a HashMap?
- A. It keeps keys sorted
- B. It allows one null key and many null values (correct)
- C. It is synchronized
- D. It keeps insertion order
Which Map keeps its keys in sorted order?
- A. LinkedHashMap
- B. Hashtable
- C. HashMap
- D. TreeMap (correct)
What does Stream.map() do?
- A. Filters out elements
- B. Transforms each element and returns a new stream (correct)
- C. Sorts the stream
- D. Collects the stream into a Map
Which stream operation is terminal?
- A. filter()
- B. sorted()
- C. collect() (correct)
- D. map()
What is the purpose of Optional<T>?
- A. To make a field optional in a constructor
- B. To allow nullable primitives
- C. To skip a method call
- D. To represent a value that may be absent without using null (correct)
What does try-with-resources do?
- A. Catches every exception type
- B. Retries a block on failure
- C. Runs code in a new thread
- D. Automatically closes resources that implement AutoCloseable (correct)
Where are objects created with new stored?
- A. In the method area only
- B. On the heap (correct)
- C. On the stack
- D. In CPU registers
What does the garbage collector do?
- A. Removes unused imports
- B. Frees memory used by objects that are no longer reachable (correct)
- C. Closes open files automatically
- D. Deletes unused source files
What is a record in modern Java (Java 16+)?
- A. A type of collection
- B. A mutable struct
- C. A database row
- D. A concise immutable data class with auto-generated accessors (correct)
What is the difference between == and equals() for objects?
- A. They are always identical
- B. == compares references; equals() compares content as defined by the class (correct)
- C. equals() only works for Strings
- D. == compares content; equals() compares references