Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

DA Python 1-7 lec

Total questions: 51

Worksheet time: 26mins

Name
Class
Date
1.

What is the output of the following code?

t = (1, 2, 3) + (4, 5)

print(t)

a)

(1, 2, 3, 4, 5)

b)

(5, 4, 3, 2, 1)

c)

[1, 2, 3, 4, 5]

d)

(1, 2, 3), (4, 5)

2.

What method is used to remove a specific value from a list?

a)

remove()

b)

pop()

c)

delete()

d)

del

3.

The ______ function returns a new sorted list from any sequence.

a)

sorted

b)

sort

c)

remove

d)

del

4.

The update method is used to merge one dictionary into another.

a)

True

b)

False

5.

Which method is used to check if a dictionary contains a specific key?

a)

in

b)

contains()

c)

has_key()

d)

key_check()

6.

The pandas library provides high-level data structures for working with structured and tabular data.

a)

True

b)

False

7.

Which of the following is a mutable object in Python?

a)

List

b)

None

c)

String

d)

Tuple

8.

What will be the output of this code? x = 10; y = 3; print(x // y)

a)

3.33

b)

3

c)

3.0

d)

4

9.

Output of this code:

nums = [1,2,3]

print(sum(nums))

a)

Error

b)

6

c)

5

d)

3

10.

What does the following code print?

a = [1,2,3]

print(a[::-1])

a)

[3,2,1]

b)

[1,2,3]

c)

[1,3,2]

d)

Error

11.

Output of this code:

d = {"a":1, "b":2}

print(d.get("c", 0))

a)

0

b)

None

c)

Error

d)

2

12.

What does the following code return?

lst = [1,2,3,4]

print(lst.index(3))

a)

2

b)

3

c)

1

d)

Error

13.

The with statement is used to handle ______ safely and ensure they are closed properly.

a)

files

b)

tuples

c)

close

d)

ensure

14.

Which of the following is not a valid input for creating a DataFrame?

a)

A single integer

b)

A dictionary of lists

c)

A NumPy array

d)

A list of dictionaries

15.

What is the output of the following code?

import numpy as np

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

print(a[1])

a)

2

b)

1

c)

3

d)

Error

16.

What does this code print?

import numpy as np

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

print(a.shape)

a)

(2, 2)

b)

(2,)

c)

(4,)

d)

Error

17.

Output of the following code?

import numpy as np

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

print(a.dtype)

a)

int64

b)

float64

c)

object

d)

error

18.

What is the result?

import numpy as np

a = np.zeros((2,3))

a)

[[0. 0. 0.]

[0. 0. 0.]]

b)

[0. 0. 0.]

c)

[[0. 0.]

[0. 0. ]

[0. 0.]]

d)

Error

19.

Output of this code?

import numpy as np

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

print(np.where(a>2))

a)

(array([2,3]),)

b)

(array([0,1]),)

c)

[3 4]

d)

Error

20.

What does this print?

import numpy as np

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

print(np.unique([1,2,2,3,3,4]))

a)

[1 2 3 4]

b)

[1 2 2 3 3 4]

c)

[2 3 4]

d)

Error

21.

What is a key difference between a pandas `Series` and a `DataFrame`?

a)

A `Series` is one-dimensional, while a `DataFrame` is two-dimensional.

b)

A `Series` can contain multiple data types, while a `DataFrame` cannot.

c)

A `DataFrame` does not have indexes, while a `Series` always does.

d)

Both `Series` and `DataFrame` are always empty by default.

22.

Which method allows you to select data from a DataFrame by row and column labels?

a)

`loc`

b)

`iloc`

c)

`index`

d)

`slice`

23.

What will the following code output?

import pandas as pd

data = {"A": [1, 2], "B": [3, 4]}

df = pd.DataFrame(data)

print(df.iloc[1])

a)

A 2, B 4

b)

A 1, B 3

c)

[1, 3]

d)

Error: No row with index 1

24.

