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 7 of 10)
- Given a class with mutable fields, what is a potential issue if instances are used as keys in a HashMap?Multiple choiceHard
- In an e-commerce application, what is the correct way to check if a customer is eligible for a discount if they are a 'premium' member using an if-else…Multiple choiceEasy
- How would you explain a common pattern for accumulating totals in a loop in an interview?Multiple choiceMedium
- Which of the following is true about using nested if-else statements to handle multiple promotion rules?Multiple choiceHard
- Which operator is used to increase a product's quantity by one?Multiple choiceEasy
- In a class hierarchy, which rule determines method resolution if both a superclass and an interface provide implementations?Multiple choiceMedium
- Which data type is most suitable for storing a product's price in an e-commerce application when precision is important?Multiple choiceEasy
- What should you consider when naming a method that checks if a cart is eligible for a discount?Multiple choiceEasy
- When are two objects considered equal in a Java Set?Multiple choiceEasy
- Which of the following expressions correctly calculates the new price after a 20% discount on a product?Multiple choiceMedium
- How can you achieve abstraction in Java?Multiple choiceMedium
- How can you ensure a method is reusable in different parts of an e-commerce application?Multiple choiceMedium
- What happens if two objects in a HashSet have different hashCodes but are considered equal by the equals method?Multiple choiceMedium
- Why is it important to implement a consistent equals method in a class?Multiple choiceEasy
- What will be the output of the following code snippet if the cart contains item prices {15, 25, 30}?Multiple choiceEasy
- How would you refactor a nested if-else statement checking multiple product codes to a switch statement for readability?Multiple choiceHard
- Given the following code, why does it fail to compile? interface A { default void foo() { } } interface B { default void foo() { } } class C implements A, B {…Multiple choiceHard
- How might using a mutable object as a key in a HashMap lead to subtle bugs?Multiple choiceHard
- Which Java collection would be most appropriate for storing unique product IDs in an e-commerce catalog?Multiple choiceMedium
- Which Scanner method is best for reading a price input as a floating-point number in a console-based e-commerce application?Multiple choiceEasy
- Consider the following method: boolean applyDiscount(double price, double discount). Which of the following changes would help it adhere to the single…Multiple choiceHard
- Why does the hashCode method in Java collections matter in practice?Multiple choiceEasy
- Which operator is used for numeric addition in Java?Multiple choiceEasy
- Which of the following correctly uses a switch statement to apply a discount based on the product category?Multiple choiceEasy
- How do you check if a non-premium customer should receive a loyalty discount using an if-else statement?Multiple choiceMedium
- In Java, which operator would you use to check if a product's stock is exactly 10?Multiple choiceEasy
- Which of the following loop guards is necessary to avoid an off-by-one error in a loop that processes each item in an array?Multiple choiceMedium
- What type of loop would be best to calculate the total price of items in a shopping cart when you know the number of items beforehand?Multiple choiceEasy
- How many times will the loop execute if the 'cart' array contains three items?Multiple choiceEasy
- What does a method return if its return type is declared as void?Multiple choiceEasy
- Which statement best matches the chapter's guidance on price types?Multiple choiceHard
- Given two products with prices stored as doubles, how can you ensure accurate total price calculation?Multiple choiceHard
- Which Java keyword would you use to declare a default method in a Java interface?Multiple choiceMedium
- What issue can arise if two unequal objects have the same hashCode in a HashSet?Multiple choiceHard
- Why does overriding hashCode but not equals in a Java class change runtime behavior?Multiple choiceMedium
- Why is it useful to breaking e-commerce store logic into smaller methods?Multiple choiceEasy
- Which of the following statements about interfaces in Java is true?Multiple choiceMedium
- What is branching doing in a rule like if (subtotal >= 100.0) shippingFee = 0.0;?Multiple choiceMedium
- Which method would you use to print a formatted string to the console without a newline at the end?Multiple choiceEasy
- Consider a class hierarchy where a class 'Product' extends 'Item' and implements 'Sellable'. If 'Item' has a method 'display()' and 'Sellable' has a default…Multiple choiceHard
- How can you convert a double to a BigDecimal in Java correctly?Multiple choiceMedium
- Given this code if 'cartTotal' is 75 and 'loyaltyMember' is false, what outcome should you expect?Multiple choiceHard
- What should you be cautious about when using int to store product quantities?Multiple choiceMedium
- Why does an accumulator variable in a loop matter in practice?Multiple choiceEasy
- If a class implements two interfaces with conflicting default methods, how should it resolve the conflict?Multiple choiceMedium
- Consider a superclass with a method that is not overridden in its subclass. Which method is called when the method is invoked on an instance of the subclass?Multiple choiceMedium
- What happens if you use 'System.out.printf()' with '%d' to print a double value?Multiple choiceMedium
- Why do teams use methods in a Java program in production code?Multiple choiceEasy
- When should a class be made final in Java?Multiple choiceMedium
- What is the result of the following code if 'prices' is an array of integers and initially 'total' is zero? while (index < prices.length) { total +=…Multiple choiceEasy