12/11/2024 8:32PM
CSC 110 Assignment 5 (4.1-5.2) Test
Questions and Answers
Given an integer variable strawsOnCamel, write a statement that uses the auto-increment
operator to increase the value of that variable by 1. - Answers✓✓strawsOnCamel++;
Given an integer variable timer, write a statement that uses the auto-decrement operator to
decrease the value of that variable by 1. - Answers✓✓timer--;
Consider this code: "int v = 20; --v; System.out.println(v++);". What value is 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 - Answers✓✓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 - Answers✓✓s is 20 and t is 40
Given an int variable k that has already been declared, use a while loop to print a single line
consisting of 88 asterisks. Use no variables other than k. - Answers✓✓k=1;
, ©Themoon EXAM SOLUTIONS
12/11/2024 8:32PM
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, use a
while loop to print a single line consisting of n asterisks. Use no variables other than n. -
Answers✓✓while(n>0){
System.out.print("*");
n--;
}
Given an int variable k that has already been declared, use a do...while loop to print a single line
consisting of 53 asterisks. Use no variables other than k. - Answers✓✓k=53;
do{
System.out.print("*");
k--;
}while(k>=1);
Given an int variable n that has already 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. -
Answers✓✓do{
System.out.print("*");
n--;
}
while(n>=1);
, ©Themoon EXAM SOLUTIONS
12/11/2024 8:32PM
Given an int variable k that has already been declared, use a for loop to print a single line
consisting of 97 asterisks. Use no variables other than k. - Answers✓✓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, use 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. - Answers✓✓for (j = 0; j < n; j++)
{
System.out.print("*");
}
Write a for loop that prints the integers 0 through 39, separated by spaces. - Answers✓✓for(int i
= 0; i<40; i++)
{
System.out.print(i + " ");
}
Write a for loop that prints the odd integers 11 through 121 inclusive, separated by spaces. -
Answers✓✓for(int i = 11; i<=121; i+=2)
{
System.out.print(i + " ");
}
Write a for loop that prints in ascending order all the positive multiples of 5 that are less than
175, separated by spaces. - Answers✓✓for(int i = 5; i < 175; i = i + 5)
{
System.out.print(i + " ");