Numpy Review Questions

Table of Contents

Preface

This page contains review questions for teaching ITSE-1302 Computer Programming: Scientific Python 1 at Austin Community College in Austin, TX.

The questions and the answers on this page are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back again.

The questions on this page are similar to the questions that you will find on the test for this competency. Therefore, it is strongly recommended that you study the material until you thoroughly understand the material covered by these questions.

Questions

Question 1.

True or False: You will often see a numpy array referred to as an ndarray. This name derives from the fact that an ndarray is an N-dimensional array.

Answer 1

Question 2

True or False: The conventional way to import the numpy library into a program is as shown below:

import numpy as npArray

Answer 2

Question 3

True or False: The following code creates a one-dimensional array and prints its contents.

myArray = np.array([1,2,3])
print(myArray)

Answer 3

Question 4

True or False: The easiest way to access the elements in an array is to use parentheses just like accessing the elements of a list as shown below:

myArray = np.array([1,2,3])
print(myArray(2),myArray(1),myArray(0))

Answer 4

Question 5

True or False: Numpy arrays are immutable.

Answer 5

Question 6

True or False: All of the elements in a numpy array must be of the same type.

Answer 6

Question 7

True or False:

The following code is a valid way to create a two-dimensional numpy array.

myArray = np.array([['r0-c0','r0-c1','r0-c2'],['r1-c0','r1-c1','r1-c2']])

Answer 7

Question 8

True or False: Just like a list, a numpy array supports the append method.

Answer 8

Question 9

True or False: Just like with Python lists, adding one numpy array to another numpy array using the plus (+) operator results in a longer array, which is the concatenation of the two original arrays.

Answer 9

Question 10

True or False: The result of multiplying a numpy array by a scalar is that each element in the array is individually multiplied by the scalar.

Answer 10

Question 11

True or False: You can square and take the square root of a numpy array containing numeric data.

Answer 11

Question 12

True or False: Multi-dimensional arrays of the same size and shape can be multiplied on an element-by-element basis.

Answer 12

Question 13

True or False: The following code creates an array with five rows and three columns populated with random values.

myRandomArray = np.random.random((5,3))

Answer 13

Question 14

True or False: The following code creates an array with two rows and three columns where every element contains the string 'hello'.

myNonZeroArray = np.full((2,3),'hello')

Answer 14

Question 15

True or False: The following code can be used to create a matrix-style array with a 1. in each diagonal position and a 0. in every other position.

mySpecialMatrix = np.ones(4,4)

Answer 15

Question 16

True or False: You can access an array using slicing in much the same way that you can use slicing to access the elements in a list. This is a mechanism by which you can extract a subarray from an array.

Answer 16

Question 17

True or False: The two statements shown below for slicing an array produce the same result.

mySubArray = myArray[0:2, 1:3]
mySubArray = np.array(myArray[0:2, 1:3])

Answer 17

Question 18

True or False: The following code will compute and display the statistical mean of all the elements in the array:

myArray = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(np.median(myArray))

Answer 18

Question 19

True or False: The sort method of a numpy array creates a new array and sorts the new array, leaving the original array untouched.

Answer 19

Question 20

True or False: The intersect1d method of a numpy array finds the intersection of two arrays and returns the sorted, unique values that are in both of the input arrays in a one-dimensional array.

Answer 20

Question 21

True or False: The union1d method of a numpy array finds the union of two arrays and returns the unique, two-dimensional sorted array of values that are in either of the two input arrays.

Answer 21

Question 22

True or False: The setdiff1d(ar1,ar2) method of a numpy array finds the set difference of two arrays and returns a one-dimensional array containing the sorted, unique values in ar1 that are not in ar2.

Answer 22

Question 23

True or False: The setxor1d method of a numpy array finds the set exclusive-or of two arrays and returns a one-dimensional array containing the sorted, unique values that are in both of the input arrays.

Answer 23

Question 24

True or False: Broadcasting allows you to perform arithmetic operations on arrays of different shapes. "Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes."

Answer 24

Question 25

True or False: A one-dimensional array can be treated as a vector in linear algebra. The sum of the products of two vectors is something called the cross product.

Answer 25


Answers

Answer 25

False.

Explanation

See NumpPy Arrays - Computing the dot product

A one-dimensional array can be treated as a vector in linear algebra. The sum of the products of two vectors is something called the dot product. A cross product is something entirely different.

Back to Question 25

Answer 24

True.

Explanation

See NumpPy Arrays - Broadcasting

See the general broadcasting rules at https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html.

Back to Question 24

Answer 23

False.

Explanation

See NumpPy Arrays - Exclusive or

