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

  1. Which Java keyword would you use to define a default method in a Java interface?Multiple choiceEasy
  2. How can you modify a while loop to correctly calculate the total price only for items priced over $20, given the following code? int total = 0; while (index <…Multiple choiceHard
  3. How can a 'for loop' be used to calculate the total price of items only if they belong to a specific category?Multiple choiceMedium
  4. In an e-commerce checkout system, how would you structure nested conditions to check if an item is in stock and if the user has enough balance?Multiple choiceMedium
  5. Which statement correctly calculates the total number of items in a shopping cart?Multiple choiceMedium
  6. When is calculateSubtotal(...) a good method extraction?Multiple choiceMedium
  7. What is the result of the following expression? int price = 50; boolean isEligibleForDiscount = price < 100 && price!= 0;?Multiple choiceMedium
  8. Why might the following while loop cause an infinite loop when calculating a cart total?Multiple choiceHard
  9. Which of the following correctly determines if a boolean 'isPremiumMember' is set to true?Multiple choiceEasy
  10. In an inheritance hierarchy, what is a reason to choose composition over inheritance?Multiple choiceMedium
  11. Given the following code, what concept does it best illustrate?Multiple choiceHard
  12. How can the following loop be optimized to stop early if a price over $100 is found?Multiple choiceHard
  13. Which Java keyword would you use to prevent a class from being subclassed in Java?Multiple choiceEasy
  14. In the context of collections, what is a key difference between a List and a Set in Java?Multiple choiceEasy
  15. Consider a boolean 'isAvailable' and a double 'userBalance'. How would you structure an if-else to ensure an item can be purchased if in stock and funds are…Multiple choiceMedium
  16. Consider the following code snippet. What will be the output when 'new Product().display();' is executed?Multiple choiceHard
  17. Which of the following methods is most likely violating the single responsibility principle in an e-commerce application?Multiple choiceHard
  18. Why can using deeply nested if-else statements in e-commerce checkout logic become a problem in practice?Multiple choiceHard
  19. Why are default methods in interfaces beneficial for API evolution?Multiple choiceMedium
  20. Which statement about method return values is true?Multiple choiceMedium
  21. Consider the following classes. What will be the output of 'new PremiumService().getInfo();'?Multiple choiceHard
  22. Given a class hierarchy where 'OnlineService' is a superclass and 'SubscriptionService' is a subclass, which method signature in 'SubscriptionService' would…Multiple choiceMedium
  23. What is a potential problem with the following code snippet when calculating cart totals? int i = 0; while (i < cart.length) { total += cart[i].price; }?Multiple choiceHard
  24. In an e-commerce application, you have a method double calculateTotalPrice(double[] prices). Which of the following is a good reason to use this method?Multiple choiceMedium
  25. What does this code if the cart total is 250 and free shipping is given for totals over 200 evaluate to?Multiple choiceMedium
  26. What will be the result of the following code when calculating the total cost with a discount?Multiple choiceMedium
  27. How can you ensure that a double value representing a price is displayed with a minimum of two fractional digits?Multiple choiceMedium
  28. Which of the following is NOT a valid reason to use interfaces with default methods?Multiple choiceEasy
  29. Why do teams use an interface over an abstract class in production code?Multiple choiceHard
  30. Why does a cart program need a loop when calculating a subtotal for many items?Multiple choiceMedium
  31. Which return type of a method that does not return any value is the default in Java?Multiple choiceEasy
  32. Given the following code snippet, identify the issue when storing objects in a HashSet?Multiple choiceHard
  33. Given the following code snippet, how can it be optimized to avoid unnecessary iterations when calculating total discounts? for (int i = 0; i <…Multiple choiceHard
  34. What will happen if you try to read an integer input using Scanner's nextInt() when the input is non-numeric?Multiple choiceHard
  35. What will be the output if the 'discounts' array is empty in the following loop?Multiple choiceMedium
  36. Which statement is true regarding the relationship between hashCode and equals?Multiple choiceMedium
  37. A developer wishes to use a custom object as a key in a HashMap. What must be carefully considered to prevent bugs?Multiple choiceHard
  38. When should you consider using a switch statement for managing product categories?Multiple choiceMedium
  39. Consider the method signature: double calculateDiscount(double price, double discountRate). What does this method do?Multiple choiceMedium
  40. In an e-commerce app, what does the following snippet print if 'isMember' is true?Multiple choiceEasy
  41. Why can using default methods in Java interfaces become a problem in production code?Multiple choiceMedium
  42. How can you represent a fixed percentage discount in Java?Multiple choiceMedium
  43. Given the following code snippet, what is the value of 'finalPrice'? int basePrice = 200; int discount = 30; int finalPrice = basePrice - discount *…Multiple choiceHard
  44. Given the method int addToCart(int currentQuantity, int newItems), what could be a potential return value?Multiple choiceMedium
  45. Why is it important to avoid duplicating logic in methods when developing an e-commerce application?Multiple choiceMedium
  46. How can polymorphism be achieved in Java?Multiple choiceMedium
  47. What happens if you use the division operator between two int variables in Java?Multiple choiceMedium
  48. Why did Java introduce default methods in interfaces?Multiple choiceEasy
  49. Given a variable 'productType', what switch statement structure can apply a handling fee based on the type?Multiple choiceMedium
  50. Given the code snippet, what will be the value of 'totalDiscount' after execution? int[] discounts = {5, 10, 15}; int totalDiscount = 0; for (int i = 0; i <=…Multiple choiceHard