wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Programming Quiz

Total questions: 28

Worksheet time: 14mins

Name
Class
Date
1.

Which function returns the square root of a number in Python?

a)

sqrt()

b)

square()

c)

pow()

d)

root()

2.

What will math.ceil(4.2) return?

a)

4

b)

5

c)

4.2

d)

Error

3.

Which function gives the value of π in the math module?

a)

math.pi()

b)

math.pi

c)

math.PI

d)

pi

4.

What is the output of pow(2, 3)?

a)

5

b)

6

c)

8

d)

9

5.

What will math.floor(-3.7) return?

a)

-3

b)

-4

c)

3

d)

4

6.

What does "hello".upper() return?

a)

"Hello"

b)

"HELLO"

c)

"hello"

d)

Error

7.

Which method removes whitespace at the beginning and end of a string?

a)

strip()

b)

remove()

c)

trim()

d)

clean()

8.

What will "Python".find("th") return?

a)

2

b)

3

c)

-1

d)

Error

9.

"banana".count("a") returns:

a)

2

b)

3

c)

4

d)

5

10.

"Python".replace("P", "C") returns:

a)

"Cython"

b)

"Python"

c)

"Chon"

d)

"PCython"

11.

"hello".capitalize() returns:

a)

"Hello"

b)

"HELLO"

c)

"hello"

d)

"hELLO"

12.

Which method adds an item to the end of a list?

a)

insert()

b)

append()

c)

add()

d)

extend()

13.

What will len([10, 20, 30]) return?

a)

2

b)

3

c)

4

d)

Error

14.

How do you remove the first occurrence of 2 in [1, 2, 3, 2]?

a)

list.remove(2)

b)

list.delete(2)

c)

list.pop(2)

d)

list.discard(2)

15.

Which statement creates a list of numbers from 0 to 9?

a)

list(0, 9)

b)

range(0, 9)

c)

list(range(10))

d)

list(range(1, 10))

16.

Which code creates a dictionary?

a)

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

b)

d = {"a", "b"}

c)

d = [("a", 1), ("b", 2)]

d)

d = ("a": 1, "b": 2)

17.

How do you get all keys from a dictionary d?

a)

d.keys()

b)

d.getkeys()

c)

d.allkeys()

d)

keys(d)

18.

What does d.get("x", 0) return if "x" is not in the dictionary?

a)

Error

b)

None

c)

0

d)

"x"

19.

Which statement updates the value of "age" to 25 in person = {"name": "Ali", "age": 20}?

a)

person.update("age", 25)

b)

person["age"] = 25

c)

update(person, "age", 25)

d)

person.set("age", 25)

20.

What does len({"a": 1, "b": 2, "c": 3}) return?

a)

2

b)

3

c)

6

d)

Error

21.

Which mode is used to open a file for writing in Python?

a)

"r"

b)

"w"

c)

"a"

d)

"rw"

22.

What does f.read() do?

a)

Reads one line

b)

Reads the whole file

c)

Reads only first 10 characters

d)

Closes the file

23.

Which command ensures that a file is automatically closed after use?

a)

open(file).close()

b)

with open(file) as f:

c)

file.openwith()

d)

close(file)

24.

If a file doesn’t exist, which mode creates a new one?

a)

"r"

b)

"w"

c)

"x"

d)

Both b and c

25.

Which keyword is used to define a function in Python?

a)

func

b)

define

c)

def

d)

function

26.

What is the correct syntax for a function that returns the sum of two numbers?

a)

def add(a, b): return a + b

b)

function add(a, b): return a + b

c)

def add(a, b): print(a + b)

d)

Both a and c

27.

What happens if a function doesn’t have a return statement?

a)

It returns 0

b)

It returns None

c)

It throws an error

d)

It prints nothing

28.

What is the output? def test(x=5, y=2): return x * y print(test())

a)

10

b)

7

c)

Error

d)

None