Está en la página 1de 3

Data Structures - 1D and 2D Arrays

Quiz 1
Review
Format:
Fill in the Blank (given a set of terms)
15 Marks (Knowledge)
True False
10 Marks (Knowledge)
Short Answer

write declaration/initialization statements

write output

write methods
30 Marks (Application
Total Quiz Value

55 Marks

Content:
Review content from Data Structures - 1D and 2D arrays (notes and practice exercises).
Answer the questions below:
1.

What is an array?

2.

Name two uses for an array.

3.

Assume you need a data structure to store even integers between 10 and 30.
a)
b)
c)
d)
e)
f)

Write the declaration statement


Write the initialization statement
Draw an abstraction for this array. Identify its parts.
Write the initialization statement in a different way.
What type of variable is arrays name?
If an integer array is not filled with data initially, what are elements by default?

4.

What public variable stores the capacity of the array? How can you access this variable?

5.

Assume the following data set has been declared:


int [ ] heights = {45, 7, 34, 89, 32, 53, 8, 9, 10};
Write methods using the following headers:
a)

//Method to total only odd number elements


public int totalOddValues()

b)

//Method to return an array with doubled value elements


public int [ ] doubleArrayValues()

c)

//Method to return an array of elements in odd numbered indexes


public int [ ] oddIndexes()

6.

What is a 2D array?

7.

Name two uses for a 2D array.

8.

Assume you need a data structure to store 5 marks for a class of 10 students.
a)
b)
c)
d)

Write the declaration statement


Write the initialization statement
Draw an abstraction for this array. Identify its parts.
Write the initialization statement in a different way.

9.

Assume you need a data structure to store profits for a 6-month period for a number of store chains
which is initially unknown.

10.

Assume the following data set has been declared:


int [ ] [ ] nums = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
Write methods using the following headers:

11.

a)

//To find the total of the array


public double totalArray()

b)

//To find the total for a specified column


public double totalColumn(int aColumn)

c)

//To find the total of each row and return an array


public double [ ] totalRows()

d)

To find the average of the row totals


public double averageRowTotal()

What will be output for the following statements:

int [ ] numbers = new int [10];


System.out.println(numbers);
for (int x = 0; x < numbers.length; x++)
numbers[x] = x;
for (int x : numbers)
System.out.println(x);
for (int x = 0; x < numbers.length; x++)
numbers[x] = x + 2;
int sum = 0;
for (int x : numbers)
sum += x;
for (int x : numbers)
System.out.println(x);
String word = "algorithm";
char [] wordLetters = word.toCharArray();
for (char x : wordLetters)
System.out.println(x);

int [ ] sample = new int [10];


for (int x = 0; x < 100; x++)
sample[x] = x;
for (int x : sample)
System.out.println(x);
12.

Write methods that make an array dynamic.


a)

//A helper method to expand an array's capacity


private void expandCapacity()

b)

//Method to add a data element to the end of the array


public void addToEnd (int element)

c)

//Method to add a data element to the middle of the array


public void add (int element, int index)

d)

//Method to remove a data element


public void remove (int element)

e)

//Method to search for a given data element and return its index
public int search (int element)

f)

//Method to add a row to the end of a 2D array


public void addRowToEnd (int [] dataRow)

g)

//Method to add a column to the end of a 2D array


public void addColumnToEnd (int [] dataColumn)

h)

//Method to add a row to the middle of a 2D array


public void addRow (int [] dataRow, int index)

i)

//Method to remove a row from the middle of a 2D array


public void removeRow (int index)

j)

//Method to remove a column from the middle of a 2D array


public void removeColumn (int index)

También podría gustarte