CSC 110 Assignment 5 (4.1-5.2) With 100% Verified
Solutions 2024-2025
Given an integer variable strawsOnCamel, write a statement that uses the ++ operator
to add 1 to the value of that variable. - Answer strawsOnCamel++;
Given an integer variable timer, write a statement that uses the -- operator to subtract 1
from the value of that variable. - Answer timer--;
Consider the following code: "int v = 20; --v; System.out.println(v++);" what gets printed,
what value is v left with?
20 is printed, v ends up with 19
19 is printed, v ends up with 20
20 is printed, v ends up with 20
19 is printed, v ends up with 19
cannot determine what is printed, v ends up with 20 - Answer 19 is printed, v ends up
with 20
Consider this code: "int s = 20; int t = s++ + --s;". What are the values of s and t?
s is 19 and t is 38
s is 20 and t is 39
s is 19 and t is 39
s is 20 and t is 40
s is 20 and t cannot be determined - Answer s is 20 and t is 40
,Given the int variable k that has already been declared, Write a while loop that prints a
single line consisting of 88 asterisks. Do not use any variables other than k. Solution
k=1; while(k<=88) { System.out.print('*'); k++; } System.out.println();
Given an int variable n that has already been declared and initialized to a positive value,
write a while loop that prints exactly n asterisks on one line. Do not use any variables
other than n. - Solution while(n>0){ System.out.print("*"); n--; }
Given an int variable k that has already been declared and initialized, write a do.while
loop prints 53 asterisks to a single line. You may only use the variable k. Solution k=53;
do{ System.out.print("*"); k--; }while(k>=1);
Given int variable n that has been declared and initialized to a positive value, use a
do.while loop to print a single line consisting of n asterisks. Use no variables other than
n. - Solution do{ System.out.print("*"); n--; }while(n>=1);
Given the already declared int variable k, use a for loop to print-to a single line-97
asterisks. You may not use any variables besides k. -Solution for(k=97;k>=1;k--){
System.out.print("*"); }
Given an int variable n that has already been declared and initialized to a positive value,
and another int variable j that has already been declared, write a for loop to print a
single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed.
Use no variables other than n and j. - Solution for (j = 0; j < n; j++) {
System.out.print("*);
}
Write for loop that prints the integers 0 through 39, separated by spaces. -Solution
for(int i = 0; i<40; i++)
{
System.out.print(i + " ");
}
, Write for loop that prints odd integers 11 through 121 inclusive, separated by spaces
-Solution for(int i = 11; i<=121; i+=2)
{
System.out.print(i + " ");
}
Use a for loop to write a program that prints all positive multiples of 5 less than 175 in
ascending order separated by spaces. Solution: for (int i = 5; i < 175; i = i + 5) {
System.out.print(i + " "); }
Write a for loop that prints the integers 50 through 1, separated by spaces. Use no
variables other than count. - Answer for (int count= 50; count >= 1; count--)
{ System.out.print(count + " ");
}
Write a for loop that prints all the even integers from 80 through 20 inclusive, separated
by spaces. - Answer for (int n = 80; n >= 20; n-=2)
{
System.out.print(n + " ";
}
Using a for loop, write that prints out in ascending order all of the positive integers less
than 200 that are divisible by both 2 and 3 separated by spaces. - Solution for (int n = 1;
n< 200; n++)
{
Solutions 2024-2025
Given an integer variable strawsOnCamel, write a statement that uses the ++ operator
to add 1 to the value of that variable. - Answer strawsOnCamel++;
Given an integer variable timer, write a statement that uses the -- operator to subtract 1
from the value of that variable. - Answer timer--;
Consider the following code: "int v = 20; --v; System.out.println(v++);" what gets printed,
what value is v left with?
20 is printed, v ends up with 19
19 is printed, v ends up with 20
20 is printed, v ends up with 20
19 is printed, v ends up with 19
cannot determine what is printed, v ends up with 20 - Answer 19 is printed, v ends up
with 20
Consider this code: "int s = 20; int t = s++ + --s;". What are the values of s and t?
s is 19 and t is 38
s is 20 and t is 39
s is 19 and t is 39
s is 20 and t is 40
s is 20 and t cannot be determined - Answer s is 20 and t is 40
,Given the int variable k that has already been declared, Write a while loop that prints a
single line consisting of 88 asterisks. Do not use any variables other than k. Solution
k=1; while(k<=88) { System.out.print('*'); k++; } System.out.println();
Given an int variable n that has already been declared and initialized to a positive value,
write a while loop that prints exactly n asterisks on one line. Do not use any variables
other than n. - Solution while(n>0){ System.out.print("*"); n--; }
Given an int variable k that has already been declared and initialized, write a do.while
loop prints 53 asterisks to a single line. You may only use the variable k. Solution k=53;
do{ System.out.print("*"); k--; }while(k>=1);
Given int variable n that has been declared and initialized to a positive value, use a
do.while loop to print a single line consisting of n asterisks. Use no variables other than
n. - Solution do{ System.out.print("*"); n--; }while(n>=1);
Given the already declared int variable k, use a for loop to print-to a single line-97
asterisks. You may not use any variables besides k. -Solution for(k=97;k>=1;k--){
System.out.print("*"); }
Given an int variable n that has already been declared and initialized to a positive value,
and another int variable j that has already been declared, write a for loop to print a
single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed.
Use no variables other than n and j. - Solution for (j = 0; j < n; j++) {
System.out.print("*);
}
Write for loop that prints the integers 0 through 39, separated by spaces. -Solution
for(int i = 0; i<40; i++)
{
System.out.print(i + " ");
}
, Write for loop that prints odd integers 11 through 121 inclusive, separated by spaces
-Solution for(int i = 11; i<=121; i+=2)
{
System.out.print(i + " ");
}
Use a for loop to write a program that prints all positive multiples of 5 less than 175 in
ascending order separated by spaces. Solution: for (int i = 5; i < 175; i = i + 5) {
System.out.print(i + " "); }
Write a for loop that prints the integers 50 through 1, separated by spaces. Use no
variables other than count. - Answer for (int count= 50; count >= 1; count--)
{ System.out.print(count + " ");
}
Write a for loop that prints all the even integers from 80 through 20 inclusive, separated
by spaces. - Answer for (int n = 80; n >= 20; n-=2)
{
System.out.print(n + " ";
}
Using a for loop, write that prints out in ascending order all of the positive integers less
than 200 that are divisible by both 2 and 3 separated by spaces. - Solution for (int n = 1;
n< 200; n++)
{