Docsity

WGU E010 Foundations of Programming (Python): OA Practice Exam, Exams of Programming Languages

Toggle sidebar
bg1
Page 1 of 30
WGU E010 FOUNDATIONS OF
PROGRAMMING (PYTHON) OA
PRACTICE EXAM COMPLETE
QUESTIONS AND CORRECT ANSWERS
WITH DETAILED EXPLANATIONS
Section 1: Python Fundamentals & Syntax (Questions 1–15)
1. What is the output of print(type(3.14))?
A) <class 'int'>
B) <class 'float'>
C) <class 'str'>
D) <class 'decimal'>
Answer: B. <class 'float'>
Explanation: type() returns the class of the object. 3.14 is a floating-point number, so the
output indicates a float class.
2. Which operator is used for exponentiation (power) in Python?
A) ^
B) **
C) *
D) //
Answer: B. **
Explanation: ** is the exponentiation operator (e.g., 2 ** 3 equals 8). The ^ operator
is used for bitwise XOR.
3. What is the result of 17 // 5 in Python?
A) 3.4
B) 3
C) 4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download WGU E010 Foundations of Programming (Python): OA Practice Exam and more Exams Programming Languages in PDF only on Docsity!

WGU E010 FOUNDATIONS OF

PROGRAMMING (PYTHON) OA

PRACTICE EXAM COMPLETE

QUESTIONS AND CORRECT ANSWERS

WITH DETAILED EXPLANATIONS

Section 1: Python Fundamentals & Syntax (Questions 1–15)

  1. What is the output of print(type(3.14))?

A)

B)

C)

D)

Answer: B. Explanation: type() returns the class of the object. 3.14 is a floating-point number, so the output indicates a float class.

  1. Which operator is used for exponentiation (power) in Python?

A) ^

B) **

C) *

D) //

Answer: B. ** Explanation: ** is the exponentiation operator (e.g., 2 ** 3 equals 8). The ^ operator is used for bitwise XOR.

  1. What is the result of 17 // 5 in Python?

A) 3.

B) 3

C) 4

D) 3.

Answer: B. 3 Explanation: // is the floor division operator; it returns the integer quotient, discarding the remainder (17 // 5 = 3).

  1. What is the correct output of the following Python code? print("Hello, World!") A) Hello, World! B) Hello, World!

C) print("Hello, World!")

D) An error occurs

Answer: A. Hello, World! Explanation: The print() function outputs the string literal to the console without the quotation marks. The quotes are used only to denote the string.

  1. Which of the following is a valid variable name in Python?

A) 2_variable

B) my-variable

C) _myVariable

D) my variable

Answer: C. _myVariable Explanation: Variable names can start with a letter or underscore, cannot start with a digit, and cannot contain spaces or hyphens. Underscore is allowed.

  1. In Python, which symbol is used for single-line comments?

A) //

B)

