Docsity

WGU D335 Introduction to Programming in Python OA Study Guide | Objective Assessment, Exams of Pathophysiology

Toggle sidebar
bg1
1 | P a g e
WGU D335 Introduction to
Programming in Python OA Study
Guide | Objective Assessment Review
2025
Prepare for WGU D335 Introduction to Programming in
Python Objective Assessment with targeted practice
questions and study materials. Covers essential Python
topics including data types, control flow, loops,
functions, strings, lists, dictionaries, error handling, and
file I/O. Aligned to the OA format and WGU competency
requirements. Organized for efficient review of key
programming concepts and syntax.
1. What is the primary purpose of the input() function in Python?
A. To display output to the console
B. To read user input from the keyboard
C. To convert a string to an integer
D. To write data to a file
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download WGU D335 Introduction to Programming in Python OA Study Guide | Objective Assessment and more Exams Pathophysiology in PDF only on Docsity!

WGU D335 Introduction to

Programming in Python OA Study

Guide | Objective Assessment Review

Prepare for WGU D335 Introduction to Programming in

Python Objective Assessment with targeted practice

questions and study materials. Covers essential Python

topics including data types, control flow, loops,

functions, strings, lists, dictionaries, error handling, and

file I/O. Aligned to the OA format and WGU competency

requirements. Organized for efficient review of key

programming concepts and syntax.

1. What is the primary purpose of the input() function in Python? A. To display output to the console B. To read user input from the keyboard C. To convert a string to an integer D. To write data to a file

B. To read user input from the keyboard The input() function pauses program execution and waits for the user to type something, returning that input as a string. It does not display output (that's print()), convert types (that's int()), or write to files.

2. Which of the following is a valid variable name in Python? A. 2nd_place B. total-score C. user_name D. class C. user_name Variable names cannot start with a digit (A), contain hyphens (B), or be reserved keywords like class (D). Underscores are allowed, making user_name valid. 3. What data type does the expression 5 / 2 produce in Python 3? A. int B. float C. string D. boolean B. float In Python 3, the single forward slash / always performs true division and returns a float, so 5 / 2 yields 2.5. To get an integer, you would use //. 4. What is the output of print(type(3.14))? A. B. C. D. B. The type() function returns the class of the value. Since 3.14 has a decimal point, it is a float, and Python represents it as . There is no double type in Python. 5. Which operator is used for exponentiation in Python? A. ^ B. ** C. // D. % *B. ** The ** operator raises a number to a power, e.g., 2 ** 3 equals 8. The ^ symbol is a bitwise XOR, not exponentiation.

11. What will print("Hello" + "World") output? A. Hello World B. HelloWorld C. Hello+World D. Error B. HelloWorld The + operator concatenates strings without adding a space. To include a space, you would need "Hello " + "World" or use a comma in print(). 12. Which method converts a string to all uppercase letters? A. .upper() B. .uppercase() C. .toUpper() D. .capitalize() A. .upper() The .upper() method returns a copy of the string with all characters converted to uppercase. .capitalize() only capitalizes the first letter. 13. What is the output of len("Python")? A. 5 B. 6 C. 7 D. Error B. 6 The len() function returns the number of characters in a string. "Python" has 6 characters: P, y, t, h, o, n. 14. Which of the following is NOT a valid Python data type? A. int B. float C. char D. bool C. char Python does not have a separate char type; single characters are simply strings of length 1. The other options are all valid Python types. 15. What does the bool data type represent? A. Only integers B. Only strings C. True or False values D. Floating-point numbers C. True or False values The bool type has only two possible values: True and False. It is often used in conditional statements.

