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 4 of 4)

  1. Why is using switch expressions with enums considered safer in Java 17?Multiple choiceEasy
  2. How does a switch expression enhance type safety compared to traditional switch statements?Multiple choiceMedium
  3. Given a 'Coordinate' value object, how can you ensure its equals method is correctly implemented?Multiple choiceHard
  4. What feature of switch expressions helps prevent bugs related to missing break statements?Multiple choiceMedium
  5. Which of the following best describes how enums enhance code readability?Multiple choiceMedium
  6. Why does the 'compact constructor' in a Java Record matter in practice?Multiple choiceMedium
  7. Which code correctly yield a value from a block in a switch expression?Multiple choiceMedium
  8. Which of the following is a valid definition of a record in Java?Multiple choiceMedium
  9. Why should validation logic be implemented inside a value object's constructor?Multiple choiceMedium
  10. Which is a characteristic of a Java record?Multiple choiceEasy
  11. In Java, which method is NOT automatically generated for a record?Multiple choiceEasy
  12. How does using a BigDecimal in a value object benefit financial calculations?Multiple choiceMedium
  13. Consider a class designed to handle monetary values. Why might a developer choose a static factory method over a constructor for creating instances of…Multiple choiceHard
  14. In what scenario would using a record be inappropriate?Multiple choiceMedium
  15. What does a static factory method return?Multiple choiceEasy
  16. When defining a 'ProductCode' value object, why might you choose to use a record?Multiple choiceHard
  17. How do you safely yield a result from a switch expression in Java 17?Multiple choiceMedium
  18. How would you explain a key advantage of using enums over strings for representing a set of predefined constants in an interview?Multiple choiceMedium
  19. Why do teams use switch expressions in Java over traditional switch statements in production code?Multiple choiceEasy
  20. Consider the following switch expression. What will be the output if 'category' is 'grocery'? string discount = switch (category) { case 'electronics' ->…Multiple choiceMedium
  21. Given the following Java text block for a SQL query, what will be the result of the indentation?Multiple choiceMedium
  22. When might a regular class be preferred over a record in Java?Multiple choiceHard
  23. What would be a reason to choose a traditional class over a Java Record?Multiple choiceMedium
  24. In what way do Java Records improve code readability?Multiple choiceMedium
  25. Which of the following is true about the arrow syntax in switch expressions?Multiple choiceEasy
  26. Which of the following is NOT a feature of Java records?Multiple choiceEasy
  27. When using a switch expression, which keyword is used to handle the default case?Multiple choiceEasy
  28. In what way do records enhance code readability?Multiple choiceMedium
  29. When embedding a JSON object in a text block, how do you handle key-value pairs?Multiple choiceMedium
  30. What main advantage do text blocks provide when embedding JSON or SQL in Java code?Multiple choiceMedium
  31. In what way can static factory methods improve validation when creating instances of classes?Multiple choiceMedium
  32. In which scenario is using a record not appropriate?Multiple choiceHard
  33. Given the following code snippet, identify the primary issue with the 'Email' value object:?Multiple choiceHard
  34. When should you avoid using Java Records?Multiple choiceMedium
  35. Why is it beneficial to use value objects for representing monetary amounts rather than using raw doubles or ints?Multiple choiceMedium