NEW
Font size
WorksheetsPython Programming Quiz
Total questions: 28
Worksheet time: 14mins
Which function returns the square root of a number in Python?
sqrt()
square()
pow()
root()
What will math.ceil(4.2) return?
4
5
4.2
Error
Which function gives the value of π in the math module?
math.pi()
math.pi
math.PI
pi
What is the output of pow(2, 3)?
5
6
8
9
What will math.floor(-3.7) return?
-3
-4
3
4
What does "hello".upper() return?
"Hello"
"HELLO"
"hello"
Error
Which method removes whitespace at the beginning and end of a string?
strip()
remove()
trim()
clean()
What will "Python".find("th") return?
2
3
-1
Error
"banana".count("a") returns:
2
3
4
5
"Python".replace("P", "C") returns:
"Cython"
"Python"
"Chon"
"PCython"
"hello".capitalize() returns:
"Hello"
"HELLO"
"hello"
"hELLO"
Which method adds an item to the end of a list?
insert()
append()
add()
extend()
What will len([10, 20, 30]) return?
2
3
4
Error
How do you remove the first occurrence of 2 in [1, 2, 3, 2]?
list.remove(2)
list.delete(2)
list.pop(2)
list.discard(2)
Which statement creates a list of numbers from 0 to 9?
list(0, 9)
range(0, 9)
list(range(10))
list(range(1, 10))
Which code creates a dictionary?
d = {"a": 1, "b": 2}
d = {"a", "b"}
d = [("a", 1), ("b", 2)]
d = ("a": 1, "b": 2)
How do you get all keys from a dictionary d?
d.keys()
d.getkeys()
d.allkeys()
keys(d)
What does d.get("x", 0) return if "x" is not in the dictionary?
Error
None
0
"x"
Which statement updates the value of "age" to 25 in person = {"name": "Ali", "age": 20}?
person.update("age", 25)
person["age"] = 25
update(person, "age", 25)
person.set("age", 25)
What does len({"a": 1, "b": 2, "c": 3}) return?
2
3
6
Error
Which mode is used to open a file for writing in Python?
"r"
"w"
"a"
"rw"
What does f.read() do?
Reads one line
Reads the whole file
Reads only first 10 characters
Closes the file
Which command ensures that a file is automatically closed after use?
open(file).close()
with open(file) as f:
file.openwith()
close(file)
If a file doesn’t exist, which mode creates a new one?
"r"
"w"
"x"
Both b and c
Which keyword is used to define a function in Python?
func
define
def
function
What is the correct syntax for a function that returns the sum of two numbers?
def add(a, b): return a + b
function add(a, b): return a + b
def add(a, b): print(a + b)
Both a and c
What happens if a function doesn’t have a return statement?
It returns 0
It returns None
It throws an error
It prints nothing
What is the output? def test(x=5, y=2): return x * y print(test())
10
7
Error
None
