CPS180 Chapter 5 Quiz

25 July 2022
4.7 (114 reviews)
36 test answers

Unlock all answers in this set

Unlock answers (32)
question
The following loop displays: for(int i = 1: i <=10;i++) { System.out.print(i + ""); i++; }
answer
1 3 5 7 9
question
Assume x is 0. What is the output of the following statement? if (x>0) printf("x is greater than 0"); else if (x<0) printf("x is less than 0"); else printf("x equals zero")
answer
x equals 0
question
Is the following loop correct? for (; ; );
answer
yes
question
double sum = 0; for (double d = 0; d < 10;) {d += 0.1; sum+=sum+d; }
answer
The program has a compile error because the adjustment is missing in the for loop.
question
What is i after the following for loop? int y = 0; for(int i = 0; i<10;++i) { y+=i; }
answer
undefined
question
The while loop and the do loop are equivalent in their expressive power; in other words, you can rewrite a while loop using a do loop, and vice versa.
answer
true
question
Which of the loop statements always have their body executed at least once?
answer
do-while loop
question
for( ; ; ) System.out.println("Welcome to Java");
answer
prints out Welcome to Java forever
question
In a for statement, if the continuation condition is blank, the condition is assumed to be
answer
true
question
You can always convert a while loop to a for loop.
answer
true
question
You can always write a program without using break or continue in a loop.
answer
true
question
A variable declared in the for loop control can be used after the loop exits.
answer
false
question
for(int i = 0; i<15; i++){ if (i%4 == 1) System.out.print(i + " ");
answer
1 5 9 13
question
Will this terminate? int balance = 10; while (true){ if (balance < 9) break; balance = balance - 9;
answer
yes
question
Will this terminate? int balance = 10; while (true){ if (balance < 9) continue; balance = balance - 9;
answer
no
question
The elements inside the for loop control are separated using semicolons instead of commas.
answer
true
question
for ( ; false; ) System.out.println("Welcome to Java")
answer
does not print anything
question
You can always convert a for loop to a while loop.
answer
true
question
How many iterations will there be? for (int i = 1; i < n; i++) //iteration
answer
n-1
question
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?
answer
Add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
question
Which of the following expression yields an integer between 0 and 100, inclusive?
answer
(int)(Math.random()*101)
question
Suppose cond1 is a Boolean expressions. When will this while condition be true? while (cond1) ...
answer
in case cond1 is true
question
double sum = 0; double d = 0; while(d != 10.0){ d +=.0; sum += sum +d;
answer
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
question
int y = 0; for(int i = 0; i<10; ++i){ y +=1; } System.out.println(y);
answer
10
question
int sum = 0; int item = 0; do{ item++; sum +=item; if(sum>4)continue; }while (item<5); System.out.println(sum);
answer
15
question
int sum = 0; int item = 0; do{ item++; sum +=item; if(sum>4)break; }while (item<5); System.out.println(sum);
answer
6
question
What is the value of balance? int balance = 10; while (balance>=1){ if (balance < 9) break; balance = balance - 9;
answer
1
question
What is the number of iterations in the following loop? for (int i = 1; i <= n; i++) //iteration
answer
n
question
A continue statement can only be used in a loop.
answer
true
question
int y = 0; for(int i = 0; i<10; ++i){ y +=1; } System.out.println(y);
answer
10
question
int y = 0; for(int i = 0; i<10; ++i){ y +=i; } System.out.println(y);
answer
45
question
int count = 0; do{ System.out.println("Welcome to Java"); }while(++count<10);
answer
10
question
What is the value of balance? int balance = 10; while(balance>=1){ if(balance<9)continue; balance = balance -9; } System.out.println(balance);
answer
the loop does not end
question
for (int i = 0;i<10;i++) for (int j = 0; j
answer
45
question
int number = 25; int i; boolean isPrime = true; for(i = 2; i< number; i++){ if(number %i == 0){ isPrime = false; break; } } System.out.println("i is " +i+ " isPrime is " +isPrime);
answer
i is 5 isPrime is false
question
int number = 25; int i; boolean isPrime = true; for(i=2; i
answer
i is 6 isPrime is false