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.
Which Java class would you typically use to reverse a String while maintaining efficiency, such as when asked to reverse words in a sentence?
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.
Which Java collection is best suited for quickly checking if a value exists, such as counting unique words in a sentence?
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.
Which is a primitive data type in Java that cannot hold null values?
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.
What is the most concise and type-safe way to iterate over all elements in a Java array?
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.
Which keyword is used to prevent other classes from inheriting a Java class?
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.
What is the default value of a boolean variable in a Java class when not initialized?
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.
Which Java block is always executed after try and catch blocks, even if an exception or return statement occurs?
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.
What does it mean if two methods in the same class have the same name but different parameter lists?
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.
Which access modifier makes a Java method accessible only within its own class?
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.
What typically happens if you try to call a method on a Java object reference that is null?
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.
When comparing the contents of two String objects in Java for equality, which method should you use?
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.
Which Java interface must be implemented to create a thread by defining its run logic?
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.
Which statement best describes a package in Java?
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.
Which object-oriented programming principle is demonstrated when a subclass provides its own implementation for a superclass method?
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.
If you have a variable of type double and want to assign it to an int variable in Java, which operation should you use?
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.
What distinguishes an interface from an abstract class in Java?
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.