Unit 4: Data Analysis with NumPy and Pandas - Practice Quiz

CAP776 — Programming In Python 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which function is commonly used to create a NumPy ndarray from a Python list?

NumPy ndarray object Easy
A. np.frame()
B. np.list()
C. np.series()
D. np.array()

2 Which attribute gives the data type of the elements in a NumPy array?

NumPy ndarray object Easy
A. size
B. dtype
C. ndim
D. shape

3 Which NumPy attribute returns the number of dimensions in an array?

Dimensions in arrays Easy
A. size
B. dtype
C. ndim
D. shape

4 What is the shape of np.array([[1, 2, 3], [4, 5, 6]])?

Dimensions in arrays Easy
A. (6, 1)
B. (3, 2)
C. (1, 6)
D. (2, 3)

5 Given a = np.array([10, 20, 30]), which expression accesses 20?

Accessing array elements Easy
A. a[2]
B. a[1]
C. a[3]
D. a[0]

6 Given a = np.array([[1, 2], [3, 4]]), which expression accesses 4?

Accessing array elements Easy
A. a[1, 0]
B. a[0, 0]
C. a[1, 1]
D. a[0, 1]

7 Which method changes the shape of a NumPy array without changing its data?

NumPy array manipulation Easy
A. convert()
B. remove()
C. reshape()
D. replace()

8 Which NumPy function joins arrays along an existing axis?

NumPy array manipulation Easy
A. np.calculate()
B. np.contains()
C. np.compare()
D. np.concatenate()

9 What does the term ndarray mean in NumPy?

N-dimensional arrays (ndarray) Easy
A. N-dimensional array
B. Nested dynamic array
C. Numeric dictionary array
D. Named data array

10 Which NumPy function creates an array filled with zeros?

N-dimensional arrays (ndarray) Easy
A. np.arange()
B. np.linspace()
C. np.empty()
D. np.zeros()

11 Which Pandas object represents one-dimensional labeled data?

Pandas fundamentals Easy
A. DataFrame
B. ndarray
C. Series
D. IndexFrame

12 Which Pandas object is commonly used for two-dimensional tabular data?

Pandas fundamentals Easy
A. Series
B. ArrayList
C. DataTable
D. DataFrame

13 Which Pandas function reads data from a CSV file?

Pandas with CSV and HTML data Easy
A. pd.import_csv()
B. pd.read_csv()
C. pd.open_csv()
D. pd.load_csv()

14 Which Pandas function reads tables from an HTML page?

Pandas with CSV and HTML data Easy
A. pd.load_html()
B. pd.import_html()
C. pd.read_html()
D. pd.open_html()

15 Which method displays the first few rows of a Pandas DataFrame?

Pandas operations Easy
A. shape()
B. info()
C. head()
D. tail()

16 Which DataFrame attribute returns the number of rows and columns?

Pandas operations Easy
A. ndim
B. dtypes
C. shape
D. size

17 Which Pandas method applies a function to values along a DataFrame axis?

Pandas with functions Easy
A. merge()
B. drop()
C. apply()
D. rename()

18 Which function can calculate the average of numeric values in a Pandas column?

Pandas with functions Easy
A. count()
B. sum()
C. mean()
D. max()

19 Which Pandas method detects missing values in a DataFrame?

Data cleaning process Easy
A. fillna()
B. isnull()
C. dropna()
D. replace()

20 Which Pandas method removes rows or columns containing missing values?

Data cleaning process Easy
A. fillna()
B. isna()
C. dropna()
D. notna()

21 What is the main advantage of using a NumPy ndarray instead of a Python list for numerical calculations?

NumPy ndarray object Medium
A. It automatically labels every element with a name
B. It supports vectorized operations on homogeneous data
C. It stores values of different types more efficiently
D. It prevents all changes to the stored values

22 Given a = np.array([[1, 2, 3], [4, 5, 6]]), what are a.ndim and a.shape, respectively?

Dimensions in arrays Medium
A. 3 and (2, 3, 1)
B. 2 and (2, 3)
C. 1 and (6,)
D. 2 and (3, 2)

23 For a = np.array([[10, 20, 30], [40, 50, 60]]), which expression returns the value 50?

Accessing array elements Medium
A. a[1, 0]
B. a[0, 1]
C. a[2, 1]
D. a[1, 1]

24 What is the result of np.reshape(np.arange(6), (3, 2))?

NumPy array manipulation Medium
A. [[1, 2], [3, 4], [5, 6]]
B. [[0, 1, 2], [3, 4, 5]]
C. [[0, 2, 4], [1, 3, 5]]
D. [[0, 1], [2, 3], [4, 5]]

25 If a = np.zeros((2, 3, 4)), how many elements does a contain?

N-dimensional arrays (ndarray) Medium
A. 36
B. 24
C. 12
D. 9

26 What is the shape of the array created by np.ones((4, 1, 2))?

Dimensions in arrays Medium
A. (4, 1, 2)
B. (4, 2)
C. (1, 4, 2)
D. (2, 1, 4)

27 For a = np.array([5, 10, 15, 20, 25]), what values are returned by a[1:4]?

