Docsity

WGU E010-FOUNDATIONS OF PROGRAMMING (PYTHON) OBJECTIVE ASSESSMENT, Exams of Advanced Education

Toggle sidebar
bg1
WGU E010-FOUNDATIONS OF PROGRAMMING
(PYTHON) OBJECTIVE ASSESSMENT PRACTICE
QUESTIONS WITH VERIFIED SOLUTIONS +
RATIONALES
1. Which of the following is a valid variable name in Python?
A. 2nd_name
B. my-name
C. my_name
D. class
Answer: C
Rationale: Python variable names may contain letters, numbers, and underscores,
but cannot begin with a digit or contain hyphens, nor can they use reserved
keywords. class is a Python keyword and cannot be used as a variable name.
2. What is the output of print(type(10 / 2))?
A. <class 'int'>
B. <class 'float'>
C. <class 'double'>
D. <class 'str'>
Answer: B
Rationale: In Python 3, the / operator performs true division and always returns a
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download WGU E010-FOUNDATIONS OF PROGRAMMING (PYTHON) OBJECTIVE ASSESSMENT and more Exams Advanced Education in PDF only on Docsity!

WGU E010-FOUNDATIONS OF PROGRAMMING

(PYTHON) OBJECTIVE ASSESSMENT PRACTICE

QUESTIONS WITH VERIFIED SOLUTIONS +

RATIONALES

  1. Which of the following is a valid variable name in Python? A. 2nd_name B. my-name C. my_name D. class Answer : C Rationale: Python variable names may contain letters, numbers, and underscores, but cannot begin with a digit or contain hyphens, nor can they use reserved keywords. class is a Python keyword and cannot be used as a variable name.
  2. What is the output of print(type(10 / 2))? A. B. C. D. Answer : B Rationale: In Python 3, the / operator performs true division and always returns a

float, even when the result is a whole number. 10 / 2 yields 5.0, so the type is float.

  1. What is the output of print(2 ** 3)? A. 6 B. 8 C. 9 D. 5 Answer : B Rationale: ** is the exponentiation operator. 2 ** 3 means 2 to the power of 3, which is 8.
  2. What is the output of print(10 % 3)? A. 3 B. 1 C. 0 D. 3. Answer : B Rationale: % is the modulo operator, returning the remainder. 10 divided by 3 leaves a remainder of 1.

