Java Backend Fundamentals: Essential Interview Quiz Quiz

Test your knowledge of Java backend interview essentials, including Core Java, OOPs, Java 8 features, multithreading, SQL, and microservices. Prepare for Java interviews with beginner-friendly, real-world questions focused on core concepts and best practices.

  1. OOP Concepts

    Which of the following is NOT a core Object Oriented Programming concept in Java?

    1. Polymorphism
    2. Encapsulation
    3. Inheritance
    4. Compilation

    Explanation: Compilation is not an OOP concept; it's related to the process of converting source code into bytecode. Encapsulation, polymorphism, and inheritance are fundamental principles of object-oriented programming. Choosing 'Compilation' is correct while the others are integral to OOP.

  2. String Creation

    If you create two String objects with the same value using String literals, how many objects are actually created in memory?

    1. 1
    2. 3
    3. 2
    4. 0

    Explanation: Using String literals with the same value results in only one object in the string pool, as Java reuses string literals. Using 'new' keyword would create additional objects. Options '2' and '3' are incorrect as there’s only one object; '0' cannot be correct since at least one object is created.

  3. Mutable vs. Immutable Classes

    What is a key characteristic that makes a Java class immutable?

    1. All fields are public
    2. Uses public setters
    3. Implements Runnable
    4. Fields are final and private

    Explanation: Immutability means the state of an object cannot be changed after creation, which is enforced by making fields final and private. Public fields or setters allow modification, breaking immutability. Implementing Runnable does not relate to immutability, so only the correct answer fits.

  4. Marker Interfaces

    What is the main purpose of a marker interface in Java?

    1. To mark methods for deprecation
    2. To convey metadata through an empty interface
    3. To declare constant fields
    4. To provide default method implementations

    Explanation: Marker interfaces are empty and used to provide metadata, such as indicating a class has a special property. They do not mark methods for deprecation, declare constants, or provide default implementations. The other options describe unrelated functionalities.

  5. Exception Handling

    Which statement accurately describes the 'finally' block in Java exception handling?

    1. It can only follow a catch block, not a try block.
    2. It must always be present in a try-catch structure.
    3. It is executed only when an exception occurs.
    4. It is executed after try and any catch blocks, regardless of exception.

    Explanation: The 'finally' block executes after try-catch blocks whether an exception occurs or not. It’s not required in every try-catch, and it may follow a try directly. The first, third, and fourth options reflect misunderstandings of the 'finally' block’s behavior.

  6. Abstract Class vs Interface

    Which statement best describes the difference between an abstract class and an interface in Java?

    1. Interfaces can extend classes, but abstract classes cannot.
    2. An interface can have constructors, but an abstract class cannot.
    3. Only interfaces allow multiple inheritance of implementation.
    4. An abstract class can provide method implementations, while a pure interface cannot.

    Explanation: Abstract classes can include implemented methods, whereas interfaces initially could only declare methods (though default/static methods are now allowed). Interfaces cannot have constructors or extend classes, and neither supports multiple inheritance of implementation the same way.

  7. HashMap Storage

    On what basis does a HashMap in Java decide where to store a key-value pair internally?

    1. Value's hashCode value
    2. Key's hashCode value
    3. Key's length
    4. Value's toString output

    Explanation: HashMap uses the hashCode of the key to compute the storage index (bucket). Value's hashCode or toString output are irrelevant for determining position. Key's length is also not used; only the hashCode of the key matters.

  8. HashMap Overwriting

    What happens if you insert the same key into a HashMap with a new value?

    1. The previous value is replaced by the new value.
    2. It throws an exception.
    3. Both values will be stored in a list.
    4. It creates a duplicate entry.

    Explanation: Inserting an existing key replaces its value with the new one; previous data is overwritten. No exception is thrown, and no duplicate or list is created. The other options misunderstand the core behavior of maps.

  9. Fail-Fast Iterators

    Which feature distinguishes fail-fast iterators from fail-safe iterators in Java?

    1. Fail-fast iterators throw exceptions if the collection is modified.
    2. Fail-fast iterators create a copy of the collection for iteration.
    3. Fail-fast iterators can only be used with arrays.
    4. Fail-fast iterators never throw exceptions.

    Explanation: Fail-fast iterators detect structural modifications and throw exceptions. They do not create a copy, can be used with lists and other collections, and do in fact throw exceptions. The other options don't represent the unique behavior of fail-fast iterators.

  10. Java 8 Features

    Which of the following was introduced in Java 8?

    1. Applets
    2. Serialization
    3. Synchronized methods
    4. Functional interfaces and Lambda expressions

    Explanation: Functional interfaces and lambda expressions are key Java 8 features. Serialization and applets existed earlier, while synchronized methods have long been part of Java. Thus, the correct answer clearly highlights Java 8’s innovation.

  11. Stream API: Filtering

    In Java 8’s Stream API, what does the filter() method do?

    1. It sorts elements in a stream.
    2. It transforms elements to a different type.
    3. It removes null values only.
    4. It selects elements matching a condition.

    Explanation: filter() selects elements that satisfy a given predicate, leaving others out. It doesn’t sort, transform types (like map()), or only remove nulls. The other options oversimplify or misrepresent what filter actually does.

  12. Sorting with Streams

    Which intermediate method is used to sort a list using Java 8's Stream API?

    1. filter()
    2. reverse()
    3. sorted()
    4. findFirst()

    Explanation: The sorted() method is used for sorting elements in a stream. filter() is for conditional selection, reverse() does not exist in Stream API, and findFirst() is a terminal operation to find an element. Only sorted() correctly achieves sorting.

  13. Functional Interfaces

    What defines a functional interface in Java?

    1. Any interface with default methods
    2. An interface implementing Runnable
    3. An interface with exactly one abstract method
    4. A class with one method

    Explanation: A functional interface has only one abstract method, allowing instances to be provided by lambda expressions. Default methods don’t change that requirement. A class is not an interface, and implementing Runnable is just a use case, not a definition.

  14. Creating Threads

    How can you create a new thread in Java?

    1. By extending the Thread class or implementing Runnable interface
    2. By importing the utils package
    3. By overriding the toString method
    4. By creating a new interface

    Explanation: Threads are commonly created by extending Thread or implementing Runnable. Creating a new interface, overriding toString, or merely importing a package does not start a new thread. These alternatives do not relate to Java's threading model.

  15. SQL Query: LIKE

    Which SQL query will find all students whose names start with the letter 'A'?

    1. SELECT * FROM students WHERE name LIKE '%A';
    2. SELECT * FROM students WHERE name LIKE 'A%';
    3. SELECT * FROM students WHERE name = 'A';
    4. SELECT * FROM students WHERE name LIKE '_A';

    Explanation: Using 'LIKE 'A%'' finds names beginning with 'A'. Using '=' finds an exact match, '%A' looks for names ending with 'A', and '_A' looks for two-character names ending with 'A'. Only the correct answer matches the requirement.

  16. Spring Boot Application Start

    Which annotation is commonly used in the main class to start a Spring Boot application?

    1. @Entity
    2. @GetMapping
    3. @SpringBootApplication
    4. @ConfigurationProperties

    Explanation: @SpringBootApplication enables configuration and component scanning for launching Spring Boot apps. @GetMapping is used for mapping web requests, @Entity defines persistence objects, and @ConfigurationProperties relates to externalized configuration. Only the correct annotation is used to start the application.