Docsity

WGU E010 FoUndations oF ProGramminG (Python) PracticE QUEstions sEction 1: Variab, Exams of Programming Languages

Toggle sidebar
bg1
WGU E010 FoUndations oF
ProGramminG (Python) PracticE
QUEstions sEction 1: VariablEs,
data tyPEs, and basic syntax
(QUEstions 1–15)……
1. What is the output of print(type(10 / 2)) in Python?
A. <class 'int'>
B. <class 'float'>
C. <class 'double'>
D. <class 'str'>
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 <class 'float'> .
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?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download WGU E010 FoUndations oF ProGramminG (Python) PracticE QUEstions sEction 1: Variab and more Exams Programming Languages in PDF only on Docsity!

WGU E010 FoUndations oF

ProGramminG (Python) PracticE

QUEstions sEction 1: VariablEs,

data tyPEs, and basic syntax

(QUEstions 1–15)……

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?

A. 2.

B. 2

C. 2.

D. 1

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)

  1. Which type of loop is best used when the number of iterations is unknown?** A. for loop B. while loop C. nested loop D. for-each loop Answer: B Rationale: A while loop runs until a condition becomes False, making it ideal when the number of iterations is not known in advance. 17. What does the break statement do in a loop?

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)

  1. What is the output of the following code?** python my_list = [1, 2, 3] my_list.append(4) print(my_list) A. [1, 2, 3] B. [1, 2, 3, 4] C. [4, 1, 2, 3] D. [1, 2, 3, 4, 4] Answer: B Rationale: The append() method adds an element to the end of a list. Calling my_list.append(4) adds 4 to the end, resulting in [1, 2, 3, 4]. 32. How do you access the value associated with the key 'name' in the dictionary person = {'name': 'Alice', 'age': 30}? A. person[0] B. person.name

A. {1, 2, 2, 3}

B. {1, 2, 3}

C. [1, 2, 3]

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)

  1. Write a complete function** calculate_discount(price, discount_percent) that calculates and returns the final price after applying a discount percentage. For example, calculate_discount(75, 20) should return 60.0. A. return price * discount_percent B. return price - discount_percent C. return price * (1 - discount_percent / 100) D. return price / discount_percent Answer: C Rationale: Convert the discount percentage to a decimal by dividing by 100. Subtract the discount from 1 to find the remaining price percentage. Multiply the original price by the remaining percentage. For example, calculate_discount(75, 20) returns 75 * (1 - 20/100) = 75
    • 0.8 = 60.. 42. What is the primary advantage of an integrated terminal in a development environment? A. Faster code execution speed B. Access to additional Python libraries C. Automatic syntax error detection D. Eliminates need to switch between applications Answer: D Rationale: An integrated terminal is built into a development environment or code editor. Its main advantage is that the developer can write code and run terminal commands in the same application, avoiding switching back and forth. 43. A dictionary prices = {'apple': 1.50, 'banana': 0.75, 'orange': 2.00} needs to have all values increased by 0.25. Which code correctly accomplishes this task? A. for value in prices: value = value + 0. B. for item in prices: prices[item] += 0. C. prices.values() + 0.

A. 14

B. 10

C. 11

D. 7

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.