Q1. What is Java primarily classified as?
Think classes, objects, and JVM.
Answer: A high-level, object-oriented programming language
Explanation: Java is designed around classes and objects and runs on the JVM.
Explanation: Java is designed around classes and objects and runs on the JVM.
Q2. Which tool compiles Java source code into bytecode?
The Java compiler command.
Answer: javac
Explanation: `javac` compiles `.java` files into `.class` bytecode for the JVM.
Explanation: `javac` compiles `.java` files into `.class` bytecode for the JVM.
Q3. Which method signature is the standard Java entry point?
It must be public and static.
Answer: public static void main(String[] args)
Explanation: JVM looks for this exact signature to start the program.
Explanation: JVM looks for this exact signature to start the program.
Q4. Which primitive type stores true/false?
Two possible values only.
Answer: boolean
Explanation: boolean holds true or false in Java.
Explanation: boolean holds true or false in Java.
Q5. Which keyword declares a class?
Java uses this to define types.
Answer: class
Explanation: Use `class MyClass { ... }` to define a class in Java.
Explanation: Use `class MyClass { ... }` to define a class in Java.
Q6. Which collection allows duplicate elements and preserves insertion order?
Think ordered list structure.
Answer: ArrayList
Explanation: ArrayList keeps insertion order and allows duplicates.
Explanation: ArrayList keeps insertion order and allows duplicates.
Q7. How do you create an object of class Person?
Person p = new ();
Use the class name with new.
Answer: Person
Explanation: `Person p = new Person();` constructs a new instance.
Explanation: `Person p = new Person();` constructs a new instance.
Q8. Which keyword prevents a method from being overridden?
It also prevents subclass modification of variables.
Answer: final
Explanation: final methods cannot be overridden by subclasses.
Explanation: final methods cannot be overridden by subclasses.
Q9. Which access modifier allows visibility only within the same package?
No keyword is used for this level.
Answer: default (no modifier)
Explanation: When no access modifier is specified, members are package-private.
Explanation: When no access modifier is specified, members are package-private.
Q10. Which statement compares string contents correctly?
Use the String API for content equality.
Answer: if (s1.equals(s2))
Explanation: equals compares string contents; == compares references.
Explanation: equals compares string contents; == compares references.
Q11. Which package contains ArrayList and HashMap?
Common collections live here.
Answer: java.util
Explanation: Collections framework classes are in java.util package.
Explanation: Collections framework classes are in java.util package.
Q12. Which loop is best when number of iterations is known?
Use a counter-based loop.
Answer: for loop
Explanation: for loops are ideal when iteration count is known ahead of time.
Explanation: for loops are ideal when iteration count is known ahead of time.
Q13. Which keyword indicates a method must be implemented by subclasses?
Used in abstract classes and methods.
Answer: abstract
Explanation: abstract methods declare a contract that subclasses must implement.
Explanation: abstract methods declare a contract that subclasses must implement.
Q14. Which collection disallows duplicate elements?
Unique elements only.
Answer: Set
Explanation: Set implementations enforce uniqueness of elements.
Explanation: Set implementations enforce uniqueness of elements.
Q15. Which statement catches exceptions thrown in a try block?
Use catch blocks to handle exceptions.
Answer: try { } catch (Exception e) { }
Explanation: catch handles exceptions thrown in the try block.
Explanation: catch handles exceptions thrown in the try block.
Q16. Which class reads tokens from System.in?
Commonly used for console input in simple programs.
Answer: Scanner
Explanation: `new Scanner(System.in)` reads tokens and lines from console input.
Explanation: `new Scanner(System.in)` reads tokens and lines from console input.
Q17. Which keyword creates inheritance from a superclass?
Used for class-to-class inheritance.
Answer: extends
Explanation: `class Child extends Parent` creates an inheritance relationship.
Explanation: `class Child extends Parent` creates an inheritance relationship.
Q18. Which keyword is used by a class to implement an interface?
A class can implement multiple interfaces.
Answer: implements
Explanation: `class C implements I1, I2` means C provides implementations for interface methods.
Explanation: `class C implements I1, I2` means C provides implementations for interface methods.
Q19. Which collection preserves insertion order for map entries?
Maintains insertion order of entries.
Answer: LinkedHashMap
Explanation: LinkedHashMap keeps entries in insertion order; HashMap does not guarantee order.
Explanation: LinkedHashMap keeps entries in insertion order; HashMap does not guarantee order.
Q20. Which operator compares object references?
Compares references, not content.
Answer: ==
Explanation: `==` checks whether two references point to the same object; `equals()` compares content if overridden.
Explanation: `==` checks whether two references point to the same object; `equals()` compares content if overridden.
Q21. Which class is used for mutable strings (not thread-safe)?
Faster alternative to StringBuffer.
Answer: StringBuilder
Explanation: StringBuilder is mutable and not synchronized; StringBuffer is synchronized (thread-safe).
Explanation: StringBuilder is mutable and not synchronized; StringBuffer is synchronized (thread-safe).
Q22. Which keyword declares a constant field?
Use final to prevent reassignment.
Answer: final
Explanation: `final` prevents reassignment; often combined with `static` for class-level constants.
Explanation: `final` prevents reassignment; often combined with `static` for class-level constants.
Q23. How do you convert String "123" to int?
Use Integer utility class.
Answer: Integer.parseInt("123")
Explanation: parseInt converts numeric string to primitive int.
Explanation: parseInt converts numeric string to primitive int.
Q24. Which interface represents a function that takes one argument and returns a result?
Part of java.util.function package.
Answer: Function
Explanation: Function takes T and returns R; Consumer consumes without returning a value.
Explanation: Function takes T and returns R; Consumer consumes without returning a value.
Q25. Which stream operation filters elements by a predicate?
Keeps only elements that match the predicate.
Answer: filter()
Explanation: filter applies a predicate and returns a stream of elements that satisfy it.
Explanation: filter applies a predicate and returns a stream of elements that satisfy it.
Q26. Which keyword ensures a block runs regardless of exceptions?
Used after try/catch blocks.
Answer: finally
Explanation: finally executes whether or not an exception occurred, typically for cleanup.
Explanation: finally executes whether or not an exception occurred, typically for cleanup.
Q27. Which collection is synchronized by default?
Older collection class with synchronized methods.
Answer: Vector
Explanation: Vector methods are synchronized; ArrayList is not synchronized by default.
Explanation: Vector methods are synchronized; ArrayList is not synchronized by default.
Q28. Which class provides thread-safe maps for concurrency?
Designed for concurrent access with better scalability.
Answer: ConcurrentHashMap
Explanation: ConcurrentHashMap supports concurrent access with fine-grained locking and better performance than Hashtable.
Explanation: ConcurrentHashMap supports concurrent access with fine-grained locking and better performance than Hashtable.
Q29. Which annotation indicates a method overrides a superclass method?
Helps the compiler check method signatures.
Answer: @Override
Explanation: @Override signals intent to override and enables compile-time checks.
Explanation: @Override signals intent to override and enables compile-time checks.
Q30. Which statement about generics is true?
Type erasure occurs at runtime.
Answer: Generics provide compile-time type safety
Explanation: Generics enforce types at compile time; type information is erased at runtime.
Explanation: Generics enforce types at compile time; type information is erased at runtime.
Q31. Which factory method creates an immutable list (Java 9+)?
Factory method introduced in Java 9.
Answer: List.of(1, 2, 3)
Explanation: List.of returns an immutable list; Arrays.asList returns a fixed-size list backed by an array.
Explanation: List.of returns an immutable list; Arrays.asList returns a fixed-size list backed by an array.
Q32. Which keyword declares a package-private member?
No explicit keyword is used.
Answer: (no modifier)
Explanation: When no access modifier is specified, the member is package-private.
Explanation: When no access modifier is specified, the member is package-private.
Q33. Which method on Optional returns a default value if empty?
Provides fallback value.
Answer: orElse()
Explanation: optional.orElse(defaultVal) returns defaultVal when Optional is empty.
Explanation: optional.orElse(defaultVal) returns defaultVal when Optional is empty.
Q34. Which statement creates a thread using a lambda (Java 8+)?
Lambda implements Runnable.
Answer: new Thread(() -> System.out.println("Hi")).start();
Explanation: Lambda provides Runnable implementation; start() runs the thread.
Explanation: Lambda provides Runnable implementation; start() runs the thread.
Q35. Which interface is used for key-value mapping?
Key-value pairs structure.
Answer: Map
Explanation: Map stores associations from keys to values (HashMap, TreeMap, LinkedHashMap).
Explanation: Map stores associations from keys to values (HashMap, TreeMap, LinkedHashMap).
Q36. Which Map implementation sorts keys by natural order?
Sorted map implementation.
Answer: TreeMap
Explanation: TreeMap sorts keys according to their natural ordering or a provided Comparator.
Explanation: TreeMap sorts keys according to their natural ordering or a provided Comparator.
Q37. Which keyword declares a method that cannot be overridden?
Prevents subclass modification of behavior.
Answer: final
Explanation: final methods cannot be overridden by subclasses.
Explanation: final methods cannot be overridden by subclasses.
Q38. Which statement about String immutability is correct?
Use StringBuilder for mutable sequences.
Answer: Strings are immutable; operations create new String objects
Explanation: String methods return new objects; StringBuilder/StringBuffer are mutable alternatives.
Explanation: String methods return new objects; StringBuilder/StringBuffer are mutable alternatives.
Q39. Which method returns the length of a string?
String API method.
Answer: length()
Explanation: `s.length()` returns the number of characters in the string.
Explanation: `s.length()` returns the number of characters in the string.
Q40. Which operator is used for string concatenation?
Operator overloaded for strings.
Answer: +
Explanation: The `+` operator concatenates strings in Java (creates a new String).
Explanation: The `+` operator concatenates strings in Java (creates a new String).
Q41. Which method splits a string into an array by regex?
Returns String[]
Answer: split()
Explanation: `s.split(regex)` splits the string into an array of substrings.
Explanation: `s.split(regex)` splits the string into an array of substrings.
Q42. Which keyword is used to create a new instance?
Used before constructor call.
Answer: new
Explanation: `new ClassName()` allocates and initializes a new object instance.
Explanation: `new ClassName()` allocates and initializes a new object instance.
Q43. Which collection is best for fast membership tests (no duplicates)?
Hash-based lookup is O(1) average.
Answer: HashSet
Explanation: HashSet provides fast average-time membership checks and enforces uniqueness.
Explanation: HashSet provides fast average-time membership checks and enforces uniqueness.
Q44. Which keyword declares a method that cannot be overridden and belongs to the class?
Belongs to the class rather than instance.
Answer: static
Explanation: `static` methods belong to the class; they are not instance methods and cannot be overridden (they can be hidden).
Explanation: `static` methods belong to the class; they are not instance methods and cannot be overridden (they can be hidden).
Q45. Which statement about garbage collection is true?
JVM manages memory automatically.
Answer: Garbage collector reclaims memory of unreachable objects
Explanation: JVM's GC frees memory for objects no longer referenced; timing is non-deterministic.
Explanation: JVM's GC frees memory for objects no longer referenced; timing is non-deterministic.
Q46. Which interface represents a predicate (boolean-valued function)?
Returns boolean result.
Answer: Predicate
Explanation: Predicate represents a boolean-valued function of one argument.
Explanation: Predicate
Q47. Which method on Stream collects elements into a List?
Terminal operation that gathers results.
Answer: collect(Collectors.toList())
Explanation: `collect(Collectors.toList())` gathers stream elements into a List.
Explanation: `collect(Collectors.toList())` gathers stream elements into a List.
Q48. Which statement creates an immutable map (Java 9+)?
Factory method introduced in Java 9.
Answer: Map.of("a",1,"b",2)
Explanation: `Map.of` creates an immutable map; `Map.copyOf` also creates an unmodifiable copy.
Explanation: `Map.of` creates an immutable map; `Map.copyOf` also creates an unmodifiable copy.
Q49. Which keyword marks a method as synchronized for thread safety?
Used to control access to methods/blocks.
Answer: synchronized
Explanation: `synchronized` ensures only one thread executes the method/block at a time for the given monitor.
Explanation: `synchronized` ensures only one thread executes the method/block at a time for the given monitor.
Q50. Which file declares module dependencies in Java 9+?
Module descriptor filename.
Answer: module-info.java
Explanation: `module-info.java` declares exported packages and required modules for the module system.
Explanation: `module-info.java` declares exported packages and required modules for the module system.
Your Score: 0 / 50
0 Comments