What will the following code output?

import pandas as pd

s = pd.Series([1, 2, 3], index=["a", "b", "c"])

print(s["b"])

a)

2

b)

1

c)

"b"

d)

Error: Invalid index access

25.

What does this code return?

import pandas as pd

df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6]})

print(df[df['A']>1])

a)

A B

1 2 5

2 3 6

b)

A B

0 1 4

c)

[2, 3]

d)

Error

26.

Output of this code?

import pandas as pd

df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6]})

print(df.iloc[1,1])

a)

5

b)

2

c)

4

d)

Error

27.

What does the following code produce?

import pandas as pd

df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6]})

print(df.loc[0,'B'])

a)

4

b)

1

c)

0

d)

Error

28.

Which library is typically used to read and parse Excel files in pandas?

a)

openpyxl

b)

pickle

c)

lxml

d)

lxml

29.

Which pandas method writes a DataFrame to pickle format?

a)

to_pickle()

b)

write_pickle()

c)

write_pickle()

d)

pickle_dump()

30.

What will the following code do?

import pandas as pd

df = pd.read_excel('data.xlsx', sheet_name='Sheet1')

print(df.head())

a)

Prints the first 5 rows of the Excel sheet

b)

Reads only the first row

c)

Writes CSV without header

31.

How do you remove rows with all NaN values?

df.dropna(how='all', inplace=True)

a)

Drops rows where all values are NaN

b)

Drops any row with NaN

c)

Drops column with NaN

d)

Fills NaN

32.

How can you strip special characters from a string column?

df['col'] = df['col'].str.replace('[^a-zA-Z0-9]', ' ', regex=True)

a)

Removes all non-alphanumeric characters

b)

Converts to lowercase

c)

Converts to uppercase

d)

Replaces spaces with underscores

33.

Output of this code?

import pandas as pd

df = pd.DataFrame({'A':[1,2,3,4,5]})

Q1 = df['A'].quantile(0.25)

Q3 = df['A'].quantile(0.75)

IQR = Q3 - Q1

df_filtered = df[(df['A'] >= Q1 - 1.5*IQR) & (df['A'] <= Q3 +

1.5*IQR)]

print(df_filtered)

a)

Filters out outliers based on IQR

b)

Keeps only outliers

c)

Drops all rows

d)

Error

34.

How do you replace all infinite values with NaN?

import numpy as np

df.replace([np.inf, -np.inf], np.nan, inplace=True)

a)

Replaces +inf/-inf with NaN

b)

Drops infinite values

c)

Converts to zero

d)

Raises error

35.

What does this code output?

import pandas as pd

df = pd.read_csv('data.csv', thousands=',')

a)

Converts numbers like '1,000' to 1000

b)

Treats ',' as delimiter

c)

Reads all values as strings

d)

Error

36.

Output of this code?

import pandas as pd

df = pd.read_csv('data.csv', skip_blank_lines=True)

a)

Ignores blank lines in the CSV

b)

Reads blank lines as NaN

c)

Error

d)

Deletes CSV

37.

What is the output of this code?

import pandas as pd

df = pd.read_csv('data.csv', low_memory=False)

a)

Prevents dtype guessing and ensures proper memory usage

b)

Reads CSV in chunks

c)

Converts everything to string

d)

Error

38.

What does this code do?

import pandas as pd

df = pd.read_sql('SELECT * FROM table1', conn, index_col='ID')

a)

Reads SQL table and sets 'ID' as index

b)

Writes SQL table

c)

Converts SQL table to CSV

d)

Creates a new SQL table

39.

What does this code do?

import pandas as pd

df = pd.read_csv('data.csv', header=span>None)

print(df.head())

a)

Reads only the first row

b)

Writes CSV without header

c)

Reads CSV without using the first row as header

d)

Reads CSV using the first row as header

40.

What does this code do?

df['col'] = df['col'].str.lower()

a)

Converts all strings in the column to lowercase

b)

