🧠 Python Day 7 Quiz

90 Questions Covering All Topics - Test Your Knowledge!

📊 1. Variables & Data Types (10 Questions)

Q1.
Which of the following is a valid variable name in Python?
A) 2variable
B) variable-name
C) _variable2
D) class
Show Answer
Answer: C) _variable2
Variable names can start with letters or underscore, not numbers. "class" is a reserved keyword.
Q2.
What will be the output of the following code?
x = 5 print(type(x))
A) int
B) <class 'int'>
C) integer
D) number
Show Answer
Answer: B) <class 'int'>
The type() function returns the full class representation of the data type.
Q3.
True or False: Python is statically typed.
Show Answer
Answer: False
Python is dynamically typed - variable types are determined at runtime, not compile time.
Q4.
What is the result of: int("123.45")?
A) 123
B) 123.45
C) ValueError
D) "123"
Show Answer
Answer: C) ValueError
int() cannot convert a string with decimal point directly. Use float() first, then int().
Q5.
Which function returns the type of a variable?
Show Answer
Answer: type()
The type() function returns the data type of any Python object.
Q6.
What are the 4 main built-in data types in Python?
Show Answer
Answer: int, float, str, bool
These are the four fundamental built-in data types in Python.
Q7.
True or False: In Python, you must declare variable types explicitly.
Show Answer
Answer: False
Python uses dynamic typing - types are inferred automatically from the assigned values.
Q8.
What will bool("") return?
A) True
B) False
C) None
D) Error
Show Answer
Answer: B) False
Empty strings are considered "falsy" values in Python, so bool("") returns False.
Q9.
Which method checks if a variable is of a specific type?
A) type()
B) isinstance()
C) check_type()
D) verify()
Show Answer
Answer: B) isinstance()
isinstance(obj, type) is the preferred way to check if an object is of a specific type.
Q10.
Convert the string "3.14" to a float and store in variable 'pi':
Show Answer
Answer: pi = float("3.14")

📝 2. Lists (10 Questions)

Q11.
What will be the output of: my_list = [1, 2, 3]; print(my_list[-1])?
A) 1
B) 2
C) 3
D) Error
Show Answer
Answer: C) 3
Negative indexing starts from the end. -1 refers to the last element.
Q12.
True or False: Lists in Python are mutable.
Show Answer
Answer: True
Lists are mutable, meaning you can change, add, or remove elements after creation.
Q13.
Which method adds an element to the end of a list?
Show Answer
Answer: append()
The append() method adds a single element to the end of the list.
Q14.
What does [1, 2, 3][1:3] return?
A) [1, 2]
B) [2, 3]
C) [1, 2, 3]
D) [2]
Show Answer
Answer: B) [2, 3]
Slicing [1:3] includes indices 1 and 2, but excludes 3. So elements at positions 1 and 2.
Q15.
How do you insert an element at a specific position in a list?
Show Answer
Answer: insert(index, value)
Example: my_list.insert(0, "first") inserts "first" at index 0.
Q16.
What is the difference between remove() and pop()?
Show Answer
Answer:
remove() removes the first occurrence of a value, pop() removes element by index and returns it.
Q17.
True or False: Lists can contain different data types.
Show Answer
Answer: True
Lists can contain mixed data types: [1, "hello", 3.14, True] is valid.
Q18.
How do you find the length of a list?
A) list.length()
B) len(list)
C) list.size()
D) count(list)
Show Answer
Answer: B) len(list)
len() is the built-in function to get the length of any sequence in Python.
Q19.
Create an empty list in Python:
Show Answer
Answer:
my_list = [] or my_list = list()
Q20.
What does the extend() method do?
A) Adds one element to the list
B) Adds multiple elements from an iterable
C) Makes the list longer
D) Copies the list
Show Answer
Answer: B) Adds multiple elements from an iterable
extend() adds all elements from another iterable (list, tuple, string) to the current list.

🔒 3. Tuples (10 Questions)

