




























































































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!





























































































(15 questions)
1. Which of the following is NOT a valid Python variable name? A) my_var B) _var C) 2var D) var E) myVar Answer: C Rationale: Variable names in Python cannot start with
a number. 2var is invalid because it begins with a digit. All other options are valid: my_var, _var, var2, and myVar follow the rules (letters, digits, underscores; cannot start with digit).
2. What is the data type of the value 3.14 in Python? A) int B) float C) str D) bool E) complex Answer: B Rationale: 3.14 contains a decimal point, so it is a floating-point number ( float ) in Python. Integers (int) are whole numbers without a decimal. 3. What is the data type of the value True in Python? A) int B) float C) str
E) Error Answer: C Rationale: The % operator returns the remainder of the division. 10 % 3 = 1 because 3 × 3 = 9, and 10 − 9 = 1.
6. What is the result of "Hello" + "World"? A) "HelloWorld" B) "Hello World" C) "Hello" + "World" D) HelloWorld E) Error Answer: A Rationale: The + operator concatenates strings. "Hello " + "World" produces "HelloWorld" (no space between them).
7. What is the result of "Hello" * 3? A) "HelloHelloHello" B) "Hello Hello Hello" C) HelloHelloHello D) "Hello3" E) Error Answer: A Rationale: The * operator repeats strings. "Hello" * 3 produces "HelloHelloHello". 8. Which of the following statements correctly converts a string to an integer? A) int("123") B) float("123") C) str(123) D) "123".to_int() E) convert("123")
10. Which of the following statements is TRUE about Python strings? A) Strings are mutable (can be changed) B) Strings are immutable (cannot be changed) C) Individual characters in a string can be changed using assignment D) Strings can only contain letters E) Strings are a numeric data type Answer: B Rationale: Python strings are immutable —once created, they cannot be changed. Individual characters cannot be modified through assignment. 11. What is the output of the following code? python name = "Alice" age = 25 print(name + age)
A) "Alice25" B) Alice C) TypeError D) "Alice 25" E) No output Answer: C Rationale: Python does not allow concatenation of a str and an int directly. This will raise a TypeError. You would need to convert: name + str(age).
12. What is the output of the following code? python x = 5 x += 3 print(x) A) 3 B) 5 C) 8
python print(type(3.0)) A) B) C) D) E) Answer: B Rationale: 3.0 is a floating-point number, so type(3.0) returns .
15. Which of the following is a valid way to get user input in Python? A) input("Enter your name: ") B) scan("Enter your name: ") C) read("Enter your name: ") D) get_input("Enter your name: ") E) console.input("Enter your name: ") Answer: A Rationale: The built-in input() function is used to get
user input in Python. It takes an optional prompt string and returns the user's input as a string. TOPIC 2: CONTROL FLOW — CONDITIONALS (if/elif/else) (15 questions)
16. What is the output of the following code? python x = 10 if x > 5: print("A") else: print("B") A) A B) B C) AB
E) No output Answer: B Rationale: x > 5 is False, but x > 2 is True (3 > 2), so the elif block executes and prints "B".
18. What is the output of the following code? python x = 1 if x > 5: print("A") elif x > 2: print("B") else: print("C") A) A B) B C) C
E) No output Answer: C Rationale: Both x > 5 and x > 2 are False, so the else block executes and prints "C".
19. Which logical operator would you use to check if both conditions A and B are true? A) or B) and C) not D) xor E) && Answer: B Rationale: The and operator returns True only if both conditions are true. or requires at least one to be true. not negates a condition. 20. What is the output of the following code?
A) 10 < n < 20 B) 10 <= n <= 20 C) 10 < n and n < 20 D) 10 <= n and n <= 20 E) Both B and D Answer: E Rationale: In Python, 10 <= n <= 20 is equivalent to 10 <= n and n <= 20. Both correctly check if n is between 10 and 20 inclusive (including 10 and 20).
22. What is the output of the following code? python x = True y = False print(x and y) A) True B) False C) 1
E) Error Answer: B Rationale: True and False evaluates to False because both conditions must be true for and to return True.
23. What is the output of the following code? python x = True y = False print(x or y) A) True B) False C) 1 D) 0 E) Error Answer: A Rationale: True or False evaluates to True because at least one condition is true.
25. What is the output of the following code? python x = 0 if x: print("A") else: print("B") A) A B) B C) Error D) No output E) AB Answer: B Rationale: In Python, 0 is considered False in a Boolean context. So the else block executes and prints "B". 26. Which of the following is a valid way to write a multi-line if statement in Python?
A) Using curly braces {} B) Using parentheses and indentation C) Using semicolons D) Using endif E) Using fi Answer: B Rationale: Python uses indentation to define blocks of code. Multi-line conditionals are written with proper indentation after the colon. Parentheses can also be used for line continuation.
27. What is the output of the following code? python x = 15 if x < 10: print("A") elif x < 20: print("B") elif x < 30: