QuizzesJava Best Practices — Concept Check
intermediate
java

Java Best Practices — Concept Check

Questions on Java Best Practices. Battle-tested Java best practices from Effective Java and industry experience — writing clean, maintainable, production-quality code.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Java Best Practices — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You are designing an API for a library. What is the best practice regarding class and member visibility?

    • AMake classes and members public by default for flexibility.
    • BUse package-private visibility to share implementation details within the package.
    • CMake classes and members private by default, expanding access only when necessary.
    • DUse protected visibility for all classes and members to allow subclassing.
  2. 2.A developer uses inheritance to create a LoggingArrayList for logging operations. What is a potential issue with this approach?

    • AInheritance allows for easy code reuse without any drawbacks.
    • BInheritance ensures a stable and robust design for logging purposes.
    • CLoggingArrayList is tightly coupled to ArrayList's implementation details.
    • DLoggingArrayList will automatically update with ArrayList changes without issues.
  3. 3.In a Java application, you observe a class with public mutable fields. What problem might this cause?

    • AIt automatically validates field values upon access.
    • BIt allows for easy access and modification of fields.
    • CIt ensures better encapsulation and immutability.
    • DIt can lead to unintended side effects and bugs.
  4. 4.When should you use Optional<T> in Java?

    • AFor method parameters to allow optional arguments.
    • BFor fields to indicate optional properties.
    • CFor collections to handle optional elements.
    • DFor method return types to indicate possible absence of a value.
  5. 5.A method returns null instead of an empty collection. What is a potential consequence?

    • AIt simplifies the method implementation.
    • BIt makes the API more intuitive and predictable.
    • CIt reduces the need for null checks in the caller.
    • DIt increases the risk of NullPointerExceptions.
  6. 6.Why is it recommended to prefer composition over inheritance in Java?

    • AInheritance is the only way to achieve polymorphism.
    • BInheritance is always more flexible than composition.
    • CComposition allows for direct access to superclass methods.
    • DComposition provides more flexibility and reduces coupling to implementation details.
  7. 7.A developer catches an exception but does not log it or take any action. What is a potential issue with this approach?

    • AIt makes debugging difficult by hiding errors.
    • BIt automatically resolves the underlying issue.
    • CIt prevents the application from crashing unexpectedly.
    • DIt ensures the application continues running smoothly.
  8. 8.A class implements a method that throws a generic Exception. Why might this be problematic?

    • AIt ensures all possible exceptions are caught.
    • BGeneric exceptions provide detailed error information.
    • CIt makes the code easier to read and maintain.
    • DIt lacks specificity, making error handling less effective.