


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This comprehensive exam covers Python 3.x fundamentals, including data types, operators, control flow, strings, lists, dictionaries, tuples, sets, file handling, exception handling, lambda functions, list comprehensions, and the math & random modules. Also includes computer history questions (Babbage, Turing, ENIAC). Every question includes a correct answer and a clear explanation. Updated to reflect current Python behavior (e.g., floor division, banker's rounding, identity vs. equality). Ideal for final exams, certification practice, coding interviews, or self-assessment.
Typology: Exams
1 / 34
This page cannot be seen from the preview
Don't miss anything!



























1. Charles Babbage is known for designing: A) The first electronic computer B) The Analytical Engine, a mechanical general-purpose computer C) The concept of a stored program D) The first programming language Answer: B Explanation: Charles Babbage designed the Analytical Engine in the 1830s–1840s. It was a mechanical general-purpose computer, though never fully built in his lifetime. 2. Who proposed the theoretical idea of a universal computing machine (Turing Machine)? A) Konrad Zuse B) Charles Babbage C) Alan Turing D) John von Neumann Answer: C Explanation: Alan Turing introduced the concept of a universal Turing machine in 1936, laying the foundation for general-purpose computing. 3. Which machine was the first all-electronic general-purpose computer (completed 1945)? A) Z B) Colossus C) ENIAC D) Analytical Engine Answer: C Explanation: ENIAC (Electronic Numerical Integrator and Computer) was completed in 1945 and was the first all-electronic, general-purpose digital computer.
Answer: B Explanation: ** is the exponentiation operator (e.g., 2**3 = 8). In many older contexts, ^ is used for bitwise XOR, not exponent.
7. What is the output? python a = 10 b = 23 print(a + b * a) A) 330 B) 240 C) 43 D) 230 Answer: B Explanation: Multiplication before addition: b * a = 23 * 10 = 230, then a + 230 = 10 + 230 = 240. 8. Which of the following is NOT a correct variable assignment in Python? A) x = 5 B) _var = 10 C) 5 + y = x D) my_var = 20 Answer: C Explanation: 5 + y = x is invalid because the left side of = must be a variable name, not an expression.
C) pow(7,6) D) Both B and C Answer: D Explanation: Both 7**6 and pow(7,6) correctly compute 117,649.
10. A symbol that defines a mathematical calculation (e.g., +, - , *, /) is called: A) Operand B) Identifier C) Operator D) Expression Answer: C Explanation: Operators perform operations on operands. 11. Which expression calculates to 19? A) (3 * 6) + 2 / 2 B) 3 * 6 + 2 / 2 C) (3 * 6 + 2) / 2 D) 3 * (6 + 2) / 2 Answer: A Explanation: (3 * 6) + 2 / 2 = 18 + 1 = 19. Division before addition, but parentheses clarify. 12. What happens when this program runs and the user enters 0 for the second number?
C) Quotient D) Modulo (same as remainder for positives) Answer: B (or D) Explanation: % returns the remainder (modulo). In 2026, "remainder" and "modulo" are nearly synonymous for positive numbers.
15. Which of the following is NOT a typical use of modular division (%)? A) Finding if a number is even or odd B) Wrapping around in circular arrays C) Finding mode (most frequent value) D) Getting last digit of a number Answer: C Explanation: Mode is found by counting frequencies, not modular division. 16. If doing modular division with a divisor of 3, what are the only possible remainders? A) 0, 1, 2, 3 B) 0, 1, 2 C) 1, 2, 3 D) 0, 3 Answer: B Explanation: Remainders must be less than the divisor: 0, 1, or 2. 17. What is the value of 7 % 24? A) 7 B) 3
Answer: A Explanation: 7 divided by 24 gives quotient 0, remainder 7.
18. Which function would you use to ensure a calculated distance is positive (absolute value)? A) abs() B) fabs() (in math module) C) sqrt() D) Both A and B are correct Answer: D Explanation: abs() works for integers and floats; math.fabs() always returns float. In 2026, abs() is preferred unless type specificity is needed. 19. Which function finds the side length of a square given its area? A) pow() B) sqrt() C) abs() D) mod() Answer: B Explanation: Side = √area. math.sqrt(area) or area ** 0.5. 20. Which function raises a number to an exponent? A) sqrt() B) pow()
C) 0 to 6 D) 1 to 7 Answer: B Explanation: randint(0,7) includes both endpoints → integers from 0 to 7 inclusive.
24. What are possible outputs of random.random() * 5? A) Integers 0 to 5 B) 0.0 to 5.0 (including 0, excluding 5) C) 0.0 to 4.999... D) 1.0 to 5. Answer: C Explanation: random.random() returns a float in [0.0, 1.0). Multiply by 5 → [0.0, 5.0). 5. is not included. 25. What is the output of print(type(10 / 2)) in Python 3+? A) B) C) D) Answer: B Explanation: In Python 3+, / always returns a float, even if the result is a whole number. 10 / 2 = 5.0 → float. 26. What is the output of print(10 // 3)? A) 3. B) 3
Answer: B Explanation: // is floor division. 10 // 3 = 3 (integer, remainder discarded).
27. Which data type is immutable? A) list B) dict C) tuple D) set Answer: C Explanation: Tuples cannot be changed after creation. Lists, dicts, and sets are mutable. 28. Which function converts a string to an integer safely (without crashing) in Python 2026 best practices? A) int() B) float() C) str.isdigit() then int() D) eval() Answer: C Explanation: Using str.isdigit() to check before conversion prevents ValueError. eval() is dangerous. 29. What is the output of bool([])? A) True B) False
C) Reads the entire file D) Writes to the file Answer: B Explanation: The with statement ensures the file is properly closed after the block exits, even if an error occurs.
33. Which method reads all lines of a file into a list? A) read() B) readline() C) readlines() D) readall() Answer: C Explanation: readlines() returns a list of strings, each ending with a newline character. 34. What is the output of print(list(range(3, 10, 2)))? A) [3, 5, 7, 9] B) [3, 5, 7, 9, 11] C) [3, 4, 5, 6, 7, 8, 9] D) [3, 6, 9] Answer: A Explanation: range(start, stop, step). Start=3, stop=10 (excluded), step=2 → 3, 5, 7,
35. What is the output of print(2 ** 3 ** 2)? A) 64 B) 512
Answer: B Explanation: Exponentiation is right-associative: 2 ** (3 ** 2) = 2 ** 9 = 512.
36. Which statement correctly defines an empty set in Python? A) {} B) set() C) [] D) () Answer: B Explanation: {} creates an empty dictionary. set() creates an empty set. 37. What is the output of print(5 in [1, 2, 3, 4])? A) True B) False C) 5 D) Error Answer: B Explanation: in checks membership. 5 is not in the list → False. 38. Which keyword exits a loop immediately? A) continue B) pass C) break D) exit
42. Which method removes whitespace from both ends of a string? A) strip() B) trim() C) rstrip() D) lstrip() Answer: A Explanation: strip() removes leading and trailing whitespace. lstrip()/rstrip() remove from left/right only. 43. What is the output of print('a b c'.split())? A) ['a', 'b', 'c'] B) 'a b c' C) ['a b c'] D) ['a', ' ', 'b', ' ', 'c'] Answer: A Explanation: split() without arguments splits on any whitespace and returns a list of words. 44. Which method joins a list of strings into one string? A) concat() B) join() C) merge() D) append() Answer: B Explanation: Example: '-'.join(['a', 'b', 'c']) → 'a-b-c'.
45. What is the output of print('hello'.find('l'))? A) 2 B) 3 C) [2, 3] D) 1 Answer: A Explanation: find() returns the lowest index where the substring is found. 'l' first appears at index 2 (0-based). 46. What does try-except handle? A) Syntax errors B) Runtime exceptions C) Logic errors D) Indentation errors Answer: B Explanation: try-except catches and handles exceptions that occur during execution (e.g., division by zero, file not found). 47. 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]
C) lambda (x): x * 2 D) lambda x, y: return x + y Answer: A Explanation: Lambda functions are anonymous, single-expression functions. They do not use return explicitly.
51. What is the output of list(map(lambda x: x**2, [1, 2, 3]))? A) [1, 4, 9] B) [1, 2, 3] C) [2, 4, 6] D) [1, 4, 6] Answer: A Explanation: map() applies the lambda (square function) to each element of the list. 52. Which function filters a list based on a condition? A) map() B) filter() C) reduce() D) apply() Answer: B Explanation: filter(function, iterable) keeps elements for which the function returns True. 53. What is the output of [x for x in range(5) if x % 2 == 0]? A) [0, 2, 4] B) [1, 3]
Answer: A Explanation: List comprehension with condition: keeps even numbers from 0 to 4.
54. What is the output of {x: x**2 for x in range(3)}? A) {0: 0, 1: 1, 2: 4} B) [0, 1, 4] C) {0, 1, 4} D) (0, 1, 4) Answer: A Explanation: Dictionary comprehension: keys are numbers, values are squares. 55. Which of the following is not a keyword in Python? A) finally B) global C) goto D) nonlocal Answer: C Explanation: goto is not a Python keyword. Python has no goto statement. 56. What is the output of print(3 == 3.0)? A) True B) False C) TypeError D) None