Docsity

CMPSC 131 — Programming with Python Programming Exam With Questions And Answers, Exams of Algorithms and Programming

Toggle sidebar
bg1
CMPSC 131 — Programming with
Python
Programming Exam With Questions And
Answers With Rationales
TOPIC 1: PYTHON BASICS — DATA TYPES, VARIABLES
& EXPRESSIONS
(15 questions)
1. Which of the following is NOT a valid Python
variable name?
A) my_var
B) _var
C) 2var
D) var2
E) myVar
Answer: C
Rationale: Variable names in Python cannot start with
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download CMPSC 131 — Programming with Python Programming Exam With Questions And Answers and more Exams Algorithms and Programming in PDF only on Docsity!

CMPSC 131 — Programming with

Python

Programming Exam With Questions And

Answers With Rationales

TOPIC 1: PYTHON BASICS — DATA TYPES, VARIABLES

& EXPRESSIONS

(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

A) 3.333...

B) 3

C) 1

D) 0

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

D) 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

D) AB

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

D) 0

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: