Hello friends if you are looking for NumPy MCQ with Answers | NumPy multiple choice questions | **NumPy** objective type questions | NumPy Question Answers | NumPy Accenture Answers

# Introduction to NumPy for Multi-dimensional Data

## 1] Which of these correctly match the following libraries in the Numpy

ecosystem with what that library is used for?

i] Contains a collection of algorithms used for image processing?

Ans: Scikit-image

## ii] Used to perform statistical operations?

Ans: Statsmodel

## iii] Data visualization tool used for plotting 2-D graphs?

Ans: Matplotlib

## 2] Let’s say you have imported the numpy package as np and you want to assign the variable “x” with a 3 by 2 array of type integer, all of whose values are 1.

Which of these commands will you use to do so?

Ans: x=np.ones((3,2),dtype=np.int32)

## 3] What will be the value stored in the variable y after we have executed the

following code

import numpy np

y=np.arange(2,4,0.5)

Ans: [2, 2.5, 3, 3.5]

## 4] Let’s say you have imported the numpy package as np and you want to print the first 2000 natural numbers in the form of an array and you want all 2000 of the numbers to be visible on screen when printing (including the number 2000). Which of these commands would you use to do so?

Ans: np.set_printoptions(threshold=np.nan) print(np.arange(1,2001))

## 5] What would be the output of the following code:

import numpy as np

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

y=np.array([ [5,6],[7,8]])

z=(x*y)

z

Ans: [ [5,12], [21,32] ]

## 6] What would be the output of the following section of code:

Import numpy as np

x=np.array([4,6,2,8])

np.median(x)

Ans: 5

## 7] Which of these slicing operations can be used to quickly get the reversed

contents of a numpy array called “array”?

Ans: array[ ::-1]

## 8] Match the following features of the numpy nditer function mentioned here

with the correct Boolean category

i] False

Ans: By default, the nditer object returns arrays that can be written on, The

nditer can accept only one dimensional arrays as input

ii]True

Ans: Using this function, we can iterate through each of the individual elements

of the array passed as an input argument

## 9] Which of these statements regarding the ravel() object in Python are true?

Ans: The ravel function reduces a multi-dimensional array to a single

dimensional array, ravel() belongs to the numpy library and can be used on any object that can be parsed.

# Advanced Operations with NumPy Arrays

## 1] Let’s say you have a two dimensional numpy array called “twod” and you want to split it row-wise into two equal halves. Then, which of these numpy functions would you call on it to do so?

Ans: vsplit(twod,2)

## 2] Some of the features of digital images in Numpy are given below. Which of these are true?

Ans: In numpy, images can be represented as a 3D matrix where the first two

dimensions represent the pixels in the image that are arranged in the form of a

grid and the third dimension specifies the number of channels for the image, A

digital image is a multidimensional array and every pixel in a digital image is

represented by a number

## 3] Let’s say you have an image that you have split into two equal halves along the x axis. You have stored these two halves of the original image in the variables x1 and x2 respectively. Which numpy function would you use to combine these two halves to reconstruct the original image?

Ans: concatenate((x1,x2),axis=1)

## 4] Let’s say you have a numpy array called “array_1” and you initialize “another array called “array_2” with the help of the following command:

array_2 = array_1.view() Match the following statements about “array_2” with the correct Boolean value

i]True

Ans: The base for array_2 points to the same object as array_1, array_1 and

array_2 contain the same elements

ii]False

Ans: array_2 points to the same object as array_1, If we re-assign array_2, then

we will end up re-assigning array_1 as well and change its contents

## 5] Let’s say you have a numpy array called “array_3” and you initialize “array_4”

with the help of a following command:

array_4 = array_3.copy()

Match the following statements about “array_4” with the correct Boolean value

i]False

Ans: If we re-assign array_4, then we will end up re-assigning array_3 as well and change its contents, If we change a single element of array_4, then the corresponding element in array_3 changes too, Changing the shape of array_4 will change the shape of array_3 as well

ii]True

Ans: array_3 and array_4 contain the same elements

## 6] Let’s say you have a 1-D numpy array called “cubes” consisting of the cubes of the numbers 1,2,3 and so on till 10. What would be the value of the array:

## cubes [ [ [ 4, 5 ], [ 1, 2 ] ] ]

Ans: [ [ 125, 216] , [ 8, 27] ]

## 7] Some of the features of Pandas is given below. Which of these are true?

