automate cybersecurity tasks with python coursera weekly challenge 4 answers

Test your knowledge: Work with files in Python

1. 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:

2. 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_attempts = login_file.reader()
  • login_attempts = login_file.read()
  • login_attempts = read(login_file)
  • login_file.read() as login_attempts

3. 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)
  • split(file, ip_addresses)
  • ip_addresses = file.split()
  • ip_addresses.split(file)

4. 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 a specific user has multiple login timestamps during unusual hours
  • A counter variable that keeps track of the number of failed login attempts
  • An if statement that checks if the login timestamp occurred at unusual hours

Test your knowledge: Debug Python code

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

  • Logic errors
  • Exceptions
  • Syntax errors
  • Iteratives

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

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

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

  • Exception
  • Syntax error
  • Logic error
  • Name error

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

Shuffle Q/A 1

Weekly challenge 4

9. What is debugging?

  • The practice of improving code efficiency.
  • The practice of identifying and fixing errors in code.
  • The practice of improving code readability.
  • The practice of calling a function from multiple places in a larger program

10. The purpose of the following code is to print the numbers from 0 to 9. 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.)

How can you fix the error?

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

Leave a Reply