

























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!


























Ace the WGU E010 Foundations of Programming (Python) OA
midterm Exam with practice questions covering variables, data
types, input/output, arithmetic operators, strings, lists, dictionaries,
conditionals, loops, functions, and basic debugging. This study guide
helps reinforce foundational coding concepts and supports effective
preparation for objective assessments. Designed to improve
problem-solving skills and boost confidence in Python programming.
Suitable for computer science, IT, and programming students.
Multiple choice.
Section 1: Variables, Data Types & Type Conversion
1. Which of the following is a valid variable name in Python?
A) 2ndVariable
B) my-var
C) _myVar
D) my var
Answer: C. _myVar
Explanation: Variable names cannot start with a digit (A),
cannot contain hyphens (B) or spaces (D). Underscore is
allowed, including at the beginning.
2. What is the data type of 3.14? 3. int 4. float 5. str 6. bool Answer: B. float Explanation: Numeric values with a decimal
point are of type float. Integers are whole numbers without a decimal
point.
7. What is the output of print(type(5 / 2))? A) B) C) D) Answer: B. Explanation: In Python, the/ operator always returns a
floating-point result, even when the division is exact.
8. Which of the following will convert the string "123" to an integer? A)
int("123") B) str("123")
Answer: B. 13
Explanation: Multiplication has higher precedence than
addition. First 5 * 2 = 10, then 3 + 10 = 13.
7. What does the % operator do in Python? 8. Percent 9. Modulus – returns the remainder of division 10. Divi-
sion
11. Exponentiation Answer: B. Modulus – returns the remainder of divi-
sion Explanation: The modulus operator % gives the remainder after in-
teger division. Example: 10 % 3 returns 1.
12. Which operator is used for floor division? A) / B) // C) % D) ** An-
swer: B. // Explanation: Floor division (//) divides and rounds down to
the nearest integer. 10 // 3equals3.
13. What is the output of print(5 ** 2)?
Answer: C. 25
Explanation: ** is the exponentiation operator. 5 raised to the
power of 2 equals 25.
10. Which comparison operator means “not equal to”?
Answer: C. !=
Explanation: != is the not-equal operator in Python. It
returns True if the two values are different.
Section 3: Strings & String Methods
11. What is the output of print("Hello" + "World")? 12. HelloWorld 13. Hello World 14. Hello+World 15. Error Answer: A. HelloWorld Explanation: The + operator concate-
nates strings. No space is automatically added.
16. What is the output of print(len("Python"))?
C) Python
D) pYTHON
Answer: B. PYTHON
Explanation: .upper() returns a new string where all letters are
uppercase.
Section 4: Lists & List Operations
16. Which of the following creates a list containing the numbers 1, 2,
and 3? A) list(1, 2, 3) B) [1, 2, 3] C) {1, 2, 3} D) (1, 2, 3) Answer: B. [1, 2, 3]
Explanation: Lists are written with square brackets []. Tuples use paren-
theses(), sets use {}.
17. What does append() do to a list? 18. Adds an element to the beginning 19. Adds an element to the end 20. Removes the last element 21. Removes the first element Answer: B. Adds an element to the end
Explanation: append() adds a single element to the end of the list. To
add at a specific position, use insert().
18. What is the output of the following code? python
x = [1, 2, 3] y = x y.append(4) print(x) A)[1, 2, 3] B) [1, 2,
19. Error Answer: B. [1, 2, 3, 4] Explanation: y = x makesy a reference to
the same list object as x. Modifying the list through ychanges the same
list that xrefers to.
20. How do you access the last element of a list named data?
A)data[0] B)data[-1] C) data[last] D) data[len(data)] Answer: B. data[-1]
Explanation: Negative indices count from the end. -1 always gives the
last element.
C) person{"name"}
D) person['name'] (also valid)
Answer: B. person["name"]
Explanation: Square brackets with the key name are used to
access dictionary values. Both single and double quotes work.
23. Which method returns all the keys in a dictionary?
A) keys()
B) values()
C) items()
D) get()
Answer: A. keys()
Explanation: dict.keys() returns a view object containing all
keys. values() returns values, items() returns key-value pairs.
Section 6: Conditionals (if/elif/else)
24. What is the output of the following code?
python
x = 5
if x > 10:
print("A")
elif x > 0:
print("B")
else:
print("C")
D)Error Answer: B. B Explanation: x > 10 isFalse, so it moves to the elif. x
> 0 isTrue, so it prints "B".
E) Which logical operator returns True only when both conditions are
true? A) or B) not C) and D) xor Answer: C. and Explanation: The and
operator requires both operands to beTrue to return True.
F) What is the output of print(True and False)?
G)True
H)False
D)Infinite Answer: B. 5 Explanation: The loop runs while i < 5. Values ofi
are 0,1,2,3,4 – five times. Thenibecomes 5 and the condition fails.
E) What is the output of the following code? python for
i in range(3): print(i, end=" ")
I) 1 2 3 4 Answer: A. 0 1 2 Explanation: range(3) generates numbers start-
ing at 0 up to, but not including, 3.
J ) Which keyword is used to exit a loop prematurely? A)exit B) stop C)
break D) continue Answer: C. break Explanation: break immediately
terminates the innermost
loop. continue skips the rest of the current iteration and goes to
the next.
Section 8: Functions
32. Which keyword is used to define a function in Python?
A) function
B) def
C) define
D) func
31. What is the output of the following code? python
for i in range(1, 5): if i == 3: continue print(i,
end=" ")
35. 2 3 4 Answer: B. 1 2 4 Explanation: When i equals 3, con-
tinue skips the print statement, so 3 is not printed.
Explanation: return ends the function and can pass a value
back. Without a return, the function returns None.
Section 9: Input & Output
36. Which function reads a line of text from the user?
A) read()
B) input()
C) get()
D) scan()
Answer: B. input()
Explanation: input() pauses the program and waits for the user
to type something and press Enter. It returns the input as a
string.
35. Which of the following is a correct function call for def
greet(name="Guest")? A)greet() B)greet("Alice")
36. Both A and B are valid 37. Neither is valid Answer: C. Both A and B are valid Explanation: The
parameter name has a default value "Guest". The function can be called
with no argument (uses default) or with an argument.
Section 10: Errors & Debugging
39. What type of error occurs when you try to access an
index that is out of range for a list?
37. What is the output of print("Hello", "World", sep="-")? 38. Hello-World 39. Hello World 40. Hello-World (no quotes) 41. Hello,World Answer: A. Hello-World Explanation: The sep parame-
ter defines the separator between multiple printed items. The default
separator is a space. Here it is changed to a hyphen.
42. What is the output of print("Hello", end="") followed by
print("World")?
43. Hello World 44. HelloWorld 45. Hello
World
46. Error Answer: B. HelloWorld Explanation: By default, print() ends
with a newline (end="\n"). Setting end="" suppresses the newline, so
"Hello" and "World" print on the same line.
41. Which of the following statements about Python is true? 42. Python uses semicolons to end statements. 43. Python uses indentation to define code blocks. 44. Python does not support object-oriented programming. 45. Python is a compiled language only. Answer: B. Python uses inden-
tation to define code blocks. Explanation: Python relies on consistent
indentation (usually 4 spaces) to group statements; braces are not
used.
46. What is the result of print(10 // 3)?
50. 3.0 Answer: A. 3 Explanation: Floor division (//) returns the integer
part of the quotient, discarding the remainder.
51. What does the range(2, 8, 2) function generate?
55. 2, 4, 6, 8, 10 Answer: B. 2, 4, 6 Explanation: Start = 2, stop = 8
(excluded), step = 2. So numbers are 2, 4, 6.
44. Which of the following is used to create an empty list? A) list() B)
45. Both A and B 46. None Answer: C. Both A and B Explanation: [] is the literal syntax;
list() is the constructor. Both produce an empty list.
47. Which method removes an item from a list by its value? A)pop()
B)remove() C) delete() D) del Answer: B. remove() Explanation:
remove(x) deletes the first occurrence of value x.pop() removes by in-
dex.
48. What is the output of print(3 ** 2 ** 3)?
52. 27 Answer: A. 512 Explanation: Exponentiation operators are evalu-
ated from right to left. 2 ** 3 = 8, then 3 ** 8 = 6561. Wait: careful: 3 ** (
** 3) = 3 ** 8 = 6561. But typical exponentiation precedence: