Preview

09 - 1d 2d and 3d Arrays

 1. Arrays, in most programming languages, can be defined as a collection of elements of a single (THE SAME) data type, eg. array of int, array of string.
The following powerpoint embed from teachingcomputing.com provides a comprehensive and useful guide to Arrays in Python

  TRUE

  FALSE

 2. In Python, there is no native array data structure. So, we use Python lists instead of an array.
Note: if you wish to use real arrays in Python, you need to use NumPy's array data structure. (Google NumPy) For mathematical problems, NumPy Array is more efficient.

  FALSE

  TRUE

 3. Python has a whole set of built in methods that you can use on lists/arrays. Which of the following is NOT a valid method?
append()	Adds an element at the end of the list
clear()	Removes all the elements from the list
copy()	Returns a copy of the list
count()	Returns the number of elements with the specified value
extend()	Add the elements of a list (or any iterable), to the end of the current list
index()	Returns the index of the first element with the specified value
insert()	Adds an element at the specified position
pop()	Removes the element at the specified position
remove()	Removes the first item with the specified value
reverse()	Reverses the order of the list
sort()	Sorts the list

  All of the items on the list are valid methods that can be used with lists/arrays in Python

  clear() - this is not a real method

  count()

  sort() and reverse()

 4. Here we have created a 1d array which is just a simple list called 'arr'. What is the output of this program?
arr = [10, 20, 30, 40, 50]
print(arr[0])
print(arr[1])
print(arr[2])

  10,10,10

  1,2,3

  10,20,30 (the first element of the array is 10 which is arr[0])

  20,30,10 (the first element o fthe array is 20 which is arr[1])

 5. Python programming supports negative indexing of arrays, something that is not available in arrays in most programming languages. The index value of -1 and-2 will output:
arr = [10, 20, 30, 40, 50]
print(arr[-1])#index of -1 gives the last element in the array
print(arr[-2])#index value of -2 gives the second last element  

  None of the above as it is a negative number

  50 and 40

  40 and 50

  10 and 20

 6. As you become a professional programmer, finding the length of an array is invaluable. What is the output here?
NTbooks= ["Matthew", "Mark", "Luke", "John", "Acts of the Apostles"]
NTbooks = len(NTbooks)
print(NTbooks)

  5

  2

  4

  3

 7. Modification of elements in arrays/lists is possible. What is the output here?
fruits = ["Apple", "Banana", "Mango", "Grapes", "Orange"]
fruits[1] = "Pineapple"
fruits[-1] = "Guava"
print(fruits)

  ['Pineapple', 'Pineapple', 'Mango', 'Grapes', 'Guava']

  ['Apple', 'Banana', 'Mango', 'Grapes', 'Apple']

  ['Apple', 'Pineapple', 'Mango', 'Grapes', 'Guava']

  ['Guava', 'Pineapple', 'Mango', 'Grapes', 'Guava']

 8. A multidimensional array is an _________________________. This means an array holds different arrays inside it.

  list within a function

  a nested loop inside an array

  array within an IF function

   array within an array (or a list within a list)

 9. The following code is printing the different lists inside a multidimensional array. Why is the last list in the array not printing?
multidimensional_array= [[1,2], [3,4], [5,6], [7,8]]
print(multidimensional_array[0])
print(multidimensional_array[1])
print(multidimensional_array[2])
print(multidimensional_array[2])

  Change line 5 to: print(multidimensional_array[3]) which will print the fourth list (7,8)

  Change line 4 to: print(multidimensional_array[2]) which will print the last list

  None of the above

  Change line 5 to: print(multidimensional_array[7,8])

 10. The following code prints the number '3' from the multidimensional array. What would you type to print the number 8?
multidimensional_array= [[1,2], [3,4], [5,6], [7,8]]
print(multidimensional_array[1][0])

  print(multidimensional_array[1][1])

  print(multidimensional_array[3][1])

  print(multidimensional_array[3][0])

  print(multidimensional_array[7][8])

 11. A matrix is a ___________. In real-world tasks youwill often have to store data in a rectangular data table.
S.No     StudentName    Science    English     History    Arts    Maths
1            Rob         80         75           85       90      95
2            John        75         80           75       85      100
3            Dave        80         80           80       80      90

  one-dimensional data structure

  three-dimensional data structure

  multi (four plus) dimensional data structure

   two-dimensional data structure

 12. The following represents a _______ matrix where ___ is the number of rows and ___ is the number of columns.
A = [['Roy',80,75,85,90,95],['John',75,80,75,85,100],['Dave',80,80,80,90,95]]

  None of the above

  3 x 6 - 3 - 6

  6 x 6 - 6 - 6

  2 x 6 - 2 - 6

 13. In python, matrix is a nested list. A list is created by placing all the items (elements) inside a square bracket [ ], separated by commas.
# a is _____________________?
a = [['Roy',80,75,85,90,95],
     ['John',75,80,75,85,100],
     ['Dave',80,80,80,90,95]]

#b is a ______________________?
b= [['Roy',80,75,85,90,95],
    ['John',75,80,75],
    ['Dave',80,80,80,90,95]]

  a is a nested list but not a matrix and b is a 2-D matrix with integers

  a is a matrix and a nested list with strings and b is just a nested loop of strings

   a is 2-D matrix with integers and b is a nested list but not a matrix

  None of the above are applicable to 'a' and 'b'

 14. Here a is a matrix that contains name and marks of the students. To see all the marks for the student 'Roy' you would use:
a = [['Roy',80,75,85,90,95],
     ['John',75,80,75,85,100],
     ['Dave',80,80,80,90,95]]

  print(a[1])

  print(a[1][2])

  print(a[0][1])

  print(a[0])

 15. In the matrix 'a' how would you output the '100' that belongs to John?
a = [['Roy',80,75,85,90,95],
     ['John',75,80,75,85,100],
     ['Dave',80,80,80,90,95]]

  print(a[1][1])

  print(a[2][5])

  print(a[1][5])

  print(a[100][3])

 16. The following shows the creation of a 3d array protoype for a cinema (the dimensions are floor, row, seat)
cinemas = []

for k in range(5):
        cinema = []
        for j in range(5):
                column = []
                for i in range(5):
                        column.append(0)
                cinema.append(column)
        cinemas.append(cinema)

print(cinema)

  TRUE

  FALSE

 17. It is useful to know that in other programming languages you have static and dynamic arrays. A static array has a defined length which does not change
array MyList[5] (this is declaring to the program that this particular array just needs enough memory to store five items)

  FALSE

  TRUE

 18. A dynamic array does not have a definite size as the program runs. It can grow and shrink as items are added or deleted. Python lists are:

  more like static arrays

  more like simple variables

  more like dynamic arrays

  more like for loops

 19. An array can be thought of as a ______ data structure and an array of arrays can be seen as a two dimensional array.

  functional

  static

  binary

  linear

 20. We can use an array of arrays to represent two-dimensional spaces like chess board made up of different squares. Noughts and crosses would…

  require a 2 x 2 grid

  None of the above

  require a four dimensional array

  require a 3 x 3 grid