Q21.
True or False: Tuples are mutable in Python.
Show Answer
Answer: False
Tuples are immutable - once created, they cannot be changed.
Q22.
How do you create a tuple with a single element?
A) (5)
B) (5,)
C) tuple(5)
D) [5]
Show Answer
Answer: B) (5,)
The comma is essential to create a single-element tuple. Without it, (5) is just parentheses around an integer.
Q23.
What are the only two methods available for tuples?
Show Answer
Answer: count() and index()
count() counts occurrences, index() finds the first occurrence of a value.
Q24.
What is tuple unpacking?
Show Answer
Answer:
Assigning tuple elements to multiple variables: x, y = (1, 2) assigns x=1, y=2
Q25.
True or False: Tuples can be used as dictionary keys.
Show Answer
Answer: True
Since tuples are immutable, they can be used as dictionary keys (unlike lists).
Q26.
What does len((1, 2, 3, 4)) return?
A) 3
B) 4
C) 10
D) Error
Show Answer
Answer: B) 4
len() returns the number of elements in the tuple.
Q27.
Convert a list [1, 2, 3] to a tuple:
Show Answer
Answer: my_tuple = tuple([1, 2, 3])
Q28.
What happens when you try to modify a tuple element?
A) It works fine
B) TypeError occurs
C) ValueError occurs
D) Nothing happens
Show Answer
Answer: B) TypeError occurs
Attempting to modify a tuple raises TypeError: 'tuple' object does not support item assignment.
Q29.
What is tuple concatenation? Give an example.
Show Answer
Answer:
Joining tuples using +: (1, 2) + (3, 4) = (1, 2, 3, 4)
Q30.
True or False: Tuples are faster than lists for accessing elements.
Show Answer
Answer: True
Tuples are faster for accessing elements because they are immutable and have less overhead.

🔄 4. Sets (10 Questions)

Q31.
What happens when you create a set from [1, 2, 2, 3, 3, 3]?
A) {1, 2, 2, 3, 3, 3}
B) {1, 2, 3}
C) [1, 2, 3]
D) Error
Show Answer
Answer: B) {1, 2, 3}
Sets automatically remove duplicates, keeping only unique elements.
Q32.
True or False: Sets are ordered collections.
Show Answer
Answer: False
Sets are unordered collections (though insertion order is preserved in Python 3.7+, they're still conceptually unordered).
Q33.
What does {1, 2, 3} | {3, 4, 5} return?
A) {3}
B) {1, 2, 3, 4, 5}
C) {1, 2, 4, 5}
D) Error
Show Answer
Answer: B) {1, 2, 3, 4, 5}
The | operator performs union operation, combining all unique elements from both sets.
Q34.
Which method safely removes an element from a set without raising an error if element doesn't exist?
Show Answer
Answer: discard()
discard() removes an element if it exists, does nothing if it doesn't. remove() raises KeyError if element not found.
Q35.
What does {1, 2, 3} & {3, 4, 5} return?
A) {3}
B) {1, 2, 3, 4, 5}
C) {1, 2, 4, 5}
D) Empty set
Show Answer
Answer: A) {3}
The & operator performs intersection, returning elements common to both sets.
Q36.
Create an empty set in Python:
Show Answer
Answer: my_set = set()
Note: {} creates an empty dictionary, not an empty set!
Q37.
What does {1, 2, 3} - {2, 3, 4} return?
A) {1}
B) {4}
C) {2, 3}
D) {1, 4}
Show Answer
Answer: A) {1}
The - operator performs difference operation, returning elements in first set but not in second.
Q38.
True or False: Sets can contain mutable objects like lists.
Show Answer
Answer: False
Sets can only contain immutable (hashable) objects. Lists are mutable and cannot be set elements.
Q39.
What method adds an element to a set?
Show Answer
Answer: add()
The add() method adds a single element to the set.
Q40.
What does {1, 2, 3} ^ {3, 4, 5} return?
A) {3}
B) {1, 2, 4, 5}
C) {1, 2, 3, 4, 5}
D) Empty set
Show Answer
Answer: B) {1, 2, 4, 5}
The ^ operator performs symmetric difference, returning elements in either set but not in both.

📖 5. Dictionaries (10 Questions)

