






















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: Python Fundamentals & Syntax (Questions 1–15)
Answer: B. Explanation: type() returns the class of the object. 3.14 is a floating-point number, so the output indicates a float class.
Answer: B. ** Explanation: ** is the exponentiation operator (e.g., 2 ** 3 equals 8). The ^ operator is used for bitwise XOR.
Answer: B. 3 Explanation: // is the floor division operator; it returns the integer quotient, discarding the remainder (17 // 5 = 3).
Answer: A. Hello, World! Explanation: The print() function outputs the string literal to the console without the quotation marks. The quotes are used only to denote the string.
Answer: C. _myVariable Explanation: Variable names can start with a letter or underscore, cannot start with a digit, and cannot contain spaces or hyphens. Underscore is allowed.
Answer: B. 6 Explanation: len() returns the number of characters in the string. "Python" has 6 characters: P, y, t, h, o, n.
Answer: A. input("Enter name: ") Explanation: The built-in input() function displays the optional prompt and returns the user's input as a string.
Answer: C. TypeError: can only concatenate str (not "int") to str Explanation: Python does not automatically convert numbers to strings when using the + operator on a string and an integer. Use str(42) to convert.
Answer: C. True
Explanation: Python uses capitalized True and False as Boolean literals. true and TRUE are not recognized as Boolean values (they would be variable names).
Answer: B. False Explanation: In Python, numeric values: 0, 0.0, and complex zero evaluate to False in a Boolean context. All non-zero numbers evaluate to True. Section 2: Control Flow (if, elif, else, loops) (Questions 16–30)
Answer: A. A Explanation: The first condition x > 5 is True (10 > 5), so the block under if executes and prints "A". The elif and else blocks are skipped.
Answer: C. break Explanation: The break statement terminates the innermost loop (while or for) immediately and resumes execution after the loop.
Answer: B. 1 3 5 Explanation: range(1, 6, 2) starts at 1, goes up to but not including 6, with a step of 2. So the values are 1, 3, and 5.
Answer: C. Both A and B Explanation: Python supports chained comparisons: 10 <= n <= 20 is equivalent to n >= 10 and n <=
range(10,20) excludes 20, so it's not inclusive of 20.
What is the output of print(not (5 > 3) or (2 == 2))?
Answer: B. True Explanation: (5 > 3) is True, so not True is False. (2 == 2) is True. False or True evaluates to True.
Answer: D. none of the above Explanation: Python has no do-while loop. Both for and while loops check conditions before the first iteration, so they may execute zero times.
Answer: B. 10 Explanation: The condition if x: treats x as a Boolean. Non-zero numbers are True, so the if branch executes, setting x to 10.
Answer: B. continue Explanation: continue jumps to the next iteration of the loop, skipping any remaining code in the current iteration. break exits the loop entirely.
Explanation: Both correctly represent the condition. range(18,66) includes 65, so age not in range(18,66) also works but is less common for numeric comparisons.
Answer: A. 0 0 0 1 Explanation: Outer loop i=0, inner loop j=0 → 0; j=1 → 0; outer i=1, inner j=0 → 0; j=1 →
Answer: B. Hi Explanation: The function greet() returns the string "Hi". The print function outputs that returned value.
Answer: C. def Explanation: The def keyword is followed by the function name, parentheses, and a colon. The function body is indented.
Answer: A. 7 Explanation: The function takes two parameters and returns their sum. add(3,4) returns 7, which is assigned to result and printed.
Answer: B. local Explanation: Variables declared inside a function have local scope; they are not accessible outside that function unless returned or passed.
Answer: B. 7 Explanation: x=1 (positional), y uses default 2, z is overridden to 4. Sum = 1+2+4 = 7.
Answer: D. Both B and C Explanation: You can call using positional arguments (multiply(2,3)) or keyword arguments (multiply(a=2,b=3)).
Answer: D. Both B and C Explanation: A variable defined inside a function is local to that function. If the function is nested inside another function, it is local to the inner function and can be accessed by the inner function (but not by the outer function).
Answer: B. nonlocal Explanation: The nonlocal keyword allows the inner function to modify the variable x from the enclosing (outer) function's scope. After inner() executes, x becomes "nonlocal".
Answer: A. 3. Explanation: The round() function rounds a number to a specified number of decimal places. round(3.14159, 2) gives 3.14.
Answer: C. len() Explanation: len() is a built-in function that returns the number of items in a container (list, tuple, string, dictionary, etc.).
Answer: B. It is a placeholder that does nothing, used where syntax requires a statement Explanation: pass is a null operation. It is often used in empty function or class definitions, or in if blocks that will be filled later.
Answer: A. [1, 2, 3, 4, 5] Explanation: The + operator concatenates two lists, producing a new list containing all elements from both.
Answer: A. [1, 2, 3, 1, 2, 3] Explanation: Repetition operator * repeats the list the given number of times, concatenating the copies.
Answer: A. append() Explanation: list.append(x) adds x to the end of the list. extend() adds all elements of an iterable, insert() adds at a given index.
Answer: A. [1, 2, 4] Explanation: pop(2) removes and returns the element at index 2 (the third element, which is 3). The remaining list is [1, 2, 4].
Answer: A. (1, 2, 3) Explanation: Tuples are defined with parentheses. A single-element tuple requires a comma: (1,). Without the comma, it's just parentheses around a value.
Answer: B. Tuples are immutable, so they can be used as dictionary keys Explanation: Immutability allows tuples to be hashable, making them suitable as keys in dictionaries. Lists are mutable and cannot be hashed.
Answer: A. [1, 3] Explanation: remove(2) removes the first occurrence of the value 2. The list becomes [1, 3]. Note: remove() modifies the list in place and returns None.
Answer: A. L[1:3]
print(a)
Answer: B. [1, 2, 3] Explanation: b = a makes b reference the same list object as a. Modifying the list via b also affects a. Section 5: Dictionaries and Sets (Questions 61–75)
Answer: A. {} Explanation: Curly braces create a dictionary. An empty set is created with set(), not with {} because {} is an empty dictionary.
Answer: A. d["name"] Explanation: The square bracket notation with the key returns the value. get() is another method that avoids KeyError.
Answer: A. 3 Explanation: len() returns the number of key-value pairs in the dictionary, which is 3.
Answer: C. update() or assignment d[key] = value Explanation: d[key] = value will add a new key-value pair if the key does not exist, or update it if it does. update() can add multiple pairs.
Answer: A. 0 Explanation: get(key, default) returns the value for the key if it exists, otherwise returns the default value (0 in this case). No error is raised.
Answer: C. {1, 2, 3} Explanation: Sets are defined with curly braces and comma-separated values, without colons. {1:2, 3:4} is a dictionary.