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 5 of 10)
- Which data type is the best fit to use for storing the number of items in a shopping cart?Multiple choiceEasy
- How can you ensure a loop calculates the total of item prices only if they are above $50?Multiple choiceMedium
- If a cart contains prices {10, 20, 30}, what is the final value of 'total' after executing the following loop with an off-by-one error?Multiple choiceHard
- Why is it beneficial to avoid duplicating logic in methods when developing an e-commerce application?Multiple choiceHard
- What will be the value of 'totalCost' if executed with cartTotal = 100 and discountApplied = false in this code? int totalCost = cartTotal - (discountApplied?…Multiple choiceHard
- How do you structure a nested if-else to check if a product is eligible for both a sale and a loyalty discount?Multiple choiceHard
- What is the outcome of the following code? int quantity = 5; quantity += 3; quantity -= 2;?Multiple choiceMedium
- Which operator would you use to check if a product's price is greater than or equal to 100?Multiple choiceEasy
- In an e-commerce application, how would you correctly increment the stock of a product by 10 units using an assignment expression?Multiple choiceEasy
- You need to read a user's input for a product name in a console application. Which Scanner method should you use?Multiple choiceMedium
- If you need to ensure that multiplication of two BigDecimal values is precise, which method should you use?Multiple choiceHard
- What happens if you override only the equals method and not hashCode in a Java class used in a HashSet?Multiple choiceEasy
- How would you explain a common mistake when implementing equals and hashCode that can lead to incorrect Set behavior in an interview?Multiple choiceMedium
- In a console-based store application, how would you correctly format a price of 19.9 to always show two decimal places when printed?Multiple choiceMedium
- Consider the expression: int result = 3 + 5 * 2 - 8 / 4 % 3. What is the value of 'result'?Multiple choiceHard
- How would you explain a good practice when naming a method that calculates the total price in an e-commerce application in an interview?Multiple choiceMedium
- If you want to take a user's checkout confirmation input as 'yes' or 'no', which Scanner method should you use?Multiple choiceMedium
- Can a Java interface contain static methods?Multiple choiceEasy
- In a cart system, how would you use an if-else to apply a discount only if 'isWeekend' is true and 'cartValue' exceeds 150?Multiple choiceMedium
- Consider the following code snippet. What will be the output when 'new Customer().getDiscount();' is executed?Multiple choiceMedium
- Why might a method be refactored to accept a List<Product> instead of an array of Product?Multiple choiceHard
- Why do teams use parameters in a method in production code?Multiple choiceMedium
- Why might a class choose to explicitly invoke a default method from one of its interfaces?Multiple choiceHard
- Which of the following is an example of a numeric literal for a double in Java?Multiple choiceEasy
- What will be the result of adding the same object twice to a HashSet?Multiple choiceEasy
- How can a class explicitly call a default method from an interface it implements?Multiple choiceHard
- How will the expression '5 * (3 + 2) - 8 / 4' be evaluated?Multiple choiceHard
- When designing an in-memory catalog using HashMap, why is it risky to use mutable objects as keys?Multiple choiceHard
- Consider this code: 'Scanner scanner = new Scanner(System.in); int value = scanner.nextInt(); scanner.nextLine();'. What problem does adding…Multiple choiceHard
- Why split checkout math into named values like subtotal, discountAmount, and finalTotal instead of writing one long expression?Multiple choiceMedium
- Which of the following code snippets will correctly format a double value to two decimal places when printing?Multiple choiceMedium
- Consider a method that calculates the total price of items in a cart. What is a good naming convention for this method?Multiple choiceMedium
- Why does parameters in a method matter in this design?Multiple choiceEasy
- In a system with many similar services, what is a downside of using a deep inheritance hierarchy?Multiple choiceHard
- In a class used as a key in a HashMap, you override equals but not hashCode. What might happen?Multiple choiceHard
- How should you structure an if-else statement to verify both product availability and minimum order quantity?Multiple choiceMedium
- What happens if a class implements two interfaces, each with a default method with the same signature?Multiple choiceEasy
- What type of value does a method that checks inventory availability in a store likely return?Multiple choiceEasy
- You have a method that calculates a discount on a product price. How can you ensure this method adheres to the single responsibility principle?Multiple choiceHard
- Which of the following statements will declare a variable to store a product quantity?Multiple choiceEasy
- Which Java package provides the Scanner class for reading input from the console?Multiple choiceEasy
- Given the following code, why does it compile successfully? interface A { default void foo() {} } class B implements A { void foo() {…Multiple choiceHard
- Why should methods in Java adhere to the Single Responsibility Principle, especially in e-commerce applications?Multiple choiceHard
- Given a total price as a BigDecimal, what is the best way to apply a 10% discount?Multiple choiceHard
- Why might you use an abstract class instead of an interface with default methods?Multiple choiceMedium
- What is one main purpose of using interfaces in Java?Multiple choiceEasy
- Why is it not recommended to use float for calculating the subtotal of an online shopping cart?Multiple choiceHard
- In an e-commerce application, which condition checks if a product is eligible for a discount when its price is above 100?Multiple choiceEasy
- Which numeric type would you use to represent a product's price if you don't need precise decimal arithmetic?Multiple choiceEasy
- Which condition will correctly apply free shipping if 'cartTotal' is greater than or equal to 50?Multiple choiceMedium