Converts to uppercase

c)

Strips spaces

d)

Deletes column

41.

How can you remove columns with more than 50% missing values?

df.dropna(thresh=len(df)*0.5, axis=1, inplace=True)

a)

Drops columns with more than 50% NaN

b)

Drops rows with >50% NaN

c)

Replaces NaN with 0

d)

Keeps only rows with >50% non-NaN

42.

What does this code produce?

import pandas as pd

df1 = pd.DataFrame({'key':[1,2,3],'A':[10,20,30]})

df2 = pd.DataFrame({'key':[3,4,5],'B':[300,400,500]})

pd.concat([df1, df2], axis=0, ignore_index=True)

a)

Stacks df1 and df2 vertically and resets the index

b)

Stacks horizontally

c)

Performs a merge

d)

Produces an error

43.

What is the result of this code?

import pandas as pd

df = pd.DataFrame({'X':[1,2],'Y':[3,4]})

pd.melt(df, id_vars=['X'])

a)

Keeps 'X' fixed and unpivots 'Y' into long format

b)

Keeps 'Y' fixed and unpivots 'X'

c)

Drops column 'Y'

d)

Produces an error

44.

What does this code do?

import pandas as pd

df1 = pd.DataFrame({'key':[1,2,3],'A':[10,20,30]})

df2 = pd.DataFrame({'key':[2,3,4],'B':[200,300,400]})

pd.merge(df1, df2, how='outer', on='key')

a)

Outer join: keeps all keys from both DataFrames, fills missing with NaN

b)

Left join

c)

Inner join

d)

Right join

45.

Output of this code?

import pandas as pd

df = pd.DataFrame({'id':[1,1,2,2],'variable':['X','Y','X','Y'],'value':[ 10,20,30,40]}

df.pivot(index='id', columns='variable', values='value')

a)

Produces an error

b)

Drops column 'value'

c)

Reshapes from wide to long

d)

Reshapes from long to wide format with 'id' as index

46.

What is the difference between merge and concat?

a)

merge joins based on columns or keys, concat stacks DataFrames along axis

b)

merge concatenates, concat joins

c)

Both do the same thing

d)

merge deletes duplicates, concat does not

47.

How can you reorder levels in a MultiIndex?

df.reorder_levels([1,0])

a)

Switches the positions of the levels

b)

Sorts the index

c)

Drops a level

d)

Creates a new column

48.

What is the key difference between reorder_levels() and swaplevel() in hierarchical

indexing?

a)

reorder_levels() allows arbitrary reordering of index levels, while swaplevel() only swaps

two levels

b)

swaplevel() can reorder multiple levels, while reorder_levels() swaps levels

c)

eorder_levels() can be used on non-hierarchical indexes, while swaplevel() cannot

d)

swaplevel() requires specifying all levels, while reorder_levels() defaults to the first two

49.

In a merge() operation, what does the how='outer' parameter do?

a)

Performs a union of the keys from both DataFrames, including all rows from both

b)

Includes only rows with keys present in both DataFrames

c)

Includes rows from the left DataFrame only

d)

Includes rows from the right DataFrame only

50.

Output of this code?

import pandas as pd

arrays = [['A','A','B','B'], [1,2,1,2]]

index = pd.MultiIndex.from_arrays(arrays, names=('letter','num'))

df = pd.DataFrame({'val':[10,20,30,40]}, index=index)

df.loc['A']

a)

Selects all rows where first level of MultiIndex is 'A'

b)

Selects rows where second level is 'A'

c)

Returns columns named 'A'

d)

Produces an error

51.

Output of this code?

import pandas as pd

df = pd.DataFrame({'id':[1,1,2,2],'variable':['X','Y','X','Y'],'value':[ 10,20,30,40]})

df.pivot(index='id', columns='variable', values='value')

a)

Reshapes from long to wide format with 'id' as index

b)

Reshapes from wide to long

c)

Drops column 'value'

d)

Produces an error