For and While Loops

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is output by the code below? int k=0; while(k<3) { System.out.print(k); k++; } A. 12 B. 012 C. 0123 D. 123 E. 01234

012

What is output by the code below? for(int g=1; g<4; g++) System.out.print(g); A. 123 B. 12 C. 1234 D. 12345 E. 4321

123

What is returned by the call go(5)? public static int go(int x) { int q=0; while( x > 0) { q = q + x; x = x - 1; } return q; } A. 36 B. 28 C. 10 D. 15 E. 21

15

What is output by the code below? int cnt=0; for(int a=0; a<10; a=a+4) { cnt++; } out.println(cnt); A. 2 B. 3 C. 4 D. 5 E. 6

3

What is returned by the call go(5) ? public static int go(int x) { int cnt = 0; while(x > 0) { x = x / 2; cnt = cnt + 1; } return cnt; } A. 5 B. 4 C. 3 D. 2 E. 1

3

Which of the following loops will print out the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10? I. for(int i=1;i<=10;i++) { out.println(i); } II. int i=0; while(i<10){ out.println(i); i++; } III. int i=0; while(i<10) { i++; out.println(i); } A. I only B. II only C. I and II only D. I and III only E. II and III only

D

What is output by the code shown below? String word = "abcd"; for(char let : word.toCharArray()) { out.print(let); } A. c B. cccc C. abcd D. dcba E. dddd

abcd

What is output by the code below? int m = 0; while(m < 4) { System.out.print(m); m++; } A. 12 B. 012 C. 0123 D. 123 E. 01234

0123

What is output by the code below? for(int f=1; f<5; f++) System.out.print(f); A. 123 B. 12 C. 1234 D. 12345 E. 4321

1234

What is output by the code below? for(int h=1; h<6; h++) System.out.print( h ); A. 123 B. 12 C. 1234 D. 12345 E. 4321

12345

What is returned by the call go(7)? public static int go(int x) { int q=0; while( x > 0) { q = q + x; x = x - 1; } return q; } A. 28 B. 36 C. 45 D. 21 E. 15

28

What is returned by the call go( 30 ) ? public static String go( int x) { String s = ""; for(int n = x; n > 0; n = n - 5) s = s + x + " "; return s; } A. 30 25 20 15 B. 30 25 20 15 10 C. 30 25 20 D. 30 25 20 15 10 5 E. 30 25 20 15 10 5 0

30 25 20 15 10 5

What is output by the code below? int n=2, tallie=0; while(n<9) { tallie=tallie+n; n++; } System.out.print(tallie); A. 104 B. 54 C. 44 D. 35 E. 65

35

What is output by the code below? int mark=0; for(int b=0; b<10; b=b+3) { mark++; } out.println(mark); A. 2 B. 3 C. 4 D. 5 E. 6

4

What is returned by the call go(9)? public static int go(int x) { int q=0; while( x > 0) { q = q + x; x = x - 1; } return q; } A. 28 B. 36 C. 45 D. 55 E. 66

45

What is output by the code below? int counter=0; for(int c=0; c<10; c=c+2) { counter++; } out.println(counter); A. 2 B. 3 C. 4 D. 5 E. 6

5

What is output by the code below? int p = 1; do { p = p + 1; }while( p < 5 ); out.println(p); A. 3 B. 4 C. 5 D. 6 E. 7

5

What is returned by the call go(30) ? public static int go(int x) { int cnt = 0; while(x > 0) { x = x / 2; cnt = cnt + 1; } return cnt; } A. 8 B. 10 C. 5 D. 4 E. 6

5

What is the output? int t=2, summer=0; while(t < 12) { summer = summer + t; t = t + 1; } System.out.print(summer); A. 104 B. 54 C. 44 D. 35 E. 65

65

What is output by the code below? int j=1, tally=0; while(j<8) { tally++; j++; } out.print(tally); A. 6 B. 7 C. 8 D. 9 E. 10

7

What is returned by the call go( 15 ) ? public static int go( int x) { int val = 0; for(int n = 1; n < x; n = n + 3) val = val + x; return val; } A. 20 B. 22 C. 24 D. 25 E. 35

75

What is the following code attempting to calculate? public static int go( int x, int y) { int cnt = 0 for(int n = x; n < y; n = n + 1) if(n % 2 != 0 ) cnt++; return cnt; } A. The code is counting the number of negative numbers between x and y. B. The code is counting the number of positive numbers between x and y. C. The code is counting the number of odd numbers between x and y. D. The code is counting the number of even numbers between x and y. E. The code is counting the number of prime numbers between x and y.

C

Which of the following loops will print out the numbers 5, 10, 15, 20, 25? I. int i=5; while(i<=25) { out.println(i); i=i+5; } II. for(int i=5;i<=25;i=i+5) { out.println(i); } III. for(int i=4;i<24;i=i+5) { out.println(i+1); } A. I only B. II only C. I and II only D. I and III only E. I,II, and III

