100% found this document useful (1 vote)
267 views

ClassEx While Loop

The document contains 8 questions about while loops in Java programs. It asks how many times print statements will execute given different loop conditions. It also asks to trace the values of variables in a sample loop, create a letter input program using a while loop, and create a grade calculator program that takes user input in a while loop.

Uploaded by

david johnson
Copyright
© Attribution Non-Commercial (BY-NC)
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
100% found this document useful (1 vote)
267 views

ClassEx While Loop

The document contains 8 questions about while loops in Java programs. It asks how many times print statements will execute given different loop conditions. It also asks to trace the values of variables in a sample loop, create a letter input program using a while loop, and create a grade calculator program that takes user input in a while loop.

Uploaded by

david johnson
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 2

1.

How many times will “hello world” be printed in the following program:

int count = 0;
while (count < 10)
{
System.out.println(“Hello World”);
count++;
}

2. How many times will “hello world” be printed in the following program:

int count = 10;


while (count < 1)
{
System.out.println(“Hello World”);
count++;
}

3. How many times will “I love Java Programming !” be printed in the following program segment?

int count = 10;


while (count >= 10)
{
System.out.println(“I love Java Programming !”);
count++;
}

4. How many times will “I love Java Programming !” be printed in the following program segment?

int count = 0;
while (count < 10)
{
System.out.println(“I love Java Programming !”);
}

5. The code below is supposed to print the integers from 10 to 1 backwards. What is wrong with it?

count = 10;
while (count >= 0)
{
System.out.println(count);
count = count + 1;
}

6. Consider the following loop:

final int LIMIT = 16; sum nextVale count


int count = 1;
int sum = 0;
int nextVal = 2;
while (sum < LIMIT)
{
sum = sum + nextVal;
nextVal = nextVal + 2;
count = count + 1;
}
• Trace this loop, that is, in the table next to the code, show values for variables nextVal, sum
and count at each iteration.

7. Create a project called LetterInput. The Main class will use a while loop to continue asking the user
for a single character. The loop will stop iterating when the user enters the letter Q or q (upper or
lower case). If the letter T/t or P/p (again upper or lowercase) is entered, print the message “Invalid
letter” to the screen and ask user to enter another letter. At the end, print out the total number of valid
letters the user has entered.

8. Create a project named Grades. In the Main class, use a while loop to continue asking the user for a
student score. From this score, calculate and print the letter student’s grade based on the range:
100 – 90: A
89.99 – 80: B
79.99 – 70: C
69.99 – 60: D
59.99 – 0: F

Your program should handle decimal scores. The loop will stop iterating when the user enters n
for no. Finally, after all the student scores have been entered, calculate the average score. An
example is:

Please enter a new Score:


75.8
75.8 is a C

Continue? (yes/no):
yes

Please enter a new Score:


82.0
82.0 is a B

Continue? (yes/no):
yes

Please enter a new Score:


93.12
93.12 is an A

Continue? (yes/no):
no

The average score is 83.64

You might also like