Essential Java Interview Questions for Beginners Quiz

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.

  1. Java Streams Basics

    What is a primary reason for using streams in Java when processing data collections?

    1. To convert Java code to JavaScript
    2. To increase the size of an array
    3. To replace all exceptions in the code
    4. To enable functional-style operations on sequences of elements

    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.

  2. Stream Operations

    Which type of stream operation returns a new stream and allows further operations, such as filtering or mapping?

    1. Intermediate operation
    2. Initial operation
    3. Terminal operation
    4. Side operation

    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.

  3. Collections Fundamentals

    What is the main difference between a List and a Set in Java?

    1. A Set has elements in insertion order; a List does not.
    2. A List allows duplicate elements; a Set does not.
    3. A List is for numbers, and a Set is for strings.
    4. A Set is always bigger than a List.

    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.

  4. Custom Exceptions

    Which class must your custom Java exception typically extend to create a checked exception?

    1. Exception
    2. Error
    3. Throwable
    4. RuntimeException

    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.

  5. Enums in Java

    How can an Enum in Java be described?

    1. A special class with a fixed set of constant values
    2. A random collection of strings
    3. A type that changes values at runtime
    4. A list of integers only

    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.

  6. Exception Handling

    Which Java keywords are used to handle exceptions in a try-catch block?

    1. throw and error
    2. begin and rescue
    3. try and catch
    4. handle and solve

    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.

  7. Functional Interfaces

    What is required for an interface to be considered a functional interface in Java?

    1. It must have exactly one abstract method
    2. It can contain only static fields
    3. It must extend the Exception class
    4. It must have at least three constructors

    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.

  8. Memory in Java

    What is a fundamental difference between heap and stack memory in Java?

    1. Stack memory contains all the files in a project.
    2. Heap memory is used only for static variables.
    3. Stack memory stores classes; heap memory stores method names.
    4. Heap memory stores objects; stack memory stores method calls and local variables.

    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.

  9. Spring Boot Basics

    What does the @RestController annotation indicate in a Spring Boot application?

    1. The class is for database configuration only
    2. The class cannot return JSON data
    3. The class handles HTTP requests and returns data directly
    4. The class will only handle file uploads

    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.

  10. Java Optional Class

    What is the main purpose of the Optional class introduced in Java 8?

    1. To avoid null pointer exceptions by representing optional values
    2. To cache objects for performance
    3. To encrypt user passwords
    4. To convert strings to integers

    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.

  11. Java Garbage Collection

    What is the main goal of garbage collection in Java?

    1. To schedule threads for execution
    2. To sort user data in an application
    3. To organize collections alphabetically
    4. To automatically free memory occupied by unreachable objects

    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.

  12. Default Methods

    Why were default methods introduced in Java interfaces?

    1. To make all interface methods private
    2. To restrict inheritance of methods
    3. To allow interfaces to provide method implementations
    4. To allow interfaces to have constructors

    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.

  13. Java Keywords

    Which keyword is used to prevent a class from being subclassed in Java?

    1. finish
    2. final
    3. finally
    4. finalise

    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.

  14. Handling Duplicates

    Which method can help remove duplicate elements from a list using Java streams?

    1. count()
    2. limit()
    3. distinct()
    4. filter()

    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.

  15. Polymorphism in Java

    What is polymorphism in the context of Java programming?

    1. The process of deleting classes
    2. The restriction of object creation
    3. The use of only final variables
    4. The ability of an object to take on many forms

    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.

  16. Bean Definition

    In a Spring application, what is a bean?

    1. A keyword to define public variables
    2. A class that only contains static methods
    3. A design pattern for database connections
    4. An object managed by the Spring IoC container

    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.