















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















Section 1: Variables, Data Types & Type Conversion
1. Which of the following is a valid variable name in Python?
Answer: C. _myVar Explanation: Variable names cannot start with a digit (A), cannot contain hyphens (B) or spaces (D). Underscore is allowed, including at the beginning.
2. What is the data type of 3.14?
Answer: B. float Explanation: Numeric values with a decimal point are of type float. Integers are whole numbers without a decimal point.
3. What is the output of print(type(5 / 2))?
Answer: B. Explanation: In Python, the / operator always returns a floating-point result, even when the division is exact.
4. Which of the following will convert the string "123" to an integer?
Answer: A. int("123") Explanation: The int() function converts a string (or other numeric type) to an integer. "123" contains only digits, so conversion is valid.
5. What is the result of bool([]) in Python?
Answer: B. False Explanation: Empty sequences (lists, strings, tuples) and the number 0 evaluate to False in a Boolean context. Non-empty sequences evaluate to True. Section 2: Arithmetic & Comparison Operators
6. What is the output of print(3 + 5 * 2)?
Answer: B. 13
Answer: C. != Explanation: != is the not-equal operator in Python. It returns True if the two values are different. Section 3: Strings & String Methods
11. What is the output of print("Hello" + "World")?
Answer: A. HelloWorld Explanation: The + operator concatenates strings. No space is automatically added.
12. What is the output of print(len("Python"))?
Answer: B. 6 Explanation: The len() function returns the number of characters in the string. "Python" has 6 letters.
13. What is the output of print("Hello"[1])?
Answer: B. e Explanation: String indexing starts at 0. Index 0 is H, index 1 is e.
14. Which method removes whitespace from both ends of a string? A) strip()
Answer: A. strip() Explanation: strip() removes leading and trailing whitespace. lstrip() and rstrip() remove from one side.
15. What is the output of print("Python".upper())?
Answer: B. PYTHON Explanation: .upper() returns a new string where all letters are uppercase. Section 4: Lists & List Operations
16. Which of the following creates a list containing the numbers 1, 2, and 3?
Answer: B. [1, 2, 3] Explanation: Lists are written with square brackets []. Tuples use parentheses (), sets use {}.
17. What does append() do to a list?
Answer: B. Adds an element to the end Explanation: append() adds a single element to the end of the list. To add at a specific position, use insert().
18. What is the output of the following code? python
Answer: B. dict = {1: "one", 2: "two"} Explanation: Dictionaries use curly braces {} with colon : separating keys and values.
22. How do you access the value associated with key "name" in dictionary person?
Answer: B. person["name"] Explanation: Square brackets with the key name are used to access dictionary values. Both single and double quotes work.
23. Which method returns all the keys in a dictionary?
Answer: A. keys() Explanation: dict.keys() returns a view object containing all keys. values() returns values, items() returns key-value pairs. Section 6: Conditionals (if/elif/else) 24. What is the output of the following code? python x = 5 if x > 10: print("A") elif x > 0: print("B") else:
print("C")
Answer: B. B Explanation: x > 10 is False, so it moves to the elif. x > 0 is True, so it prints "B".
25. Which logical operator returns True only when both conditions are true?
Answer: C. and Explanation: The and operator requires both operands to be True to return True.
26. What is the output of print(True and False)?
Answer: B. False Explanation: and returns False if at least one operand is False.
27. Which of the following is a correct way to check if a number n is between 10 and 20 (inclusive)?
Answer: B. if 10 <= n <= 20: Explanation: Python supports chained comparisons. <= includes the boundary numbers. Option D excludes 20 because range(10,20) stops at 19.
Answer: C. break Explanation: break immediately terminates the innermost loop. continue skips the rest of the current iteration and goes to the next.
31. What is the output of the following code? python for i in range(1, 5): if i == 3: continue print(i, end=" ")
Answer: B. 1 2 4 Explanation: When i equals 3, continue skips the print statement, so 3 is not printed. Section 8: Functions
32. Which keyword is used to define a function in Python?
Answer: B. def Explanation: Functions are defined using the def keyword, followed by the function name, parentheses, and a colon.
33. What is the output of the following code? python def my_func(a, b): return a * b print(my_func(3, 4))
Answer: B. 12
Explanation: The function returns the product of the two arguments. 3 * 4 = 12.
34. What is a return statement used for?
Answer: B. To exit the function and optionally send a value back to the caller Explanation: return ends the function and can pass a value back. Without a return, the function returns None.
35. Which of the following is a correct function call for def greet(name="Guest")?
Answer: C. Both A and B are valid Explanation: The parameter name has a default value "Guest". The function can be called with no argument (uses default) or with an argument. Section 9: Input & Output
36. Which function reads a line of text from the user?
Answer: B. input() Explanation: input() pauses the program and waits for the user to type something and press Enter. It returns the input as a string.
37. What is the output of print("Hello", "World", sep="-")?
Answer: B. Invalid Explanation: int("abc") raises a ValueError. The except block catches it and prints "Invalid". Section 11: Additional Practice Questions
41. Which of the following statements about Python is true? A) Python uses semicolons to end statements.
Answer: B. Python uses indentation to define code blocks. Explanation: Python relies on consistent indentation (usually 4 spaces) to group statements; braces are not used.
42. What is the result of print(10 // 3)?
Answer: A. 3 Explanation: Floor division (//) returns the integer part of the quotient, discarding the remainder.
43. What does the range(2, 8, 2) function generate?
Answer: B. 2, 4, 6
Explanation: Start = 2, stop = 8 (excluded), step = 2. So numbers are 2, 4, 6.
44. Which of the following is used to create an empty list?
Answer: C. Both A and B Explanation: [] is the literal syntax; list() is the constructor. Both produce an empty list.
45. Which method removes an item from a list by its value?
Answer: B. remove() Explanation: remove(x) deletes the first occurrence of value x. pop() removes by index.
46. What is the output of print(3 ** 2 ** 3)?
Answer: A. 512 Explanation: Exponentiation operators are evaluated from right to left. 2 ** 3 = 8, then 3 ** 8 =
Answer: A. if key in dict: Explanation: The in operator checks membership for dictionary keys. It is the preferred and most readable method.
50. Which function returns the number of items in a container?
Answer: C. len() Explanation: len() is the built-in function that works on strings, lists, dictionaries, tuples, sets, etc.
51. What is the output of the following code? python numbers = [1, 2, 3, 4, 5] result = numbers[1:4:2] print(result)
Answer: A. [2, 4] Explanation: Slice [start:stop:step] starts at index 1 (2), stops before index 4 (so includes indices 1,2,3), but step 2 takes index 1 (2) then index 3 (4). So result is [2, 4].
52. Which of the following will produce an infinite loop?
Answer: C. while 1 == 1: print("hi") Explanation: The condition 1 == 1 is always True, so the loop never terminates. Option B has break, so it runs once then exits.
53. What is the output of the following code? python def add(x, y=10): return x + y print(add(5))
Answer: C. 15 Explanation: The function has a default value for y. When called with one argument, y takes its default value 10, so 5 + 10 = 15.
54. Which of the following will output the string "HELLO"?
Answer: A. print("hello".upper()) Explanation: .upper() converts all characters to uppercase. The other options produce different results.
55. What is the result of 3 + "3"?
Answer: C. Error Explanation: You cannot directly add an integer and a string in Python. This raises a TypeError.
56. Which statement is used to import a module?
Answer: B. import Explanation: The import keyword is used to load modules. Example: import math.
Explanation: The modulus operator returns the remainder of integer division. 10 divided by 4 is 2 remainder 2.
61. Which of the following will sort a list nums in descending order?
Answer: A. nums.sort(reverse=True) Explanation: The sort() method can take the reverse parameter. reverse() just reverses the current order, does not sort.
62. What is the output of the following code? python def multiply(a, b): return a * b print(multiply(2, 3, 4))
Answer: C. Error Explanation: The function is defined to take exactly two arguments. Passing three arguments raises a TypeError.
63. Which operator is used to check if two values are equal?
Answer: B. == Explanation: Single = is assignment. == tests equality.
64. What is the output of print("Hello" * 2)?
Answer: A. HelloHello Explanation: Multiplying a string by an integer repeats the string that many times.
65. Which of the following is a valid way to create an empty dictionary?
Answer: A. {} Explanation: {} creates an empty dictionary. set() creates an empty set.
66. Which of the following will add the key "age" with value 25 to dictionary person?
Answer: B. person["age"] = 25 Explanation: Dictionary assignment is done using square brackets and assignment.
67. What is the output of the following code? python x = "Hello World" print(x[6:11])
Answer: A. World Explanation: Indices: H0 e1 l2 l3 o4 (space)5 W6 o7 r8 l9 d10. Slice 6:11 takes indices 6,7,8,9,10 → World.
68. Which of the following statements is true about break and continue?