Test your fundamental knowledge of Java and Spring Boot with this beginner-friendly multiple-choice quiz. Covering core Java topics, collections, exceptions, streams, and basic Spring Boot concepts, this quiz is ideal for interview preparation and self-assessment.
What is a primary reason for using streams in Java when processing data collections?
Explanation: Streams in Java allow functional-style operations such as map, filter, and reduce on data collections, making code concise and expressive. Increasing array size is unrelated to streams, as arrays have fixed sizes. Streams do not directly manage or replace exceptions. Converting Java to JavaScript is not the purpose of streams.
Which type of stream operation returns a new stream and allows further operations, such as filtering or mapping?
Explanation: Intermediate operations like filter and map in Java Streams return a new stream, enabling operation chaining. Terminal operations, such as forEach or collect, end the stream's processing. 'Side operation' and 'Initial operation' are not standard terms in the Java Streams API.
What is the main difference between a List and a Set in Java?
Explanation: Lists in Java can contain duplicate elements, whereas Sets prevent duplicates. Ordering in Sets depends on the implementation and is not always by insertion order. Both Lists and Sets can store any object type, not just numbers or strings. The relative size of a Set and a List is determined by their contents, not their type.
Which class must your custom Java exception typically extend to create a checked exception?
Explanation: To create a checked exception, your custom exception should extend the Exception class. Extending Error creates system-related exceptions that should generally not be caught. Throwable is the direct parent of both Exception and Error, but it's not common to extend directly. RuntimeException is used for unchecked exceptions.
How can an Enum in Java be described?
Explanation: Enums are special Java classes that define a set of named constant values that do not change at runtime. They are not variable or random collections, nor are they restricted to strings or integers. Enums provide type safety over using constants directly.
Which Java keywords are used to handle exceptions in a try-catch block?
Explanation: The correct keywords for handling exceptions in Java are try and catch. 'Begin' and 'rescue' are not Java keywords, and 'throw' is used for throwing but not handling exceptions. 'Handle' and 'solve' are not part of Java syntax for exception management.
What is required for an interface to be considered a functional interface in Java?
Explanation: A functional interface in Java contains exactly one abstract method, enabling lambda expressions. The number of fields or constructors does not define a functional interface. Extending Exception is unrelated to interfaces.
What is a fundamental difference between heap and stack memory in Java?
Explanation: Objects and their instance variables are stored in heap memory, while method calls and local variables reside on the stack. Stack memory does not store classes or project files. Static variables can be part of the heap or method area, but not exclusively heap memory.
What does the @RestController annotation indicate in a Spring Boot application?
Explanation: @RestController allows a class to handle HTTP requests and return data, typically as JSON or XML. It is not specialized for file uploads only or restricted from returning JSON. Database configuration is managed elsewhere in Spring Boot.
What is the main purpose of the Optional class introduced in Java 8?
Explanation: The Optional class is designed to represent possible absence of a value and help avoid null pointer exceptions. It is not used for caching, encryption, or type conversion tasks.
What is the main goal of garbage collection in Java?
Explanation: Java garbage collection works by freeing memory allocated to objects that are no longer reachable, helping to prevent memory leaks. It does not sort data, manage CPU scheduling, or organize collections.
Why were default methods introduced in Java interfaces?
Explanation: Default methods let Java interfaces provide a default method body to support interface evolution without breaking existing code. Interfaces cannot have constructors, and default methods do not restrict or privatize method inheritance.
Which keyword is used to prevent a class from being subclassed in Java?
Explanation: The 'final' keyword prevents a class from being extended. 'Finally' relates to exception handling, 'finalise' is a common typo, and 'finish' is not a Java keyword.
Which method can help remove duplicate elements from a list using Java streams?
Explanation: The distinct() method returns a stream with duplicate elements eliminated. limit() restricts the number of elements, count() returns a total, and filter() selects elements based on a condition but does not guarantee duplicate removal.
What is polymorphism in the context of Java programming?
Explanation: Polymorphism allows objects to be referenced by their own class or any of their superclass types, enabling flexibility in code. It does not refer to class deletion, final variables, or limiting object creation.
In a Spring application, what is a bean?
Explanation: A bean is any object instantiated, assembled, and managed by the Spring Inversion of Control container. Beans are not synonymous with database patterns, keyword definitions, or classes limited to static methods.