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 4 of 10)
- You have two BigDecimal values: a = new BigDecimal("0.1") and b = new BigDecimal("0.10"). What is the result of a.compareTo(b)?Multiple choiceHard
- Given a try-catch-finally block if a return statement is executed in both the try and finally blocks, what outcome should you expect?Multiple choiceEasy
- Identify the flaw in this code that calculates an order total with tax, where 'taxRate' is 0.1?Multiple choiceHard
- In the context of encapsulation, why should a class in an e-commerce system avoid exposing collections directly?Multiple choiceHard
- Which collection should you use if you need to frequently access elements by their index?Multiple choiceEasy
- What happens if a NullPointerException is thrown but not caught in a method?Multiple choiceMedium
- Which approach is recommended for handling frequent monetary operations to maintain accuracy and performance?Multiple choiceMedium
- In Java, how can you ensure that a class has only one instance?Multiple choiceMedium
- What happens if you divide one BigDecimal by another without specifying a rounding mode and the result cannot be represented exactly?Multiple choiceMedium
- What will be the value of 'totalPrice' after executing the following code? int unitPrice = 20; int quantity = 3; int totalPrice = unitPrice * quantity;?Multiple choiceEasy
- In a console shopping cart application, why might you separate input handling from calculations?Multiple choiceHard
- What happens if a subclass does not implement an abstract method from its superclass?Multiple choiceEasy
- Why would you choose to use a switch statement instead of multiple if-else statements for handling order status?Multiple choiceMedium
- Which part of the following code is responsible for avoiding an off-by-one error?Multiple choiceMedium
- Why can using overly complex methods in an e-commerce application become a problem in production code?Multiple choiceMedium
- How can a class implementing an interface with a default method call that method from within an overridden method?Multiple choiceMedium
- What keyword is used to inherit class properties in Java?Multiple choiceEasy
- If 'boolean onSale = false; int quantity = 10;', what does the expression 'onSale || quantity > 5' evaluate to?Multiple choiceEasy
- In Java, which of the following statements about abstract classes and interfaces is correct?Multiple choiceMedium
- How can you check if a variable 'points' is exactly 100?Multiple choiceEasy
- What happens when you use a switch statement with an integer variable representing shipping zones to apply specific fees?Multiple choiceMedium
- In the following code, what will happen if 'cartItems' is an empty array? for(int i = 0; i < cartItems.length; i++) { total += cartItems[i].price; }?Multiple choiceMedium
- Why does the 'System.out.println()' method in a Java console application matter in practice?Multiple choiceEasy
- Why does hashCode in a HashSet matter in this design?Multiple choiceEasy
- Given the following loop if no prices are over $100, what outcome should you expect?Multiple choiceHard
- In a console application, what common issue arises when using Scanner's nextLine() method after nextInt()?Multiple choiceHard
- Which design principle helps decide whether to use an interface or an abstract class?Multiple choiceMedium
- Why might using double for currency calculations in an e-commerce application lead to inaccuracies?Multiple choiceMedium
- When converting a double representing a price to BigDecimal, which constructor should you avoid?Multiple choiceMedium
- Which of the following statements is true about default methods in interfaces?Multiple choiceEasy
- Why does using int division to calculate the average price of items in a cart change runtime behavior?Multiple choiceHard
- Which of the following is true about a class that implements an interface in Java?Multiple choiceEasy
- How does a HashMap determine the bucket in which to store an entry?Multiple choiceMedium
- How can you separate input handling from business logic in a simple Java console application?Multiple choiceMedium
- Identify the logical error in the following statement: boolean freeShipping = (cartTotal > 50) || (hasCoupon = true);?Multiple choiceHard
- In what scenario can a default method in an interface be overridden?Multiple choiceMedium
- Why is it a bad practice to use mutable fields in equals and hashCode implementations?Multiple choiceMedium
- Given the following code snippet, what output should you expect?Multiple choiceMedium
- Why might using the 'instanceof' operator in a method implementation be considered a bad practice?Multiple choiceHard
- Why do teams use an abstract class in Java in production code?Multiple choiceEasy
- A method named calculateFinalPrice takes a price and a discount rate. Which of the following best ensures the method's reliability?Multiple choiceHard
- Why is it important for hashCode to be consistent with equals when using objects as keys in a HashMap?Multiple choiceMedium
- What does the following code snippet if the total cart amount is 100 and free shipping applies to amounts over 50 evaluate to?Multiple choiceMedium
- When performing currency conversion, why choose BigDecimal over double?Multiple choiceHard
- Which loop structure would you use to ensure a piece of code runs at least once to add taxes to a cart total?Multiple choiceMedium
- What will be the result of the following expression: 'int result = 20 / 4 + 3 * 2 - 1;'?Multiple choiceHard
- If an interface defines a default method and a class implements this interface but does not override the default method, what happens when the method is…Multiple choiceMedium
- How would you correctly add two BigDecimal values representing product prices in Java?Multiple choiceMedium
- Given the following code snippet, what will be the value of 'discount' after execution? boolean isMember = true; int totalAmount = 150; double discount =…Multiple choiceMedium
- Which method should be overridden to ensure correct behavior of keys in a HashMap?Multiple choiceMedium