Python Beginner Quiz
30 questions on Python for Beginners.
Question 1: Which keyword is used to define a function in Python?
- A. function
- B. define
- C. def β (correct answer)
- D. fun
Explanation: In Python, the def keyword is used to create a function. Example: def greet(): print("Hello")
Question 2: What is the output of print(type(10))?
- A.
<class 'float'>
- B.
<class 'int'> β (correct answer)
- C.
<class 'number'>
- D.
<class 'str'>
Explanation: In Python, whole numbers like 10 are of the int (integer) data type.
Question 3: Which of the following is the correct way to create a variable in Python?
- A.
int x = 10;
- B.
var x = 10
- C.
x = 10 β (correct answer)
- D.
x := 10
Explanation: Python uses dynamic typing. You simply assign a value to a variable name β no type declaration needed.
Question 4: What will be the output of the following code?
``python
x = "Hello"
print(x[1])
``
- A. H
- B. e β (correct answer)
- C. l
- D. Error
Explanation: String indexing in Python starts at 0. So x[0] is 'H' and x[1] is 'e'.
Question 5: Which of the following data types is immutable in Python?
- A. List
- B. Dictionary
- C. Set
- D. Tuple β (correct answer)
Explanation: Tuples are immutable β once created, their elements cannot be changed. Lists, dictionaries, and sets are all mutable.
Question 6: What is the correct syntax to take user input in Python 3?
- A.
scanf("Enter: ")
- B.
input("Enter: ") β (correct answer)
- C.
cin >> x
- D.
read("Enter: ")
Explanation: The input() function reads user input from the console and always returns it as a string.
Question 7: What will be the output?
``python
print(3 ** 2)
``
- A. 6
- B. 9 β (correct answer)
- C. 5
- D. 32
Explanation: The ** operator is the exponentiation operator in Python. 3 ** 2 means 3 raised to the power of 2 = 9.
Question 8: Which loop is used when you know the number of iterations in advance?
- A. while
- B. do-while
- C. for β (correct answer)
- D. repeat
Explanation: The for loop is typically used for iterating over a known sequence (list, range, string). Python does not have a do-while loop.
Question 9: What does the len() function do?
- A. Returns the last element
- B. Returns the length of an object β (correct answer)
- C. Returns the index
- D. Returns the type
Explanation: len() returns the number of items in a sequence (string, list, tuple, dict, etc.).
Question 10: What will the following code print?
``python
my_list = [1, 2, 3, 4]
print(my_list[-1])
``
- A. 1
- B. 3
- C. 4 β (correct answer)
- D. Error
Explanation: Negative indexing starts from the end. my_list[-1] returns the last element, which is 4.
Question 11: What is the output of bool("")?
- A. True
- B. False β (correct answer)
- C. None
- D. Error
Explanation: An empty string "" is a falsy value in Python. bool("") returns False. Non-empty strings are truthy.
Question 12: Which method adds an element at the end of a list?
- A.
list.add()
- B.
list.insert()
- C.
list.append() β (correct answer)
- D.
list.push()
Explanation: The append() method adds a single element to the end of the list. insert() adds at a specific index.
Question 13: What does range(1, 5) produce?
- A. 1, 2, 3, 4, 5
- B. 1, 2, 3, 4 β (correct answer)
- C. 0, 1, 2, 3, 4
- D. 0, 1, 2, 3, 4, 5
Explanation: range(start, stop) generates numbers from start up to (but not including) stop. So range(1, 5) gives 1, 2, 3, 4.
Question 14: Which keyword is used to handle exceptions in Python?
- A. catch
- B. except β (correct answer)
- C. handle
- D. error
Explanation: Python uses try/except blocks for exception handling. catch is used in Java and C++, not Python.
Question 15: What is the output?
``python
x = [1, 2, 3]
y = x
y.append(4)
print(x)
``
- A. [1, 2, 3]
- B. [1, 2, 3, 4] β (correct answer)
- C. [4, 1, 2, 3]
- D. Error
Explanation: y = x does not create a copy. Both x and y reference the **same list** in memory, so modifying y also modifies x.
Question 16: What is the output of this code?
``python
def greet(name="World"):
return f"Hello, {name}!"
print(greet())
``
- A. Hello, !
- B. Hello, World! β (correct answer)
- C. Error
- D. Hello, name!
Explanation: The function has a default parameter name="World". When called without arguments, it uses the default value.
Question 17: What does "hello".upper() return?
- A. Hello
- B. HELLO β (correct answer)
- C. hello
- D. hELLO
Explanation: The upper() method converts all characters in the string to uppercase.
Question 18: Which statement correctly creates a dictionary?
- A.
d = [key: value]
- B.
d = {key: value} β (correct answer)
- C.
d = (key, value)
- D.
d = <key, value>
Explanation: Dictionaries are created using curly braces {} with key-value pairs separated by colons.
Question 19: What is the output?
``python
nums = [1, 2, 3, 4, 5]
print(nums[1:4])
``
- A. [1, 2, 3, 4]
- B. [2, 3, 4] β (correct answer)
- C. [2, 3, 4, 5]
- D. [1, 2, 3]
Explanation: Slicing [1:4] extracts elements from index 1 up to (but not including) index 4.
Question 20: What keyword is used to create an anonymous function?
- A. def
- B. anonymous
- C. lambda β (correct answer)
- D. func
Explanation: Lambda functions are anonymous, single-expression functions. Example: square = lambda x: x ** 2.
Question 21: What will set([1, 2, 2, 3, 3, 3]) return?
- A. [1, 2, 3]
- B. {1, 2, 3} β (correct answer)
- C. (1, 2, 3)
- D. Error
Explanation: Sets automatically remove duplicate values. Converting a list with duplicates to a set keeps only unique elements.
Question 22: What does pass do in Python?
- A. Exits the loop
- B. Skips the current iteration
- C. Does nothing (placeholder) β (correct answer)
- D. Passes a value to a function
Explanation: pass is a null operation β a placeholder used when a statement is syntactically required but no action is needed.
Question 23: What is the output?
``python
for i in range(3):
print(i, end=" ")
``
- A. 1 2 3
- B. 0 1 2 β (correct answer)
- C. 0 1 2 3
- D. 1 2
Explanation: range(3) generates 0, 1, 2 (three values starting from 0). The end=" " prints them on the same line.
Question 24: Which module is used to generate random numbers?
- A. math
- B. random β (correct answer)
- C. numbers
- D. rand
Explanation: The random module provides functions like random.randint(), random.choice(), and random.shuffle().
Question 25: What is the output?
``python
d = {"a": 1, "b": 2, "c": 3}
print(d.get("z", 0))
``
- A. Error
- B. None
- C. 0 β (correct answer)
- D. z
Explanation: The get() method returns the value for a key, or a default value (here 0) if the key doesn't exist β no error raised.
Question 26: What is the output?
``python
x = [i ** 2 for i in range(5)]
print(x)
``
- A. [1, 4, 9, 16, 25]
- B. [0, 1, 4, 9, 16] β (correct answer)
- C. [0, 2, 4, 6, 8]
- D. [1, 2, 3, 4, 5]
Explanation: This is a list comprehension. range(5) gives 0-4, and each value is squared: 0Β²=0, 1Β²=1, 2Β²=4, 3Β²=9, 4Β²=16.
Question 27: What is the output?
``python
def func(*args):
print(type(args))
func(1, 2, 3)
``
- A.
<class 'list'>
- B.
<class 'tuple'> β (correct answer)
- C.
<class 'dict'>
- D.
<class 'set'>
Explanation: *args collects positional arguments into a **tuple**, not a list. **kwargs collects keyword arguments into a dictionary.
Question 28: What does this code do?
``python
with open("file.txt", "r") as f:
content = f.read()
``
- A. Writes to file.txt
- B. Reads file.txt and auto-closes it β (correct answer)
- C. Deletes file.txt
- D. Creates file.txt
Explanation: The with statement is a context manager that opens the file, reads its content, and automatically closes it when the block exits.
Question 29: What is the output?
``python
class Dog:
def __init__(self, name):
self.name = name
d = Dog("Buddy")
print(d.name)
``
- A. Dog
- B. name
- C. Buddy β (correct answer)
- D. Error
Explanation: __init__ is the constructor. self.name = name stores "Buddy" as an instance variable, which is accessed with d.name.
Question 30: What is the difference between is and ==?
- A. They are identical
- B.
== checks value; is checks identity (same object in memory) β (correct answer)
- C.
is checks value; == checks identity
- D.
is is for strings only
Explanation: == compares the **values** of two objects. is checks whether two variables point to the **exact same object** in memory.