Test your proficiency in Java with this engaging quiz featuring medium-level questions based on frequently asked Java interview topics, including OOP concepts, data types, access specifiers, and loop mechanisms.
In Java, what is the main difference between an inner class and a sub-class?
Explanation: An inner class is defined within another class and has access to all its members, while a sub-class extends another class and gains its public and protected members. The other options confuse inheritance and nesting or misrepresent access rules for sub-classes and inner classes.
Which access specifier allows a field to be accessible only within its own class in Java?
Explanation: Private limits access to within the defining class only, making it the most restrictive. Default allows access within the same package, protected extends access to subclasses, and public allows universal access, so these do not match the question’s requirement.
Why would a developer declare a variable as static in Java?
Explanation: Static variables are shared among all objects of a class, meaning only one copy exists. Immutability is accomplished with the final keyword. Serialization and sub-class access are unrelated to the static keyword.
What is the main goal of encapsulation in Java?
Explanation: Encapsulation binds data and methods into one unit, promoting modularity and data hiding. Allowing direct access, preventing inheritance, and making all fields public negate the purpose of encapsulation.
For which scenario is a singleton class most appropriately used in Java?
Explanation: The singleton pattern ensures that only one instance exists, suitable for unique resources. Creating many instances or using only static methods are unrelated. An abstract class can never be instantiated, which contradicts the singleton idea.
Which type of Java loop ensures the loop body executes at least once?
Explanation: A do-while loop checks its condition after execution, ensuring at least one run. For and while loops both check conditions before running, so they may not execute at all. Nested for loops are just an organizational structure and do not guarantee execution.
How is an infinite loop commonly declared using a for loop in Java?
Explanation: A for loop with empty initialization, condition, and increment expressions runs indefinitely. The second option executes only once, while the next two options never execute or execute just once due to the false condition.
What is the key difference between 'break' and 'continue' statements inside a Java loop?
Explanation: Break leaves the loop, while continue skips only the rest of the current iteration. They do not terminate the program, nor does continue end the loop as implied in the distractors.
How do float and double variables mainly differ in Java?
Explanation: Float is a 4-byte, single-precision type, while double is 8 bytes and double precision. The other answers either swap the sizes, incorrectly assign usage, or claim incorrect similarities.
What does the final keyword accomplish when used with a variable in Java?
Explanation: Final variables are constants and cannot be reassigned after initialization. The other options incorrectly describe access modifiers or suggest multiple assignments, which final does not allow.
Which Java access specifier would you choose to allow methods to be accessible within the same package but not from subclasses in other packages?
Explanation: Default access (package-private) restricts access to classes in the same package. Protected would also grant access to subclasses in other packages, public allows universal access, and private limits access to only within the defining class.
What is the main function of a constructor in Java?
Explanation: A constructor sets up new object instances. It cannot define only static fields, terminate a program, or govern access by itself; the other options mix up unrelated concepts.
Inheritance in Java primarily enables what?
Explanation: Inheritance lets a new class gain properties and methods of an existing class, supporting code reuse. The other answers confuse inheritance with unrelated mechanisms such as file organization or method restriction.
Data encapsulation in Java assists in 'data hiding.' What does this mean?
Explanation: Data hiding means preventing external access to variables or methods, thus safeguarding internal state. Encryption and file system hiding are unrelated, while methods are always visible to the compiler.
In which Java loop is the exit condition checked after executing the loop body at least once?
Explanation: The do-while loop always runs the loop's body first, then checks the conditional expression. For and while loops check conditions before running, while enhanced for is a variant of the basic for loop.
Which statement about variable access in Java is correct?
Explanation: Private variables are the most restrictive, accessible only within their own class. Protected variables are also available to subclasses and within the package, default variables do not cross package boundaries or reach subclasses outside, and public variables are universally accessible.