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 3 of 10)
- In the following code snippet, what will be the value of 'isEligibleForDiscount' after execution?Multiple choiceMedium
- Given the class Customer with a private field email, how can you provide access to this field in a controlled manner?Multiple choiceMedium
- Given a list of product names, which collection would help you efficiently find out how many times each product appears?Multiple choiceMedium
- How would you explain the time complexity for retrieving an element by index from an ArrayList in an interview?Multiple choiceMedium
- When reading code, what is a sign that a class might have an unsafe design?Multiple choiceMedium
- What potential problem can arise from using mutable static fields in a class?Multiple choiceMedium
- How would you explain a potential issue with a class design where most fields are set to public in an e-commerce application in an interview?Multiple choiceHard
- Which Java collection allows you to store key-value pairs, ensuring unique keys?Multiple choiceEasy
- How does the Optional class help in modern Java code?Multiple choiceEasy
- In Java, what is the purpose of the 'throws' keyword in a method declaration?Multiple choiceMedium
- Consider an enum PaymentStatus with values PENDING, COMPLETED, FAILED. How would you extend it to include a method that returns a user-friendly message for…Multiple choiceHard
- If you need to store a monetary value in a database and perform calculations, why might you choose BigDecimal over a database's native decimal type?Multiple choiceHard
- Which Java collection is best suited for maintaining a collection of unique elements that need to be accessed in order of insertion?Multiple choiceEasy
- How does the Optional class improve code readability and safety?Multiple choiceMedium
- Why does the following code snippet result in a logical error?Multiple choiceMedium
- Why is BigDecimal preferred over double for handling monetary values in Java?Multiple choiceEasy
- Why might using a TreeSet be less efficient than a HashSet for frequent insertions and deletions?Multiple choiceHard
- When storing a monetary value in a BigDecimal, what is a recommended best practice to avoid precision issues?Multiple choiceMedium
- Which collection is most suitable for storing unique elements with no concern for order?Multiple choiceEasy
- In a HashSet, what determines the position of an element?Multiple choiceMedium
- Why does fields in a Java class matter in this design?Multiple choiceEasy
- How can the following code snippet be optimized to avoid unnecessary calculations?Multiple choiceHard
- Consider a class Customer with a method addToOrderHistory(Product product). Which aspect of this design could indicate a need for refactoring?Multiple choiceMedium
- Why do teams use custom exceptions in Java in production code?Multiple choiceMedium
- Which of the following statements about constructors in Java is true?Multiple choiceMedium
- Why is it important to specify a rounding mode when performing division with BigDecimal?Multiple choiceEasy
- In a value object pattern, what characteristic is essential to ensure the integrity of the data?Multiple choiceMedium
- Which line of code will cause a logical flaw in the following snippet?Multiple choiceMedium
- In an e-commerce application, a 'Customer' class has a 'getOrders()' method that returns a list of orders. What is a safe practice for handling this list?Multiple choiceMedium
- In an e-commerce system, a 'Product' class has a method 'setPrice(double price)'. What could be a potential issue with this setter method?Multiple choiceMedium
- What will be the result of the following code?Multiple choiceEasy
- In a class Order, you notice that the constructor initializes fields that are never used again. What is likely a better design choice?Multiple choiceHard
- Why can having many public fields in a Java class become a bug in practice?Multiple choiceEasy
- In a nested try-catch block, where is an exception handled if both inner and outer blocks have catch clauses for the same exception?Multiple choiceHard
- Given the following code snippet, what will be the output?Multiple choiceMedium
- Which of the following practices best helps maintain invariants in a Java class?Multiple choiceMedium
- Which approach is the best fit to add two BigDecimal objects, value1 and value2?Multiple choiceMedium
- Why is it generally a bad idea to catch the Exception class directly in Java?Multiple choiceHard
- How would you explain a key benefit of using Java records over traditional classes for immutable data in an interview?Multiple choiceEasy
- What happens if you try to modify an immutable object in Java?Multiple choiceMedium
- Why is BigDecimal usually preferred over double for money calculations?Multiple choiceMedium
- In an e-commerce application, consider a Customer class that has public fields for id and name. What is a potential risk with this design?Multiple choiceMedium
- A Java class has a mutable collection exposed through a public getter. What refactoring ensures encapsulation while maintaining functionality?Multiple choiceHard
- Which block is always executed in a try-catch-finally structure, regardless of whether an exception is thrown?Multiple choiceEasy
- Consider a static factory method public static Order createOrder(String id, BigDecimal amount). What flexibility does this pattern provide over direct…Multiple choiceHard
- How does a try-with-resources statement differ from a regular try-catch-finally block?Multiple choiceMedium
- Why does a value object in domain-driven design matter in practice?Multiple choiceEasy
- Which method would you use to ensure two BigDecimal values representing money are considered equal if they have the same numeric value but different scales?Multiple choiceHard
- What happens if you try to retrieve a value from a HashMap using a key that is not present?Multiple choiceMedium
- Which refactoring step can help convert a setter-heavy design to a safer design pattern?Multiple choiceHard