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

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

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

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.

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

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

14. 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”

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

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

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

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

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

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

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

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

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

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

What is the error related to?

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

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

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

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

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

  • Parsing
  • Slicing
  • Debugging
  • Splitting

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

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

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

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

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

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

Leave a Reply