For Loops Quiz

By Mickey Arnold
Last updated 10 months ago
14 Questions

What is output by the code below?

for(int i=0; i<=10; i=i+2)
out.print(i);

What is output by the code below?
for(int i=20; i>0; i=i-4)
out.print(i);

What is output by the code below?
for(int i=5; i<20; i=i+3)
out.print(i);

What is output by the code below?
for(int i=15; i>2; i=i-2)
System.out.print(i);

What is output by the code below?
int ans = 0;
for(int i=10; i>-1; i=i-2)
ans += i;
System.out.print( ans );

What is output by the code below?
int ans = 0;
for(int i=0; i<12; i=i+3)
ans += i;
System.out.print( ans );

What is output by the code below?
int ans = 0;
for(int i=0; i<20; i=i+3)
if( i % 2 == 0 )
ans += i;
System.out.print( ans );

What is returned by the call go( 7 ) ?
public static String go( int x)
{
String s = "";
for(int n = x; n > 0; n = n - 2)
s = s + n + " ";
return s;
}

What is returned by the call go( 17 ) ?
public static int go( int x)
{
int val = 0;
for(int n = 1; n < x; n = n + 4)
val = val + n;
return val;
}

What is returned by the call go( 7, 12 ) ?
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;
}

int tot = 0;
for( int i=2; i<17; i++ ) {
tot = tot + i;
}
out.println( tot );

int count=0;
for(int j=4; j<23; j++) {
if(j%2==1)
count++;
}
out.println( count );

int sum=0;
for(int m=5; m<18; m++) {
if(m%2!=1)
sum=sum+m;
}
out.println(sum);

DIRECTIONS : Write a return method named loopy that returns a String. loopy takes in two parameters - an integer named count and an String named value. loopy will return a new String containing count copies of value.

loopy( "dog", 3 ) returns "dogdogdog"
public static String loopy(String value,int cnt )
{
//Code goes here.
}