Docsity

WGU Foundations of Programming (Python) Final Examination, Exams of Network Programming

Toggle sidebar
bg1
Page 1 of 20
WGU E010 FOUNDATIONS OF
PROGRAMMING (PYTHON) OA FINAL
EXAM
QUESTIONS AND CORRECT ANSWERS
WITH DETAILED EXPLANATIONS
CURRENT TESTING
Data Types & Type Conversion
1. What is the output of the following code?
python print(type(10 / 2))
A) <class 'int'>
B) <class 'float'>
C) <class 'double'>
D) <class 'str'>
Answer: B. <class 'float'>
Explanation: In Python, the / operator always returns a float,
even when the two numbers divide evenly. 10 / 2 evaluates to
5.0, which is a float.
2. Which of the following data types is immutable?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download WGU Foundations of Programming (Python) Final Examination and more Exams Network Programming in PDF only on Docsity!

WGU E010 FOUNDATIONS OF

PROGRAMMING (PYTHON) OA FINAL

EXAM

QUESTIONS AND CORRECT ANSWERS

WITH DETAILED EXPLANATIONS

CURRENT TESTING

Data Types & Type Conversion

1. What is the output of the following code? python print(type(10 / 2)) A) B) C) D) Answer: B. Explanation: In Python, the / operator always returns a float, even when the two numbers divide evenly. 10 / 2 evaluates to 5.0, which is a float. 2. Which of the following data types is immutable?

A) list B) dict C) set D) tuple Answer: D. tuple Explanation: Tuples cannot be changed after they are created. Lists, dictionaries, and sets are mutable.

3. What is the result of print(10 // 3)? A) 3.333… B) 3 C) 4 D) 3. Answer: B. 3 Explanation: The // operator performs floor division (integer division). It returns the quotient without the remainder, so 10 // 3 evaluates to 3. 4. What is the output of print(2 + 3 * 4)? A) 20 B) 14 C) 24 D) 10 Answer: B. 14 Explanation: Multiplication has higher precedence than addition. First, 3 * 4 = 12, then 2 + 12 = 14. 5. What does the input() function return when a user types "42"? A) int

8. Given x = "Hello" , what does print(x[1]) output? A) H B) e C) l D) o Answer: B. e Explanation: Strings are zero-indexed. Index 0 → H, index 1 → e. 9. What is the output of print("Python"[2:5])? A) Pyt B) tho C) yth D) hon Answer: B. tho Explanation: Slicing [2:5] takes characters from index 2 up to (but not including) index 5. Indices: P0 y1 t2 h3 o4 n5 → slice "tho". 10. What does print(x[-1]) output if x = [1, 2, 3]? A) 2 B) 2 C) 2 D) Error 2 Answer: CExplanation: Negative indexing starts from the end of the sequence. - 1 refers to the last element. Error! Bookmark not defined. 11. How do you write a single-line comment in Python?

A) //

B)

C)

