








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!









1. What is the output of print(type(10 / 2)) in Python? A. B. C. D. Answer: B Rationale: In Python, the / operator performs true division and always returns a float, even when numbers divide evenly. 10 / 2 = 5.0, so type(5.0) is . 2. Which of the following is a valid variable name in Python? A. 2nd_place B. my_var C. my-var D. class Answer: B Rationale: Python variable names must start with a letter or underscore, cannot contain hyphens, and cannot be reserved keywords. 2nd_place starts with a digit, my-var contains a hyphen, and class is a reserved keyword. 3. What is the data type of the value 3.14 in Python? A. int B. float C. string D. Boolean Answer: B Rationale: Numbers with a decimal point are of type float (floating-point). Integers have no decimal point. 4. Given x = 5 and y = 2 , what is the result of x // y?
Answer: B Rationale: The floor division operator // returns the integer quotient and discards the remainder. 5 // 2 = 2.
5. Which operator is used to check if two values are equal in Python? A. = B. == C. != D. === Answer: B Rationale: == is the equality comparison operator. = is assignment. != means not equal. === is not valid in Python. 6. What is the output of print(type(10))? A. B. C. 'int' D. Answer: A Rationale: The type() function returns the type object; printing it displays . 7. What will the following code output? print(3 + 2 * 4) A. 20 B. 14 C. 11 D. 24 Answer: C Rationale: Operator precedence: multiplication before addition. 2 * 4 = 8, then 3 + 8 = 11. 8. Which correctly converts the string "123" to an integer? A. int("123") B. integer("123") C. str(123) D. float("123")
13. What will the following code print? x = "Hello"; print(x[1]) A. H B. e C. l D. o Answer: B Rationale: String indexing starts at 0. x[0] = 'H', x[1] = 'e', x[2] = 'l', x[3] = 'l', x[4] = 'o'. The output is 'e'. 14. What is the output of print(10 % 3)? A. 3 B. 1 C. 0 D. 3. Answer: B Rationale: The % operator is the modulo operator, returning the remainder of division. 10 divided by 3 is 3 with a remainder of 1. 15. What is the output of print("5" + "3")? A. 8 B. 53 C. Error D. 5 3 Answer: B Rationale: When the + operator is used with strings, it performs concatenation. "5" + "3" results in "53", not numeric addition. **Section 2: Control Flow—Conditionals and Loops (Questions 16–30)
A. Skips the current iteration B. Exits the loop completely C. Restarts the loop from the beginning D. Pauses the loop temporarily Answer: B Rationale: The break statement immediately terminates the loop entirely, not just skipping to the next iteration.
18. What does the continue statement do inside a loop? A. Terminates the loop entirely B. Skips the rest of the current iteration and moves to the next iteration C. Pauses the program D. Restarts the loop from the beginning Answer: B Rationale: The continue statement skips the remaining code in the current loop iteration and proceeds directly to the next iteration's condition check. 19. What will the following code output? python for i in range(3): print(i) A. 1 2 3 B. 0 1 2 C. 0 1 2 3 D. 1 2 Answer: B Rationale: range(3) generates a sequence of numbers starting at 0 up to, but not including, 3: 0, 1, 2. The loop prints each value on a new line, resulting in 0, 1, 2. 20. What does the following code print? python count = 0 while count < 3: print(count) count += 1 A. 0 1 2 B. 1 2 3
B. colon C. comma D. semicolon Answer: B Rationale: In Python, an if statement line must end with a colon :. The colon tells Python that an indented block of code follows.
25. Which Python data structure automatically prevents duplicate values? A. list B. tuple C. set D. string Answer: C Rationale: A set is a Python data structure that stores only unique elements. Duplicate values are automatically removed. 26. What is the output of print("python".upper())? A. python B. Python C. PYTHON D. pYTHON Answer: C Rationale: The .upper() method converts all characters in a string to uppercase. 27. What is the output of print(len("Programming"))? A. 10 B. 11 C. 12 D. 9 Answer: B Rationale: len() counts the number of characters. "Programming" has 11 characters. 28. What is the output of print("Python"[2:5])? A. Pyt B. tho C. yth D. hon
Answer: B Rationale: String slicing uses [start:end] where start is inclusive and end is exclusive. Index 2:'t', 3:'h', 4:'o'. Slicing from index 2 to 5 gives "tho".
29. How can the first four characters be extracted from the string 'computer'? A. 'computer'[1:4] B. 'computer'[0:4] C. 'computer'[0:3] D. 'computer'[1:5] Answer: B Rationale: The first character has index 0. To extract the first four characters, the slice should start at index 0 and stop at index 4 (exclusive). 'computer'[0:4] returns "comp". 30. A program extracts the last 3 characters representing a file extension. Which slicing approach works for any filename? A. filename[9] B. filename[-3] C. filename[-3:] D. filename[9:] Answer: C Rationale: The slice filename[-3:] extracts the last three characters from a string using negative indexing. **Section 3: Data Structures—Lists, Tuples, Dictionaries, Sets (Questions 31–40)
D. Error Answer: B Rationale: Sets automatically remove duplicate values. The duplicate value 2 appears only once in the set, resulting in {1, 2, 3}.
37. Which collection type is immutable? A. list B. dict C. set D. tuple Answer: D Rationale: Tuples cannot be changed after creation, making them immutable. Lists, dictionaries, and sets are mutable. 38. What is the output of the following code? python x = [1, 2] x.append(3) print(x) A. [1, 2] B. [3] C. [1, 2, 3] D. [1, 2, 3, 3] Answer: C Rationale: The append() method adds an element to the end of a list. Starting with [1, 2], appending 3 results in [1, 2, 3]. 39. What is the output of print(len("Python"))? A. 5 B. 6 C. 7 D. Error Answer: B Rationale: The len() function returns the number of characters in a string. "Python" has 6 characters.
40. Which statement correctly opens a file for reading? A. open("data.txt", "r") B. read("data.txt") C. file.read() D. open.read() Answer: A Rationale: The open() function with mode "r" opens a file for reading. The correct syntax is open("data.txt", "r"). **Section 4: Functions, Debugging, and Program Construction (Questions 41–50)
Answer: B Rationale: Multiplication occurs before addition. 3 * 2 = 6; 4 + 6 = 10.
47. Which data type stores whole numbers? A. float B. str C. int D. bool Answer: C Rationale: Integers represent whole numbers. Floats represent decimal numbers. Strings represent text. Booleans represent True/False. 48. Which operator means "not equal"? A. = B. == C. != D. <> Answer: C Rationale: != compares inequality. = is assignment. == is equality. <> was used in Python 2 but is not valid in Python 3. 49. What is the output of the following code? python x = 5 x += 2 print(x) A. 5 B. 7 C. 2 D. 52 Answer: B Rationale: The += operator adds the right operand to the left operand and assigns the result. x starts at 5, then 5 + 2 = 7.
50. Which symbol begins a single-line comment in Python? A. // B. # C. * D. % Answer: B Rationale: The # symbol begins a single-line comment in Python. The // operator is floor division, not a comment.