AP Computer Science A : Iterations

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

Example Question #1 : Iterations

int var=0;

int array[4];

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

{

var=var+1;

}

What is the value of var?

Possible Answers:

6

4

8

10

5

Correct answer:

5

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

 

Example Question #1 : Iterations

public static boolean remove(int[] arr, int val) {

       boolean found = false;

       int i;

       for(i = 0; i < arr.length && !found; i++) {

              if(arr[i] == val) {

                     found = true;

              }

       }

      // START

       if(found) {

              for(int j = i; j < arr.length;j++) {

                     arr[j - 1] = arr[j];

              }

              arr[arr.length - 1] = 0;

       }

       // END

       return found;

}

What does the code between // START and // END do?

Possible Answers:

Overwrite the array values with default values

Set the deleted index's value to 0

Delete the values in the array

Shift the contents of the array

None of the other options

Correct answer:

Shift the contents of the array

Explanation:

The easiest way to approach this question is to comment on this line by line. Notice that the condition only works if the given value was found. (This is the use of the boolean found.)  If the desired value has indeed been located, the code then goes through the array, picking up just past the location of the index. (At this point, i is one past the index of the value to be deleted.) Then, it goes through the code and moves backward the value at each index. In effect, this shifts the content backward, giving the appearance of having performed a deletion. This is done on the line arr[j - 1] = arr[j];. Finally, at the end, the final index value is set to a default value, namely 0. (This is done on the line arr[arr.length - 1] = 0;.)

Example Question #41 : Program Implementation

#include <iostream>

 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?

Possible Answers:

i=3,sum=6

i=5,sum=7

i=4, sum=6

i=3, sum=8

i=4,sum=4

Correct answer:

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 

Example Question #1 : Iterations

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));

}

Possible Answers:

False

True

Correct answer:

True

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. 

Example Question #43 : Program Implementation

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?

Possible Answers:

An error will be thrown 

Hello World Hi

Hello World

Hi

Hello World

Hi

Correct answer:

Hello World

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.

Example Question #44 : Program Implementation

Suppose you are given the following lines of code (and x is some initialized integer):

int k = arr.length;

for (int i = -1; i <k-2; i++)

{

     if (arr[i+2] < x)

          System.out.print("Hello");

}

What is the maximum number of times "Hello" print?

Possible Answers:

k-1

An ArrayIndexOutOfBoundsException will be thrown

k

k-2

0

Correct answer:

k-1

Explanation:

The answer is k-1, where k is the length of the array.

 

This problem tests the students' understanding of loop itterations. The loop counter begins at -1, but the first element of arr to be accessed is at index=1. Likewise, the loop exits when i<k-2 is no longer true, and the final index of arr to be accessed is at index=k-3+2=k-1, which is the last element of arr. Therefore, we do not get an ArrayIndexOutOfBoundsException because the array is being accessed properly.

 

Since the (k-1)th element of arr is accessed (the last element) and the 0th element of arr is skipped (the first element to be accessed is at index=1, and arrays are 0 indexed), there are k-1 loop itterations. If all the values in arr do not exceed x, then "Hello" will printed k-1 times.

Learning Tools by Varsity Tutors