D) """

Answer: B. # Explanation: The hash symbol # begins a comment. Everything after it on that line is ignored by the interpreter. Collections: Lists, Dictionaries & Tuples

12. What does the append() method do when called on a list? A) Removes the last item B) Adds an element to the end of the list C) Sorts the list D) Replaces the entire list Answer: B. Adds an element to the end of the list Explanation: append() adds a single element to the end of a list, increasing the length of the list by one. 13. Which Python structure stores data as key-value pairs? A) list B) tuple C) dictionary D) array Answer: C. dictionary Explanation: Dictionaries map keys to values ({key: value}). They are mutable and unordered.

C) [0, 1, 2, 3]

D) (0, 1, 2)

Answer: A. [0, 1, 2] Explanation: range(3) generates a sequence of integers starting from 0 up to (but not including) 3. In Python 3, range() returns an iterable, but when printed or converted to a list, it produces [0, 1, 2]. Loops & Conditionals

17. Which loop is most appropriate when the number of iterations is not known beforehand? A) for loop B) while loop C) nested loop D) foreach loop Answer: B. while loop Explanation: A while loop continues as long as a condition remains true. It is ideal when you do not know how many times the loop should run. 18. What does the break statement do? A) Skips one iteration and continues B) Exits the loop entirely C) Restarts the loop D) Raises an exception Answer: B. Exits the loop entirely Explanation: break immediately terminates the nearest enclosing loop (for or while). Program execution resumes after the loop.

19. Which keyword is used to define a function in Python? A) function B) def C) define D) func Answer: B. def Explanation: Functions are defined using the def keyword, followed by the function name and parentheses containing optional parameters. 20. What is the output of the following? python def func(x, lst=[]): lst.append(x) return lst print(func(1)) print(func(2)) A) [1] then [2] B) [1] then [1, 2] C) [1] then [2, 2] D) Error Answer: B. [1] then [1, 2] Explanation: Default mutable arguments (like lst=[]) are created only once when the function is defined. Therefore, the same list is reused across all calls, leading to accumulated modifications. **Error Handling & Exceptions

  1. Which keyword is used to handle exceptions in Python?**

Explanation: The ** operator raises the left operand to the power of the right operand. 2³ = 8. Object-Oriented Concepts

24. Which keyword is used to define a class in Python? A) function B) class C) object D) struct Answer: B. class Explanation: Classes are defined using the class keyword, followed by the class name and a colon. The class body contains methods and attributes. **List & String Operations

  1. What is the output of the following code?** python sports = ["basketball", "baseball", "football", "soccer", "tennis"] last = sports[-3:] print(last) A) ['basketball', 'baseball', 'football'] B) ['baseball', 'football', 'soccer'] C) ['football', 'soccer', 'tennis'] D) ['sports', 'list', 'slice'] Answer: C. ['football', 'soccer', 'tennis']

Explanation: Slicing with - 3: takes the last three items of the list. Indices: ... football(-3), soccer(-2), tennis(-1).

26. What does the range(3) function produce? A) 0 1 2 B) 1 2 3 C) 0 1 2 3 D) 1 2 3 4 Answer: A. 0 1 2 Explanation: range(3) generates numbers starting at 0 and stopping before 3. In a loop, it would yield 0, 1, and 2. 27. What is the output of the following code? python print("Python"[1:4]) A) Pyt B) yth C) ytho D) hon Answer: B. yth Explanation: [1:4] takes characters from index 1 up to but not including index 4. P0 y1 t2 h3 o4 → "yth". **Advanced/Exam-Style Questions

  1. Which of the following statements correctly describes how** Python executes code? A) Python compiles code into machine language before execution.

More Questions

31. What is the output of the following code? python print(type("Hello" * 3)) A) B) C) D) Answer: C. Explanation: Multiplying a string by an integer repeats the string. The result is still a string. 32. Which of the following is used to convert a number to a string in Python? A) to_string() B) str() C) int() D) toString() Answer: B. str() Explanation: The str() function converts its argument into a string. int() converts to an integer. 33. What is the output of the following code? python x = [1, 2, 3, 4] print(x[1:3])

A) [1, 2, 3]

B) [2, 3]

C) [2, 3, 4]

D) [1, 2, 3, 4]

Answer: B. [2, 3] Explanation: [1:3] takes elements at index 1 and 2 (3 is excluded). So elements 2 and 3 are selected.

34. Which of the following will raise an error? A) int(3.14) B) float(5) C) int("3.14") D) float("5.0") Answer: C. int("3.14") Explanation: The string must represent a valid integer literal. "3.14" cannot be converted directly to an integer because of the decimal point. 35. What is the output of the following code? python print("Hello" + "World") A) HelloWorld B) Hello World C) Hello+World D) "Hello" + "World" Answer: A. HelloWorld

B) function square(x) { return x * x; } C) def square(x) { return x * x } D) square(x): return x * x Answer: A. def square(x): return x * x Explanation: Python uses the def keyword, a colon, and indentation to define functions. Braces are not used.

39. What is the output of the following code? python print("Hello\nWorld") A) Hello\nWorld B) Hello World C) Hello (newline) World D) HelloWorld Answer: C. Hello (newline) World Explanation: The escape sequence \n inserts a newline character. The output will appear on two lines. 40. Which of the following is a valid way to check if a variable num is even? A) if num / 2 == 0 B) if num % 2 == 0 C) if num // 2 == 0 D) if num ** 2 == 0 Answer: B. if num % 2 == 0

Explanation: The modulus operator % returns the remainder of division. If the remainder when dividing by 2 is 0, the number is even.

41. What is the output of the following code? python print(10 > 5 and 5 > 10) A) True B) False C) Error D) None Answer: B. False Explanation: The and operator returns True only if both conditions are True. Here, 10 > 5 is True, but 5 > 10 is False, so the overall expression is False. 42. Which of the following correctly reads a line of text from the user and strips the trailing newline? A) input().strip() B) input().split() C) input().read() D) input().stripnewline() Answer: A. input().strip() Explanation: input() returns the user’s input as a string, including a trailing newline. The .strip() method removes leading/trailing whitespace, including the newline character. 43. What is the output of the following code? python print(5 * "Hi")

Explanation: The len() function returns the number of characters in a string. "Python" has 6 letters.

46. Which of the following is a correct way to create an empty set in Python? A) set() B) {} C) [] D) () Answer: A. set() Explanation: {} creates an empty dictionary, not a set. The correct way to create an empty set is with set(). 47. What is the output of the following code? python print(3 + 4 * 5) A) 23 B) 35 C) 24 D) 27 Answer: A. 23 Explanation: Multiplication has higher precedence than addition. First, 4 * 5 = 20, then 3 + 20 = 23. 48. Which of the following functions will return the length of a list? A) count() B) size() C) len() D) length() Answer: C. len()

Explanation: The built-in len() function returns the number of items in a container (list, tuple, string, etc.).

49. What is the output of the following code? python print(4 ** 0.5) A) 2 B) 2. C) 16 D) 16. Answer: B. 2. Explanation: Raising a number to the power of 0.5 calculates the square root. The result is a floating-point number (2.0). 50. Which of the following is a correct way to combine two lists a and b into a new list? A) a.add(b) B) a + b C) a.append(b) D) a.extend(b) Answer: B. a + b Explanation: The + operator concatenates two lists and returns a new list. append() adds the entire list as a single element, and extend() modifies the original list in place.