Q41.
What will {"a": 1, "b": 2}["c"] return?
A) None
B) KeyError
C) 0
D) ""
Show Answer
Answer: B) KeyError
Accessing a non-existent key with bracket notation raises KeyError.
Q42.
Which method safely gets a value from a dictionary without raising an error?
Show Answer
Answer: get()
dict.get(key, default) returns the value or default if key doesn't exist.
Q43.
True or False: Dictionary keys must be unique.
Show Answer
Answer: True
Dictionary keys must be unique. If you assign to an existing key, it updates the value.
Q44.
How do you get all keys from a dictionary?
A) dict.keys()
B) dict.get_keys()
C) keys(dict)
D) dict.all_keys()
Show Answer
Answer: A) dict.keys()
The keys() method returns a view of all dictionary keys.
Q45.
Create a dictionary from two lists: keys = ["a", "b"] and values = [1, 2]
Show Answer
Answer: my_dict = dict(zip(keys, values))
Q46.
What does the pop() method do for dictionaries?
Show Answer
Answer:
pop(key) removes the key-value pair and returns the value. Can provide default if key not found.
Q47.
True or False: Lists can be used as dictionary keys.
Show Answer
Answer: False
Dictionary keys must be immutable (hashable). Lists are mutable and cannot be keys.
Q48.
How do you check if a key exists in a dictionary?
A) key in dict
B) dict.has_key(key)
C) dict.contains(key)
D) exists(key, dict)
Show Answer
Answer: A) key in dict
Use the 'in' operator to check if a key exists in the dictionary.
Q49.
What method returns both keys and values as pairs?
Show Answer
Answer: items()
The items() method returns key-value pairs as tuples.
Q50.
Update dictionary d1 with all key-value pairs from d2:
Show Answer
Answer: d1.update(d2)

🔄 6. Loops & Conditionals (10 Questions)

Q51.
What will this code print?
for i in range(3): if i == 1: continue print(i)
A) 0 1 2
B) 0 2
C) 1 2
D) 0
Show Answer
Answer: B) 0 2
continue skips the current iteration when i=1, so only 0 and 2 are printed.
Q52.
True or False: Python uses curly braces {} to define code blocks.
Show Answer
Answer: False
Python uses indentation to define code blocks, not curly braces.
Q53.
What does range(1, 10, 2) generate?
A) [1, 3, 5, 7, 9]
B) [1, 3, 5, 7, 9, 11]
C) [2, 4, 6, 8]
D) [1, 2, 3, 4, 5, 6, 7, 8, 9]
Show Answer
Answer: A) [1, 3, 5, 7, 9]
range(start, stop, step) generates numbers from 1 to 9 (excluding 10) with step 2.
Q54.
Write a while loop that prints numbers from 1 to 3:
Show Answer
Answer: i = 1 while i <= 3: print(i) i += 1
Q55.
What does the enumerate() function do?
Show Answer
Answer:
enumerate() returns both index and value of each item in an iterable: for i, val in enumerate(list)
Q56.
What's the difference between break and continue?
Show Answer
Answer:
break exits the loop completely, continue skips the current iteration and moves to the next.
Q57.
True or False: elif is short for "else if".
Show Answer
Answer: True
elif is Python's way of writing "else if" for multiple conditions.
Q58.
What will this nested loop print?
for i in range(2): for j in range(2): print(i, j)
A) 0 0, 0 1, 1 0, 1 1
B) 0 1, 1 0
C) 1 1, 2 2
D) 0, 1
Show Answer
Answer: A) 0 0, 0 1, 1 0, 1 1
Nested loop prints all combinations of i and j values.
Q59.
How do you create an infinite loop?
Show Answer
Answer: while True:
This creates an infinite loop that runs until broken with break statement.
Q60.
What happens if no condition in if-elif-else is True?
A) Error occurs
B) else block executes
C) Nothing happens
D) First condition executes anyway
Show Answer
Answer: B) else block executes
If present, the else block executes when no if/elif conditions are True.

🔤 7. String Manipulation (10 Questions)

Q61.
What does "Hello World".lower() return?
A) "hello world"
B) "Hello World"
C) "HELLO WORLD"
D) Error
Show Answer
Answer: A) "hello world"
lower() converts all characters to lowercase.
Q62.
True or False: Strings in Python are mutable.
Show Answer
Answer: False
Strings are immutable in Python. String methods return new strings, they don't modify the original.
Q63.
How do you remove whitespace from both ends of a string?
Show Answer
Answer: strip()
strip() removes whitespace from the beginning and end of a string.