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

  1. You have two BigDecimal values: a = new BigDecimal("0.1") and b = new BigDecimal("0.10"). What is the result of a.compareTo(b)?Multiple choiceHard
  2. Given a try-catch-finally block if a return statement is executed in both the try and finally blocks, what outcome should you expect?Multiple choiceEasy
  3. Identify the flaw in this code that calculates an order total with tax, where 'taxRate' is 0.1?Multiple choiceHard
  4. In the context of encapsulation, why should a class in an e-commerce system avoid exposing collections directly?Multiple choiceHard
  5. Which collection should you use if you need to frequently access elements by their index?Multiple choiceEasy
  6. What happens if a NullPointerException is thrown but not caught in a method?Multiple choiceMedium
  7. Which approach is recommended for handling frequent monetary operations to maintain accuracy and performance?Multiple choiceMedium
  8. In Java, how can you ensure that a class has only one instance?Multiple choiceMedium
  9. What happens if you divide one BigDecimal by another without specifying a rounding mode and the result cannot be represented exactly?Multiple choiceMedium
  10. What will be the value of 'totalPrice' after executing the following code? int unitPrice = 20; int quantity = 3; int totalPrice = unitPrice * quantity;?Multiple choiceEasy
  11. In a console shopping cart application, why might you separate input handling from calculations?Multiple choiceHard
  12. What happens if a subclass does not implement an abstract method from its superclass?Multiple choiceEasy
  13. Why would you choose to use a switch statement instead of multiple if-else statements for handling order status?Multiple choiceMedium
  14. Which part of the following code is responsible for avoiding an off-by-one error?Multiple choiceMedium
  15. Why can using overly complex methods in an e-commerce application become a problem in production code?Multiple choiceMedium
  16. How can a class implementing an interface with a default method call that method from within an overridden method?Multiple choiceMedium
  17. What keyword is used to inherit class properties in Java?Multiple choiceEasy
  18. If 'boolean onSale = false; int quantity = 10;', what does the expression 'onSale || quantity > 5' evaluate to?Multiple choiceEasy
  19. In Java, which of the following statements about abstract classes and interfaces is correct?Multiple choiceMedium
  20. How can you check if a variable 'points' is exactly 100?Multiple choiceEasy
  21. What happens when you use a switch statement with an integer variable representing shipping zones to apply specific fees?Multiple choiceMedium
  22. In the following code, what will happen if 'cartItems' is an empty array? for(int i = 0; i < cartItems.length; i++) { total += cartItems[i].price; }?Multiple choiceMedium
  23. Why does the 'System.out.println()' method in a Java console application matter in practice?Multiple choiceEasy
  24. Why does hashCode in a HashSet matter in this design?Multiple choiceEasy
  25. Given the following loop if no prices are over $100, what outcome should you expect?Multiple choiceHard
  26. In a console application, what common issue arises when using Scanner's nextLine() method after nextInt()?Multiple choiceHard
  27. Which design principle helps decide whether to use an interface or an abstract class?Multiple choiceMedium
  28. Why might using double for currency calculations in an e-commerce application lead to inaccuracies?Multiple choiceMedium
  29. When converting a double representing a price to BigDecimal, which constructor should you avoid?Multiple choiceMedium
  30. Which of the following statements is true about default methods in interfaces?Multiple choiceEasy
  31. Why does using int division to calculate the average price of items in a cart change runtime behavior?Multiple choiceHard
  32. Which of the following is true about a class that implements an interface in Java?Multiple choiceEasy
  33. How does a HashMap determine the bucket in which to store an entry?Multiple choiceMedium
  34. How can you separate input handling from business logic in a simple Java console application?Multiple choiceMedium
  35. Identify the logical error in the following statement: boolean freeShipping = (cartTotal > 50) || (hasCoupon = true);?Multiple choiceHard
  36. In what scenario can a default method in an interface be overridden?Multiple choiceMedium
  37. Why is it a bad practice to use mutable fields in equals and hashCode implementations?Multiple choiceMedium
  38. Given the following code snippet, what output should you expect?Multiple choiceMedium
  39. Why might using the 'instanceof' operator in a method implementation be considered a bad practice?Multiple choiceHard
  40. Why do teams use an abstract class in Java in production code?Multiple choiceEasy
  41. A method named calculateFinalPrice takes a price and a discount rate. Which of the following best ensures the method's reliability?Multiple choiceHard
  42. Why is it important for hashCode to be consistent with equals when using objects as keys in a HashMap?Multiple choiceMedium
  43. What does the following code snippet if the total cart amount is 100 and free shipping applies to amounts over 50 evaluate to?Multiple choiceMedium
  44. When performing currency conversion, why choose BigDecimal over double?Multiple choiceHard
  45. Which loop structure would you use to ensure a piece of code runs at least once to add taxes to a cart total?Multiple choiceMedium
  46. What will be the result of the following expression: 'int result = 20 / 4 + 3 * 2 - 1;'?Multiple choiceHard
  47. If an interface defines a default method and a class implements this interface but does not override the default method, what happens when the method is…Multiple choiceMedium
  48. How would you correctly add two BigDecimal values representing product prices in Java?Multiple choiceMedium
  49. Given the following code snippet, what will be the value of 'discount' after execution? boolean isMember = true; int totalAmount = 150; double discount =…Multiple choiceMedium
  50. Which method should be overridden to ensure correct behavior of keys in a HashMap?Multiple choiceMedium