The setxor1d method finds the set exclusive-or of two arrays and returns a one-dimensional array containing the sorted, unique values that are in only one (not both) of the input arrays.

Back to Question 23

Answer 22

True.

Explanation

See NumpPy Arrays - Difference

Back to Question 22

Answer 21

False.

Explanation

See NumpPy Arrays - Union

The union1d method finds the union of two arrays and returns the unique, one-dimensional sorted array (not two-dimensional) of values that are in either of the two input arrays.

Back to Question 21

Answer 20

True.

Explanation

See NumpPy Arrays - Intersection

Back to Question 20

Answer 19

False.

Explanation

See NumpPy Arrays - Sorting a one dimensional array

The array sort method is an in-place sort. This means that the method modifies the array on which it is called.

Back to Question 19

Answer 18

False.

Explanation

See NumpPy Arrays - Mean values, sums, and medians

The code shown will compute the statistical median of all the elements in the array, not the statistical mean.

Back to Question 18

Answer 17

False.

Explanation

See NumpPy Arrays - Modify an element in the subarray and Extract a subarray into an independent array

When you extract a subarray by slicing an array using the first statement shown above, you don't create a new array. Instead you simply create a new reference to a rectangular portion of the original array. If you modify an element in the subarray, you are in fact modifying an element in the original array.

The second statement extracts a rectangular portion of the original array into a new subarray that is independent of the original array. If you modify an element in the new subarray, you will not modify an element in the original array.

This is a very important distinction that can lead to subtle programming errors if not fully understood and taken into account when programming.

Back to Question 17

Answer 16

True.

Explanation

See NumpPy Arrays - Extract a subarray by slicing

Back to Question 16

Answer 15

False.

Explanation

See NumpPy Arrays - A matrix with ones on the diagonal and zeros elsewhere

The correct code for this purpose is shown below:

mySpecialMatrix = np.eye(4,4)

Note the name of the method. The method named ones is used to create an array and put a float 1.0 in each array element - see An array full of ones.

Back to Question 15

Answer 14

True.

Explanation

See NumpPy Arrays - Create an array full of other values

The full method takes two arguments. The first argument is a tuple that specifies the dimensions of the desired array. The second argument specifies the value that is to be put into each element in the array.

Back to Question 14

Answer 13

True.

Explanation

See NumpPy Arrays - Create an array full of random values

Note that the syntax of the statement lacks the typical np.array term.

Back to Question 13

Answer 12

True.

Explanation

See NumpPy Arrays - Multiplication of lists and arrays

Back to Question 12

Answer 11

True.

Explanation

See NumpPy Arrays - Squares and square roots

You can square and take the square root of an array containing numeric data. When you do, the result is a set of new values in the array with the operation having been performed on an element-by-element basis.

Back to Question 11

Answer 10

True.

Explanation

See NumpPy Arrays - Multiplication by a scalar

Back to Question 10

Answer 9

False.

Explanation

See NumpPy Arrays - Addition of lists and arrays

The plus sign with lists does concatenation but the plus sign with arrays does element-by-element addition (vector addition). Many of the functions available for processing arrays operate on an element-by-element basis.

Back to Question 9

Answer 8

False.

Explanation

See NumpPy Arrays - The append method

You cannot append a value to an array by calling the append method on the array. An attempt to do so will throw an error. Once created, the size of an array is fixed.

Back to Question 8

Answer 7

True.

Explanation

See NumpPy Arrays - A two-dimensional array

Back to Question 7

Answer 6

True.

Explanation

See NumpPy Arrays - All of the elements in a numpy array must be of the same type

Sometimes when you try to mix types in a numpy array, one type will be converted into the other type and an error will not be thrown. Other times, an error will be thrown. This probably depends on whether or not one type can be converted into the other type.

Back to Question 6

Answer 5

False.

Explanation

See NumpPy Arrays - Arrays are mutable.

Back to Question 5

Answer 4

False.

Explanation

See NumpPy Arrays - Accessing an array with an index

The easiest way to access the elements in an array is to use square brackets just like accessing the elements of a list as shown below:

myArray = np.array([1,2,3])

print(myArray[2],myArray[1],myArray[0])

Back to Question 4

Answer 3

True.

Explanation

See NumpPy Arrays - Creating a numpy array

Back to Question 3

Answer 2

False.

Explanation

See NumpPy Arrays - introductory section

The conventional way to import the numpy library into a program is as shown below:

import numpy as np

Back to Question 2

Answer 1

True.

Explanation

See NumpPy Arrays - introductory section

Back to Question 1

Houskeeping material

File name: NumpyReview.htm

Revised: 04/24/18

Copyright 2018, Richard Baldwin

-end-