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

  1. Given a class with mutable fields, what is a potential issue if instances are used as keys in a HashMap?Multiple choiceHard
  2. In an e-commerce application, what is the correct way to check if a customer is eligible for a discount if they are a 'premium' member using an if-else…Multiple choiceEasy
  3. How would you explain a common pattern for accumulating totals in a loop in an interview?Multiple choiceMedium
  4. Which of the following is true about using nested if-else statements to handle multiple promotion rules?Multiple choiceHard
  5. Which operator is used to increase a product's quantity by one?Multiple choiceEasy
  6. In a class hierarchy, which rule determines method resolution if both a superclass and an interface provide implementations?Multiple choiceMedium
  7. Which data type is most suitable for storing a product's price in an e-commerce application when precision is important?Multiple choiceEasy
  8. What should you consider when naming a method that checks if a cart is eligible for a discount?Multiple choiceEasy
  9. When are two objects considered equal in a Java Set?Multiple choiceEasy
  10. Which of the following expressions correctly calculates the new price after a 20% discount on a product?Multiple choiceMedium
  11. How can you achieve abstraction in Java?Multiple choiceMedium
  12. How can you ensure a method is reusable in different parts of an e-commerce application?Multiple choiceMedium
  13. What happens if two objects in a HashSet have different hashCodes but are considered equal by the equals method?Multiple choiceMedium
  14. Why is it important to implement a consistent equals method in a class?Multiple choiceEasy
  15. What will be the output of the following code snippet if the cart contains item prices {15, 25, 30}?Multiple choiceEasy
  16. How would you refactor a nested if-else statement checking multiple product codes to a switch statement for readability?Multiple choiceHard
  17. Given the following code, why does it fail to compile? interface A { default void foo() { } } interface B { default void foo() { } } class C implements A, B {…Multiple choiceHard
  18. How might using a mutable object as a key in a HashMap lead to subtle bugs?Multiple choiceHard
  19. Which Java collection would be most appropriate for storing unique product IDs in an e-commerce catalog?Multiple choiceMedium
  20. Which Scanner method is best for reading a price input as a floating-point number in a console-based e-commerce application?Multiple choiceEasy
  21. Consider the following method: boolean applyDiscount(double price, double discount). Which of the following changes would help it adhere to the single…Multiple choiceHard
  22. Why does the hashCode method in Java collections matter in practice?Multiple choiceEasy
  23. Which operator is used for numeric addition in Java?Multiple choiceEasy
  24. Which of the following correctly uses a switch statement to apply a discount based on the product category?Multiple choiceEasy
  25. How do you check if a non-premium customer should receive a loyalty discount using an if-else statement?Multiple choiceMedium
  26. In Java, which operator would you use to check if a product's stock is exactly 10?Multiple choiceEasy
  27. Which of the following loop guards is necessary to avoid an off-by-one error in a loop that processes each item in an array?Multiple choiceMedium
  28. What type of loop would be best to calculate the total price of items in a shopping cart when you know the number of items beforehand?Multiple choiceEasy
  29. How many times will the loop execute if the 'cart' array contains three items?Multiple choiceEasy
  30. What does a method return if its return type is declared as void?Multiple choiceEasy
  31. Which statement best matches the chapter's guidance on price types?Multiple choiceHard
  32. Given two products with prices stored as doubles, how can you ensure accurate total price calculation?Multiple choiceHard
  33. Which Java keyword would you use to declare a default method in a Java interface?Multiple choiceMedium
  34. What issue can arise if two unequal objects have the same hashCode in a HashSet?Multiple choiceHard
  35. Why does overriding hashCode but not equals in a Java class change runtime behavior?Multiple choiceMedium
  36. Why is it useful to breaking e-commerce store logic into smaller methods?Multiple choiceEasy
  37. Which of the following statements about interfaces in Java is true?Multiple choiceMedium
  38. What is branching doing in a rule like if (subtotal >= 100.0) shippingFee = 0.0;?Multiple choiceMedium
  39. Which method would you use to print a formatted string to the console without a newline at the end?Multiple choiceEasy
  40. Consider a class hierarchy where a class 'Product' extends 'Item' and implements 'Sellable'. If 'Item' has a method 'display()' and 'Sellable' has a default…Multiple choiceHard
  41. How can you convert a double to a BigDecimal in Java correctly?Multiple choiceMedium
  42. Given this code if 'cartTotal' is 75 and 'loyaltyMember' is false, what outcome should you expect?Multiple choiceHard
  43. What should you be cautious about when using int to store product quantities?Multiple choiceMedium
  44. Why does an accumulator variable in a loop matter in practice?Multiple choiceEasy
  45. If a class implements two interfaces with conflicting default methods, how should it resolve the conflict?Multiple choiceMedium
  46. Consider a superclass with a method that is not overridden in its subclass. Which method is called when the method is invoked on an instance of the subclass?Multiple choiceMedium
  47. What happens if you use 'System.out.printf()' with '%d' to print a double value?Multiple choiceMedium
  48. Why do teams use methods in a Java program in production code?Multiple choiceEasy
  49. When should a class be made final in Java?Multiple choiceMedium
  50. What is the result of the following code if 'prices' is an array of integers and initially 'total' is zero? while (index < prices.length) { total +=…Multiple choiceEasy