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

  1. In a nested try-catch block, if an exception is thrown in the inner try block and not caught there, what happens?Multiple choiceHard
  2. What does executing a throw statement inside a catch block evaluate to?Multiple choiceMedium
  3. In Java, what happens if an exception is thrown in a method and not caught within that method?Multiple choiceEasy
  4. Which method would you use to add an element to a List at a specific position?Multiple choiceEasy
  5. Consider the following code snippet. What is the issue with the 'applyDiscount' method?Multiple choiceHard
  6. How do you correctly initialize a BigDecimal to represent the monetary value 50.75?Multiple choiceMedium
  7. Why might using == with Integer objects sometimes return false even if they have the same value?Multiple choiceHard
  8. In Java, what is a potential issue with using autoboxing with primitive types in a loop?Multiple choiceMedium
  9. Why does avoiding floating-point types for monetary calculations matter in practice?Multiple choiceEasy
  10. Why is it advisable to use getter methods instead of directly accessing a class's fields?Multiple choiceEasy
  11. Given a record Product(String name, BigDecimal price), what is a potential issue with setting the price inappropriately?Multiple choiceMedium
  12. Why do teams use enums in Java in production code?Multiple choiceEasy
  13. How can you iterate over a HashMap to get both keys and values?Multiple choiceMedium
  14. Which principle does encapsulation primarily support?Multiple choiceEasy
  15. Given two BigDecimal objects, BigDecimal a = new BigDecimal("1.0") and BigDecimal b = new BigDecimal("1.00"), what is the result of a.equals(b)?Multiple choiceHard
  16. How would you explain a checked exception in a Java interview?Multiple choiceEasy
  17. Which of the following code snippets demonstrates a weak object design in Java?Multiple choiceMedium
  18. Which line contains a compile-time error in the following code snippet?Multiple choiceMedium
  19. When refactoring procedural code into an object-oriented design, what is a common indicator that a set of related functions and variables should become a class?Multiple choiceMedium
  20. How would you explain a common method to ensure an object's immutability in a Java interview?Multiple choiceMedium
  21. What will be the result of the following code snippet when calculating the final cart total?Multiple choiceMedium
  22. Which value of an instance variable of type int in Java is the default in Java?Multiple choiceEasy
  23. Why is it useful to trace one checkout example by hand before trusting the program output?Multiple choiceMedium
  24. Which Java keyword restricts access to variables and methods to the containing class only?Multiple choiceEasy
  25. How would you explain a common pitfall when using autoboxing with wrapper classes in a loop in an interview?Multiple choiceHard
  26. Given comparing two Integer objects using == if both have the same value, what outcome should you expect?Multiple choiceEasy
  27. How can immutability help in multi-threaded Java applications?Multiple choiceHard
  28. What does the 'protected' keyword allow in Java?Multiple choiceEasy
  29. How would you explain a common technique to achieve immutability in a Java class in an interview?Multiple choiceEasy
  30. Which method would you use to ensure a BigDecimal representing money is rounded to two decimal places using half-up rounding?Multiple choiceMedium
  31. Why do teams use enums over constant integer values in Java in production code?Multiple choiceEasy
  32. Why does the 'finally' block in a try-catch-finally statement matter in this design?Multiple choiceEasy
  33. Which scale should you use when constructing a BigDecimal for currency representation?Multiple choiceEasy
  34. Given a record Order(String id, BigDecimal amount), how can you ensure the amount is never null upon creation?Multiple choiceMedium
  35. Consider the class Product with a method applyDiscount(double discount). If the discount calculation logic is complex and used in multiple places, what is a…Multiple choiceHard
  36. Given a TreeSet storing product IDs, why might a new product ID not be added even if it appears to be unique?Multiple choiceHard
  37. Why does calling stripTrailingZeros() on a BigDecimal change runtime behavior?Multiple choiceMedium
  38. In the provided code, why might the exception be swallowed?Multiple choiceHard
  39. Which of the following best describes an object in Java?Multiple choiceEasy
  40. In an e-commerce application, you have a class ShoppingCart that contains a method addProduct(Product product). The method directly modifies a List<Product>…Multiple choiceMedium
  41. In a class Cart, you find a method calculateDiscount() that accesses another class's private fields directly. What is a better design approach?Multiple choiceHard
  42. In an e-commerce app, you have a class Order with a method calculateTotal(). The method uses variables defined in the method body. What is a sign that some of…Multiple choiceHard
  43. When iterating over a LinkedHashMap, in what order are the entries returned?Multiple choiceMedium
  44. In Java, what is the main benefit of using classes to model real-world entities in an e-commerce application?Multiple choiceEasy
  45. How can you ensure a resource is closed after use, regardless of whether an exception occurs?Multiple choiceMedium
  46. Which of the following statements is true about BigDecimal arithmetic operations?Multiple choiceEasy
  47. In a domain-driven design context, why are value objects preferred over entity objects for certain data types?Multiple choiceHard
  48. Why is using BigDecimal preferred for financial calculations in Java?Multiple choiceHard
  49. What does the following expression evaluate to?Multiple choiceEasy
  50. What will be printed by the following code snippet?Multiple choiceMedium