Accessing array elements Medium
A. [10, 15, 20, 25]
B. [15, 20, 25]
C. [5, 10, 15]
D. [10, 15, 20]

28 What does np.concatenate((a, b), axis=0) do when a and b are two-dimensional arrays with the same number of columns?

NumPy array manipulation Medium
A. Sorts their values into one row
B. Joins them by adding columns
C. Joins them by adding rows
D. Multiplies corresponding elements

29 Given a = np.array([2, 4, 6]), what is the result of a * 3 + 1?

NumPy array manipulation Medium
A. [7, 13, 19]
B. [9, 15, 21]
C. [6, 12, 18]
D. [3, 5, 7]

30 Which Pandas structure is most appropriate for representing tabular data with labeled rows and columns?

Pandas fundamentals Medium
A. A NumPy scalar
B. A Series
C. A DataFrame
D. An Index

31 What does df['Sales'] generally return when df is a Pandas DataFrame containing a column named Sales?

Pandas fundamentals Medium
A. A single column name
B. A Python dictionary
C. A NumPy matrix
D. A Pandas Series

32 Which statement correctly loads a CSV file named sales.csv into a Pandas DataFrame?

Pandas with CSV and HTML data Medium
A. pd.read_csv('sales.csv')
B. pd.load_csv('sales.csv')
C. pd.import_csv('sales.csv')
D. pd.DataFrame.csv('sales.csv')

33 What does pd.read_html('page.html') typically return?

Pandas with CSV and HTML data Medium
A. A dictionary of HTML tags
B. One NumPy array for every row
C. One string containing the page source
D. A list of DataFrame objects

34 Which expression selects rows from df where the Age column is greater than 30?

Pandas operations Medium
A. df[df['Age'] > 30]
B. df['Age' > 30]
C. df.where('Age' > 30)
D. df.select(Age > 30)

35 What is the purpose of df.groupby('Department')['Salary'].mean()?

Pandas operations Medium
A. Calculating the overall salary total
B. Calculating average salary for each department
C. Replacing salary values with department names
D. Sorting departments by salary values

36 What does df['Price'].apply(lambda x: x * 1.1) do?

Pandas with functions Medium
A. Replaces every price with the column mean
B. Adds 1.1 to every price
C. Rounds each price to 1.1 places
D. Multiplies every price by 1.1

37 Which statement applies a function to each row of a DataFrame named df?

Pandas with functions Medium
A. df.function(axis='rows')
B. df.map(function, axis=1)
C. df.apply(function, axis=0)
D. df.apply(function, axis=1)

38 Which command removes rows containing at least one missing value from df?

Data cleaning process Medium
A. df.remove_nulls()
B. df.dropna()
C. df.clearna()
D. df.delete(missing=True)

39 Which statement replaces missing values in the Score column with that column's mean?

Data cleaning process Medium
A. df['Score'].fillna(0)
B. df['Score'].replace_mean(None)
C. df['Score'].fillna(df['Score'].mean())
D. df['Score'].dropna(df['Score'].mean())

40 What is the main purpose of df.drop_duplicates() during data cleaning?

Data cleaning process Medium
A. Converting strings into numbers
B. Sorting rows in descending order
C. Removing repeated rows
D. Removing columns with null values

41 What is printed by the following code?

a = np.arange(12).reshape(3, 4)

b = a[:, 1:3]

b += 100

c = a[[0, 2], [1, 2]]

print(c, np.shares_memory(a, b))

NumPy ndarray object Hard
A. [101 110] True
B. [1 10] True
C. [101 110] False
D. [100 109] False

42 Given a = np.array([200, 250], dtype=np.uint8) and b = a + np.array([100], dtype=np.uint8), what are b.tolist() and b.sum()?

NumPy ndarray object Hard
A. [300, 350] and 650
B. [44, 94] and 138
C. [44, 94] and 394
D. [255, 255] and 510

43 Arrays x and y have shapes (2, 1, 3) and (1, 4, 1), respectively. What is the shape of x + y under NumPy broadcasting?

Dimensions in arrays Hard
A. (1, 4, 3)
B. (2, 1, 1)
C. (2, 4, 3)
D. (2, 4, 1)

44 If x.shape == (2, 3), what is the shape of np.expand_dims(x, axis=(0, -1))?

Dimensions in arrays Hard
A. (1, 2, 1, 3)
B. (1, 2, 3, 1)
C. (1, 1, 2, 3)
D. (2, 1, 3, 1)

45 For a = np.arange(12).reshape(3, 4), what does a[[0, 2], [1, 3]] return?

Accessing array elements Hard
A. array([[1, 3], [9, 11]])
B. array([1, 11])
C. array([[1, 11]])
D. array([3, 9])

46 Let x = np.arange(24).reshape(2, 3, 4). What is the result of x[:, [0, 2], [1, 3]]?

Accessing array elements Hard
A. array([[1, 11], [13, 23]])
B. array([[1, 9], [15, 23]])
C. array([[1, 13], [11, 23]])
D. array([1, 11, 13, 23])

47 What does np.resize(np.array([1, 2, 3]), (2, 4)) produce?

