Python Basics Quiz
Section A: Multiple Choice Questions
1. What is the correct syntax to output “Hello World” in Python?
a) echo("Hello World")
b) print("Hello World")
c) console.log("Hello World")
d) printf("Hello World")
2. Python files have the extension:
a) .pt
b) .java
c) .py
d) .pyt
3. What will print(3 + 2 * 2) return?
a) 10
b) 7
c) 12
d) 9
4. Which one is a valid variable name?
a) 2value
b) value_2
c) value-2
d) value.2
5. What does # symbol represent?
a) String
b) Comment
c) Operator
d) Escape sequence
6. What is the data type of True in Python?
a) Integer
b) Float
c) Boolean
d) String
7. How do you access the first character in a string x = "Python"?
a) x[1]
b) x(0)
c) x[0]
d) x.0
8. What is the output of len("Hello")?
a) 4
b) 5
c) 6
d) Error
9. Which of the following is immutable?
a) List
b) Dictionary
c) Tuple
d) Set
10. Which of the following is a logical operator?
a) +
b) and
c) //
d) %
11. What is the result of "AI"[::-1]?
a) IA
b) AI
c) ""
d) SyntaxError
12. What is the output of print(type(3.5))?
a) <class 'float'>
b) <class 'int'>
c) <class 'str'>
d) <class 'double'>
13. Which operator is used for floor division?
a) /
b) //
c) %
d) **
14. What will x = [1, 2, 3]; x[1] = 100; print(x) output?
a) [1, 2, 3]
b) [1, 100, 3]
c) [100, 2, 3]
d) Error
15. What does elif mean in Python?
a) Else if
b) Else loop
c) Eliminate if
d) End if
16. How do you start an if block in Python?
a) if (x > 10) then:
b) if x > 10:
c) if x > 10 {
d) if x > 10 then
17. How can you add an item to a list?
a) list.add(4)
b) list.append(4)
c) list.insert(4)
d) list.put(4)
18. Which method returns the number of dictionary keys?
a) count()
b) length()
c) len()
d) size()
19. A tuple is created using:
a) []
b) {}
c) ()
d) <>
20. Which keyword is used to define a function?
a) func
b) define
c) def
d) function
Section B Output-Based
1. What is the output?
number =23
guess = int(input('Enter an integer: '))
if guess == number:
print('Congratulations, you guessed it')
print("(but you don't win any prizes!)")
elif guess > number:
print('No, it is a little higher than that.')
else:
print('No, it is a little lower than that.')
print('Done')
2. What will this code return?
a=5
b=3
print(a ** b)
3. What is the result of this code?
lst = [1, 2, 3]
lst.pop()
print(lst)
4. Output?
i=1
while i < 6:
print(i)
if i == 4:
break
i += 1
5. What will this code do?
day = int(input("enter day"))
match day
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case 6:
print("Saturday")
case 7:
print("Sunday")
6. What is the output?
s = "Hello World"
print(s[-5:])
7. Result?
data = [10, 20, 30]
data[1] = 50
print(data)
8. What is printed?
a = float(input("enter first no"))
b = float(input("enter second no"))
c = float(input("enter third no"))
d = float(input("enter fourth no"))
if a>b and a>c and a>d:
print(f"{a} is greater")
elif b>a and b>c and b>d:
print(f"{b} is greater")
elif c>a and c>b and c>d:
print(f"{c} is greater")
else:
print(f"{d} is greater")
9. Output of the following:
h= int(input("height in cm"))
b = int(input("breadth in cm"))
area = 0.5*b*h
print(f"area of triangle is {area} cm")
10. Result of the following:
bicycle = ['trek','cannondale','redline','specialized']
massege = f"My first bicycle was a {bicycle[0].title()}."
print(massege)
Section C: Fill in the Blanks
1. Complete to declare a variable x with value 10:
______ = 10
2. Fill to print the 3rd character of string s = "Python"
print(s[___])
3. Complete the list syntax:
mylist = [1, 2, ___]
4. Insert the correct keyword to check equality:
if a ___ b:
print("Equal")
5. Fill in the correct string slicing to get 'hon':
x = "Python"
print(x[3:___])
6. Add a comment on this line:
# This is a ________
7. Complete the dictionary:
d = {'name': 'AI', 'year': ___}
8. Complete this tuple:
t = (5, 10, ___)
9. Use an operator to calculate power:
print(2 ___ 3)
10. Complete the if statement:
if x > 10:
print("x is ______")
SectionD: Python (Loops, Functions, Classes)
1. What will the following code output and why?
def func(n):
return [lambda x: x * i for i in range(n)]
print([f(2) for f in func(3)])
2. How does the __init__ method differ from __new__ in class creation?
3. What is the output of this code snippet and why?
for i in range(3):
def f(x=i):
return x
print(f())
4. How do Python’s iterators differ from generators in memory usage and
behavior?
5. Given:
class A:
def __init__(self):
self.var = 1
def display(self):
print(self.var)
class B(A):
def __init__(self):
super().__init__()
self.var = 2
obj = B()
obj.display()
6. What is the output of the following code?
for i in range(2, 5):
print(i, end=", ")
a) 2, 3, 4, 5,
b) 2, 3, 4,
c) 3, 4, 5,
d) 2, 3, 4, 5
7. Which of the following is the correct way to define a function in Python?
a) function myFunc():
b) def myFunc():
c) func myFunc():
d) define myFunc():
8.What will this code print?
def add(x, y=2):
return x + y
print(add(3))
a) 5
b) 5
c) 6
d) Error
9. What is self in Python class methods?
a) A built-in function
b) A keyword to call super class
c) A reference to the current instance
d) An unused variable
10. How do you initialize a class in Python?
a) new MyClass()
b) obj = MyClass()
c) init MyClass()
d) MyClass.init()
Section D: Fill in the Blanks
1. What is the output?
x=5
x = x + (x = 3)
print(x)
2. Output?
int a = 10;
if (a = 5)
printf("True");
else
printf("False");
3. What will be printed?
for i in range(3):
for j in range(i):
print(j, end=" ")
print()
4. Output?
int i = 0;
while (i++ < 5)
System.out.print(i);
5. Output?
x=0
for i in range(1, 6):
x += i
if x > 6:
break
print(x)
6. Output?
int i = 0;
do {
printf("%d ", i);
i += 2;
} while (i < 5);
7.
x = 10
y = x % 3 * 2 // 1
print(y)
8.
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0)
continue;
System.out.print(i + " ");
}
9. Output?
int i = 5;
while (--i > 0) {
printf("%d ", i);
}
10. Output pattern?
for i in range(3, 0, -1):
for j in range(i):
print("*", end="")
print()
11.
int x = 1;
for (int i = 1; i < 5; i++) {
x *= i;
}
printf("%d", x);
❓ What is the value of x?
12.
i=0
while i < 3:
i += 1
print(i)
i += 1
❓ Output?
13.
int x = 2, y = 5;
while (x++ < y)
y--;
System.out.println(x + "," + y);
❓ Final values of x and y?
14.
int a = 2;
int b = 3;
if (a++ > 2 || ++b > 3)
printf("%d %d", a, b);
❓ Output?
15.
x=0
for i in range(1, 4):
for j in range(1, i+1):
x += j
print(x)
❓ Final value of x?
16.
int i = 1;
do {
if (i % 2 == 0)
System.out.print(i);
i++;
} while (i <= 5);
❓ Output?
17.
for i in range(5):
if i == 2:
continue
print(i, end=" ")
❓ Output?
18.
int i = 1, j = 1;
while (i++ <= 5) {
if (j++ % 2 == 0)
continue;
printf("%d ", j);
}
❓ Output?
19.
x=1
for i in range(1, 4):
x *= i
print(x, end=" ")
❓ What gets printed?
20.
int sum = 0;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
sum += j;
}
}
System.out.println(sum);
❓ Final sum value?