Unit I: Java Fundamentals
1. Which component is responsible for converting bytecode to machine code?
Show Answer
c) JVM - The Java Virtual Machine interprets/compiles bytecode to machine code at runtime.
2. What is the size of int data type in Java?
Show Answer
b) 4 bytes - int is always 32 bits (4 bytes) in Java regardless of platform.
3. Which keyword is used to prevent method overriding?
Show Answer
b) final - A final method cannot be overridden in subclasses.
4. What is the default value of a boolean instance variable?
Show Answer
b) false - Boolean instance variables default to false.
5. Which operator is used to check if an object is an instance of a class?
Show Answer
b) instanceof - The instanceof operator checks object type at runtime.
6. Which access modifier makes a member visible only within the same package?
Show Answer
c) default - Package-private access when no modifier is specified.
7. What is the output of: System.out.println(10 + 20 + "Hello");
Show Answer
b) 30Hello - Left to right: 10+20=30, then 30+"Hello"="30Hello".
8. Which method is used to compare string contents?
Show Answer
c) equals() - The equals() method compares string content; == compares references.
9. Which class is mutable and thread-safe for string operations?
Show Answer
c) StringBuffer - StringBuffer is mutable and synchronized (thread-safe).
10. What is method overloading?
Show Answer
b) Same method name, different parameters in same class - Compile-time polymorphism.
11. Which loop is guaranteed to execute at least once?
Show Answer
c) do-while - Condition is checked after the loop body executes.
12. How many objects can a Java class extend?
Show Answer
b) 1 - Java supports single inheritance for classes.
13. What does JDK stand for?
Show Answer
a) Java Development Kit - Contains JRE + development tools (compiler, debugger).
14. Which keyword is used to create a constant in Java?
Show Answer
b) final - final variables cannot be reassigned. const is reserved but not used.
15. What is the index of the first element in an array?
Show Answer
b) 0 - Java arrays are zero-indexed.
16. What is the output of: int x = 5; System.out.println(x++ + ++x);
Show Answer
c) 12 - x++ returns 5 (then x=6), ++x returns 7 (x=7). 5+7=12.
17. Which method must be defined in every Java application?
Show Answer
b) main() - public static void main(String[] args) is the entry point.
18. What is the size of char data type in Java?
Show Answer
b) 2 bytes - char uses Unicode and is 16 bits (2 bytes).
19. Which of these is NOT a valid primitive data type?
Show Answer
c) String - String is a class (reference type), not a primitive.
20. What does the 'static' keyword mean when applied to a method?
Show Answer
a) The method can be called without creating an object - Static methods belong to the class, not instances.
21. What is the difference between '==' and '.equals()' for Strings?
Show Answer
b) == compares references, equals() compares content - Always use equals() for string comparison.
22. Which loop is guaranteed to execute at least once?
Show Answer
c) do-while loop - Checks condition after execution, so runs at least once.
23. What is the output of: System.out.println(10 / 3);
Show Answer
a) 3 - Integer division truncates the decimal part.
24. Which keyword is used to inherit a class?
Show Answer
b) extends - Used for class inheritance (implements is for interfaces).
25. What is encapsulation?
Show Answer
d) All of the above - Encapsulation involves bundling data with methods and restricting direct access.
Unit II: Exception Handling, I/O, Multithreading
1. Which is the parent class of all exceptions?
Show Answer
c) Throwable - Both Exception and Error extend Throwable.
2. Which block always executes regardless of exception?
Show Answer
c) finally - Executes even if exception occurs (except System.exit()).
3. Which keyword is used to declare that a method may throw an exception?
Show Answer
b) throws - Used in method signature; throw is used to actually throw an exception.
4. Which exception is checked at compile time?
Show Answer
c) IOException - Checked exceptions must be handled or declared.
5. Which stream is used to read bytes from a file?
Show Answer
b) FileInputStream - Byte stream for reading files; FileReader is for characters.
6. Which method is used to start a thread?
Show Answer
b) start() - Calling run() directly doesn't create a new thread.
7. Which interface must be implemented to create a thread?
Show Answer
b) Runnable - Contains single run() method; alternative to extending Thread.
8. What is the default priority of a thread?
Show Answer
b) 5 - Thread.NORM_PRIORITY = 5 (range is 1-10).
9. Which keyword is used to prevent race conditions?
Show Answer
b) synchronized - Ensures only one thread accesses critical section at a time.
10. Which method releases the lock and waits?
Show Answer
b) wait() - Releases lock and waits; sleep() doesn't release lock.
11. Which class is used for buffered character reading?
Show Answer
b) BufferedReader - Character stream with buffering; provides readLine().
12. What happens if you call run() instead of start()?
Show Answer
b) Method runs in current thread - No new thread is spawned.
13. Which method wakes up all waiting threads?
Show Answer
b) notifyAll() - Wakes all threads; notify() wakes only one.
14. try-with-resources requires resources to implement which interface?
Show Answer
d) Both a and b - Closeable extends AutoCloseable; both work.
15. Which thread state means the thread is waiting for a lock?
Show Answer
b) BLOCKED - Waiting to acquire a monitor lock.
16. What type of exception is NullPointerException?
Show Answer
b) Unchecked exception - Extends RuntimeException.
17. Can a method declare multiple exceptions in throws clause?
Show Answer
b) Yes, separated by commas - public void method() throws IOException, SQLException
18. What happens if finally block throws an exception?
Show Answer
b) Finally block exception is thrown, try/catch exceptions are lost - Finally block exception overrides.
19. Which exception occurs when parsing invalid string to number?
Show Answer
c) NumberFormatException - When Integer.parseInt("abc") fails.
20. What is the difference between FileInputStream and FileReader?
Show Answer
b) FileInputStream for bytes, FileReader for characters - Byte stream vs Character stream.
Unit III: Modern Java Features
1. How many abstract methods can a functional interface have?
Show Answer
b) 1 - Exactly one abstract method; can have default/static methods.
2. What is the correct lambda syntax for no parameters?
Show Answer
b) () -> expression - Empty parentheses required for no parameters.
3. Which built-in functional interface returns boolean?
Show Answer
c) Predicate - test() method returns boolean.
4. What is the method reference syntax for static methods?
Show Answer
b) ClassName::methodName - Double colon operator for method references.
5. Which stream operation is terminal?
Show Answer
d) collect() - Terminal operations produce results and end the stream.
6. Which stream operation removes duplicates?
Show Answer
b) distinct() - Returns stream with unique elements.
7. What does var keyword do in Java?
Show Answer
b) Local variable type inference - Compiler infers type from initializer.
8. Which Java version introduced Records?
Show Answer
d) Java 16 - Records became standard in Java 16 (preview in 14/15).
9. What keyword is used in switch expressions to return a value from a block?
Show Answer
b) yield - Used in switch expression blocks to return value.
10. Text blocks use which delimiter?
Show Answer
a) """ - Triple double quotes for multi-line text blocks.
11. Which annotation marks a functional interface?
Show Answer
b) @FunctionalInterface - Optional but recommended annotation.
12. What does sealed class restrict?
Show Answer
c) Which classes can extend it - Uses permits keyword to list allowed subclasses.
13. Which stream method transforms elements?
Show Answer
b) map() - Transforms each element using a function.
14. Records are implicitly:
Show Answer
b) final - Records cannot be extended.
15. Base64.getEncoder() is used for:
Show Answer
b) Encoding to Base64 - Converts bytes to Base64 string.
Unit IV: Collections Framework
1. Which collection does not allow duplicates?
Show Answer
c) HashSet - Set interface implementations don't allow duplicates.
2. Which collection maintains insertion order and allows duplicates?
Show Answer
c) ArrayList - List maintains order and allows duplicates.
3. Which Map implementation maintains sorted order?
Show Answer
c) TreeMap - Sorted by keys using natural ordering or Comparator.
4. Which collection is synchronized by default?
Show Answer
c) Vector - Legacy class; thread-safe but slower.
5. What is the time complexity of ArrayList.get(index)?
Show Answer
a) O(1) - Direct index access in array.
6. Iterator's remove() method removes:
Show Answer
c) Current element - The last element returned by next().
7. Which interface is used for natural ordering?
Show Answer
b) Comparable - Defines compareTo() for natural ordering.
8. HashMap allows how many null keys?
Show Answer
b) 1 - One null key, multiple null values allowed.
9. Which collection is best for frequent insertions/deletions?
Show Answer
b) LinkedList - O(1) insertion/deletion at known position.
10. PriorityQueue orders elements by:
Show Answer
b) Natural ordering or Comparator - Heap-based priority queue.
11. Which method of Comparable interface must be implemented?
Show Answer
b) compareTo() - Returns negative, zero, or positive integer.
12. LinkedHashSet maintains:
Show Answer
b) Insertion order - HashSet with linked list for ordering.
13. Hashtable vs HashMap - which allows null?
Show Answer
c) Only HashMap - Hashtable throws NullPointerException for null key/value.
14. Which Queue method removes and returns head, returning null if empty?
Show Answer
b) poll() - remove() throws exception if empty.
15. Properties class extends:
Show Answer
b) Hashtable - Legacy class for configuration properties.
Unit V: Spring Framework Concepts
1. What does IoC stand for?
Show Answer
b) Inversion of Control - Container manages object creation and dependencies.
2. Which is the recommended type of dependency injection?
Show Answer
c) Constructor injection - Promotes immutability and clear dependencies.
3. What is the default bean scope in Spring?
Show Answer
b) singleton - One instance per Spring container.
4. Which annotation marks a class as a Spring-managed service?
Show Answer
d) Both a and b - @Service is a specialization of @Component.
5. AOP is used for handling:
Show Answer
b) Cross-cutting concerns - Logging, security, transactions, etc.
6. Which advice type wraps the join point?
Show Answer
c) @Around - Can control if/when the method executes.
7. @Autowired performs injection by:
Show Answer
b) Type - Matches by type; use @Qualifier for name.
8. Which annotation is used for initialization callback?
Show Answer
b) @PostConstruct - Called after dependency injection.
9. @RestController is equivalent to:
Show Answer
b) @Controller + @ResponseBody - Automatically serializes return values.
10. Which HTTP method is used to create a new resource?
Show Answer
b) POST - Creates new resource; PUT updates existing.
11. @SpringBootApplication combines which annotations?
Show Answer
a) @Configuration, @EnableAutoConfiguration, @ComponentScan
12. Which scope creates a new bean for each HTTP request?
Show Answer
c) request - Web-scoped bean; one per HTTP request.
13. @PathVariable extracts value from:
Show Answer
c) URI path - e.g., /users/{id} extracts id.
14. Which runner interface receives raw command line arguments?
Show Answer
b) CommandLineRunner - Receives String... args.
15. @Qualifier is used when:
Show Answer
b) Multiple beans of same type exist - Specifies which bean to inject.