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 6 of 10)
- Which Java keyword would you use to define a default method in a Java interface?Multiple choiceEasy
- How can you modify a while loop to correctly calculate the total price only for items priced over $20, given the following code? int total = 0; while (index <…Multiple choiceHard
- How can a 'for loop' be used to calculate the total price of items only if they belong to a specific category?Multiple choiceMedium
- In an e-commerce checkout system, how would you structure nested conditions to check if an item is in stock and if the user has enough balance?Multiple choiceMedium
- Which statement correctly calculates the total number of items in a shopping cart?Multiple choiceMedium
- When is calculateSubtotal(...) a good method extraction?Multiple choiceMedium
- What is the result of the following expression? int price = 50; boolean isEligibleForDiscount = price < 100 && price!= 0;?Multiple choiceMedium
- Why might the following while loop cause an infinite loop when calculating a cart total?Multiple choiceHard
- Which of the following correctly determines if a boolean 'isPremiumMember' is set to true?Multiple choiceEasy
- In an inheritance hierarchy, what is a reason to choose composition over inheritance?Multiple choiceMedium
- Given the following code, what concept does it best illustrate?Multiple choiceHard
- How can the following loop be optimized to stop early if a price over $100 is found?Multiple choiceHard
- Which Java keyword would you use to prevent a class from being subclassed in Java?Multiple choiceEasy
- In the context of collections, what is a key difference between a List and a Set in Java?Multiple choiceEasy
- Consider a boolean 'isAvailable' and a double 'userBalance'. How would you structure an if-else to ensure an item can be purchased if in stock and funds are…Multiple choiceMedium
- Consider the following code snippet. What will be the output when 'new Product().display();' is executed?Multiple choiceHard
- Which of the following methods is most likely violating the single responsibility principle in an e-commerce application?Multiple choiceHard
- Why can using deeply nested if-else statements in e-commerce checkout logic become a problem in practice?Multiple choiceHard
- Why are default methods in interfaces beneficial for API evolution?Multiple choiceMedium
- Which statement about method return values is true?Multiple choiceMedium
- Consider the following classes. What will be the output of 'new PremiumService().getInfo();'?Multiple choiceHard
- Given a class hierarchy where 'OnlineService' is a superclass and 'SubscriptionService' is a subclass, which method signature in 'SubscriptionService' would…Multiple choiceMedium
- What is a potential problem with the following code snippet when calculating cart totals? int i = 0; while (i < cart.length) { total += cart[i].price; }?Multiple choiceHard
- In an e-commerce application, you have a method double calculateTotalPrice(double[] prices). Which of the following is a good reason to use this method?Multiple choiceMedium
- What does this code if the cart total is 250 and free shipping is given for totals over 200 evaluate to?Multiple choiceMedium
- What will be the result of the following code when calculating the total cost with a discount?Multiple choiceMedium
- How can you ensure that a double value representing a price is displayed with a minimum of two fractional digits?Multiple choiceMedium
- Which of the following is NOT a valid reason to use interfaces with default methods?Multiple choiceEasy
- Why do teams use an interface over an abstract class in production code?Multiple choiceHard
- Why does a cart program need a loop when calculating a subtotal for many items?Multiple choiceMedium
- Which return type of a method that does not return any value is the default in Java?Multiple choiceEasy
- Given the following code snippet, identify the issue when storing objects in a HashSet?Multiple choiceHard
- Given the following code snippet, how can it be optimized to avoid unnecessary iterations when calculating total discounts? for (int i = 0; i <…Multiple choiceHard
- What will happen if you try to read an integer input using Scanner's nextInt() when the input is non-numeric?Multiple choiceHard
- What will be the output if the 'discounts' array is empty in the following loop?Multiple choiceMedium
- Which statement is true regarding the relationship between hashCode and equals?Multiple choiceMedium
- A developer wishes to use a custom object as a key in a HashMap. What must be carefully considered to prevent bugs?Multiple choiceHard
- When should you consider using a switch statement for managing product categories?Multiple choiceMedium
- Consider the method signature: double calculateDiscount(double price, double discountRate). What does this method do?Multiple choiceMedium
- In an e-commerce app, what does the following snippet print if 'isMember' is true?Multiple choiceEasy
- Why can using default methods in Java interfaces become a problem in production code?Multiple choiceMedium
- How can you represent a fixed percentage discount in Java?Multiple choiceMedium
- Given the following code snippet, what is the value of 'finalPrice'? int basePrice = 200; int discount = 30; int finalPrice = basePrice - discount *…Multiple choiceHard
- Given the method int addToCart(int currentQuantity, int newItems), what could be a potential return value?Multiple choiceMedium
- Why is it important to avoid duplicating logic in methods when developing an e-commerce application?Multiple choiceMedium
- How can polymorphism be achieved in Java?Multiple choiceMedium
- What happens if you use the division operator between two int variables in Java?Multiple choiceMedium
- Why did Java introduce default methods in interfaces?Multiple choiceEasy
- Given a variable 'productType', what switch statement structure can apply a handling fee based on the type?Multiple choiceMedium
- Given the code snippet, what will be the value of 'totalDiscount' after execution? int[] discounts = {5, 10, 15}; int totalDiscount = 0; for (int i = 0; i <=…Multiple choiceHard