Ans: A particular column of a pandas dataframe can be referenced by its column

header, The column header of a Pandas dataframe can be treated in the same

way as the index label of a numpy array

## 8] Let’s say you imported numpy as np and you have initialized a 1-D array of integers called “array”. What would np.all (x < 50) return?

Ans: This function would return a true boolean value if all the entries in your array are less than 50 and false otherwise

## 9] Let’s say you have a Pandas data frame called “phone_data” which contains the data of various phones released in 2018 and their prices. It has the following three columns: “manufacturer”, “phone name” and “ price”. You want only the names of all the phones that are priced more than 10,000. Which of these commands can be used to print these values?

Ans: phone_data[phone_data[‘price’] > 10000][‘phone name’]

## 10] What are the conditions under which broadcasting can take place between

two elements in Numpy?

Ans: A smaller array can be broadcast on a larger array only when the

corresponding dimensions of the two arrays being operated upon are compatible

i.e. when the corresponding dimensions are equal or one of the two dimensions is

1, Broadcasting works when at least one of the elements is a scalar

## 11] Match the following statements about broadcasting with the correct Boolean value: i]False

Ans: The array [ [ 1, 2] , [ 3, 4] ] and the scalar 10 are incompatible with

broadcasting

## ii]True

Ans: The scalar 10 and the scalar 20 are compatible with broadcasting, The array [

[ 1, 2] , [ 3, 4] ] and the array [ 1, 2 ,3 ] are incompatible with broadcasting, The

array [ [ 1, 2] , [ 3, 4] ] and the array [ [1], [2] ] are compatible with broadcasting.

# Introduction to Pandas and DataFrames

## 1] Which of these correctly match the following libraries in the Numpy

ecosystem with what that library is used for?i] Used to perform statistical operations

Ans: Statsmodel

## ii] Data visualization tool used for large datasets

Ans: Bokeh

## iii] Specifically meant for machine learning, data mining, and data analysis

Ans: Scikit-learn

## 2] Which of these statements related to the Pandas Series object are true?

Ans: Pandas Series object is similar to a Python list, Once we create a Pandas

Series object, an index representing the positions for each of the data points is

automatically created for the list

import pandas as pd

companies_ceo = {

## 3] In the following Python code, typing which Python command will give the

user the CEO of Facebook? { ‘Amazon’ : ‘Jeff Bezos’

‘Apple’ : ‘Tim Cook‘,

‘SpaceX’: ‘Elon Musk‘

‘Facebook’: ‘Mark Zuckerberg’

‘Netflix’: ‘Reed Hastings’

}

companies_ceo_series= pd.Series(companies_ceo)

Ans: companies_ceo_series[3], companies_ceo_series[‘Facebook’]

## 4] Match the following statements related to Pandas DataFrames with the

correct boolean values.

i]True

Ans: All the data within a particular column in a Pandas DataFrame must be of

the same data type

## ii]False

Ans: Once a Pandas DataFrame has been created, it is not possible to add a new

column to this DataFrame, Data in different columns of a Pandas DataFrame

cannot be of different data types

## 5] In the following Python code, typing what command will create a DataFrame

called “companies_ceo” whose first column has all the entries of the

‘companies’ list and whose second column has all the entries of the ‘ceo’ list,

with the column names as the names of the respective variables?

import pandas as pd

companies = {

‘Amazon’

‘Apple’ ‘SpaceX’

‘Facebook’

‘Netflix’

}

ceo = {

‘Jeff Bezos’

‘Tim Cook‘,

‘Elon Musk‘

‘Mark Zuckerberg’

‘Reed Hastings’

}

Ans: companies_ceo_tuple = list (zip(companies, ceo)) companies_ceo =

pd.dataframe(companies_ceo_tuple, columns=[‘companies’, ‘ceo’])

## 6] Let’s say you have a pandas DataFrame called “frame” and you want to

export this DataFrame along with its index as a CSV file called “data_frame”

located in the datasets folder of our workspace.

Ans: frame.to_csv(‘datasets/data_frame.csv’)

## 7] Let’s say you have a pandas DataFrame called “panda” which has 8 rows in total and you want to remove the last row from this DataFrame. Which of these Python commands would you use to do so?

Ans: panda.drop(panda.index[7])

## 8] Let’s say you have saved a dataset in a pandas DataFrame called “dataset”

which has tons of records and you only want to access the details of the records in only the 5th, 8th and 14th index. Which of these Python commands can you use to do so?

