For Loops and While Loops Quizzes
final int MIN = 10, final MAX = 20; int num = 15; for (int count1=1; count1 <= 7; count1++) { for (int count2=1; count2 <= 5; count2++) System.out.print ("#"); System.out.println(); }
##### ##### ##### ##### ##### ##### #####
What is returned by the call go( 3.5 )? public static double go( double w ) { while(w > 0) { w = w - 0.75; } return w; }
-0.25
int x=-2; while(x<9) { x++; System.out.print(x + " "); }
-1 0 1 2 3 4 5 6 7 8 9
for(int i=-3; i<19; i=i+3) { out.print(i + " "); }
-3 0 3 6 9 12 15 18
int k=5; String s=""; while(k>0){ s=k+" "+s; k--; } System.out.print(s);
1 2 3 4 5
What is returned by the call go( 4 )? public static int go(int x) { int q=0; while( x > 0) { q = q + x; x = x - 1; } return q;
10
int b=3; String list=""; while(b<10) { b=b+2; if(b%2==1) list=b+" "+list; } System.out.print(list);
11 9 7 5
final int MIN = 10, final MAX = 20; int num = 15; for (int value=num; value <= MAX; value++) 15 System.out.println (value);
15 16 17 18 19 20
for(int j=17; j>-2; j=j-2) { out.print(j + " "); }
17 15 13 11 9 7 5 3 1 -1
for(int s=1; s<19; s++) { total=total+s; } out.println(total);
171
What is output by the code below? int j=3, ans=0; while(j<7) { ans+=j; j++; } System.out.print( ans );
18
for(int x=20; x<50; x=x+3) { out.print(x + " "); }
20 23 26 29 32 35 38 41 44 47
What is output by the code below? int j=1; while(j<8) { j++; System.out.print( j ); }
2345678
int j=21, x=0; while(j>0) { if( j % 2 == 0) x++; j=j-3; } System.out.print( x );
3
int m=1, total=0; while(m<9){ total=total+m; m++; } System.out.print(total );
36
for(int m=37; m>0; m=m-4) { out.print(m + " "); }
37 33 29 25 21 17 13 9 5 1
What is output by the code below? int j=11, x=0; while(j>0) { x++; j=j-3; } System.out.print( x );
4
What is returned by the call go(8) ? public static int go(int x) { int cnt = 0; while(x > 0) { x = x / 2; cnt = cnt + 1; } return cnt; }
4
int n=23, z=0; while( n > 16 ) { z = z + n; n-=4; } System.out.print( z );
42
What is output by the code below? int n=13, z=0; while( n > 4 ) { z += n; n-=2; } System.out.print( z );
45
int t=2, x=0; while(t < 12) { x += t; t = t + 1; } System.out.print( x );
65
int z=2, sum=0; while(z<12){ z++; sum=sum+z; } System.out.print(sum);
75