Conditional statements in Java

Harshh🎀
4 min readNov 4, 2024

--

Here is a list of common interview questions involving conditional statements in Java. These questions test your ability to use `if`, `if-else`, `if-else if`, and `switch` statements to solve various problems.

— -

### **Basic Questions**

1. **What is a conditional statement, and why is it used in programming?**
— Explain the concept of conditional statements and discuss common types (`if`, `else`, `switch`).

2. **Explain the difference between `if` and `if-else` statements.**
— Describe when you would use `if` alone versus `if-else`.

3. **What is the purpose of the `switch` statement?**
— Describe how a `switch` statement works and provide examples of when it’s preferable to use over `if-else`.

— -

### **Programming Questions**

4. **Write a program to check if a number is positive, negative, or zero.**
— Use an `if-else if-else` statement to evaluate the number’s status.

5. **Create a program to determine if a number is even or odd.**
— Use an `if-else` statement to check if the number is divisible by 2.

6. **Write a program to check if a person is eligible to vote (18 or older).**
— Use an `if` statement to determine eligibility based on age.

7. **Develop a program that checks if a given year is a leap year.**
— Use an `if-else` structure to evaluate if the year is divisible by 4, 100, and 400.

8. **Using a `switch` statement, write a program that displays the day of the week for a given number (1 for Sunday, 2 for Monday, etc.).**
— Implement a `switch` case for each day of the week.

9. **Write a program to find the largest of three numbers.**
— Use an `if-else if-else` structure to compare three numbers and determine the largest.

10. **Check if a character is a vowel or consonant using an `if-else` statement.**
— Use `if-else` to check if the character is one of the vowels (`a`, `e`, `i`, `o`, `u`) or a consonant.

11. **Write a program to classify a grade based on marks: A for 90–100, B for 80–89, etc.**
— Use `if-else if-else` or `switch` to assign grades based on given ranges.

12. **Create a program that reads the month number (1 to 12) and displays the number of days in that month.**
— Use a `switch` statement, handling cases for each month.

13. **Write a program that checks if a given number is within a specified range (e.g., 1–100).**
— Use an `if` statement to check if the number falls within the range.

— -

### **Intermediate and Advanced Questions**

14. **Create a program to calculate the absolute value of a number.**
— Use an `if` statement to determine if the number is negative and, if so, convert it to positive.

15. **Write a program to check if a character is uppercase, lowercase, or not a letter.**
— Use `if-else` statements and ASCII values to classify the character.

16. **Design a program that takes the temperature as input and classifies it as “Cold”, “Warm”, or “Hot”.**
— Use `if-else if-else` to set ranges for different temperature classifications.

17. **Write a program to simulate a simple calculator that performs addition, subtraction, multiplication, or division based on user input.**
— Use a `switch` statement to determine the operation based on the user’s choice.

18. **Write a program that prompts for a username and password and checks if they are correct.**
— Use nested `if` statements to verify the inputs for authentication.

19. **Create a program that assigns a discount based on purchase amount: 10% if above $100, 20% if above $500, etc.**
— Use `if-else if-else` to assign discounts based on ranges of the purchase amount.

20. **Check if three given lengths can form a triangle.**
— Use the triangle inequality theorem with `if` statements to validate.

21. **Develop a program to check if a given year, month, and day are a valid date.**
— Use `if-else` and `switch` statements to verify the day count in each month, accounting for leap years.

22. **Write a program to determine if a number is a palindrome.**
— Use conditional logic to check if the reverse of the number matches the original.

23. **Create a program that calculates the electricity bill based on usage in kWh, applying different rates for various ranges of consumption.**
— Use `if-else if-else` to apply rates based on ranges of consumption.

24. **Write a program to check if a number is divisible by both 3 and 5, by either 3 or 5, or by neither.**
— Use `if-else if-else` to test divisibility and print results accordingly.

25. **Create a program to classify a number as small, medium, or large based on given thresholds.**
— Use `if-else if-else` to classify based on ranges.

— -

### **Logical Reasoning Questions**

26. **Explain what happens if you don’t include a `break` statement in a `switch` case.**
— Discuss how omitting `break` leads to fall-through behavior.

27. **What is the difference between `==` and `equals()` when used in conditional statements?**
— Explain when to use `==` and when to use `.equals()` for comparison.

28. **How would you refactor a complex `if-else if-else` structure with multiple conditions?**
— Discuss simplifying complex conditional logic with nested conditions or using a `switch` statement.

29. **What’s the purpose of the `default` case in a `switch` statement?**
— Explain the role of `default` and its importance in handling unexpected values.

30. **Can a `switch` statement be used with strings in Java? If so, in which version was this introduced?**
— Explain Java’s support for `switch` with strings, introduced in Java 7.

— -

These questions will help you practice various conditional statement scenarios and improve your ability to apply logical reasoning in different coding situations. Preparing answers and sample code for these will strengthen your understanding of Java conditionals for interviews.

--

--

No responses yet