CPS 180 - E2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the value of balance after the following code is executed? int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; }

C. 1

Analyze the following code: import java.util.Scanner; public class Test { public static void main(String[] args) { int sum = 0; for (int i = 0; i < 100000; i++) { Scanner input = new Scanner(System.in); sum += input.nextInt(); } } }

C. The program compiles and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop.

Analyze the following code: public class Test { public static void main (String args[]) { int i = 0; for (i = 0; i < 10; i++); System.out.println(i + 4); } }

C. The program compiles despite the semicolon (;) on the for loop line, and displays 14. and D. The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4);

How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);

10

How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (++count < 10);

10

How many times will the following code print "Welcome to Java"? int count = 0; while (count < 10) { System.out.println("Welcome to Java"); count++; }

10

How many times will the following code print "Welcome to Java"? int count = 0; while (count++ < 10) { System.out.println("Welcome to Java"); }

10

What is the value in count after the following loop is executed? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 9); System.out.println(count);

10

How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 10);

11

What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; if (sum >= 4) continue; sum += item; } while (item < 5);

A. 6

Do the following two statements in (I) and (II) result in the same value in sum? (I): for (int i = 0; i < 10; ++i) { sum += i; } (II): for (int i = 0; i < 10; i++) { sum += i; }

A. Yes

To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?

A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.

Analyze the following code. int count = 0; while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B } // Point C

A. count < 100 is always true at Point A and E. count < 100 is always false at Point C

What is y after the following for loop statement is executed? int y = 0; for (int i = 0; i < 10; ++i) { y += 1; }

B. 10

Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; }

B. No

What is the output after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number; i++) { if (number % i == 0) { isPrime = false; break; } }

B. i is 5 isPrime is false

What is the number of iterations in the following loop? for (int i = 1; i <= n; i++) { // iteration }

B. n

Analyze the following fragment: double sum = 0; double d = 0; while (d != 10.0) { d += 0.1; sum += sum + d; }

C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

What is the number of iterations in the following loop? for (int i = 1; i < n; i++) { // iteration }

C. n - 1

The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; }

D. 1 3 5 7 9

How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j)

D. 45

Analyze the following statement: double sum = 0; for (double d = 0; d < 10;) { d += 0.1; sum += sum + d; }

D. The program compiles and runs fine

Analyze the following code. double sum = 0; for (double d = 0; d < 10; sum += sum + d) { d += 0.1; }

D. The program complies and runs fine.

Suppose the input for number is 9. What is the output from running the following program? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input.nextInt(); int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } System.out.println("i is " + i); if (isPrime) System.out.println(number + " is prime"); else System.out.println(number + " is not prime"); } }

D. i is 4 followed by 9 is not prime

What is the output after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } }

D. i is 6 isPrime is false

Which of the following loops prints "Welcome to Java" 10 times? A: for (int count = 1; count <= 10; count++) { System.out.println("Welcome to Java"); } B: for (int count = 0; count < 10; count++) { System.out.println("Welcome to Java"); } C: for (int count = 1; count < 10; count++) { System.out.println("Welcome to Java"); } D: for (int count = 0; count <= 10; count++) { System.out.println("Welcome to Java"); }

E. AB

Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? A: double sum = 0; for (int i = 1; i <= 99; i++) { sum = i / (i + 1); } System.out.println("Sum is " + sum); B: double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1); } System.out.println("Sum is " + sum); C: double sum = 0; for (int i = 1; i <= 99; i++) { sum += 1.0 * i / (i + 1); } System.out.println("Sum is " + sum); D: double sum = 0; for (int i = 1; i <= 99; i++) { sum += i / (i + 1.0); } System.out.println("Sum is " + sum); E: double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1.0); } System.out.println("Sum is " + sum);

E. CD

What balance after the following code is executed? int balance = 10; while (balance >= 1) { if (balance < 9) continue; balance = balance - 9; }

E. The loop does not end

a variable declared in the for loop control can be used after the loop exits

false

a continued statement can be used only in a loop

true

in a for statement, if the continuation condition is blank, the condition is assumed to be

true

you can always write a program without using break or continue in a loop

true


Kaugnay na mga set ng pag-aaral

Bonds And Other Financial Instruments

View Set

Chapter 8: Alkenes Reactions and Synthesis

View Set

ACCT208 chapter 3 practice questions

View Set