Skip to content
Sharp

Modern Java interview questions

Cleaner Domain Modeling, Enums, Java Records, Modern Java Design Choices, Records, Static Factory Methods

185 questions with answers and explanations · 185 flashcards

Questions (page 2 of 4)

  1. Why does overriding hashCode in a value object change runtime behavior?Multiple choiceMedium
  2. How does using Java text blocks simplify embedding JSON structures in code?Multiple choiceEasy
  3. How does the Java compiler handle whitespace at the end of each line in a text block?Multiple choiceHard
  4. How would you explain a potential disadvantage of using BigDecimal for high-frequency number crunching in an interview?Multiple choiceHard
  5. Which characteristic best describes a Java record?Multiple choiceEasy
  6. How would you explain a common feature of Java records that makes them suitable for value objects in an interview?Multiple choiceEasy
  7. Which version of Java introduced text blocks?Multiple choiceEasy
  8. How do you include a double quote within a Java text block?Multiple choiceEasy
  9. How would you explain a potential advantage of using static factory methods over constructors in terms of instance caching in an interview?Multiple choiceEasy
  10. What happens if you use inconsistent indentation in a Java text block?Multiple choiceHard
  11. Which is a potential downside of using static factory methods extensively?Multiple choiceMedium
  12. Which Java type is commonly used in value objects to represent currency values and ensure precision?Multiple choiceEasy
  13. Why should BigDecimal be preferred over double for currency calculations in Java?Multiple choiceHard
  14. Which of the following statements is true about static factory methods?Multiple choiceMedium
  15. What does a static factory method in Java mainly do?Multiple choiceEasy
  16. How do records in Java help in achieving immutability?Multiple choiceMedium
  17. Which switch expression feature helps ensure cleaner code in Java?Multiple choiceMedium
  18. If a value object needs to implement Comparable, what is a best practice to ensure the comparison logic remains consistent?Multiple choiceHard
  19. How would you explain a key characteristic of a Java record in an interview?Multiple choiceEasy
  20. Which feature is automatically provided by Java records?Multiple choiceEasy
  21. Why might you choose a wrapper class like Integer over a primitive int?Multiple choiceMedium
  22. What will happen if a switch expression does not cover all possible enum values and lacks a default case?Multiple choiceHard
  23. Consider a 'Price' value object with a BigDecimal field. Why is this a better choice than using a double for the price?Multiple choiceMedium
  24. Which visibility of a field in a Java record is the default in Java?Multiple choiceEasy
  25. When should you usually prefer a traditional class over a Record?Multiple choiceMedium
  26. Which design choice best communicates intent for a lightweight data carrier without behavior in Java?Multiple choiceMedium
  27. In Java 17, what is a major benefit of using switch expressions with enums?Multiple choiceEasy
  28. Which of the following is a valid way to define a text block in Java?Multiple choiceEasy
  29. How can static factory methods enhance validation during object creation?Multiple choiceHard
  30. Which of the following scenarios is most appropriate for using a Java record?Multiple choiceMedium
  31. When is a Java record usually a better fit than a traditional class?Multiple choiceMedium
  32. Given a record 'Product' used in an e-commerce application, what is a potential drawback of directly exposing its components?Multiple choiceHard
  33. When using switch expressions with enums in Java 17, what happens if you omit a case and there is no default?Multiple choiceMedium
  34. Static factory methods can return:?Multiple choiceEasy
  35. What limitation of Java records might require consideration when modeling domain logic?Multiple choiceHard
  36. How can you ensure thread safety when using a value object in a multi-threaded environment?Multiple choiceMedium
  37. How can you customize the toString() method in a Java Record?Multiple choiceHard
  38. Why might a developer choose a static factory method over a constructor when creating a BigDecimal instance?Multiple choiceMedium
  39. How would you explain a key benefit of using switch expressions in Java 17 for handling order states in an e-commerce system in an interview?Multiple choiceEasy
  40. In a value object representing a 'DateRange', how might you enforce that the start date precedes the end date?Multiple choiceHard
  41. Which statement best describes why static factory methods can lead to clearer creation intent in Java?Multiple choiceMedium
  42. Why might you choose to implement a 'PhoneNumber' as a value object?Multiple choiceMedium
  43. Which of the following is a potential pitfall when using text blocks for SQL queries?Multiple choiceHard
  44. Why can using a Java record for representing a mutable user profile in an application become a problem in production code?Multiple choiceMedium
  45. In a modern Java application, how can switch expressions improve the checkout process logic?Multiple choiceHard
  46. When implementing a complex financial calculation, why might you choose BigDecimal over double?Multiple choiceHard
  47. How can you add behavior to a Java record?Multiple choiceMedium
  48. Which is an advantage of using static factory methods in a large e-commerce application?Multiple choiceMedium
  49. Why should a value object be immutable?Multiple choiceEasy
  50. Why do teams use small dedicated types, such as 'ZipCode', in a domain model in production code?Multiple choiceMedium