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

  1. In an e-commerce application, why is it useful to have a method like boolean isEligibleForFreeShipping(double cartTotal)?Multiple choiceMedium
  2. Which of the following is NOT a valid method declaration in Java?Multiple choiceEasy
  3. What will the following code output if 'prices' is an empty array?Multiple choiceEasy
  4. Given a boolean 'isHoliday' and a 'customerType' string, how would you structure an if-else statement to give discounts only on holidays to 'VIP' customers?Multiple choiceHard
  5. In a large HashMap, what is a potential drawback of all keys having the same hashCode?Multiple choiceMedium
  6. In the context of Java interfaces, what is a default method?Multiple choiceMedium
  7. Which of the following is a valid way to declare a double representing a product price?Multiple choiceEasy
  8. Which loop structure would be most appropriate for applying a discount to each item in a cart when the number of items is unknown?Multiple choiceMedium
  9. Which Scanner method would you use to read a floating-point number from the console?Multiple choiceEasy
  10. What is autoboxing in Java?Multiple choiceEasy
  11. What happens if a class implements two interfaces that have methods with the same signature?Multiple choiceMedium
  12. Given the code snippet below, what will be the result of the comparison using '=='?Multiple choiceMedium
  13. Which of the following variable names is best for storing the total amount in a shopping cart?Multiple choiceMedium
  14. When does a local variable declared inside a method stop existing?Multiple choiceEasy
  15. Which of the following is a checked exception?Multiple choiceMedium
  16. Why might you choose 'int' over 'long' for storing the number of items in a small store's inventory?Multiple choiceMedium
  17. Why does using Integer.valueOf(int) over new Integer(int) matter in practice?Multiple choiceHard
  18. What do these Integer == comparisons print with and without cache-range values?Multiple choiceMedium
  19. Why might you choose to use a wrapper type instead of a primitive in a Java domain model?Multiple choiceMedium
  20. In the expression 'total = (price + tax) * discount;', which part is calculated first?Multiple choiceMedium
  21. Which code correctly declare a variable to store a product's name?Multiple choiceEasy
  22. Why is left == right risky when left and right are Integer objects?Multiple choiceHard
  23. What happens if you try to access a local variable outside its block scope?Multiple choiceMedium
  24. In an e-commerce application, what can be a consequence of shadowing a local variable by declaring another with the same name in an inner block?Multiple choiceHard
  25. Why does the following code throw a ClassCastException at runtime? class Payment {} class CreditCardPayment extends Payment {} class CashPayment extends…Multiple choiceHard
  26. Consider the following code snippet. What is the output of the code?Multiple choiceMedium
  27. In Java, which of the following is true about method overriding?Multiple choiceMedium
  28. Which of the following is a reason to prefer using a wrapper type over a primitive type in a Java domain model?Multiple choiceEasy
  29. Which primitive data type would you use to store the number of products available in stock?Multiple choiceEasy
  30. Which Java keyword would you use to declare that a method can throw an exception?Multiple choiceEasy
  31. What happens when you compare two Integer objects using the == operator?Multiple choiceMedium
  32. Given the following code, which method will be executed when display() is called?Multiple choiceMedium
  33. Why does the 'throws' keyword in a method signature matter in practice?Multiple choiceEasy
  34. What will be the value of 'total' after the following code executes? int total = 50; total += 30;?Multiple choiceMedium
  35. In Java, how can you determine if a product is in stock if its quantity is stored in an integer variable named 'stock'?Multiple choiceMedium
  36. In an e-commerce application, why might it be problematic to use the same variable name for different purposes in a single method?Multiple choiceHard
  37. How would you explain a potential pitfall of relying on autoboxing in a loop in an interview?Multiple choiceMedium
  38. In which case would using a primitive type over a wrapper type be more appropriate?Multiple choiceEasy
  39. In the context of an e-commerce application, what does the expression '(cartTotal > 100) && (hasDiscount)' evaluate?Multiple choiceMedium
  40. In a method that calculates discounts for multiple products, why should you avoid using the same variable name for different calculations within nested blocks?Multiple choiceHard
  41. When recalculating a discount in a checkout system, what is a potential pitfall of reusing the same variable name within nested blocks?Multiple choiceHard
  42. Why might 'long' be a good choice for tracking the total number of orders in a large e-commerce system?Multiple choiceHard
  43. Which practice is the safest default for managing variable scope inside methods when calculating the shipping cost?Multiple choiceMedium
  44. What is one downside of autoboxing in Java?Multiple choiceMedium
  45. When should a custom exception be created in an e-commerce application?Multiple choiceHard
  46. Which of the following is a valid declaration for a product's stock quantity in Java?Multiple choiceEasy
  47. Why is exception propagation important in a Java application?Multiple choiceMedium
  48. Given the following code, why does it compile and execute successfully? interface Order { void process(); } class OnlineOrder implements Order { public void…Multiple choiceHard
  49. What happens when a checked exception is not declared or handled in Java?Multiple choiceMedium
  50. Which wrapper class is NOT immutable in Java?Multiple choiceEasy