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 2 of 10)
- Which keyword in Java is used to ensure that a method cannot be overridden by subclasses?Multiple choiceEasy
- Which method would you use to convert a BigDecimal to a plain string representation without scientific notation?Multiple choiceMedium
- When dealing with currency in Java, why is it crucial to use BigDecimal instead of float or double?Multiple choiceEasy
- Which Java collection would you use if you need to store a list of product IDs that allows duplicates and maintains insertion order?Multiple choiceEasy
- In an e-commerce application, what is a safe way to handle a price field in a Product class?Multiple choiceMedium
- Which Java collection would you choose if you need to maintain the order of insertion without allowing duplicate values?Multiple choiceEasy
- Which bug is the main problem in the following code snippet that tries to calculate the total cart value?Multiple choiceEasy
- Which of the following statements correctly creates a BigDecimal that represents the monetary value 19.99?Multiple choiceHard
- In an e-commerce application, how can encapsulation improve the design of a class representing a shopping cart?Multiple choiceMedium
- Which statement is true about the 'if' condition in Java?Multiple choiceEasy
- Which statement correctly updates the 'total' variable to add a discount?Multiple choiceMedium
- What does comparing two BigDecimal objects using compareTo() method evaluate to?Multiple choiceMedium
- What does this minimal total-printing snippet output?Multiple choiceEasy
- Why do teams use private access modifiers for fields in a Java class in production code?Multiple choiceEasy
- How can BigDecimal pitfalls be avoided when performing financial calculations?Multiple choiceMedium
- Given a static factory method in a class Product, how does it improve object creation over constructors?Multiple choiceMedium
- Identify the logical error in the following checkout process code?Multiple choiceHard
- In a simple e-commerce application, you have a variable for the price of a product. Which one of the following variable declarations is correct for a product…Multiple choiceEasy
- In which scenario might using BigDecimal be less suitable compared to other types?Multiple choiceHard
- Which Java keyword would you use to manually throw an exception in Java?Multiple choiceEasy
- How does a LinkedList differ from an ArrayList in terms of element insertion efficiency?Multiple choiceMedium
- Given a class with public fields in an e-commerce application, what is a likely issue?Multiple choiceMedium
- What would happen if you try to add a null key to a TreeMap?Multiple choiceHard
- Consider a HashMap storing product names as keys and their prices as values. What potential issue should you consider when using mutable objects as keys?Multiple choiceHard
- How can you correct the following code to ensure the discount is applied correctly?Multiple choiceMedium
- How would you explain a key advantage of using Java records for domain modeling in a modern Java application in an interview?Multiple choiceEasy
- You have a method calculateShippingCost() that takes multiple parameters related to an order. What refactoring approach should you consider?Multiple choiceMedium
- How would you correctly round a BigDecimal to two decimal places using half-up rounding mode?Multiple choiceMedium
- Which of the following exceptions is a checked exception?Multiple choiceEasy
- In a class Inventory, you notice several methods that manipulate a List<Product>. When refactoring, what is a better object-oriented approach?Multiple choiceHard
- When refactoring a procedural e-commerce script into an object-oriented design, what is a sign that a group of related functions should become a method in a…Multiple choiceMedium
- In the following code, which exception will be thrown if 'array' is null?Multiple choiceHard
- In an e-commerce application, you have the following procedural code managing a list of products. What is a good candidate for refactoring this into an…Multiple choiceEasy
- When handling exceptions, why is it important to avoid catching the generic Exception class?Multiple choiceMedium
- Consider a class where a public field references a mutable list. What is a safer design approach for this scenario?Multiple choiceHard
- Which of the following is the correct way to subtract one BigDecimal from another?Multiple choiceMedium
- What is true about a class marked as 'abstract' in Java?Multiple choiceEasy
- In a HashMap, what happens when two keys have the same hash code?Multiple choiceMedium
- What does this LinkedHashSet print after inserting a duplicate fruit?Multiple choiceMedium
- Which of the following is a logic error when applying a discount to a product?Multiple choiceMedium
- Consider a method public static Product createProduct(String name, BigDecimal price). What design pattern does this method demonstrate?Multiple choiceMedium
- Which method would you use to efficiently check the presence of a key in a HashMap?Multiple choiceMedium
- Which statement best describes exception propagation in Java?Multiple choiceMedium
- Why do teams use BigDecimal for financial applications in production code?Multiple choiceEasy
- Why can using too many setters in a class become a problem in practice?Multiple choiceMedium
- Why can using public fields in a Java class become risky in production code?Multiple choiceMedium
- You notice that a Java class uses a mix of public fields and methods. What refactoring strategy can improve its encapsulation?Multiple choiceHard
- How would you explain a potential consequence of not overriding hashCode() when overriding equals() in a class used as a key in a HashMap in an interview?Multiple choiceHard
- In the following code, which line will print to the console when an exception occurs in processOrder()?Multiple choiceMedium
- How can you correct the following code to ensure 'finalPrice' is calculated correctly?Multiple choiceHard