WorksheetsPython Programming Quiz
Total questions: 45
Worksheet time: 23mins
Who developed Python?
Dennis Ritchie
Guido van Rossum
James Gosling
Bjarne Stroustrup
Which of the following is true about Python?
Python is an interpreted language
Python is a compiled language
Python is a low-level language
Python is a hardware-dependent language
What will be the output of the following code? print("Hello\nWorld")
Hello World
Hello
World
Hello\nWorld
Error
What will be the output of the following code if the user enters 5? x = input("Enter a number: ") print(type(x))
<class 'int'>
<class 'str'>
<class 'float'>
Error
What will be the output of the following code?
x = 10
y = x
x = 20
print(y)
10
20
Error
None
Which statement is used to display output in Python?
display()
echo()
print()
output()
Which extension is used for Python files?
.py
.java
.c
.exe
What is the output of the following code?
a, b = 5, 10
a, b = b, a
print(a, b)
5 10
10 5
Error
None
What is the main difference between a program and a script in Python?
A program is compiled, while a script is interpreted
A program cannot be executed, while a script can
A script is written in Java, while a program is written in Python
There is no difference
Which of the following is typically true about scripts?
They are large and complex
They are executed line by line
They require a separate compiler
They must be written in C++
What is the output of the following code?
x = 5
y = x ** 2
print(y)
10
25
5
Error
Python uses which of the following?
Compiler
Interpreter
Both
None
What will be the output of the following code?
s = "Python"
print(s[::-1])
nohtyP
Python
Pyt
Error
What does an interpreter do?
Converts the entire code at once
Translates code line by line
Checks syntax errors after execution
Creates an executable file
Which of the following is NOT a token in Python?
Keywords
Variables
Functions
Operators
Which of the following is NOT a valid Python variable name?
my_var
2var
_name
var_name
How do you write a comment in Python?
// Comment
/* Comment */
# Comment
-- Comment
Which of the following is not a Python keyword?
True
False
None
value
What is the output of type(10.5)?
int
float
double
decimal
Which of the following is a mutable data type in Python?
Tuple
String
List
Set
What will be the result of 3 + 5 * 2?
16
13
10
11
What function is used to take user input in Python?
scan()
input()
read()
enter()
What will be the output of print("Hello", "World", sep="-")?
Hello-World
Hello World
Hello, World
HelloWorld
What will "hello"[1:4] return?
h
ell
llo
hello
What will be the output of the following code?
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
My name is Alice and I am 25 years old.
My name is {name} and I am {age} years old.
Error
None
What will be the result of "Python".upper()?
python
PYTHON
PytHoN
None
Which of the following is a correct syntax for an if statement in Python?
if x > 5 then:
if (x > 5):
if x > 5:
if x > 5 {}
What will be the output of the following code?
x = 10
if x % 2 == 0:
print("Even")
else:
print("Odd")
Even
Odd
Error
None
Which loop executes at least once, regardless of the condition?
for loop
while loop
do-while loop
None of the above
What will be the output of the following code?
for i in range(1, 5):
print(i, end=" ")
1 2 3 4
1 2 3 4 5
0 1 2 3 4
Error
How many times will the loop execute?
i = 1
while i < 5:
print(i, end=" ")
i += 1
1 time
4 times
Infinite
Error
What keyword is used to define a function in Python?
define
function
def
fun
What will be the output of the following function?
def test(a, b=5):
return a * b
print(test(3))
3
15
5
Error
What will be the output of the following function?
def greet(name="Guest"):
print("Hello", name)
greet()
Hello Guest
Hello
Error
None
What does a return statement do in Python?
Stops the function execution and returns a value
Stops the program execution
Skips a statement
None of the above
What is recursion in Python?
A function calling itself
A function calling another function
A function that repeats
None of the above
What is the output of the following recursive function?
def fact(n):
if n == 0:
return 1
return n * fact(n - 1)
print(fact(3))
3
6
9
Error
Which of the following is a valid lambda function?
lambda x, y: x + y
lambda (x, y) -> x + y
lambda x y: x + y
def lambda(x, y): return x + y
What is the output of the following lambda function?
square = lambda x: x * x
print(square(4))
8
16
Error
None
What is the correct way to swap two variables in Python?
temp = a; a = b; b = temp
a, b = b, a
swap(a, b)
None of the above
What will be the output of the following code?
lst = [10, 20, 30, 40, 50]
print(lst[1:4])
[10, 20, 30]
[20, 30, 40]
[30, 40, 50]
Error
What is the output of the following list comprehension?
lst = [x for x in range(5)]
print(lst)
[0, 1, 2, 3, 4]
[1, 2, 3, 4, 5]
Error
None
How do you calculate the Euclidean distance between two points (x1, y1) and (x2, y2)?
((x2 - x1) + (y2 - y1))
((x2 - x1)**2 + (y2 - y1)**2) ** 0.5
(x1 - x2) + (y1 - y2)
(x1 * x2) + (y1 * y2)
What is the output of the following code?
def add_sub(x,y):
c=x+y
d=x-y
return c,d
print(add_sub(10,5))
(15,10)
(15,15)
(15,5)
(5,15)
What is the output of the following code?
def func(a, b=5, c=10):
print ('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
a is 3 and b is 7 and c is 10
a is 3 and b is 5 and c is 10
a is 3 and b is 7 and c is 7
a is 3 and b is 3 and c is 10
