
































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 statement correctly displays the text Hello, Python! in a Python program? A. echo("Hello, Python!") B. print("Hello, Python!") C. display = "Hello, Python!" D. output("Hello, Python!") Correct Answer: B Rationale: Python uses the built-in print() function to display information in the output console. The text must be enclosed in quotation marks because it is a string literal. Assigning text to a variable stores the value but does not automatically display it. The other proposed functions are not standard Python functions for ordinary console output. Question 2 Which statement correctly assigns the integer value 25 to a variable named age? A. integer age = 25 B. var age := 25 C. age == 25 D. age = 25 Correct Answer: D
Rationale: Python variables are created through assignment using the single equals sign (=). Python is dynamically typed, so a programmer does not need to declare the variable's data type before assigning a value. The double equals operator (==) compares values rather than assigning them, while the other examples do not represent the required standard assignment syntax. Question 3 What is the data type returned by type(42)? A. int B. float C. str D. bool Correct Answer: A Rationale: The value 42 is an integer because it is a whole number without a decimal component. Python represents whole-number values using the int type. The type() function identifies the class of an object, so type(42) returns the integer class. A decimal value such as 42.0 would instead be a float. Question 4 What is the output of the following code? x = 7 y = 3 print(x / y) A. 2 B. 2. C. 2. D. 2. Correct Answer: C Rationale: The / operator performs true division in Python, producing a floating-point result even when both operands are integers. Dividing 7 by 3 produces a repeating decimal, which Python represents using its available floating-point precision. The // operator would perform floor division instead, producing 2.
B. y C. n D. Python Correct Answer: A Rationale: Python strings use zero-based indexing, meaning the first character is located at index 0. In the string "Python", the character at that position is P. Index 1 would return y, and index 5 would return n. Indexing allows individual characters to be accessed without changing the original string. Question 8 What is the result of the following code? print("Programming"[0:4]) A. Pro B. Prog r C. Programming D. Prog Correct Answer: D Rationale: String slicing uses the format [start:stop], where the starting index is included and the stopping index is excluded. Therefore, [0:4] extracts the characters at positions 0, 1, 2 and 3. These characters form Prog. Slicing creates a new string and does not modify the original string. Question 9 Which statement converts the string "125" into the integer 125? A. str("125") B. float("125") C. int("125")
D. number("125") Correct Answer: C Rationale: The built-in int() function converts a valid integer-formatted string into an integer object. Therefore, int("125") returns the numeric value 125 , which can then be used in arithmetic calculations. By contrast, str() produces a string, while float() returns a floating- point number. Python does not provide a standard built-in function named number(). Question 10 A program receives a user's age through input(). Why might the programmer need to convert the result before performing arithmetic? A. input() always returns a Boolean value. B. input() returns a string. C. input() automatically returns an integer. D. input() cannot receive numerical information. Correct Answer: B Rationale: Python's input() function returns the information entered by the user as a string, even when the user types digits. Arithmetic operations generally require numeric values, so the programmer must convert the result using functions such as int() or float(). For example, age = int(input("Enter age: ")) converts a valid whole-number response into an integer. Question 11 Which of the following is a valid Python comment? A. // Calculate total B. C. /* Calculate total */ D. # Calculate total Correct Answer: D Rationale: Python uses the hash symbol (#) to introduce a single-line comment. The interpreter ignores the comment when executing the program. Comments help explain complex operations, document decisions and improve maintainability. The other examples use comment conventions
What is the output of the following code? print(bool("")) A. None B. False C. True D. An error occurs. Correct Answer: B Rationale: Python treats an empty string as a false value when evaluated in a Boolean context. The bool() function converts the supplied object into its Boolean equivalent, so bool("") returns False. Non-empty strings, including "False", are truthy because they contain characters. This distinction is important when validating user input and conditional expressions. Question 15 Which statement correctly checks whether x is equal to 10? A. x = 10 B. x != 10 C. x >= 10 D. x == 10 Correct Answer: D Rationale: The equality comparison operator is ==. It compares the value of x with 10 and returns either True or False. A single equals sign performs assignment instead of comparison. The != operator checks inequality, while >= checks whether the value is greater than or equal to
Question 16 What is the output of the following code? x = 8 y = 12 print(x < y and y > 10)
A. True B. False C. 8 D. 12 Correct Answer: A Rationale: The and operator returns a truthy result only when both conditions are true. Here, 8 < 12 is true and 12 > 10 is also true. Since both comparisons succeed, the complete expression evaluates to True. Logical operators are useful for combining several conditions when a decision depends on multiple requirements. Question 17 Which variable name follows Python's identifier naming rules? A. 2total B. total-cost C. total_cost D. class Correct Answer: C Rationale: Python identifiers may contain letters, numbers and underscores, but they cannot begin with a digit. Hyphens are interpreted as subtraction operators, so they cannot be used within ordinary variable names. Reserved keywords such as class are also invalid identifiers for assignment. The name total_cost follows the naming rules and is readable. Question 18 What is the result of the following expression? print(10 // 3) A. 3. B. 3 C. 1
Therefore, f"Total: {total}" displays the text followed by the current value of the variable. F-strings provide readable and flexible string formatting. Question 21 Which Python collection is ordered, mutable and allows duplicate elements? A. Set B. Dictionary keys C. List D. Tuple Correct Answer: C Rationale: A Python list maintains the order of its elements, allows duplicate values and can be modified after creation. Programmers can append, remove, replace and rearrange list elements. Tuples are ordered but immutable, while sets do not preserve ordinary positional indexing and store unique elements. Lists are therefore suitable for collections that require frequent modification. Question 22 What is the output of this code? numbers = [10, 20, 30, 40] print(numbers[2]) A. 20 B. 30 C. 40 D. 10 Correct Answer: B Rationale: Python lists use zero-based indexing. The first element has index 0, the second has index 1 and the third has index 2. Therefore, numbers[2] accesses the value 30. Understanding indexing is essential when retrieving individual elements, updating list values and avoiding IndexError exceptions caused by invalid positions. Question 23
What is the result of the following code? items = [1, 2, 3] items.append(4) print(items) A. [4, 1, 2, 3] B. [1, 2, 3] C. [1, 2, 3, [4]] D. [1, 2, 3, 4] Correct Answer: D Rationale: The append() method adds one element to the end of a list. In this example, the value 4 is added after the existing elements, producing [1, 2, 3, 4]. Unlike insert(), which places an element at a specified position, append() always adds the new element at the end. Question 24 A programmer has the list [1, 2, 3] and wants to add every element from [4, 5] individually. Which method is most appropriate? A. extend() B. append() C. remove() D. clear() Correct Answer: A Rationale: The extend() method iterates over another iterable and adds its individual elements to the existing list. Applying numbers.extend([4, 5]) to [1, 2, 3] produces [1, 2, 3, 4, 5]. By contrast, append([4, 5]) would add the entire second list as a single nested element. Question 25 What is the output of this code? values = [5, 10, 15, 20] print(values[-1]) A. 5
Correct Answer: D Rationale: A tuple is commonly created using comma-separated values enclosed in parentheses. The expression (5, 10) creates an ordered, immutable tuple containing two elements. Lists use square brackets, while curly braces create sets or dictionaries. Parentheses are not strictly required in every tuple expression, but they make the structure clear and readable. Question 28 Why is a tuple suitable for storing fixed configuration values that should not be modified? A. Tuples are immutable. B. Tuples automatically encrypt their contents. C. Tuples allow elements to be changed more quickly than lists. D. Tuples prevent all access to their elements. Correct Answer: A Rationale: Tuples are immutable, meaning their element references cannot be reassigned or removed after the tuple has been created. This makes them useful for representing fixed collections such as coordinates, configuration values and records that should remain unchanged. Immutability does not prevent reading or indexing tuple elements, and it does not automatically provide encryption or security. Question 29 Which statement creates an empty dictionary? A. data = [] B. data = () C. data = {} D. data = set() Correct Answer: C Rationale: An empty dictionary is created using a pair of curly braces: {}. Dictionaries store information as key-value pairs, allowing values to be retrieved using their associated keys. An empty set must instead be created with set(), because {} is reserved for an empty dictionary. Square brackets create lists and parentheses create tuples.
Question 30 What is the output of the following code? student = {"name": "Amina", "score": 85} print(student["score"]) A. name B. 85 C. Amina D. score Correct Answer: B Rationale: Dictionaries store values using keys. In this example, the key "score" is associated with the integer value 85. Accessing student["score"] retrieves the corresponding value. Dictionary keys must be valid hashable objects, and attempting to access a missing key using square brackets raises a KeyError unless the key exists. Question 31 Which dictionary method allows a programmer to retrieve a key's value without raising a KeyError when the key is missing? A. append() B. remove() C. sort() D. get() Correct Answer: D Rationale: The dictionary get() method retrieves the value associated with a key and can return a specified default when the key does not exist. For example, student.get("grade", "Not available") returns the default text if "grade" is absent. This is safer than direct square- bracket access when dictionary keys may be optional or unpredictable. Question 32 What is the output of this code?
C. Python automatically converts the string into a list. D. The string becomes an empty value. Correct Answer: B Rationale: Python strings are immutable, so individual characters cannot be reassigned after the string is created. Attempting to execute word[0] = "b" raises a TypeError. To produce "bat", the programmer could create a new string using concatenation, slicing or a list conversion followed by joining the characters. Question 35 What is the output of the following code? text = " Python " print(text.strip()) A. "Python" B. " Python" C. "Python " D. " Python " Correct Answer: A Rationale: The strip() string method returns a new string with leading and trailing whitespace removed. It does not remove spaces occurring between words. Therefore, the result is "Python". Since strings are immutable, the original variable is not modified unless the returned value is assigned back to it, for example, text = text.strip(). Question 36 Which statement correctly sorts a list in ascending order in place? A. numbers.order() B. numbers.sorted() C. numbers.sort() D. sort(numbers, ascending=True) Correct Answer: C
Rationale: The list method sort() rearranges the existing list in place, using ascending order by default. It returns None, so assigning its return value back to the list would accidentally replace the variable with None. The built-in sorted() function is an alternative that returns a new sorted list while leaving the original iterable unchanged. Question 37 What is the output of this code? values = [3, 6, 9] result = values.pop() print(result) A. 3 B. [3, 6] C. None D. 9 Correct Answer: D Rationale: The pop() method removes and returns an element from a list. When no index is supplied, it removes the final element. Therefore, values.pop() removes 9 and assigns that value to result. The remaining list becomes [3, 6], while the printed result is 9. Question 3 8 A program must preserve the insertion order of key-value pairs while allowing values to be updated. Which Python structure is most appropriate? A. Tuple B. Dictionary C. Frozen set D. String Correct Answer: B Rationale: Python dictionaries preserve insertion order in modern Python versions and are mutable, allowing existing values to be updated and new key-value pairs to be added. They provide efficient access through keys and are suitable for records, lookup tables and structured data. Tuples and strings are immutable, while frozen sets do not provide key-value mapping.
B. if C. define D. import Correct Answer: B Rationale: The if statement evaluates a Boolean expression and executes its indented code block when the condition is true. It is a fundamental control-flow structure used for decision- making. Additional elif and else clauses can provide alternative execution paths, allowing programs to respond differently to varying inputs and conditions. Question 42 What is the output of the following code? temperature = 30 if temperature > 25: print("Hot") else: print("Cool") A. Cool B. 30 C. No output D. Hot Correct Answer: D Rationale: The condition temperature > 25 evaluates to true because the variable contains 30. Python therefore executes the indented statement inside the if block and skips the else block. The program displays Hot. Conditional statements allow applications to make decisions based on changing values rather than following one fixed execution path. Question 43 Which keyword provides an alternative condition after an initial if condition fails? A. elif B. otherwise
C. when D. switch Correct Answer: A Rationale: Python uses elif, meaning “else if,” to test another condition when the preceding if or elif condition is false. Multiple elif clauses may appear in one conditional structure. Python checks them in order and executes the first matching branch, making elif useful for grading systems, menus and classification tasks. Question 44 What is the output of this code? x = 5 if x > 3: if x < 10: print("Valid") A. Invalid B. 5 C. Valid D. No output Correct Answer: C Rationale: The program contains nested conditional statements. The first condition, x > 3 , is true because 5 is greater than 3. The second condition, x < 10 , is also true. Consequently, Python enters both indented blocks and displays Valid. Nested conditions are useful when a decision depends on several related requirements. Question 45 Which logical operator returns true when at least one of two conditions is true? A. and B. or C. not D. is