Skip to content
Sharp

Core Java interview questions

BigDecimal and Money, Code Reading and Debugging, Code Reading and Refactoring to Objects, Code Reading and State Safety, Code Reading with Collections, Code Reading with Exceptions

473 questions with answers and explanations · 472 flashcards

Questions (page 3 of 10)

  1. In the following code snippet, what will be the value of 'isEligibleForDiscount' after execution?Multiple choiceMedium
  2. Given the class Customer with a private field email, how can you provide access to this field in a controlled manner?Multiple choiceMedium
  3. Given a list of product names, which collection would help you efficiently find out how many times each product appears?Multiple choiceMedium
  4. How would you explain the time complexity for retrieving an element by index from an ArrayList in an interview?Multiple choiceMedium
  5. When reading code, what is a sign that a class might have an unsafe design?Multiple choiceMedium
  6. What potential problem can arise from using mutable static fields in a class?Multiple choiceMedium
  7. How would you explain a potential issue with a class design where most fields are set to public in an e-commerce application in an interview?Multiple choiceHard
  8. Which Java collection allows you to store key-value pairs, ensuring unique keys?Multiple choiceEasy
  9. How does the Optional class help in modern Java code?Multiple choiceEasy
  10. In Java, what is the purpose of the 'throws' keyword in a method declaration?Multiple choiceMedium
  11. Consider an enum PaymentStatus with values PENDING, COMPLETED, FAILED. How would you extend it to include a method that returns a user-friendly message for…Multiple choiceHard
  12. If you need to store a monetary value in a database and perform calculations, why might you choose BigDecimal over a database's native decimal type?Multiple choiceHard
  13. Which Java collection is best suited for maintaining a collection of unique elements that need to be accessed in order of insertion?Multiple choiceEasy
  14. How does the Optional class improve code readability and safety?Multiple choiceMedium
  15. Why does the following code snippet result in a logical error?Multiple choiceMedium
  16. Why is BigDecimal preferred over double for handling monetary values in Java?Multiple choiceEasy
  17. Why might using a TreeSet be less efficient than a HashSet for frequent insertions and deletions?Multiple choiceHard
  18. When storing a monetary value in a BigDecimal, what is a recommended best practice to avoid precision issues?Multiple choiceMedium
  19. Which collection is most suitable for storing unique elements with no concern for order?Multiple choiceEasy
  20. In a HashSet, what determines the position of an element?Multiple choiceMedium
  21. Why does fields in a Java class matter in this design?Multiple choiceEasy
  22. How can the following code snippet be optimized to avoid unnecessary calculations?Multiple choiceHard
  23. Consider a class Customer with a method addToOrderHistory(Product product). Which aspect of this design could indicate a need for refactoring?Multiple choiceMedium
  24. Why do teams use custom exceptions in Java in production code?Multiple choiceMedium
  25. Which of the following statements about constructors in Java is true?Multiple choiceMedium
  26. Why is it important to specify a rounding mode when performing division with BigDecimal?Multiple choiceEasy
  27. In a value object pattern, what characteristic is essential to ensure the integrity of the data?Multiple choiceMedium
  28. Which line of code will cause a logical flaw in the following snippet?Multiple choiceMedium
  29. In an e-commerce application, a 'Customer' class has a 'getOrders()' method that returns a list of orders. What is a safe practice for handling this list?Multiple choiceMedium
  30. In an e-commerce system, a 'Product' class has a method 'setPrice(double price)'. What could be a potential issue with this setter method?Multiple choiceMedium
  31. What will be the result of the following code?Multiple choiceEasy
  32. In a class Order, you notice that the constructor initializes fields that are never used again. What is likely a better design choice?Multiple choiceHard
  33. Why can having many public fields in a Java class become a bug in practice?Multiple choiceEasy
  34. In a nested try-catch block, where is an exception handled if both inner and outer blocks have catch clauses for the same exception?Multiple choiceHard
  35. Given the following code snippet, what will be the output?Multiple choiceMedium
  36. Which of the following practices best helps maintain invariants in a Java class?Multiple choiceMedium
  37. Which approach is the best fit to add two BigDecimal objects, value1 and value2?Multiple choiceMedium
  38. Why is it generally a bad idea to catch the Exception class directly in Java?Multiple choiceHard
  39. How would you explain a key benefit of using Java records over traditional classes for immutable data in an interview?Multiple choiceEasy
  40. What happens if you try to modify an immutable object in Java?Multiple choiceMedium
  41. Why is BigDecimal usually preferred over double for money calculations?Multiple choiceMedium
  42. In an e-commerce application, consider a Customer class that has public fields for id and name. What is a potential risk with this design?Multiple choiceMedium
  43. A Java class has a mutable collection exposed through a public getter. What refactoring ensures encapsulation while maintaining functionality?Multiple choiceHard
  44. Which block is always executed in a try-catch-finally structure, regardless of whether an exception is thrown?Multiple choiceEasy
  45. Consider a static factory method public static Order createOrder(String id, BigDecimal amount). What flexibility does this pattern provide over direct…Multiple choiceHard
  46. How does a try-with-resources statement differ from a regular try-catch-finally block?Multiple choiceMedium
  47. Why does a value object in domain-driven design matter in practice?Multiple choiceEasy
  48. Which method would you use to ensure two BigDecimal values representing money are considered equal if they have the same numeric value but different scales?Multiple choiceHard
  49. What happens if you try to retrieve a value from a HashMap using a key that is not present?Multiple choiceMedium
  50. Which refactoring step can help convert a setter-heavy design to a safer design pattern?Multiple choiceHard