C. /*

D. --

Answer : B Rationale: Python uses # to begin single-line comments. Everything after # on that line is ignored by the interpreter.

  1. What is the output of print("Hello"[1])? A. H B. e C. l D. o Answer : B Rationale: String indexing starts at 0. Index 0 is 'H', index 1 is 'e'.
  2. What is the output of print(len("Python"))? A. 5 B. 6 C. 7 D. 0

Answer : B Rationale: len() returns the number of characters in the string. "Python" has 6 characters.

  1. What is the output of print(bool(0))? A. True B. False C. 0 D. Error Answer : B Rationale: In Python, 0 is falsy. Non-zero numbers are truthy.
  2. What is the output of print(bool(""))? A. True B. False C. None D. Error Answer : B Rationale: An empty string is falsy in Python. Non-empty strings are truthy.
  3. Which of the following is a Boolean value in Python? A. "True"

Answer : B Rationale: "Hello" is a string (str), enclosed in quotes.

  1. Which of the following data types 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 all mutable.
  2. What is the output of print(range(3)) in a list context? A. [1, 2, 3] B. [0, 1, 2] C. [0, 1, 2, 3] D. [3, 2, 1] Answer : B Rationale: range(3) produces the sequence 0, 1, 2 (stopping before 3).
  3. What is the correct way to create a variable named age with the value 25? A. age == 25

B. age = 25 C. int age = 25 D. var age = 25 Answer : B Rationale: The assignment operator = is used to assign a value to a variable. == is comparison, int age is C-style syntax, var is not used in Python.

  1. Which of the following is NOT a Python keyword? A. if B. for C. value D. def Answer : C Rationale: value is a normal identifier, not a reserved keyword. if, for, and def are Python keywords.
  2. What is the output of print(5 > 3 and 2 < 1)? A. True B. False C. None D. Error

B. float C. string D. bool Answer : C Rationale: input() always returns a string, even when the user enters numbers. Conversion (e.g., int()) is required for numeric operations.

  1. What is the output of print("Hello" + " " + "World")? A. HelloWorld B. Hello World C. Hello+World D. Error Answer : B Rationale: The + operator concatenates strings. "Hello" + " " + "World" produces "Hello World".
  2. Which of the following correctly converts the string "10" to an integer? A. str("10") B. float("10") C. int("10") D. bool("10")

Answer : C Rationale: int() converts a compatible string to an integer. str() converts to string, float() to float, bool() to Boolean.

  1. What is the output of print(10 > 5)? A. 10 B. 5 C. True D. False Answer : C Rationale: 10 > 5 is a comparison that evaluates to True.
  2. Which statement about Python dictionaries is correct? A. Dictionary keys must be integers B. Dictionary keys must be unique and immutable C. Dictionaries cannot store string values D. Dictionaries are ordered by value Answer : B Rationale: Dictionary keys must be unique and of an immutable type (e.g., strings, numbers, tuples). Values can be any type. Dictionaries preserve insertion order in Python 3.7+, but are not ordered by value.

C. Error D. "10" Answer : B Rationale: When a string is multiplied by an integer, Python repeats the string. "5"

  • 2 yields "55".
  1. What is the primary purpose of a function in Python? A. To store multiple values B. To group reusable code that performs a specific task C. To define the data type of a variable D. To comment code Answer : B Rationale: Functions group reusable code into a named block that can be called with different inputs to perform a specific task.
  2. Which keyword is used to define a function in Python? A. function B. def C. func D. define

Answer : B Rationale: The def keyword begins a function definition in Python.

  1. What is the output of the following code? python def add(a, b): return a + b print(add(3, 4)) A. 34 B. 7 C. add(3, 4) D. Error Answer : B Rationale: The function add returns the sum of its arguments. add(3, 4) returns 7, which is printed.
  2. What happens if a function has no return statement? A. The function returns 0 B. The function returns None C. The function raises an error D. The function returns an empty string

A. 0

B. 2

C. 3

D. 4

Answer : C Rationale: The loop increments x until x < 3 is false. Starting at 0, x becomes 1, 2,

  1. When x reaches 3, the condition fails and the loop exits, printing 3.
  2. Which statement immediately exits a loop? A. continue B. pass C. break D. return Answer : C Rationale: break immediately terminates the innermost loop. continue skips to the next iteration; pass does nothing; return exits a function.
  3. What is the output of the following code? python for i in range(5): if i == 2:

break print(i) A. 0, 1 B. 0, 1, 2 C. 0, 1, 2, 3, 4 D. 2 Answer : A Rationale: The loop prints i for 0 and 1. When i reaches 2, break exits the loop before printing 2.

  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. Error

python prices = {'apple': 1.50, 'banana': 0.75} for item in prices: prices[item] = prices[item] + 0. print(prices['apple']) A. 1. B. 1. C. 0. D. 1. Answer : B Rationale: The loop iterates over the dictionary's keys. For each key, the value is increased by 0.25. 1.50 + 0.25 = 1.75.

  1. What is the output of the following code? python my_set = {1, 2, 2, 3, 3, 3} print(len(my_set)) A. 6 B. 3

C. 2

D. Error Answer : B Rationale: Sets store unique elements. The set contains 1, 2, and 3, so len() returns 3.

  1. Which tool is used to execute Python code line-by-line interactively within a terminal? A. Text editor B. Python shell (REPL) C. Compiler D. Debugger only Answer : B Rationale: The Python shell (Read-Eval-Print Loop, REPL) allows interactive line- by-line execution. A text editor writes files, a compiler compiles, and a debugger is for troubleshooting.
  2. What is the output of the following code? python def greet(name="World"): return "Hello, " + name