




























































































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!





























































































Question 1. Which logical operator in Python short‑circuits evaluation when its first operand determines the final result? A) and B) or C) not D) == Answer: A Explanation: and stops evaluating as soon as a falsy operand is found because the whole expression cannot be true. Question 2. What is the output of print(5 > 3 and 2 < 4)? A) True B) False C) 5 D) Raises a SyntaxError Answer: A Explanation: Both relational expressions are true, and and returns True when both operands are true. Question 3. Which of the following relational operators has the highest precedence? A) == B) != C) > D) >= Answer: C
Explanation: All relational operators have the same precedence; however, Python evaluates them left‑to‑right, so none is higher than another. Trick question—answer C is the most commonly used but technically they share precedence. Question 4. In the expression not (a == b), which operation is performed first? A) not B) == C) Parentheses D) Both not and == simultaneously Answer: C Explanation: Parentheses force evaluation of a == b before applying not. Question 5. Which data type is immutable in Python? A) list B) dict C) tuple D) set Answer: C Explanation: Tuples cannot be altered after creation, unlike lists, dicts, and sets. Question 6. Which statement correctly represents a ternary conditional expression? A) x = if a > b: a else: b B) x = a if a > b else b C) x = (a > b)? a : b D) x = a > b? a : b Answer: B
Question 9. Which of the following for loops correctly iterates over the characters of the string "Python"? A) for i in range("Python"): print(i) B) for char in "Python": print(char) C) for char in range("Python"): print(char) D) for i in enumerate("Python"): print(i) Answer: B Explanation: Direct iteration over a string yields each character. Question 10. What is the result of list(range(2, 10, 3))? A) [2, 5, 8] B) [2, 3, 4, 5, 6, 7, 8, 9] C) [2, 5, 8, 11] D) [3, 6, 9] Answer: A Explanation: range(start, stop, step) produces numbers 2, 5, 8 (stop is exclusive). Question 11. When would you prefer a while loop over a for loop? A) When the number of iterations is known in advance B) When you need to iterate over a list C) When the loop should continue until a condition changes dynamically D) When you need the loop index for arithmetic Answer: C Explanation: while loops are ideal when termination depends on a condition that may change inside the loop.
Question 12. Which statement correctly exits only the innermost loop? A) break B) continue C) return D) exit() Answer: A Explanation: break terminates the current (innermost) loop. Question 13. In nested loops, what does the continue statement affect? A) All outer loops B) Only the innermost loop where it appears C) The function containing the loops D) It restarts the entire program Answer: B Explanation: continue skips the remainder of the current iteration of the innermost loop. Question 14. What does zip([1,2,3], ['a','b','c']) produce? A) [(1, 'a'), (2, 'b'), (3, 'c')] B) [(1,2,3), ('a','b','c')] C) {'1':'a','2':'b','3':'c'} D) [(1,'a'), (2,'b')] Answer: A Explanation: zip pairs elements positionally, stopping at the shortest iterable.
Question 18. What will any([0, False, 5]) return? A) True B) False C) 0 D) Raises TypeError Answer: A Explanation: any returns True if at least one element is truthy; 5 is truthy. Question 19. Which statement about the all() function is true? A) It returns True if any element is true. B) It returns False only when all elements are false. C) It returns True only when every element evaluates to True. D) It works only with numeric iterables. Answer: C Explanation: all requires every element to be truthy. Question 20. In a function, what is the scope of a variable defined inside an if block? A) Global B) Local to the function, accessible throughout the function after definition C) Local to the if block only D) It becomes a keyword Answer: B Explanation: Python does not create a new block scope for if; the variable is local to the entire function.
Question 21. What does the following function return when called with n = 0?
def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) A) 0 B) 1 C) RecursionError D) None Answer: B Explanation: Base case n == 0 returns 1, the definition of 0!. Question 22. Which statement correctly defines a sentinel‑controlled loop for reading integers until the user enters -1? A) while True: x = int(input()); if x == - 1: break B) while x != - 1: x = int(input()) C) for x in range(-1): D) while x == - 1: Answer: A Explanation: An infinite loop with a break when sentinel -1 is entered. Question 23. What will be printed?
{1:1, 2:4, 3:9}{1:2, 2:3, 3:4}[(1,1), (2,4), (3,9)]Answer: B Explanation: Dictionary comprehension maps each element to its square. Question 26. Which built‑in exception is raised by int('abc')? A) ValueError B) TypeError C) SyntaxError D) NameError Answer: A Explanation: Converting a non‑numeric string to int triggers a ValueError. Question 27. In a try/except block, which clause runs only when no exception occurs? A) except B) finally C) else D) raise Answer: C Explanation: The else block executes after the try block if no exception was raised. Question 28. What is the purpose of the finally clause? A) To handle a specific exception type B) To execute code regardless of whether an exception occurred
C) To re‑raise the caught exception D) To skip the rest of the try block Answer: B Explanation: finally always runs, even if the try block exits via return or an exception. Question 29. Which statement raises a custom exception with a message? A) raise Exception('Invalid input') B) throw Exception('Invalid input') C) error('Invalid input') D) assert('Invalid input') Answer: A Explanation: raise creates and throws an exception object. Question 30. What will the following code print?
try: 1/ except ZeroDivisionError: print('Zero') else: print('No error') finally: print('Done') A) Zero Done
B) def f(l): l = l + [5] C) def f(l): l = [5] D) def f(l): del l Answer: A Explanation: append mutates the list object passed by reference. Question 34. In Python, what is the result of 3 < 2 < 5? A) True B) False C) Raises SyntaxError D) Returns a tuple Answer: A Explanation: Chain comparisons are evaluated as 3 < 2 and 2 < 5; both are False? Wait 3 < 2 is False, so overall False. Actually correct answer is B. Answer: B Explanation: The expression is equivalent to (3 < 2) and (2 < 5). Since 3 < 2 is False, the whole expression is False. Question 35. Which statement correctly checks if a variable x is not equal to 10? A) if x != 10: B) if x =! 10: C) if not x == 10: D) Both A and C Answer: D Explanation: Both x != 10 and not x == 10 are valid ways to test inequality.
Question 36. What does the following code output?
a = [1,2,3] b = a b.append(4) print(a) A) [1, 2, 3] B) [1, 2, 3, 4] C) None D) Raises TypeError Answer: B Explanation: b references the same list object as a; mutation via b affects a. Question 37. Which loop is guaranteed to execute its body at least once? A) while B) for C) do...while (Python does not have this) D) None of the above Answer: D Explanation: Python has no built‑in do...while; both while and for may skip the body if the condition is false initially. Question 38. Which expression correctly tests whether the string s contains the substring "py"? A) "py" in s
Answer: A Explanation: Curly braces with a single expression create a set comprehension. Question 41. In a try/except block, which clause must appear first? A) except B) else C) finally D) try Answer: D Explanation: The try keyword starts the block; it must be the first line. Question 42. What does bool('False') evaluate to? A) True B) False C) Raises ValueError D) None Answer: A Explanation: Any non‑empty string is truthy, regardless of its content. Question 43. Which of the following is a correct way to catch multiple exception types in one clause? A) except (ValueError, TypeError): B) except ValueError or TypeError: C) except ValueError, TypeError: D) except [ValueError, TypeError]:
Answer: A Explanation: Parentheses allow grouping of exception classes. Question 44. What will be printed?
for i in range(3): if i == 1: continue print(i) A) 0 1 2 B) 0 2 C) 1 2 D) 0 1 Answer: B Explanation: When i == 1, continue skips the print; outputs 0 and 2. Question 45. Which statement correctly swaps the values of a and b without a temporary variable? A) a, b = b, a B) a = a + b; b = a - b; a = a - b C) temp = a; a = b; b = temp D) Both A and B Answer: D Explanation: Both tuple unpacking and arithmetic trick swap values.
def fib(n): while n > 1: n = n- 1 return n C)
def fib(n): return n * fib(n-1) D) None of the above Answer: A Explanation: Classic recursive definition with base case n <= 1. Question 49. What will be the output of the following code?
x = 0 while x < 3: x += 1 if x == 2: break else: ## Certificate Practice Exam print('Done') print('End') A) Done End B) End C) Done D) No output Answer: B Explanation: break prevents the else block from running; only End is printed. Question 50. Which of the following correctly creates a generator that yields squares of numbers 0‑4? A) (x**2 for x in range(5)) B) [x**2 for x in range(5)] C) {x**2 for x in range(5)} D) list(x**2 for x in range(5)) Answer: A Explanation: Parentheses with a comprehension produce a generator expression. Question 51. What does isinstance(5, bool) return? A) True B) False C) Raises TypeError D) None Answer: B