Java Developer Interview Basics Quiz Quiz

Test your foundational Java knowledge with this easy quiz tailored for entry-level Java developer interviews. Covering core Java concepts and practical scenarios, this quiz helps candidates prepare for real-world coding assessments and technical interview questions.

  1. String Reversal and Immutability

    Which Java class would you typically use to reverse a String while maintaining efficiency, such as when asked to reverse words in a sentence?

    1. StringTokenizer
    2. String
    3. Scanner
    4. StringBuilder

    Explanation: StringBuilder is efficient for modifying strings, including reversing their contents. While String is immutable and cannot be changed in place, StringBuilder provides a reverse() method for this purpose. Scanner is used for input reading, and StringTokenizer is mainly for splitting a string into tokens, not reversing it. Therefore, StringBuilder is the most suitable option.

  2. Data Structures for Fast Lookup

    Which Java collection is best suited for quickly checking if a value exists, such as counting unique words in a sentence?

    1. TreeMap
    2. ArrayList
    3. HashSet
    4. Vector

    Explanation: HashSet provides constant-time performance for lookups, making it ideal for checking uniqueness or existence of values. ArrayList allows duplicates and requires linear time for lookups. TreeMap stores key-value pairs in sorted order and isn't a collection of standalone values. Vector is like ArrayList but is synchronized and also permits duplicates, making HashSet the best choice.

  3. Primitive vs. Reference Types

    Which is a primitive data type in Java that cannot hold null values?

    1. Float
    2. Integer
    3. int
    4. String

    Explanation: The int type is a Java primitive, and primitives cannot be assigned null—they always have a default value when declared. Integer and Float are wrapper classes for primitives and can be set to null since they are objects. String is also an object type, so it can be null. Only int among these is a primitive.

  4. Looping through Arrays

    What is the most concise and type-safe way to iterate over all elements in a Java array?

    1. Enhanced for loop
    2. While loop
    3. Goto statement
    4. Do-while loop

    Explanation: The enhanced for loop (for-each loop) allows for clean and type-safe iteration over arrays. While and do-while loops require manual handling of indices and can lead to errors. Java does not have a goto statement, making it invalid here. The enhanced for loop is both concise and less error-prone.

  5. Java Keywords

    Which keyword is used to prevent other classes from inheriting a Java class?

    1. final
    2. private
    3. abstract
    4. static

    Explanation: The 'final' keyword prevents a class from being subclassed. The 'static' keyword is used for class-level variables and methods. 'Private' limits accessibility but does not control inheritance. 'Abstract' allows a class to be a base for others but does not restrict inheritance. Only 'final' fits this requirement.

  6. Default Values

    What is the default value of a boolean variable in a Java class when not initialized?

    1. false
    2. true
    3. 0
    4. null

    Explanation: Boolean instance variables in Java are initialized to false by default. Unlike objects, they do not take a null value. 0 is the default for numeric types, not for booleans. 'True' is not correct as the default is always 'false' for booleans.

  7. Java Exception Handling

    Which Java block is always executed after try and catch blocks, even if an exception or return statement occurs?

    1. assert
    2. throw
    3. finally
    4. catch

    Explanation: The finally block is always executed after try and catch, regardless of exceptions or returns. The catch block only runs if an exception occurs, throw is used to manually throw exceptions, and assert is for testing assumptions in code. Therefore, only 'finally' guarantees execution.

  8. Method Overloading

    What does it mean if two methods in the same class have the same name but different parameter lists?

    1. Method overloading
    2. Polymorphing
    3. Refactoring
    4. Method overriding

    Explanation: Method overloading refers to having multiple methods with the same name but different parameter lists in the same class. Method overriding involves changing the behavior of a parent class method in a subclass. Polymorphing is not a Java term, and refactoring means restructuring code. Overloading is correct.

  9. Access Modifiers

    Which access modifier makes a Java method accessible only within its own class?

    1. protected
    2. private
    3. default
    4. public

    Explanation: The private access modifier restricts method visibility to within the same class. Protected allows access within subclasses and the same package. Public makes the method accessible everywhere. The default (no modifier) allows access within the same package. 'Private' is the strictest.

  10. NullPointerException Basics

    What typically happens if you try to call a method on a Java object reference that is null?

    1. NullPointerException
    2. ClassCastException
    3. ArrayIndexOutOfBoundsException
    4. StackOverflowError

    Explanation: Calling a method on a null object reference triggers a NullPointerException. StackOverflowError occurs due to excessive recursion or stack usage. ClassCastException is related to improper casting between types. ArrayIndexOutOfBoundsException happens when accessing invalid array indices. NullPointerException matches the described scenario.

  11. Comparing Strings

    When comparing the contents of two String objects in Java for equality, which method should you use?

    1. hashCode()
    2. == operator
    3. equals()
    4. compareTo()

    Explanation: The equals() method compares String contents for equality. The == operator compares object references, not their actual values. compareTo() checks lexicographical order rather than equality. hashCode() returns an integer representation, not used for direct comparison. equals() is correct for value comparison.

  12. Thread Basics

    Which Java interface must be implemented to create a thread by defining its run logic?

    1. Runnable
    2. Serializable
    3. Readable
    4. Cloneable

    Explanation: The Runnable interface requires implementation of the run() method, which defines thread logic. Serializable and Cloneable are marker interfaces for object serialization and cloning, respectively. Readable is unrelated to thread creation. Runnable is the correct answer for creating basic threads.

  13. Class Paths and Packages

    Which statement best describes a package in Java?

    1. It is a loop structure
    2. It is a special method
    3. It is a namespace for organizing classes
    4. It is an error type

    Explanation: A package in Java is used for grouping related classes and interfaces, acting as a namespace. It is not a loop structure; control structures like while or for serve that purpose. An error type refers to classes representing errors and exceptions. A package is neither an error nor a method.

  14. Object-Oriented Principles

    Which object-oriented programming principle is demonstrated when a subclass provides its own implementation for a superclass method?

    1. Casting
    2. Initialization
    3. Encapsulation
    4. Polymorphism

    Explanation: Polymorphism allows a subclass to define its own behavior for methods declared in the superclass. Encapsulation is about hiding internal data with accessors and mutators. Casting deals with type conversion, and initialization concerns creating or setting up objects. Only polymorphism fits method overriding.

  15. Type Casting

    If you have a variable of type double and want to assign it to an int variable in Java, which operation should you use?

    1. Explicit casting
    2. Implicit casting
    3. Autoboxing
    4. Boxing

    Explanation: Converting from double to int is a narrowing conversion in Java, requiring explicit casting such as (int) value. Implicit casting is used for widening conversions, like int to double. Boxing and autoboxing refer to converting between primitives and their wrapper classes. Explicit casting is needed here.

  16. Interfaces vs. Abstract Classes

    What distinguishes an interface from an abstract class in Java?

    1. Interfaces can be instantiated directly
    2. Abstract classes cannot have constructors
    3. Interfaces cannot have implemented instance methods
    4. Abstract classes must have only static methods

    Explanation: Traditionally, interfaces do not have implemented instance methods; all methods are abstract (prior to Java 8). However, abstract classes may contain both abstract and concrete methods. Interfaces cannot be instantiated, so option two is false. Abstract classes can have constructors and instance methods, making options three and four incorrect.