Testing

Help Questions

AP Computer Science A › Testing

Questions 1 - 10
1

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

2

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

3

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

4

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

5

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

6

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

7

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

8

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

9

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

10

Consider the Array

int[] arr = {1, 2, 3, 4, 5};

What are the values in arr after the following code is executed?

for (int i = 0; i < arr.length - 2; i++)

{

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

23451

12345

54321

15432

OutOfBoundsException

Explanation

We start with an array, arr, of size 5, containing {1, 2, 3, 4, 5}.

The loop in the code,

for (int i = 0; i < arr.length - 2; i++)

loops through the array up to the second to last cell, given that arr.length - 2 is the index of the second to last cell, and it starts at the first cell.

Let's look at the code inside the loop.

int temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

When i = 0,

arr[0] == 1

arr[1] == 2

temp = 1

arr[0] = 2

arr[1] = 1

arr[] == {2, 1, 3, 4, 5}

When i = 1

arr[1] == 1

arr[2] == 3

temp = 1

arr[1] = 3

arr[2] = 1

arr[] == {2, 3, 1, 4, 5}

When i = 2

arr[2] == 1

arr[3] == 4

temp = 1

arr[2] = 4

arr[3] = 1

arr[] == {2, 3, 4, 1, 5}

When i = 3

arr[3] == 1

arr[4] == 3

temp = 1

arr[3] = 3

arr[4] = 1

arr[] == {2, 3, 4, 5, 1}

As the loop progresses, it moves whatever value is in arr[0] all the way to the end of the array.

Page 1 of 5