C

What is the following code attempting to calculate? public static int go( int x, int y) { int cnt = 0 for(int n = x; n < y; n = n + 1) if(n % 2 == 0 ) cnt++; return cnt; } A. The code is counting the number of negative numbers between x and y. B. The code is counting the number of positive numbers between x and y. C. The code is counting the number of odd numbers between x and y. D. The code is counting the number of even numbers between x and y. E. The code is counting the number of prime numbers between x and y.

D

Which of the following loops will print out the numbers 2, 4, 6, 8, 10? I. int i=2; while(i<10){ out.println(i); i=i+2; } II. for(int i=2;i<=10;i=i+2) { out.println(i); } III. int i=2; while(i<=10){ out.println(i); i=i+2; } A. I only B. II only C. I and II only D. I and III only E. II and III only

E

What is returned by the call what(11) ? public boolean what(int num) { boolean check = true; for(int i = 2; i < Math.sqrt(num); i++) { if(num % i == 0) return false; } return true; } A. 0 B. 1 C. false D. true E. -1

true

What is returned by the call what(227) ? public boolean what(int num) { boolean check = true; for(int i = 2; i < Math.sqrt(num); i++) { if(num % i == 0) return false; } return true; } A. 0 B. 1 C. false D. true E. -1

true

What is returned by the call go( 5.0 ) ? public static double go( double w ) { while(w > 0) { w = w - 0.75; } return w; } A. 0.50 B. 0.25 C. 0.75 D. -0.25 E. -0.75

-0.25

What is output by the code below? for(int ab=0; ab <= 1; ab++) { for(int cd=1; cd <= 2; cd++) System.out.print( ab % cd ); } A. 0001 B. 0011 C. 1100 D. 0000 E. 0101

0001

What is returned by the call go( 5, 25 ) ? public static int go( int x, int y) { int cnt = 0 for(int n = x; n < y; n = n + 1) if(n % 2 != 0 ) cnt++; return cnt; } A. 7 B. 8 C. 9 D. 10 E. 11

10

What is returned by the call go( 10 ) ? public static String go( int x) { String s = ""; for(int n = x; n > -3; n = n - 3) s = s + x + " "; return s; } A. 10 7 4 1 -2 B. 7 4 1 -2 -5 -8 C. 10 7 4 1 -2 -5 D. 10 7 4 1 -2 -5 -8 E. 7 4 1 -2 -5

10 7 4 1 -2

What is output by the code below? for(int e=1; e<3; e++) System.out.print(e); A. 123 B. 12 C. 1234 D. 12345 E. 4321

12

What is output by the code below? for(int i=18; i>0; i=i-5) out.print(i); A. 1383 B. 83 C. 1813 D. 181383 E. 1318

181383

What is output by the code below? int cntr=0; for(int d=0; d<10; d=d+5) { cntr++; } out.println(cntr); A. 2 B. 3 C. 4 D. 5 E. 6

2

What is output by the code below? int u=2; while(5 > u) { System.out.print(u); u++; } A. 2 B. 23 C. 234 D. 34 E. 2345

234

What is returned by the call go( 10 ) ? public static int go( int x) { int val = 0 for(int n = 1; n < x; n = n + 4) val = val + x; return val; } A. 20 B. 22 C. 24 D. 25 E. 15

30

What is returned by the call go( 53 ) ? public static String go( int x) { String s = ""; for(int n = x; n > 0; n = n - 11) s = s + x + " "; return s; } A. 53 42 31 20 B. 53 42 31 C. 42 31 20 9 D. 53 42 31 20 9 -2 E. 53 42 31 20 9

53 42 31 20 9

What is output by the code below? int v = 2; do { v = v + 1; }while( v < 6 ); System.out.println(v); A. 3 B. 4 C. 5 D. 6 E. 7

6

Which of the following loops will print out the numbers 3, 5, 7, 9, 11? I. int i=1; while(i<10) { i=i+2; out.println(i); } II. int i=3; while(i<=11) { out.println(i); i=i+2; } III. for(int i=4;i<=12;i=i+2) { out.println(i-1); } A. I only B. II only C. I and III only D. II and III only E. I, II, and III

E

What is returned by the call what(30) ? public boolean what(int num) { boolean check = true; for(int i = 2; i < Math.sqrt(num); i++) { if(num % i == 0) return false; } return true; } A. 0 B. 1 C. false D. true E. -1

false


Ensembles d'études connexes

Complete Unit Circle 6 Trig Functions

View Set

HEMATOPOIESIS AND ERYTHROPOIESIS Chapters 3 and 4

View Set

Chapter 14: Collective Bargaining and Labor Relations

View Set

Professional Nursing: Healthcare Law

View Set

Contemporary social problems chapter 3

View Set