For Loops Quiz
By Mickey Arnold
starstarstarstarstarstarstarstarstarstar
Last updated 10 months ago
14 Questions
1 point
1
Question 1
1.
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=0; i<=10; i=i+2)
out.print(i);
1 point
1
Question 2
2.
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=20; i>0; i=i-4)
out.print(i);
1 point
1
Question 3
3.
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=5; i<20; i=i+3)
out.print(i);
1 point
1
Question 4
4.
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?
for(int i=15; i>2; i=i-2)
System.out.print(i);
1 point
1
Question 5
5.
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=10; i>-1; i=i-2)
ans += i;
System.out.print( ans );
1 point
1
Question 6
6.
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<12; i=i+3)
ans += i;
System.out.print( ans );
1 point
1
Question 7
7.
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 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 );
1 point
1
Question 8
8.
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( 7 ) ?
public static String go( int x)
{
String s = "";
for(int n = x; n > 0; n = n - 2)
s = s + n + " ";
return s;
}
1 point
1
Question 9
9.
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( 17 ) ?
public static int go( int x)
{
int val = 0;
for(int n = 1; n < x; n = n + 4)
val = val + n;
return val;
}
1 point
1
Question 10
10.
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;}
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;
}
1 point
1
Question 11
11.
int tot = 0;for( int i=2; i<17; i++ ) { tot = tot + i;}out.println( tot );
int tot = 0;
for( int i=2; i<17; i++ ) {
tot = tot + i;
}
out.println( tot );
1 point
1
Question 12
12.
int count=0;for(int j=4; j<23; j++) { if(j%2==1) count++;}out.println( count );
int count=0;
for(int j=4; j<23; j++) {
if(j%2==1)
count++;
}
out.println( count );
1 point
1
Question 13
13.
int sum=0;for(int m=5; m<18; m++) { if(m%2!=1) sum=sum+m;}out.println(sum);
int sum=0;
for(int m=5; m<18; m++) {
if(m%2!=1)
sum=sum+m;
}
out.println(sum);
7 points
7
Question 14
14.
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.}
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.
}