C) /*

D)

  1. What is the value of len("Python")?

A) 5

B) 6

C) 7

D) 4

Answer: B. 6 Explanation: len() returns the number of characters in the string. "Python" has 6 characters: P, y, t, h, o, n.

  1. Which statement reads input from the user as a string?

A) input("Enter name: ")

B) read("Enter name: ")

C) get_input("Enter name: ")

D) console.read("Enter name: ")

Answer: A. input("Enter name: ") Explanation: The built-in input() function displays the optional prompt and returns the user's input as a string.

  1. What is the output of print("The answer is " + 42)?

A) The answer is 42

B) The answer is 42 (with no quotes)

C) TypeError: can only concatenate str (not "int") to str

D) The answer is 42.

Answer: C. TypeError: can only concatenate str (not "int") to str Explanation: Python does not automatically convert numbers to strings when using the + operator on a string and an integer. Use str(42) to convert.

  1. Which of the following is a valid Boolean value in Python?

A) true

B) TRUE

C) True

D) 1 (but True is the proper Boolean literal)

Answer: C. True

Explanation: Python uses capitalized True and False as Boolean literals. true and TRUE are not recognized as Boolean values (they would be variable names).

  1. What does bool(0) return?

A) True

B) False

C) 0

D) None

Answer: B. False Explanation: In Python, numeric values: 0, 0.0, and complex zero evaluate to False in a Boolean context. All non-zero numbers evaluate to True. Section 2: Control Flow (if, elif, else, loops) (Questions 16–30)

  1. What is the output of the following code? python x = 10 if x > 5: print("A") elif x > 7: print("B") else: print("C")

A) A

B) B

C) C

D) No output

Answer: A. A Explanation: The first condition x > 5 is True (10 > 5), so the block under if executes and prints "A". The elif and else blocks are skipped.

  1. Which logical operator would be used to check if both conditions a > 5 and b < 10 are True?

A) or

B) and

C) not

Answer: C. break Explanation: The break statement terminates the innermost loop (while or for) immediately and resumes execution after the loop.

  1. What is the output of the following code? python for i in range(1, 6, 2): print(i, end=" ")

A) 1 2 3 4 5

B) 1 3 5

C) 2 4 6

D) 1 3 5 7

Answer: B. 1 3 5 Explanation: range(1, 6, 2) starts at 1, goes up to but not including 6, with a step of 2. So the values are 1, 3, and 5.

  1. Which of the following correctly checks if a number n is between 10 and 20 (inclusive)?

A) 10 <= n <= 20

B) n >= 10 and n <= 20

C) Both A and B

D) n in range(10,20)

Answer: C. Both A and B Explanation: Python supports chained comparisons: 10 <= n <= 20 is equivalent to n >= 10 and n <=

  1. range(10,20) excludes 20, so it's not inclusive of 20.

  2. What is the output of print(not (5 > 3) or (2 == 2))?

A) False

B) True

C) None

D) Error

Answer: B. True Explanation: (5 > 3) is True, so not True is False. (2 == 2) is True. False or True evaluates to True.

  1. Which loop is guaranteed to execute at least once?

A) for loop

B) while loop

C) do-while loop (Python does not have one)

D) none of the above

Answer: D. none of the above Explanation: Python has no do-while loop. Both for and while loops check conditions before the first iteration, so they may execute zero times.

  1. What is the value of x after the following? python x = 5 if x: x = 10 else: x = 0

A) 5

B) 10

C) 0

D) None

Answer: B. 10 Explanation: The condition if x: treats x as a Boolean. Non-zero numbers are True, so the if branch executes, setting x to 10.

  1. Which of the following statements will skip the current iteration and continue with the next?

A) break

B) continue

C) pass

D) skip

Answer: B. continue Explanation: continue jumps to the next iteration of the loop, skipping any remaining code in the current iteration. break exits the loop entirely.

Explanation: Both correctly represent the condition. range(18,66) includes 65, so age not in range(18,66) also works but is less common for numeric comparisons.

  1. What is the output of the following nested loop? python for i in range(2): for j in range(2): print(i*j, end=" ")

A) 0 0 0 1

B) 0 1 0 1

C) 0 0 0 0

D) 0 1 1 2

Answer: A. 0 0 0 1 Explanation: Outer loop i=0, inner loop j=0 → 0; j=1 → 0; outer i=1, inner j=0 → 0; j=1 →

  1. So output: 0 0 0 1. Section 3: Functions & Scope (Questions 31–45)
  2. What is the output of the following code? python def greet(): return "Hi" print(greet())

A) greet

B) Hi

C) None

D) Error

Answer: B. Hi Explanation: The function greet() returns the string "Hi". The print function outputs that returned value.

  1. Which keyword is used to define a function in Python?

A) func

B) define

C) def

D) function

Answer: C. def Explanation: The def keyword is followed by the function name, parentheses, and a colon. The function body is indented.

  1. What is the output of this code? python def add(a, b): return a + b result = add(3, 4) print(result)

A) 7

B) 12

C) add(3,4)

D) Error

Answer: A. 7 Explanation: The function takes two parameters and returns their sum. add(3,4) returns 7, which is assigned to result and printed.

  1. A variable defined inside a function is called a __________ variable. A) global

B) local

C) static

D) constant

Answer: B. local Explanation: Variables declared inside a function have local scope; they are not accessible outside that function unless returned or passed.

  1. What is the output? python x = 10 def change(): x = 20 change() print(x)

A) 10

B) 20

C) None

  1. What is the output? python def func(x, y=2, z=3): return x + y + z print(func(1, z=4))

A) 6

B) 7

C) 8

D) Error

Answer: B. 7 Explanation: x=1 (positional), y uses default 2, z is overridden to 4. Sum = 1+2+4 = 7.

  1. Which of the following is a valid way to call a function defined as def multiply(a, b):?

A) multiply(2)(3)

B) multiply(2, 3)

C) multiply(a=2, b=3)

D) Both B and C

Answer: D. Both B and C Explanation: You can call using positional arguments (multiply(2,3)) or keyword arguments (multiply(a=2,b=3)).

  1. What is the scope of a variable defined inside a function?

A) Global

B) Local to that function

C) Enclosing function only if nested

D) Both B and C

Answer: D. Both B and C Explanation: A variable defined inside a function is local to that function. If the function is nested inside another function, it is local to the inner function and can be accessed by the inner function (but not by the outer function).

  1. What will the following code print? python def outer(): x = "local" def inner(): nonlocal x x = "nonlocal" inner() print(x) outer()

A) local

B) nonlocal

C) Error

D) None

Answer: B. nonlocal Explanation: The nonlocal keyword allows the inner function to modify the variable x from the enclosing (outer) function's scope. After inner() executes, x becomes "nonlocal".

  1. What is the output of print(round(3.14159, 2))?

A) 3.

B) 3.

C) 3.141 D) 3.

Answer: A. 3. Explanation: The round() function rounds a number to a specified number of decimal places. round(3.14159, 2) gives 3.14.

  1. Which function returns the length of a list?

A) size()

B) count()

C) len()

D) length()

Answer: C. len() Explanation: len() is a built-in function that returns the number of items in a container (list, tuple, string, dictionary, etc.).

  1. What is the purpose of the pass statement?

A) It terminates the program

B) It is a placeholder that does nothing, used where syntax requires a statement

C) It skips the next iteration of a loop

D) It raises an exception

Answer: B. It is a placeholder that does nothing, used where syntax requires a statement Explanation: pass is a null operation. It is often used in empty function or class definitions, or in if blocks that will be filled later.

Answer: A. [1, 2, 3, 4, 5] Explanation: The + operator concatenates two lists, producing a new list containing all elements from both.

  1. What will [1, 2, 3] * 2 evaluate to?

A) [1, 2, 3, 1, 2, 3]

B) [2, 4, 6]

C) [1, 2, 3, 2]

D) Error

Answer: A. [1, 2, 3, 1, 2, 3] Explanation: Repetition operator * repeats the list the given number of times, concatenating the copies.

  1. Which method adds an element to the end of a list?

A) append()

B) add()

C) insert()

D) extend()

Answer: A. append() Explanation: list.append(x) adds x to the end of the list. extend() adds all elements of an iterable, insert() adds at a given index.

  1. What is the output of the following code? python my_list = [1, 2, 3, 4] my_list.pop(2) print(my_list)

A) [1, 2, 4]

B) [1, 2, 3]

C) [1, 3, 4]

D) [2, 3, 4]

Answer: A. [1, 2, 4] Explanation: pop(2) removes and returns the element at index 2 (the third element, which is 3). The remaining list is [1, 2, 4].

  1. Which of the following creates a tuple?

A) (1, 2, 3)

B) [1, 2, 3]

C) {1, 2, 3}

D) (1)

Answer: A. (1, 2, 3) Explanation: Tuples are defined with parentheses. A single-element tuple requires a comma: (1,). Without the comma, it's just parentheses around a value.

  1. Why might you choose a tuple over a list?

A) Tuples are mutable

B) Tuples are immutable, so they can be used as dictionary keys

C) Tuples are faster for all operations

D) Tuples can hold only one data type

Answer: B. Tuples are immutable, so they can be used as dictionary keys Explanation: Immutability allows tuples to be hashable, making them suitable as keys in dictionaries. Lists are mutable and cannot be hashed.

  1. What is the output of [1, 2, 3].remove(2) then printing the list?

A) [1, 3]

B) [1, 2, 3]

C) [2, 3]

D) Error

Answer: A. [1, 3] Explanation: remove(2) removes the first occurrence of the value 2. The list becomes [1, 3]. Note: remove() modifies the list in place and returns None.

  1. How do you get a slice containing elements from index 1 to 3 (exclusive of 3) of list L?

A) L[1:3]

B) L[1:4]

C) L[2:4]

D) L[1:2]

Answer: A. L[1:3]

print(a)

A) [1, 2]

B) [1, 2, 3]

C) [3, 2, 1]

D) [1, 2, 3, 3]

Answer: B. [1, 2, 3] Explanation: b = a makes b reference the same list object as a. Modifying the list via b also affects a. Section 5: Dictionaries and Sets (Questions 61–75)

  1. Which of the following creates an empty dictionary?

A) {}

B) set()

C) []

D) ()

Answer: A. {} Explanation: Curly braces create a dictionary. An empty set is created with set(), not with {} because {} is an empty dictionary.

  1. How do you access the value associated with key "name" in dictionary d?

A) d["name"]

B) d(name)

C) d->name

D) d.get(name) (but the primary method is d["name"])

Answer: A. d["name"] Explanation: The square bracket notation with the key returns the value. get() is another method that avoids KeyError.

  1. What will len({"a":1, "b":2, "c":3}) return?

A) 3

B) 6

C) 2

D) Error

Answer: A. 3 Explanation: len() returns the number of key-value pairs in the dictionary, which is 3.

  1. Which method adds a new key-value pair to a dictionary?

A) add()

B) append()

C) update() or assignment d[key] = value

D) insert()

Answer: C. update() or assignment d[key] = value Explanation: d[key] = value will add a new key-value pair if the key does not exist, or update it if it does. update() can add multiple pairs.

  1. What is the output of the following? python d = {"x": 1, "y": 2} print(d.get("z", 0))

A) 0

B) None

C) "z"

D) Error

Answer: A. 0 Explanation: get(key, default) returns the value for the key if it exists, otherwise returns the default value (0 in this case). No error is raised.

  1. Which of the following is a valid set literal?

A) [1, 2, 3]

B) (1, 2, 3)

C) {1, 2, 3}

D) {1:2, 3:4}

Answer: C. {1, 2, 3} Explanation: Sets are defined with curly braces and comma-separated values, without colons. {1:2, 3:4} is a dictionary.

  1. How do you add an element to a set s?