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)
- In a nested try-catch block, if an exception is thrown in the inner try block and not caught there, what happens?Multiple choiceHard
- What does executing a throw statement inside a catch block evaluate to?Multiple choiceMedium
- In Java, what happens if an exception is thrown in a method and not caught within that method?Multiple choiceEasy
- Which method would you use to add an element to a List at a specific position?Multiple choiceEasy
- Consider the following code snippet. What is the issue with the 'applyDiscount' method?Multiple choiceHard
- How do you correctly initialize a BigDecimal to represent the monetary value 50.75?Multiple choiceMedium
- Why might using == with Integer objects sometimes return false even if they have the same value?Multiple choiceHard
- In Java, what is a potential issue with using autoboxing with primitive types in a loop?Multiple choiceMedium
- Why does avoiding floating-point types for monetary calculations matter in practice?Multiple choiceEasy
- Why is it advisable to use getter methods instead of directly accessing a class's fields?Multiple choiceEasy
- Given a record Product(String name, BigDecimal price), what is a potential issue with setting the price inappropriately?Multiple choiceMedium
- Why do teams use enums in Java in production code?Multiple choiceEasy
- How can you iterate over a HashMap to get both keys and values?Multiple choiceMedium
- Which principle does encapsulation primarily support?Multiple choiceEasy
- 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
- How would you explain a checked exception in a Java interview?Multiple choiceEasy
- Which of the following code snippets demonstrates a weak object design in Java?Multiple choiceMedium
- Which line contains a compile-time error in the following code snippet?Multiple choiceMedium
- 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
- How would you explain a common method to ensure an object's immutability in a Java interview?Multiple choiceMedium
- What will be the result of the following code snippet when calculating the final cart total?Multiple choiceMedium
- Which value of an instance variable of type int in Java is the default in Java?Multiple choiceEasy
- Why is it useful to trace one checkout example by hand before trusting the program output?Multiple choiceMedium
- Which Java keyword restricts access to variables and methods to the containing class only?Multiple choiceEasy
- How would you explain a common pitfall when using autoboxing with wrapper classes in a loop in an interview?Multiple choiceHard
- Given comparing two Integer objects using == if both have the same value, what outcome should you expect?Multiple choiceEasy
- How can immutability help in multi-threaded Java applications?Multiple choiceHard
- What does the 'protected' keyword allow in Java?Multiple choiceEasy
- How would you explain a common technique to achieve immutability in a Java class in an interview?Multiple choiceEasy
- Which method would you use to ensure a BigDecimal representing money is rounded to two decimal places using half-up rounding?Multiple choiceMedium
- Why do teams use enums over constant integer values in Java in production code?Multiple choiceEasy
- Why does the 'finally' block in a try-catch-finally statement matter in this design?Multiple choiceEasy
- Which scale should you use when constructing a BigDecimal for currency representation?Multiple choiceEasy
- Given a record Order(String id, BigDecimal amount), how can you ensure the amount is never null upon creation?Multiple choiceMedium
- 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
- Given a TreeSet storing product IDs, why might a new product ID not be added even if it appears to be unique?Multiple choiceHard
- Why does calling stripTrailingZeros() on a BigDecimal change runtime behavior?Multiple choiceMedium
- In the provided code, why might the exception be swallowed?Multiple choiceHard
- Which of the following best describes an object in Java?Multiple choiceEasy
- 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
- 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
- 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
- When iterating over a LinkedHashMap, in what order are the entries returned?Multiple choiceMedium
- In Java, what is the main benefit of using classes to model real-world entities in an e-commerce application?Multiple choiceEasy
- How can you ensure a resource is closed after use, regardless of whether an exception occurs?Multiple choiceMedium
- Which of the following statements is true about BigDecimal arithmetic operations?Multiple choiceEasy
- In a domain-driven design context, why are value objects preferred over entity objects for certain data types?Multiple choiceHard
- Why is using BigDecimal preferred for financial calculations in Java?Multiple choiceHard
- What does the following expression evaluate to?Multiple choiceEasy
- What will be printed by the following code snippet?Multiple choiceMedium