Ans: dataset.loc[5,8,14], dataset.loc[[5,8,14],:]

## 9] Match the following statements related to the iloc indexer in Pandas with the correct boolean values. i] False

Ans: The column headers can be passed as input arguments in the form of a

string to the iloc function without any errors, When we pass 2:6 as input

argument to the iloc function, we get all details of the records located in the

second index all the way up to the 5th index of the DataFrame

## ii]True

Ans: The iloc indexer is similar to the loc indexer and can be used to access

records located at a particular index in a Pandas DataFrame

## 10] Match the following statements related to the concept of multiIndex in

Pandas with the correct Boolean values.

i]True

Ans: MultiIndex is useful when we have large datasets where using numeric

indexes to refer to each record is unintuitive, MultiIndex lets the user effectively

store and manipulate higher dimensional data in a 2-dimensional tabular

structure

ii]False

Ans: The MultiIndex for a row is some composite key made up of exactly one

column

## 11] Which of these statements related to the pivot function in Pandas is true?

Ans: The combination of the row index and the column header must be unique

in order to generate a pivot table, The Pivot function summarizes the details of

each column in a DataFrame

## 12] What happens when we call the stack () function on a Pandas DataFrame?

Ans: It will create a new DataFrame such that a single row in the original

DataFrame is stacked into multiple rows in the new DataFrame depending on

the number of columns for each row in the original DataFrame

# Manipulating & Analyzing Data in Pandas DataFrames

## 1] Consider the following Python code. What command would you use to iterate through the “companies_ceo” DataFrame and print the list of all the CEOs in this DataFrame?

import pandas as pd

companies = {

‘Company’ : [‘Facebook’, ‘Apple’, ‘Amazon’, ‘Netflix’],

‘CEO’ : [‘Mark Zuckerberg’, ‘Jeff Bezos’, ‘Tim Cook’, ‘,’Reed Hastings’ ],

}

companies_ceo = pd.DataFrame(companies)

Ans: for row in companies_ceo.iterrows(): print(row[1]), for row in

companies_ceo.itertuples(): print(row.CEO

## 2] Which of the following formats does Pandas not support natively when

exporting the contents of a Dataframe?

Ans: JPEG

## 3] Let’s say you have created a Pandas DataFrame called “unsorted” and you

want to sort the contents of this DataFrame column wise in alphabetical order

of the header name. Then, which function would you call on the “unsorted”

DataFrame to do so?

Ans: unsorted.sort_index(axis=1)

## 4] Match the following functions that you can call on a Pandas DataFrame

correctly with what they do

i] All the rows which contain a NaN value in any cell of that row are removed

Ans: .dropna()

## ii] Every cell in the Dataset which has a NaN value will be replaced with 0

Ans: .fillna(0)

## iii] Returns a Boolean array containing true or false values and returns the value in a cell as true if it contains NaN

Ans: .isnull()

## iv] Returns a Boolean array containing true or false values and returns the value in a cell as true if it does not contain NaN

Ans: .notnull()

## 5] Match the following statements related to the .xs function in Pandas

DataFrame with their correct Boolean values.?

i] False

Ans: The .xs function cannot be used to return a cross section of columns

ii]True

Ans: By default, the .xs function only takes a look at values in the first level

index, The .xs function is used when our Pandas DataFrame makes use of a

MultiIndex

## 6] Let’s say you have imported Python as pd and have instantiated two

DataFrames called “frame_1” and “frame_2” with the exact same schema. What command will you use to combine these two DataFrames into a single

DataFrame and make sure that the combined DataFrame has its own unique index?

Ans: pd.concat( [frame_2, frame_1], ignore_index = True ), pd.concat( [frame_1,

frame_2], ignore_index = True )

## 7] The ‘how’ argument in the Pandas merge function allows us to specify what kind of join operation we want to perform on the given Pandas DataFrames. What are the valid values that we can give for this argument?

Ans: left, inner, outer, right

## 8] Some statements related to working with SQL Databases in Python are given below. Match them with their correct Boolean values.

i] False

Ans: Once we have created a table, we can use sqlite3’s .execute() function to

recreate the same table with the same table name so that we have duplicates of a table

ii]True

Ans: The sqlite3 library in Python allows us to create Databases on our local file

system, All the changes that we make to an SQL database on a Jupyter

notebook by connecting with it, will be committed to the database only after

we execute sqlite3’s .commit() function