Automate Cybersecurity Tasks with Python | Week 4

Week 4 – Python in Practice

Course Name: Automate Cybersecurity Tasks with Python

Course Link: Automate Cybersecurity Tasks with Python

These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Test your knowledge: Python and automation

Q1. Which of the following potential signs of suspicious activity can you track with automated Python programs? Select all that apply.

  • Whether login attempts occurred from IP addresses that are not established work zones
  • Whether several failed login attempts occurred within a short span of time
  • Whether login attempts occurred outside of normal work hours
  • Whether phishing attempts occurred through in-person interactions

Answer: Whether login attempts occurred from IP addresses that are not established work zones
Whether several failed login attempts occurred within a short span of time
Whether login attempts occurred outside of normal work hours


Q2. Which Python component contributes to automation by allowing you to perform the same actions a certain number of times based on a sequence?

  • Conditional statements
  • for loops
  • Bracket notation
  • while loops

Answer: for loops


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q3. Why is knowing how to work with files important for automation?

  • In order to create a function, it’s necessary to incorporate a file into it.
  • It is necessary to save a file in order to review what you have automated.
  • String and list methods are only accessible through files.
  • Cybersecurity-related information is often found in log files.

Answer: Cybersecurity-related information is often found in log files.


Q4. Which of the following are common file formats for security logs? Select all that apply.

.txt
.csv

.jpeg
.gif

Answer: .txt
.csv


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Portfolio Activity: Update a file through a Python algorithm

Question 1 to Question 8

Answer: Yes


Test your knowledge: Work with files in Python

Q1. You want to open the file “logs.txt” and store it in the file variable for the purpose of reading it. You also want to ensure all resources are released and the file is closed after you read it. What is the correct line of code to do this?

  • file = open(“logs.txt”, “r”):
  • with open(“logs.txt”, “r”) as file:
  • with file.open(“logs.txt”, “r”):
  • with open(“r”, “logs.txt”) as file:

Answer: with open(“logs.txt”, “r”) as file:


Q2. After you’ve opened a log file as login_file, which line of code can you use to read the file and store it in a variable called login_attempts?

  • login_file.read() as login_attempts
  • login_attempts = login_file.read()
  • login_attempts = login_file.reader()
  • login_attempts = read(login_file)

Answer: login_attempts = login_file.read()


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q3. You just read a log file into a variable called file. The file variable contains a string of multiple IP addresses that are each separated by a whitespace. Which line of code separates each individual IP address and stores it as a list in a variable called ip_addresses?

  • ip_addresses.split(file)
  • ip_addresses = split(file)
  • split(file, ip_addresses)
  • ip_addresses = file.split()

Answer: ip_addresses = file.split()


Q4. You need to check for unusual login activity. Specifically, you need to check a list of login timestamps to determine if any of the login times occurred at unusual hours. If you want to automate this through Python, what would be part of your code? Select two answers.

  • A for loop that iterates through the list of timestamps
  • An if statement that checks if the login timestamp occurred at unusual hours
  • An if statement that checks if a specific user has multiple login timestamps during unusual hours
  • A counter variable that keeps track of the number of failed login attempts

Answer: A for loop that iterates through the list of timestamps
An if statement that checks if the login timestamp occurred at unusual hours


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Test your knowledge: Debug Python code

Q1. What types of errors might you encounter while debugging code? Select three answers.

  • Logic errors
  • Exceptions
  • Syntax errors
  • Iteratives

Answer: Logic errors
Exceptions
Syntax errors


Q2. The purpose of this code is to indicate whether a particular operating system needs to be updated. However, it contains a syntax error. 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.)
Based on what you discover, how can you fix the error?

  • Use single equals signs (=) and not double equals signs (==).
  • Indent the elsif statement.
  • Remove all colons (:).
  • Change the keyword elsif to elif.

Answer: Change the keyword elsif to elif.


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q3. You have written code that assigns security incident tickets to the appropriate cybersecurity team based on its priority level. If the priority level is 1, it should get forwarded to Team A. If the priority level is 2, it should get forwarded to Team B. When testing your code, you notice that an incident with priority level 2 is forwarded to Team A instead of Team B. What type of error is this?

  • Name error
  • Exception
  • Syntax error
  • Logic error

Answer: Logic error


Q4. You have written code that uses a search algorithm to find an employee’s IP address. When testing your code, an error message indicates that an unknown index is being accessed. What type of error is this?

  • Exception
  • Logic error
  • Syntax error
  • Iterative

Answer: Exception


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Module 4 challenge

Q1. What is debugging?

count = 0
while count < 10
    print("number", count)
    count = count + 1

How can you fix the error?

  • Add a missing colon (:)
  • Remove the quotation marks around number
  • Change indentation
  • Spell a variable correctly

Answer: Add a missing colon (:)


Q2. 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.)

list = ["user1", "user2", "user3", "user4"]
for user in list:
    if user != "user3":
    print("Warning: user3 should not access the system.")

How can you fix the error?

  • Change “user3” to “user2” in the conditional.
  • Change the indentation so that the line that prints the warning is not indented.
  • Change “user3” to “user1” in the conditional.
  • Change the != operator to the == operator in the conditional.

Answer: Change the != operator to the == operator in the conditional.


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q3. You did not assign a value to a variable before using it in a conditional. What type of error is this?

  • Index out of bounds
  • Logic error
  • Syntax error
  • Exception

Answer: Syntax error


Q4. Why might you use print statements when debugging code?

  • To prevent errors from occurring
  • To identify which sections of the code are working properly
  • To create error messages
  • To add missing syntax to the code

Answer: To identify which sections of the code are working properly


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


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

  • “r”
  • read()
  • open()
  • “access.txt”

Answer: “r”
open()
“access.txt”


