[Go to site: main page, start]

0% found this document useful (0 votes)
52 views4 pages

Java Control Structures Practice Questions

The document provides a comprehensive list of practice questions and solutions for Java control structures, categorized into sections: IF-ELSE, WHILE, DO-WHILE, and FOR. Each section is divided into beginner, intermediate, and advanced level questions, covering various topics such as number classification, loops, and patterns. It serves as a resource for learners to enhance their understanding and application of Java control structures.

Uploaded by

kumud yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views4 pages

Java Control Structures Practice Questions

The document provides a comprehensive list of practice questions and solutions for Java control structures, categorized into sections: IF-ELSE, WHILE, DO-WHILE, and FOR. Each section is divided into beginner, intermediate, and advanced level questions, covering various topics such as number classification, loops, and patterns. It serves as a resource for learners to enhance their understanding and application of Java control structures.

Uploaded by

kumud yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Control Structures Practice Questions & Solutions

Section: IF-ELSE

Beginner Level Questions

1. Check if a number is even or odd.

2. Check if a number is positive, negative, or zero.

3. Find the largest of two numbers.

4. Check if a year is a leap year.

5. Check if a person is eligible to vote (age >= 18).

Intermediate Level Questions

6. Find the largest among three numbers.

7. Check if a character is a vowel or consonant.

8. Find the grade from marks: 90+ -> A, 80-89 -> B, 70-79 -> C, below 70 -> F.

9. Check if a number is divisible by 5 and 11.

10. Check if a triangle is valid (sum of 3 angles = 180 degrees).

Advanced Level Questions

11. Classify a triangle based on sides (Equilateral, Isosceles, Scalene).

12. Check if a number is a palindrome using only if-else.

13. Check if a number is Armstrong or not using only if-else.

14. Create a mini ATM menu with options using if-else.

Section: WHILE

Beginner Level Questions

1. Print numbers from 1 to 10.

2. Print even numbers up to N.

3. Calculate sum of digits of a number.

4. Reverse a number.

5. Print multiplication table of a number.


Java Control Structures Practice Questions & Solutions

Intermediate Level Questions

6. Count the number of digits in a number.

7. Find the factorial of a number.

8. Check if a number is a palindrome.

9. Generate Fibonacci series up to N terms.

10. Count vowels in a word.

Advanced Level Questions

11. Find GCD (HCF) of two numbers using loop.

12. Find LCM of two numbers.

13. Convert binary to decimal.

14. Generate prime numbers between 1 and N using loop.

Section: DO-WHILE

Beginner Level Questions

1. Print menu until user chooses to exit.

2. Read numbers until user enters a negative number.

3. Repeat asking password until it matches the original.

4. Sum of N natural numbers.

5. Print table of a number till user wants.

Intermediate Level Questions

6. Create a simple number guessing game.

7. Keep accepting marks till user enters -1, then print average.

8. Count even and odd numbers till user stops.

9. Validate input: user must enter number between 1 to 10.

10. Find cube of numbers entered till user says 'no'.


Java Control Structures Practice Questions & Solutions

Advanced Level Questions

11. Simulate ATM operations with continuous options (Withdraw, Deposit, Exit).

12. Password retry system (3 tries max).

13. Find repeated digits in a number.

14. Print all digits of a number in reverse.

Section: FOR

Beginner Level Questions

1. Print numbers from 1 to N.

2. Print the square of first N numbers.

3. Print factorial of a number using for.

4. Print sum of all even numbers up to N.

5. Print a simple star pattern:

**

***

****

Intermediate Level Questions

6. Print reverse star pattern:

****

***

**

7. Print number triangle:

12

123

1234
Java Control Structures Practice Questions & Solutions

8. Find sum of series: 1 + 2 + 3 + ... + N

9. Check if a number is prime.

10. Count frequency of a digit in a number.

Advanced Level Questions

11. Print pyramid pattern:

***

*****

12. Print Floyd's Triangle.

13. Check Armstrong number using for loop.

14. Generate multiplication table for 1 to 10.

15. Find all prime numbers between 1 and 100.

Common questions

Powered by AI

To check if a number is a palindrome using 'if-else' and loops, first, use a loop to reverse the digits of the number by repeatedly taking the modulus to extract digits and store them in reverse order. Then, use 'if-else' to compare the reversed number to the original. If they match, it's a palindrome; otherwise, it's not .

To generate the Fibonacci series up to N terms using a loop in Java, initialize the first two numbers of the Fibonacci sequence and use a loop to iterate from the third position up to N, calculating each subsequent number by summing the two preceding numbers. Output each number during iteration to obtain the full series .

To check Armstrong numbers using loops and conditional statements, use a loop to separate individual digits of a number, raise each digit to the power equal to the total number of digits, and sum these values. After calculating the sum, use an 'if' condition to compare this sum to the original number. If they are identical, the number is an Armstrong number. This requires extracting digits, performing power operations, and comparison using 'if-else' .

To implement a password retry system with a three-try limit, use a 'do-while' loop to ask for the password input, and an 'if' condition to check its correctness. Maintain a counter for attempts and increment it with each invalid try. If the counter exceeds three before a correct password is entered, the system should exit or lock further attempts. Use informative prompts to guide the user .

A while loop can be used to count the number of digits by continuously dividing the number by 10 until it reaches zero. Each division operation effectively removes one digit from the end of the number. Increment a counter variable each time the loop runs, which will give the total count of digits in the number once the loop finishes .

To verify if a triangle is valid based on its angles using control statements, sum the three angles. Then, use an 'if-else' conditional structure to check if this sum is exactly 180 degrees. If true, the angles indicate a valid triangle; otherwise, it's invalid .

Printing pyramid patterns using loop structures in Java involves two nested loops: an outer loop to manage the number of rows and an inner loop to print spaces and asterisks. The space loop should decrease as the row number increases, while the asterisk loop prints a progressive number of asterisks, forming a pyramid shape visually. This requires precise control of iteration bounds to achieve symmetry .

Using a 'for' loop to calculate the factorial of a number is significant because it provides a clear, iterative method that initializes a counter to 1 and multiplies it by each integer up to the number itself. This approach is efficient as it ensures all numbers from 1 to the desired value are considered, and it succinctly avoids issues like stack overflow that might arise in recursive methods .

To determine if a year is a leap year using Java control structures, utilize an 'if-else' statement to check two conditions: first, if the year is divisible by 4. If true, then check if the year is not divisible by 100 unless it is also divisible by 400. A leap year is one that satisfies these conditions: divisible by 4, not divisible by 100 unless it is also divisible by 400 .

To determine if a number is divisible by both 5 and 11, apply an 'if-else' structure to check the number against both conditions: the number modulus 5 should be zero, and similarly, the number modulus 11 should also be zero. If both conditions are met, the number is divisible by both 5 and 11 .

You might also like