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.
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.
True or False: The conventional way to import the numpy library into a program is as shown below:
import numpy as npArray
True or False: The following code creates a one-dimensional array and prints its contents.
myArray = np.array([1,2,3])
print(myArray)
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))
True or False: Numpy arrays are immutable.
True or False: All of the elements in a numpy array must be of the same type.
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']])
True or False: Just like a list, a numpy array supports the append method.
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.
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.
True or False: You can square and take the square root of a numpy array containing numeric data.
True or False: Multi-dimensional arrays of the same size and shape can be multiplied on an element-by-element basis.
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))
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')
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)
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.
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])
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))
True or False: The sort method of a numpy array creates a new array and sorts the new array, leaving the original array untouched.
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.
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.
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.
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.
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."
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.
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.
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.
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.
True.
Explanation
See NumpPy Arrays - Difference
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.
True.
Explanation
See NumpPy Arrays - Intersection
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.
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.
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.
True.
Explanation
See NumpPy Arrays - Extract a subarray by slicing
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.
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.
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.
True.
Explanation
See NumpPy Arrays - Multiplication of lists and arrays
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.
True.
Explanation
See NumpPy Arrays - Multiplication by a scalar
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.
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.
True.
Explanation
See NumpPy Arrays - A two-dimensional array
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.
False.
Explanation
See NumpPy Arrays - Arrays are mutable.
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])
True.
Explanation
See NumpPy Arrays - Creating a numpy array
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
True.
Explanation
See NumpPy Arrays - introductory section
File name: NumpyReview.htm
Revised: 04/24/18
Copyright 2018, Richard Baldwin
-end-