Q6. 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 = logins.split()
  • logins.split() as device_ids
  • device_ids = device_ids.split(logins)
  • device_ids = split(device_ids, logins)

Answer: device_ids = logins.split()


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q7. 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
  • parsing
  • debugging
  • dividing

Answer: parsing


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q8. 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 = file.read()
  • text = read(file, “r”)
  • text = read(file)

Answer: text = file.read()


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


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

  • An if statement to check if a login attempt failed
  • A for loop to iterate through all items in the logins list
  • A counter variable to keep track of the number of failed logins
  • A split() function to split the login information into a list

Answer: An if statement to check if a login attempt failed
A for loop to iterate through all items in the logins list
A split() function to split the login information into a list


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q10. You included username_list[10] in your code, but username_list only contains five elements. What type of error is this?

  • Logic error
  • Exception
  • Name error
  • Syntax error

Answer: Exception


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q11. If you know there is a logic error somewhere inside a function, how can you figure out the exact location?

  • Place print statements in and around the function
  • Move the function to another location
  • Delete the function from the program
  • Write comments in and around the function

Answer: Place print statements in and around the function


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q12. If you want to read a file called “logs.txt”, which line of code allows you to open this file for purposes of reading it and store it in a variable called file?

  • with open(“logs.txt”, file, “r”):
  • with file.open(“logs.txt”, “r”):
  • with open(“logs.txt”, “r”) as file:
  • with open(“logs.txt”) as file:

Answer: with open(“logs.txt”, “r”) as file:


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q13. 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?

  • file_text.split() as usernames
  • usernames = usernames.split(file_text)
  • usernames = file_text.split()
  • usernames = split(usernames, file_text)

Answer: usernames = file_text.split()


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q14. What are the three types of errors you will encounter while debugging?

  • Logic errors, comment errors, and iterative errors
  • Exceptions, logic errors, iterative errors
  • Syntax errors, exceptions, and comment errors
  • Syntax errors, logic errors, and exceptions

Answer: Syntax errors, logic errors, and exceptions


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q15. 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.)

device_id = "p35rv47
for char in device_id:
    print(char)

What is the error related to?

  • A misspelled variable
  • A missing double equals sign (==)
  • A missing quotation mark (“)
  • A missing colon (:)

Answer: A missing quotation mark (“)


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q16. When debugging code, what are effective ways to determine which sections of code are working properly? Select all that apply.

  • Add comments in the code
  • Use a debugger
  • Delete blank lines from the code
  • Add print statements

Answer: Use a debugger
Add print statements


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q17. What does the following code do?
with open(“logs.txt”, “r”) as file:

  • It copies a file called “logs.txt” into a new file “r”.
  • It opens a file called “logs.txt” in write mode and stores it in a variable called file.
  • It copies a file called “r” into a new file “logs.txt”.
  • It opens a file called “logs.txt” in read mode and stores it in a variable called file.

Answer: It opens a file called “logs.txt” in read mode and stores it in a variable called file.


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q18. What is parsing?

  • The process of reading data line by line
  • The process of copying data to other files
  • The process of writing data to a new file
  • The process of converting data into a more readable format

Answer: The process of converting data into a more readable format


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q19. What is the practice of identifying and fixing errors in code?

  • Parsing
  • Slicing
  • Debugging
  • Splitting

Answer: Debugging


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q20. The purpose of this code is to print “user flagged” if the username is “jhill”, and otherwise to print “user okay”. 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.)

def check_user(name):
    if name == "jhill":
        print("user flagged")
        print("user okay")
check_user("jhill")

How can you fix this error?

  • Call check_user() before the function definition.
  • Remove indentation from the line that prints “user okay” so that it is not part of the conditional.
  • Use the != operator instead of the == operator in the conditional header.
  • Add an else statement before the line that prints “user okay”.

Answer: Add an else statement before the line that prints “user okay”.


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q21. You did not define a function before calling it. What type of error is this?

  • Index out of bounds
  • Syntax error
  • Logic error
  • Exception

Answer: Logic error


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q22. What does the following code do?
read_text = text.read()

  • Reads the string text and stores it the file read_text
  • Splits the text variable, which contains a string, and stores it as a list in read_text
  • Reads the text variable, which contains a file, and stores it as a string in read_text
  • Replaces the contents of the file read_text with the contents of the file text

Answer: Reads the text variable, which contains a file, and stores it as a string in read_text


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


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

  • 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
  • A counter variable that increments when a failed login is detected
  • An if statement that checks if there were more than three failed login attempts

Answer: A for loop that iterates through the list of logins
A counter variable that increments when a failed login is detected
An if statement that checks if there were more than three failed login attempts


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q24. 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.)

Answer:
def search_list(username):
    for item in username:
        print(item)
search_list(["elarson", "bmoreno", "tshah"])

What is the error related to?

  • A misspelled variable
  • A missing quotation mark (“)
  • A missing comma (,)
  • A missing colon (:)

Answer: A missing colon (:)


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q25. You ask your code to divide something by 0, but an error occurs. What type of error is this?

  • Syntax error
  • Exception
  • Logic error
  • Index out of bounds

Answer: Exception


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


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

Answer: Splits a string variable called logins into a list of strings and stores it in the variable usernames


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


Q27. 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 for loop to iterate through all items in the devices list
  • An if statement that checks if elements in devices contain the substring “i71”
  • A split() function to split the string containing the information in the log file into a devices list
  • A counter variable to keep track of the number of devices containing the substring “i71”

Answer: A counter variable to keep track of the number of devices containing the substring “i71”


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz


More Weeks of this course: Click Here

More Coursera Courses: http://progiez.com/coursera


These are Automate Cybersecurity Tasks with Python Answers Week 4 Coursera Quiz
The content uploaded on this website is for reference purposes only. Please do it yourself first.