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

  1. Which data type is the best fit to use for storing the number of items in a shopping cart?Multiple choiceEasy
  2. How can you ensure a loop calculates the total of item prices only if they are above $50?Multiple choiceMedium
  3. If a cart contains prices {10, 20, 30}, what is the final value of 'total' after executing the following loop with an off-by-one error?Multiple choiceHard
  4. Why is it beneficial to avoid duplicating logic in methods when developing an e-commerce application?Multiple choiceHard
  5. What will be the value of 'totalCost' if executed with cartTotal = 100 and discountApplied = false in this code? int totalCost = cartTotal - (discountApplied?…Multiple choiceHard
  6. How do you structure a nested if-else to check if a product is eligible for both a sale and a loyalty discount?Multiple choiceHard
  7. What is the outcome of the following code? int quantity = 5; quantity += 3; quantity -= 2;?Multiple choiceMedium
  8. Which operator would you use to check if a product's price is greater than or equal to 100?Multiple choiceEasy
  9. In an e-commerce application, how would you correctly increment the stock of a product by 10 units using an assignment expression?Multiple choiceEasy
  10. You need to read a user's input for a product name in a console application. Which Scanner method should you use?Multiple choiceMedium
  11. If you need to ensure that multiplication of two BigDecimal values is precise, which method should you use?Multiple choiceHard
  12. What happens if you override only the equals method and not hashCode in a Java class used in a HashSet?Multiple choiceEasy
  13. How would you explain a common mistake when implementing equals and hashCode that can lead to incorrect Set behavior in an interview?Multiple choiceMedium
  14. In a console-based store application, how would you correctly format a price of 19.9 to always show two decimal places when printed?Multiple choiceMedium
  15. Consider the expression: int result = 3 + 5 * 2 - 8 / 4 % 3. What is the value of 'result'?Multiple choiceHard
  16. How would you explain a good practice when naming a method that calculates the total price in an e-commerce application in an interview?Multiple choiceMedium
  17. If you want to take a user's checkout confirmation input as 'yes' or 'no', which Scanner method should you use?Multiple choiceMedium
  18. Can a Java interface contain static methods?Multiple choiceEasy
  19. In a cart system, how would you use an if-else to apply a discount only if 'isWeekend' is true and 'cartValue' exceeds 150?Multiple choiceMedium
  20. Consider the following code snippet. What will be the output when 'new Customer().getDiscount();' is executed?Multiple choiceMedium
  21. Why might a method be refactored to accept a List<Product> instead of an array of Product?Multiple choiceHard
  22. Why do teams use parameters in a method in production code?Multiple choiceMedium
  23. Why might a class choose to explicitly invoke a default method from one of its interfaces?Multiple choiceHard
  24. Which of the following is an example of a numeric literal for a double in Java?Multiple choiceEasy
  25. What will be the result of adding the same object twice to a HashSet?Multiple choiceEasy
  26. How can a class explicitly call a default method from an interface it implements?Multiple choiceHard
  27. How will the expression '5 * (3 + 2) - 8 / 4' be evaluated?Multiple choiceHard
  28. When designing an in-memory catalog using HashMap, why is it risky to use mutable objects as keys?Multiple choiceHard
  29. Consider this code: 'Scanner scanner = new Scanner(System.in); int value = scanner.nextInt(); scanner.nextLine();'. What problem does adding…Multiple choiceHard
  30. Why split checkout math into named values like subtotal, discountAmount, and finalTotal instead of writing one long expression?Multiple choiceMedium
  31. Which of the following code snippets will correctly format a double value to two decimal places when printing?Multiple choiceMedium
  32. Consider a method that calculates the total price of items in a cart. What is a good naming convention for this method?Multiple choiceMedium
  33. Why does parameters in a method matter in this design?Multiple choiceEasy
  34. In a system with many similar services, what is a downside of using a deep inheritance hierarchy?Multiple choiceHard
  35. In a class used as a key in a HashMap, you override equals but not hashCode. What might happen?Multiple choiceHard
  36. How should you structure an if-else statement to verify both product availability and minimum order quantity?Multiple choiceMedium
  37. What happens if a class implements two interfaces, each with a default method with the same signature?Multiple choiceEasy
  38. What type of value does a method that checks inventory availability in a store likely return?Multiple choiceEasy
  39. You have a method that calculates a discount on a product price. How can you ensure this method adheres to the single responsibility principle?Multiple choiceHard
  40. Which of the following statements will declare a variable to store a product quantity?Multiple choiceEasy
  41. Which Java package provides the Scanner class for reading input from the console?Multiple choiceEasy
  42. Given the following code, why does it compile successfully? interface A { default void foo() {} } class B implements A { void foo() {…Multiple choiceHard
  43. Why should methods in Java adhere to the Single Responsibility Principle, especially in e-commerce applications?Multiple choiceHard
  44. Given a total price as a BigDecimal, what is the best way to apply a 10% discount?Multiple choiceHard
  45. Why might you use an abstract class instead of an interface with default methods?Multiple choiceMedium
  46. What is one main purpose of using interfaces in Java?Multiple choiceEasy
  47. Why is it not recommended to use float for calculating the subtotal of an online shopping cart?Multiple choiceHard
  48. In an e-commerce application, which condition checks if a product is eligible for a discount when its price is above 100?Multiple choiceEasy
  49. Which numeric type would you use to represent a product's price if you don't need precise decimal arithmetic?Multiple choiceEasy
  50. Which condition will correctly apply free shipping if 'cartTotal' is greater than or equal to 50?Multiple choiceMedium