16. What is the result of bool(0)? A. True B. False C. 0 D. Error B. False In Python, 0 , 0.0, empty strings, and empty collections are considered "falsy" and evaluate to False when converted to a boolean. 17. Which function converts a value to an integer? A. str() B. float() C. int() D. bool() C. int() The int() function converts a value (like a string or float) to an integer. str() converts to string, float() to float, and bool() to boolean. 18. What is the output of int(3.9)? A. 4 B. 3 C. 3. D. Error B. 3 The int() function truncates the decimal portion rather than rounding. So int(3.9) returns 3 , not 4. 19. What is the purpose of the print() function? A. To read input from the user B. To display output to the console C. To convert data types D. To define a variable B. To display output to the console The print() function outputs its arguments to the console, adding a newline by default. It is the primary way to display information to the user. 20. Which of the following creates a list? A. my_list = (1, 2, 3) B. my_list = [1, 2, 3] C. my_list = {1, 2, 3} D. my_list = <1, 2, 3> B. my_list = [1, 2, 3] Lists are created using square brackets []. Parentheses () create tuples, and curly braces {} create sets or dictionaries.

square brackets, tuples use parentheses, and sets use curly braces with single values.

26. How do you access the value associated with the key "name" in a dictionary called person? A. person["name"] B. person.name C. person(0) D. person{"name"} A. person["name"] Dictionary values are accessed using square brackets with the key inside, like person["name"]. Dot notation is not used for dictionary access. 27. What happens if you try to access a key that does not exist in a dictionary? A. Returns None B. Returns an empty string C. Raises a KeyError D. Creates the key with a default value C. Raises a KeyError Attempting to access a non-existent key in a dictionary raises a KeyError. To avoid this, you can use the .get() method. 28. Which loop is best when you know exactly how many times you want to repeat a block of code? A. while loop B. for loop C. do-while loop D. infinite loop B. for loop A for loop is ideal when the number of iterations is known, often used with range(). A while loop is better when the condition depends on something other than a counter. 29. What does range(5) produce? A. 1, 2, 3, 4, 5 B. 0, 1, 2, 3, 4 C. 0, 1, 2, 3, 4, 5 D. 5, 4, 3, 2, 1 B. 0, 1, 2, 3, 4 The range(5) function generates numbers starting from 0 up to, but not including, 5. So it produces 0, 1, 2, 3, and 4. 30. What is the output of list(range(2, 5))? A. [2, 3, 4, 5]

B. [2, 3, 4]

C. [3, 4, 5]

D. [2, 5]

B. [2, 3, 4]

The range(2, 5) starts at 2 and stops before 5, producing 2, 3, and 4. Converting it to a list gives [2, 3, 4].