NumPy array manipulation Hard
A. array([[1, 2, 3, 0], [0, 0, 0, 0]])
B. array([[1, 2, 3, 2], [3, 1, 2, 3]])
C. array([[1, 2, 3, 1], [1, 2, 3, 1]])
D. array([[1, 2, 3, 1], [2, 3, 1, 2]])

48 If a and b both have shape (2, 3), what is the shape of np.stack([a, b], axis=1)?

NumPy array manipulation Hard
A. (2, 6)
B. (2, 3, 2)
C. (4, 3)
D. (2, 2, 3)

49 Array A has shape (5, 1, 3, 4) and array B has shape (1, 7, 4, 2). What is the shape of A @ B?

N-dimensional arrays (ndarray) Hard
A. (1, 7, 3, 2)
B. (5, 1, 3, 2)
C. (5, 7, 3, 2)
D. (5, 7, 4, 4)

50 Consider x = np.arange(120).reshape(2, 3, 4, 5) and y = np.moveaxis(x, (0, 3), (2, 0)). Which statement is correct?

N-dimensional arrays (ndarray) Hard
A. y.shape == (5, 3, 4, 2) and y[1, 2, 0, 3] == 46
B. y.shape == (3, 5, 2, 4) and y[1, 2, 0, 3] == 56
C. y.shape == (5, 2, 3, 4) and y[1, 2, 0, 3] == 46
D. y.shape == (5, 3, 2, 4) and y[1, 2, 0, 3] == 56

51 Given s1 = pd.Series([10, 20], index=['a', 'b']) and s2 = pd.Series([1, 2], index=['b', 'c']), what does s1.add(s2, fill_value=0) contain?

Pandas fundamentals Hard
A. a: NaN, b: 21.0, c: NaN
B. a: 10.0, b: 21.0, c: 2.0
C. a: 10.0, b: 20.0, c: 3.0
D. a: 11.0, b: 22.0, c: 2.0

52 What is s.tolist() after executing s = pd.Series([10, 20, 30], index=['a', 'a', 'b']) followed by s.loc['a'] += 5?

Pandas fundamentals Hard
A. [15, 20, 30]
B. [15, 25, 30]
C. [15, 25, 35]
D. [10, 25, 30]

53 A CSV contains code values NA, Missing, and valid. It is read using pd.read_csv(file, keep_default_na=False, na_values=['Missing']). What is df['code'].isna().tolist()?

Pandas with CSV and HTML data Hard
A. [True, False, False]
B. [False, False, False]
C. [False, True, False]
D. [True, True, False]

54 An HTML document contains three tables: a report table containing Revenue, a report table containing Costs, and an other table containing Revenue. What does pd.read_html(html, match='Revenue', attrs={'class': 'report'}) return?

Pandas with CSV and HTML data Hard
A. A list containing both report tables
B. A list containing the first and third tables
C. A DataFrame containing the first table
D. A list containing only the first table

55 A left DataFrame has key values [1, NaN, NaN], and a right DataFrame has key values [NaN, 2]. How many rows result from left.merge(right, on='key', how='inner')?

Pandas operations Hard
A. 2 rows
B. 3 rows
C. 0 rows
D. 1 row

56 For df = pd.DataFrame({'g': ['A', 'A', 'B'], 'x': [1, 3, 10]}), what is (df['x'] - df.groupby('g')['x'].transform('mean')).tolist()?

Pandas operations Hard
A. [-1.0, 1.0, 8.0]
B. [1.0, 3.0, 10.0]
C. [-2.0, 0.0, 0.0]
D. [-1.0, 1.0, 0.0]

57 Let df = pd.DataFrame({'a': [3, 1], 'b': [2, 8]}) and r = df.apply(lambda s: pd.Series({'min': s.min(), 'span': s.max() - s.min()})). Which statement is correct?

Pandas with functions Hard
A. r.shape == (2, 4) and r.loc['min', 'a'] == 3
B. r.shape == (2, 2) and r.loc['span', 'b'] == 7
C. r.shape == (4, 2) and r.loc['span', 'a'] == 1
D. r.shape == (2, 2) and r.loc['span', 'b'] == 6

58 What is the result of pd.Series([' a ', None]).map(str.strip, na_action='ignore').tolist()?

Pandas with functions Hard
A. [' a ', None]
B. ['a', 'None']
C. ['a', None]
D. ['a', NaN]

59 A DataFrame has id values [1, 1, 2, 3, 3]. Which IDs remain after df.drop_duplicates(subset=['id'], keep=False)?

Data cleaning process Hard
A. The second 1, 2, and second 3
B. The first 1, 2, and first 3
C. Only ID 2
D. IDs 1, 2, and 3

60 Given s = pd.Series([1.0, np.nan, np.nan, 4.0, np.nan]), what does s.interpolate(limit=1, limit_direction='both', limit_area='inside').tolist() produce?

Data cleaning process Hard
A. [1.0, 2.0, 3.0, 4.0, NaN]
B. [1.0, 2.0, 3.0, 4.0, 4.0]
C. [1.0, NaN, 3.0, 4.0, 4.0]
D. [1.0, 2.0, NaN, 4.0, NaN]