Iterations

Help Questions

AP Computer Science A › Iterations

Questions 1 - 10
1

True or False.

This code snippet will iterate 5 times.

ArrayList<String> arrList = new ArrayList<String>();

arrList.add("string0");

arrList.add("string1");

arrList.add("string2");

arrList.add("string3");

arrList.add("string4");

for (int i = 0; i < arrList.size(); i++) {

System.out.println(arrList.get(i));

}

True

False

Explanation

The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.

2

Suppose you have the following code:

public static void main(String\[\] args) {

int a =2;

if (a%2==0)

System.out.println("Hello World");

else

System.out.println("Hi");

}

If the main method is called, what will be printed?

Hello World

Hello World

Hi

Hello World Hi

Hi

An error will be thrown

Explanation

"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.

3

True or False.

This code snippet will iterate 5 times.

ArrayList<String> arrList = new ArrayList<String>();

arrList.add("string0");

arrList.add("string1");

arrList.add("string2");

arrList.add("string3");

arrList.add("string4");

for (int i = 0; i < arrList.size(); i++) {

System.out.println(arrList.get(i));

}

True

False

Explanation

The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.

4

int var=0;

int array\[4\];

for (int j=0; j<=4;j++)

{

var=var+1;

}

What is the value of var?

5

10

6

4

8

Explanation

The for loop simply sums up the numbers 0 through 3, which is 6.

First iteration through the loop, j=0:

var=0+1=1

Second iteration, j=1:

var=1+1=2

Third iteration, j=2:

var=2+1=3

Fourth iteration, j=3:

var=3+1=4

Fifth iteration, j=4:

var=4+1=5

5

int var=0;

int array\[4\];

for (int j=0; j<=4;j++)

{

var=var+1;

}

What is the value of var?

5

10

6

4

8

Explanation

The for loop simply sums up the numbers 0 through 3, which is 6.

First iteration through the loop, j=0:

var=0+1=1

Second iteration, j=1:

var=1+1=2

Third iteration, j=2:

var=2+1=3

Fourth iteration, j=3:

var=3+1=4

Fifth iteration, j=4:

var=4+1=5

6

int var=0;

int array\[4\];

for (int j=0; j<=4;j++)

{

var=var+1;

}

What is the value of var?

5

10

6

4

8

Explanation

The for loop simply sums up the numbers 0 through 3, which is 6.

First iteration through the loop, j=0:

var=0+1=1

Second iteration, j=1:

var=1+1=2

Third iteration, j=2:

var=2+1=3

Fourth iteration, j=3:

var=3+1=4

Fifth iteration, j=4:

var=4+1=5

7

Suppose you have the following code:

public static void main(String\[\] args) {

int a =2;

if (a%2==0)

System.out.println("Hello World");

else

System.out.println("Hi");

}

If the main method is called, what will be printed?

Hello World

Hello World

Hi

Hello World Hi

Hi

An error will be thrown

Explanation

"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.

8

Suppose you have the following code:

public static void main(String\[\] args) {

int a =2;

if (a%2==0)

System.out.println("Hello World");

else

System.out.println("Hi");

}

If the main method is called, what will be printed?

Hello World

Hello World

Hi

Hello World Hi

Hi

An error will be thrown

Explanation

"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.

9

True or False.

This code snippet will iterate 5 times.

ArrayList<String> arrList = new ArrayList<String>();

arrList.add("string0");

arrList.add("string1");

arrList.add("string2");

arrList.add("string3");

arrList.add("string4");

for (int i = 0; i < arrList.size(); i++) {

System.out.println(arrList.get(i));

}

True

False

Explanation

The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.

10

#include

using namespace std;

int main()

{

int i=0;

int sum =0;

for(i;i<4;i+=2)

{

sum=sum+i;

i--;

}

return 0;

}

What is the value of i and sum?

i=4, sum=6

i=3, sum=8

i=5,sum=7

i=4,sum=4

i=3,sum=6

Explanation

Let's take a look at the declaration of the foor loop. It will run as long at the integer "i" is less than four. It also increases by 2 after each iteration. i+=2 is equivalent to i=i+2.

Now let's look at the contents of the loop itself. The integer "sum" starts at 0 and is added to i at each iteration. After this we see that 1 number is subtracted from i after each iteration. Now after examining the structure of the loop, let's begin to calculate each iteration.

Iteration 1: i=0, sum=0, then i=-1.

Iteration 2: i=1, sum=1, then i=0.

Iteration3: i=2, sum=3, then i=1,

Iteration 4: i=3, sum =6, then i=3.

There is no iteration 5 because "i" can only be less than four to enter the loop, so the program ends there.

i=3, sum=6

Page 1 of 2
Return to subject