Q: What are the three types of errors you will encounter while debugging?
- Syntax errors, exceptions, and comment errors
- Exceptions, logic errors, iterative errors
- Syntax errors, logic errors, and exceptions
- Logic errors, comment errors, and iterative errors
Q: The purpose of the following code is to print the characters in a
device ID. Run this code, analyze its output, and then debug it. (If you want
to undo your changes to the code, you can click the Reset button.)
Coursera
img
What is the error related to?
Coursera
img
- A misspelled variable
- A missing colon (:)
- A missing quotation mark (“)
- A missing double equals sign (==)
Q: Why might you use print statements when debugging code?
- To prevent errors from occurring
- To create error messages
- To identify which sections of the code are working properly
- To add missing syntax to the code
Q: What does the following code do?
logins = "pwashing jhill
tshah"
usernames = logins.split()
- Removes the last username in the logins variable and stores the string in the usernames variable
- Removes the blank spaces that split the usernames in the variable logins and stores the string in the variable usernames
- Splits a string variable called logins into single characters
- Splits a string variable called logins into a list of strings and stores it in the variable usernames
Q: What is the process of converting data into a more readable format?
- Slicing
- Debugging
- Splitting
- Parsing
Q: What does the following code do?
read_text = text.read()
- Reads the string text and stores it the file read_text
- Replaces the contents of the file read_text with the contents of the file text
- Reads the text variable, which contains a file, and stores it as a string in read_text
- Splits the text variable, which contains a string, and stores it as a list in read_text
Q: You want to check for unusual login activity. Specifically, you want
to read a log file that contains information on each login attempt, including
whether it failed or was successful. You should then parse the data into a
logins list, and then you should separate all failed log entries into a
separate failed_logins list. If you want to automate this through Python, what
would be part of your code? Select three answers.
- A for loop to iterate through all items in the logins list
- A split() function to split the login information into a list
- An if statement to check if a login attempt failed
- A counter variable to keep track of the number of failed logins
Q: The purpose of the following code is to iterate through a list and
print a warning message if it finds “user3” in the list. Run this code, analyze
its output, and debug it. (If you want to undo your changes to the code, you
can click the Reset button.)
Coursera
img
- Change “user3” to “user1” in the conditional.
- Change the != operator to the == operator in the conditional.
- Change “user3” to “user2” in the conditional.
- Change the indentation so that the line that prints the warning is not indented.
Q: When debugging code, what are effective ways to determine which
sections of code are working properly? Select all that apply.
- Use a debugger
- Add print statements
- Delete blank lines from the code
- Add comments in the code
Q: What does the following code do?
with open("logs.txt",
"r") as file:
- It opens a file called “logs.txt” in write mode and stores it in a variable called file.
- It opens a file called “logs.txt” in read mode and stores it in a variable called file.
- It copies a file called “r” into a new file “logs.txt”.
- It copies a file called “logs.txt” into a new file “r”.
Q: You’ve read a log file into the variable file_text. The file_text
variable contains a string of 50 usernames of employees at your company. In
order to pass it into a function that checks the login count of each user, the
string should be divided into a list of separate usernames. How do you convert
this string into a list and store it in a variable usernames?
- usernames = split(usernames, file_text)
- usernames = file_text.split()
- usernames = usernames.split(file_text)
- file_text.split() as usernames
Q: After you’ve opened a log file as file, which line of code will help
you read the file into a variable called text?
- text = read(file)
- text = read(file, “r”)
- text.read(file)
- text = file.read()
Q: You want to check for unusual login activity. Specifically, you want
to check if there were more than three failed login attempts in the last 10
minutes by the last user who logged in. If you want to automate this through
Python, what would be part of your code? Select three answers.
- An if statement that checks if there were more than three failed login attempts
- A counter variable that increments when a failed login is detected
- A for loop that iterates through the list of logins
- A line of code that reassigns a counter variable to 0 if there is a failed login attempt
Q: What is debugging?
- The practice of identifying and fixing errors in code.
- The practice of calling a function from multiple places in a larger program
- The practice of improving code readability.
- The practice of improving code efficiency.
Q: You did not define a function before calling it. What type of error
is this?
- Index out of bounds
- Syntax error
- Logic error
- Exception
Q: The logins variable is a string containing 20 device IDs. The device
IDs are separated by spaces. In order to pass it into a function that checks
the login count of each device, the string should be divided into a list of
separate IDs. How do you convert this string into a list and store it in a
device_ids variable?
- device_ids = split(device_ids, logins)
- device_ids = logins.split()
- logins.split() as device_ids
- device_ids = device_ids.split(logins)
Q: Fill in the blank: If you use the .split() method to convert a
string into a list so that it can be read more easily, this would be an example
of _____.
- slicing
- dividing
- parsing (CORRECT)
- debugging
Q: What does the following code do?
new_format = old_format.read()
- Inserts the string stored in the new_format variable into the file stored in the old_format variable
- Reads the old_format variable, which contains a file, and stores it as a string in new_format
- Prints the contents of old_format
- Detects certain text patterns in old_format
Q: The purpose of the following code is to search a list. Run this
code, analyze its output, and then debug it. (If you want to undo your changes
to the code, you can click the Reset button.)
Coursera
img
What is the error related to?
Coursera
img
- A missing colon (:)
- A missing comma (,)
- A missing quotation mark (“)
- A misspelled variable
Q: Which of these functions or arguments should you include in a with
statement if you want Python to open a file called access.txt so that it can be
read? Select three answers.
- “access.txt”
- read()
- “r”
- open()
Q: You included username_list[10] in your code, but username_list only
contains five elements. What type of error is this?
- Name error
- Exception
- Syntax error
- Logic error
Q: If you know there is a logic error somewhere inside a function, how
can you figure out the exact location?
- Delete the function from the program
- Move the function to another location
- Place print statements in and around the function
- Write comments in and around the function
Q: You want to check if a device is running a particular operating
system that needs updates. Devices that contain a substring of “i71” in their
device ID are running this operating system. First, you want to read in a log
file that contains the device ID for all devices and convert it into a string.
You should then parse this string into a devices list. Then, you should
separate all device IDs that contain the substring “i71” into a separate list
called updates_list. If you want to automate this through Python, what would be
part of your code? Select three answers.
- A counter variable to keep track of the number of devices containing the substring “i71”
- A split() function to split the string containing the information in the log file into a devices list
- An if statement that checks if elements in devices contain the substring “i71”
- A for loop to iterate through all items in the devices list