Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

AP Computer Science Principles Arrays

Total questions: 6

Worksheet time: 6mins

Name
Class
Date
1.

//Precondition: mat is initialized

public static void matStuff(int[][] mat, int row)

{

int numCols = mat[0].length;

for (int col = 0; col < numCols; col++)

mat[row][col] = row;

}


Suppose mat is originally


1 4 9 0

2 7 8 6

5 1 4 3


After the method call matStuff(mat, 1), matrix mat will be

a)

1 1 1 1

2 7 8 6

5 1 4 3

b)

1 4 9 0

1 7 8 6

1 1 4 3

c)

1 1 9 0

2 1 8 6

5 1 4 3

d)

1 4 9 0

1 1 1 1

5 1 4 3

e)

1 1 1 1

1 1 1 1

1 1 1 1

2.

Which of the following initializes an 8 X 10 matrix with integer values that are prefect squares (0 is a perfect square)?


I int[][] mat = new int[8][10];


II int[][] mat = new int[8][10];

for(int r = 0; r < mat.length; r++)

for(int c = 0; c < mat[r].length; c++)

mat[r][c] = r - r;


III int[][] mat = new int[8][10];

for(int c = 0; c < mat[0].length; c++)

for(int r = 0; r < mat.length; r++)

mat[r][c] = c * c;

a)

I only

b)

II only

c)

III only

d)

I and II only

e)

I and III only

3.

Given the following matrix definition:


double [][] a = new double[5][8];


This array will hold the following values after it is instantiated:

a)

Zeros

b)

False

c)

No values, each element will be empty

d)

Random Numbers

e)

Null

4.

Given the following matrix definition:


boolean[][] a = new boolean[5][8];


This array will hold the following values after it is instantiated:

a)

Zeros

b)

False

c)

No values, each element will be empty

d)

Random Numbers

e)

Null

5.

Consider a class that has this private instance variable:


private int[][] mat;


The class has the following method, alter.


public void alter (int c)

{

for (int i = 0; i < mat.length; i++)

for (int j = c + 1; j < mat[0].length; j++)

mat[i][j – 1] = mat [i][j];

}


If a 3 x 4 matrix mat is


1 3 5 7

2 4 6 8

3 5 7 9


then alter(1) will change mat to

a)

1 5 7 7

2 6 8 8

3 7 9 9

b)

1 5 7

2 6 8

3 7 9

c)

1 3 5 7

3 5 7 9

d)

1 3 5 7

3 5 7 9

3 5 7 9

e)

1 7 7 7

2 8 8 8

3 9 9 9

6.
a)

I only

b)

II only

c)

III only

d)

I and II only

e)

I, II, and III