31. Which statement is used to exit a loop prematurely? A. continue B. pass C. break D. return C. break The break statement immediately terminates the loop it is inside. continue skips to the next iteration, and pass does nothing. 32. What does the continue statement do in a loop? A. Ends the loop B. Skips the rest of the current iteration and moves to the next C. Restarts the loop from the beginning D. Pauses the loop B. Skips the rest of the current iteration and moves to the next The continue statement stops the current iteration and jumps back to the top of the loop for the next iteration, without executing the remaining code in the loop body. 33. What is the correct syntax for an if statement? A. if x > 5 then: B. if x > 5: C. if (x > 5) D. if x > 5 { B. if x > 5: Python uses a colon at the end of the if condition and does not use parentheses or then. The indented block follows on the next line. 34. Which keyword is used for an alternative condition in an if-else chain? A. else if B. elseif C. elif D. elseif C. elif Python uses elif (short for "else if") to check additional conditions after an initial if. It is not else if or elseif.

40. What is the output of print(10 % 3)? A. 3 B. 1 C. 0 D. 3. B. 1 The modulus operator % returns the remainder of division. 10 divided by 3 is 3 with a remainder of 1, so the result is 1. 41. Which function is used to get the length of a string? A. length() B. size() C. len() D. count() C. len() The len() function returns the number of characters in a string. It is a built-in function, not a method, so it is called as len(string). 4 2. What is the output of "Hello"[1]? A. H B. e C. l D. o B. e String indexing starts at 0, so "Hello"[0] is 'H', "Hello"[1] is 'e', and so on. 43. What does the .lower() method do? A. Converts a string to uppercase B. Converts a string to lowercase C. Capitalizes the first letter D. Removes whitespace B. Converts a string to lowercase The .lower() method returns a copy of the string with all characters converted to lowercase. .upper() does the opposite. 44. How do you check if a string contains only digits? A. .isdigit() B. .isnumeric() C. .isinteger() D. Both A and B D. Both A and B Both .isdigit() and .isnumeric() can check if a string contains only digits, though .isnumeric() is more inclusive of other numeric characters. For basic digit checking, both work.

45. What is the purpose of the try and except blocks? A. To define functions B. To handle exceptions (errors) gracefully C. To create loops D. To import modules B. To handle exceptions (errors) gracefully The try block contains code that might raise an exception, and the except block contains code that runs if an exception occurs, preventing the program from crashing. 46. Which exception is raised when you divide by zero? A. ValueError B. TypeError C. ZeroDivisionError D. ArithmeticError C. ZeroDivisionError Attempting to divide a number by zero raises a ZeroDivisionError. It is a specific type of ArithmeticError. 47. Which exception is raised when you use an undefined variable? A. NameError B. ValueError C. TypeError D. KeyError A. NameError A NameError is raised when you try to use a variable that has not been defined or is out of scope. 48. What is the correct way to open a file for reading in Python? A. open("file.txt", "r") B. open("file.txt", "read") C. open("file.txt", "w") D. open("file.txt") A. open("file.txt", "r") The open() function takes the filename and mode. "r" is for reading, "w" is for writing, and "a" is for appending. If no mode is given, it defaults to "r". 49. What is the purpose of the with statement when opening files? A. It creates a new file B. It automatically closes the file when done C. It encrypts the file D. It reads the file line by line B. It automatically closes the file when done Using with open(...) as f: ensures the file is properly closed after the block of

D. Error B. 3 The len() function returns the number of elements in the tuple. This tuple has 3 elements.

55. How do you add a new key-value pair to a dictionary? A. my_dict.add("key", "value") B. my_dict["key"] = "value" C. my_dict.append("key", "value") D. my_dict.insert("key", "value") B. my_dict["key"] = "value" You add or modify a dictionary entry by assigning a value to a key using square brackets, like my_dict["key"] = "value". There is no .add() or .append() method for dictionaries. 56. Which method returns a list of all keys in a dictionary? A. .keys() B. .values() C. .items() D. .get() A. .keys() The .keys() method returns a view object containing all the keys in the dictionary. .values() returns values, and .items() returns key-value pairs. 57. What is the output of list(range(3))? A. [1, 2, 3] B. [0, 1, 2] C. [0, 1, 2, 3] D. [3] B. [0, 1, 2] range(3) produces 0, 1, 2. Converting to a list gives [0, 1, 2]. 58. What does the pass statement do? A. Skips the current iteration B. Exits the loop C. Does nothing (acts as a placeholder) D. Raises an exception C. Does nothing (acts as a placeholder) The pass statement is a null operation; it does nothing when executed. It is used as a placeholder where code is syntactically required but no action is needed. 59. Which of the following is a correct while loop? A. while x < 5: B. while (x < 5)

C. while x < 5 do: D. while x < 5 { A. while x < 5: The while loop syntax requires the condition followed by a colon, then an indented block. Parentheses are optional, and do or { are not used.

60. What is an infinite loop? A. A loop that never executes B. A loop that runs forever because the condition never becomes false C. A loop that runs exactly 100 times D. A loop with a syntax error B. A loop that runs forever because the condition never becomes false An infinite loop occurs when the loop's condition always evaluates to True, causing the loop to never terminate. This can happen if the loop variable is not updated. 61. What is the output of print("a" in "abc")? A. True B. False C. Error D. None A. True The in operator checks if a substring exists within a string. Since "a" is in "abc", it returns True. 62. What is the output of print(3 in [1, 2, 3])? A. True B. False C. Error D. None A. True The in operator also works with lists, checking if an element exists in the list. Since 3 is in the list, it returns True. 63. Which method removes and returns the last element of a list? A. .remove() B. .pop() C. .delete() D. .discard() B. .pop() The .pop() method removes and returns the last element of a list by default. .remove() removes a specific value, and .delete() is not a method.

69. How do you sort a list in place? A. sorted(my_list) B. my_list.sort() C. my_list.order() D. my_list.arrange() B. my_list.sort() The .sort() method sorts the list in place, modifying the original list. The sorted() function returns a new sorted list without changing the original. 70. What does the sorted() function return? A. It sorts the list in place B. A new sorted list C. The largest element D. The smallest element B. A new sorted list The sorted() function returns a new list containing all items from the original iterable in ascending order, leaving the original unchanged. 71. What is the output of print(bool(""))? A. True B. False C. Error D. None B. False An empty string is considered "falsy" in Python, so bool("") returns False. Any non-empty string returns True. 72. What is the output of print(bool("Hello"))? A. True B. False C. Error D. None A. True A non-empty string is considered "truthy" in Python, so bool("Hello") returns True. 73. Which of the following is a valid float literal? A. 3 B. 3. C. "3.0" D. 3/ B. 3. A float literal must contain a decimal point. 3 is an integer, "3.0" is a string, and 3/ 1 is an expression that evaluates to a float but is not a literal.

74. What is the result of float(5)? A. 5 B. 5. C. "5" D. Error B. 5. The float() function converts an integer to a floating-point number, so float(5) returns 5.0. 75. What is the result of str(5)? A. 5 B. "5" C. 5. D. Error B. "5" The str() function converts a value to a string, so str(5) returns the string "5". 76. What is the result of int("5")? A. 5 B. "5" C. 5. D. Error A. 5 The int() function converts a string of digits to an integer, so int("5") returns 5. 77. What happens if you try int("abc")? A. Returns 0 B. Returns None C. Raises a ValueError D. Raises a TypeError C. Raises a ValueError The int() function cannot convert a non-numeric string, so it raises a ValueError. 78. What is the output of print(1 == 1.0)? A. True B. False C. Error D. None A. True In Python, 1 and 1.0 are considered equal in value, even though they are different types. The == operator compares values, so it returns True. 79. What is the output of print(1 is 1.0)? A. True

84. What is the correct way to write a multi-line string? A. Using single quotes B. Using double quotes C. Using triple quotes D. Using a backslash at the end of each line C. Using triple quotes Triple quotes (""" or ''') allow a string to span multiple lines, preserving line breaks.

85. What does the .strip() method do to a string? A. Removes all spaces B. Removes leading and trailing whitespace C. Converts to uppercase D. Splits the string B. Removes leading and trailing whitespace The .strip() method removes whitespace from the beginning and end of a string, but not from the middle. 86. What does the .split() method do? A. Removes whitespace B. Splits a string into a list of substrings C. Joins a list into a string D. Reverses the string B. Splits a string into a list of substrings The .split() method divides a string into a list based on a delimiter (default is whitespace). 87. What is the output of "a,b,c".split(",")? A. ["a", "b", "c"] B. "a b c" C. ["a,b,c"] D. ("a", "b", "c") A. ["a", "b", "c"] The .split(",") method splits the string at each comma and returns a list of the resulting substrings. 88. What does the .join() method do? A. Splits a string into a list B. Joins elements of an iterable into a single string C. Adds two strings together D. Reverses a string B. Joins elements of an iterable into a single string The .join() method takes an iterable (like a list) and concatenates its elements into a single string, using the string it is called on as the separator.

89. What is the output of "-".join(["a", "b", "c"])? A. "abc" B. "a-b-c" C. ["a", "b", "c"] D. "a b c" B. "a-b-c" The .join() method uses the hyphen as the separator, so it produces "a-b-c". 90. What is the output of print("Hello".replace("l", "L"))? A. HeLLo B. HeLlo C. Hello D. HeLLo A. HeLLo The .replace() method replaces all occurrences of the first argument with the second. So all "l" characters become "L". 91. Which of the following is a mutable data type? A. tuple B. string C. list D. int C. list Lists are mutable, meaning their elements can be changed, added, or removed after creation. Tuples, strings, and integers are immutable. 92. What is the output of my_list = [1, 2, 3]; my_list[0] = 10; print(my_list)? A. [1, 2, 3] B. [10, 2, 3] C. [10, 1, 2, 3] D. Error B. [10, 2, 3] Lists are mutable, so you can change an element by assigning a new value to its index. This changes the first element from 1 to 10. 93. What happens if you try to change an element of a tuple? A. It works the same as a list B. It raises a TypeError C. It raises a ValueError D. It creates a new tuple B. It raises a TypeError Tuples are immutable, so attempting to modify an element raises a TypeError.