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 2 of 10)

  1. Which keyword in Java is used to ensure that a method cannot be overridden by subclasses?Multiple choiceEasy
  2. Which method would you use to convert a BigDecimal to a plain string representation without scientific notation?Multiple choiceMedium
  3. When dealing with currency in Java, why is it crucial to use BigDecimal instead of float or double?Multiple choiceEasy
  4. Which Java collection would you use if you need to store a list of product IDs that allows duplicates and maintains insertion order?Multiple choiceEasy
  5. In an e-commerce application, what is a safe way to handle a price field in a Product class?Multiple choiceMedium
  6. Which Java collection would you choose if you need to maintain the order of insertion without allowing duplicate values?Multiple choiceEasy
  7. Which bug is the main problem in the following code snippet that tries to calculate the total cart value?Multiple choiceEasy
  8. Which of the following statements correctly creates a BigDecimal that represents the monetary value 19.99?Multiple choiceHard
  9. In an e-commerce application, how can encapsulation improve the design of a class representing a shopping cart?Multiple choiceMedium
  10. Which statement is true about the 'if' condition in Java?Multiple choiceEasy
  11. Which statement correctly updates the 'total' variable to add a discount?Multiple choiceMedium
  12. What does comparing two BigDecimal objects using compareTo() method evaluate to?Multiple choiceMedium
  13. What does this minimal total-printing snippet output?Multiple choiceEasy
  14. Why do teams use private access modifiers for fields in a Java class in production code?Multiple choiceEasy
  15. How can BigDecimal pitfalls be avoided when performing financial calculations?Multiple choiceMedium
  16. Given a static factory method in a class Product, how does it improve object creation over constructors?Multiple choiceMedium
  17. Identify the logical error in the following checkout process code?Multiple choiceHard
  18. In a simple e-commerce application, you have a variable for the price of a product. Which one of the following variable declarations is correct for a product…Multiple choiceEasy
  19. In which scenario might using BigDecimal be less suitable compared to other types?Multiple choiceHard
  20. Which Java keyword would you use to manually throw an exception in Java?Multiple choiceEasy
  21. How does a LinkedList differ from an ArrayList in terms of element insertion efficiency?Multiple choiceMedium
  22. Given a class with public fields in an e-commerce application, what is a likely issue?Multiple choiceMedium
  23. What would happen if you try to add a null key to a TreeMap?Multiple choiceHard
  24. Consider a HashMap storing product names as keys and their prices as values. What potential issue should you consider when using mutable objects as keys?Multiple choiceHard
  25. How can you correct the following code to ensure the discount is applied correctly?Multiple choiceMedium
  26. How would you explain a key advantage of using Java records for domain modeling in a modern Java application in an interview?Multiple choiceEasy
  27. You have a method calculateShippingCost() that takes multiple parameters related to an order. What refactoring approach should you consider?Multiple choiceMedium
  28. How would you correctly round a BigDecimal to two decimal places using half-up rounding mode?Multiple choiceMedium
  29. Which of the following exceptions is a checked exception?Multiple choiceEasy
  30. In a class Inventory, you notice several methods that manipulate a List<Product>. When refactoring, what is a better object-oriented approach?Multiple choiceHard
  31. When refactoring a procedural e-commerce script into an object-oriented design, what is a sign that a group of related functions should become a method in a…Multiple choiceMedium
  32. In the following code, which exception will be thrown if 'array' is null?Multiple choiceHard
  33. In an e-commerce application, you have the following procedural code managing a list of products. What is a good candidate for refactoring this into an…Multiple choiceEasy
  34. When handling exceptions, why is it important to avoid catching the generic Exception class?Multiple choiceMedium
  35. Consider a class where a public field references a mutable list. What is a safer design approach for this scenario?Multiple choiceHard
  36. Which of the following is the correct way to subtract one BigDecimal from another?Multiple choiceMedium
  37. What is true about a class marked as 'abstract' in Java?Multiple choiceEasy
  38. In a HashMap, what happens when two keys have the same hash code?Multiple choiceMedium
  39. What does this LinkedHashSet print after inserting a duplicate fruit?Multiple choiceMedium
  40. Which of the following is a logic error when applying a discount to a product?Multiple choiceMedium
  41. Consider a method public static Product createProduct(String name, BigDecimal price). What design pattern does this method demonstrate?Multiple choiceMedium
  42. Which method would you use to efficiently check the presence of a key in a HashMap?Multiple choiceMedium
  43. Which statement best describes exception propagation in Java?Multiple choiceMedium
  44. Why do teams use BigDecimal for financial applications in production code?Multiple choiceEasy
  45. Why can using too many setters in a class become a problem in practice?Multiple choiceMedium
  46. Why can using public fields in a Java class become risky in production code?Multiple choiceMedium
  47. You notice that a Java class uses a mix of public fields and methods. What refactoring strategy can improve its encapsulation?Multiple choiceHard
  48. How would you explain a potential consequence of not overriding hashCode() when overriding equals() in a class used as a key in a HashMap in an interview?Multiple choiceHard
  49. In the following code, which line will print to the console when an exception occurs in processOrder()?Multiple choiceMedium
  50. How can you correct the following code to ensure 'finalPrice' is calculated correctly?Multiple choiceHard