automate cybersecurity tasks with python coursera weekly challenge 3 answers
Test your knowledge: Work with strings
1. Which of the following statements correctly describe strings? Select all that apply.
- Strings cannot contain numeric characters.
- Strings must be placed in brackets ([ ]).
- Strings must be placed in quotation marks (โ โ).
- Strings are immutable.
2. What does the following code return?
device_id = "uu0ktt0vwugjyf2"
print(device_id[2:5])
- โu0ktโ
- โ0kttโ
- โu0kโ
- โ0ktโ
3. What does the following code display?
device_id = "Tj1C58Dakx"
print(device_id.lower())
- โtj1C58Dakxโ
- โTj1C58Dakxโ
- โtj1c58dakxโ
- โTJ1C58DAKXโ
4. You want to find the index where the substring "192.168.243.140" starts within the string contained in the variable ip_addresses. Complete the Python code to find and display the starting index. (If you want to undo your changes to the code, you can click the Reset button.)

What index does the substring "192.168.243.140" start at?
- 31
- 33
- 34
- 32
Test your knowledge: Work with lists and develop algorithms
5. Review the following code:
my_list = ["a", "b", "c", "d"]
my_list[2] = 4
print(my_list)
What will it display?
- [โaโ, 4, โcโ, โdโ]
- An error message
- [โaโ, โbโ, 4, โdโ]
- [โaโ, โbโ, โ4โ, โdโ]
6. You are working with the list ["cwvQSQ","QvPvX5","ISyT3a","S7vgN0"]. Its elements represent machine IDs, and the list is stored in a variable named machine_ids. Which line of code will add the ID of "yihhLL" at index 3?
- machine_ids.append(โyihhLLโ,3)
- machine_ids.insert(โyihhLLโ,3)
- machine_ids.append(โyihhLLโ)
- machine_ids.insert(3,โyihhLLโ)
7. Which line of code will remove the username "tshah" from the following list?
access_list = ["elarson", "bmoreno", "tshah", "sgilmore"]
- access_list[โtshahโ].remove()
- access_list.remove(โtshahโ)
- access_list.remove(2)
- access_list.remove(3)
8. As a security analyst, you are responsible for developing an algorithm that automates removing usernames that match specific criteria from an access list. What Python components would you help implement this? Select three answers.
- A for loop that iterates through the usernames in the access list
- The .remove() method
- The .append() method
- An if statement that compares a username to the criteria for removal
Test your knowledge: Regular expressions
9. Which regular expression symbol represents one or more occurrences of a specific character?
- \d
- \w
- *
- +
10. As a security analyst, you are responsible for finding employee IDs that end with the character and number sequence "a6v". Given that employee IDs consist of both numbers and alphabetic characters and are at least four characters long, which regular expression pattern would you use?
- โ\w*a6vโ
- โa6vโ
- โ\wa6vโ
- โ\w+a6vโ
11. You have imported the re module into Python with the code import re. You want to use the findall() function to search through a string. Which function call enables you to search through the string contained in the variable text in order to return all matches to a regular expression stored in the variable pattern?
- findall(pattern, text)
- findall(text, pattern)
- re.findall(pattern, text)
- re.findall(text, pattern)
12. Which strings match the regular expression pattern "\w+"? Select all that apply.
- โ3โ
- โFirstNameโ
- โ#nameโ
- โโ
Weekly challenge 3
13. Which line of code converts the integer 7 to a string?
- str(โ7โ)
- str(7)
- string(7)
- string(โ7โ)
14. Which line of code returns a copy of the string "HG91AB2" as "hg91ab2"?
- print(โHG91AB2โ.lower())
- print(โHG91AB2โณ(lower))
- print(lower.โHG91AB2โณ())
- print(lower(โHG91AB2โ))
15. What is the index of the character "4" in the string "h204D3921"?
- 2
- 5
- 3
- 4
16. You need to take a slice from an employee ID. Specifically, you must extract the characters with indices of 3, 4, 5, and 6. Complete the Python code to take this slice and display it. (If you want to undo your changes to the code, you can click the Reset button.)

What string does the code output?
- โx430โ
- โ37ร4โ
- โ7ร43โ
- โ237xโ
17. What is the output of the following code?
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
print(list1 + list2)
- [1, 2, 3, โaโ, โbโ, โcโ]
- An error message
- [1, โaโ, 2, โbโ, 3, โcโ]
- [6, โabcโ]
18. A variable named my_list contains the list [1,2,3,4]. Which line of code adds the element 5 to the end of the list?
- my_list.insert(4,5)
- my_list.insert(5)
- my_list.insert(5,4)
- my_list.insert(5,5)
19. What is an algorithm?
- A function that finds matches to a pattern
- A set of guidelines to keep code consistent
- A function that returns information
- A set of rules to solve a problem
20. What does the \w symbol match to in a regular expression?
- Any letter
- Any character and symbol
- Any alphanumeric character
- Any number
21. You have imported the re module into Python with the code import re. Which code searches the device_ids string variable for a pattern of "r15\w+"?
- re.findall(device_ids, โr15\w+โ)
- findall(โr15\w+โ, device_ids)
- re.findall(โr15\w+โ, device_ids)
- findall(device_ids, โr15\w+โ)
22. Which method adds input to the end of a list?
- .append()
- .lower()
- .insert()
- .index()
23. What is the output of the following code?
print(len("125"))
- 3
- 10
- 8
- 5
24. Which line of code returns a copy of the string "bmoreno" as "BMORENO"?
- print(โbmorenoโ.upper())
- print(upper.โbmorenoโ())
- print(upper(โbmorenoโ))
- print(โbmorenoโ(upper))
25. What is the index of the character "c" in the string "encryption"?
- 2
- 3
- 1
- 4
26. What is the output of the following code?
username_list = ["elarson", "bmoreno", "tshah"]
device_id_list = ["us2c0R5", "2R78TBR", "bt3MIEz"]
print(username_list + device_id_list)
- [โelarsonโ, โus2c0R5โ, โbmorenoโ, โ2R78TBRโ, โtshahโ, โbt3MIEzโ]
- [โus2c0R5โ, โ2R78TBRโ, โbt3MIEzโ, โelarsonโ, โbmorenoโ, โtshahโ]
- An error message
- [โelarsonโ, โbmorenoโ, โtshahโ, โus2c0R5โ, โ2R78TBRโ, โbt3MIEzโ]
27. A variable named my_list contains the list [1,2,3,4]. Which line of code removes the last element in the list?
- remove (my_list, 3)
- remove(my_list, 4)
- my_list.remove(3)
- my_list.remove(4)
28. What module do you need to import to use regular expressions in Python?
- os
- time
- re
- csv
29. What is the result when .upper() is applied to a string?
- The character that appears most frequently in the string is extracted from it and returned.
- The value of the string is reassigned to the value of the string in the line preceding it.
- The value of the string is reassigned to contain all uppercase letters.
- A copy of the string is returned with all uppercase letters.
30. What is the output of the following code?
approved_users = ["bmoreno", "elarson", "tshah", "eraab"]
print(approved_users[1])
- โelarsonโ
- [โbmorenoโ, โelarsonโ, โtshahโ, โeraabโ, 1]
- โbmorenoโ
- [1, โbmorenoโ, โelarsonโ, โtshahโ, โeraabโ]
31. Fill in the blank: A(n) _____ is a set of rules to solve a problem.
- append
- algorithm
- regular expression
- index
32. Which of the following strings match with the regular expression pattern of "\w"? Select all that apply.
- โWโ
- โsecurityโ
- โ2โ
- โ1Bโ
33. What does the re.findall() function return?
- All possible regular expressions that match to a given string
- A list of all matches to a regular expression in a given string
- The first match to a regular expression in a given string
- All occurrences of the pattern โreโ in a given string
34. What does the code username_list.append("bmoreno") method do?
- Returns all matches to the pattern โbmorenoโ in the username_list list
- Inserts โbmorenoโ at the beginning of the username_list list
- Adds โbmorenoโ to the end of the username_list list
- Updates all instances of โbmorenoโ in the username_list list to uppercase letters
35. Which line of code returns the number of characters in the string assigned to the username variable?
- print(len(username))
- print(username.len())
- print(str(username))
- print(username.str())
36. Which code joins a list of new_users to a list of approved_users and assigns the value to a third variable named users?
- users(new_users[1], approved_users[2])
- users = insert(new_users, approved_users)
- users = new_users + approved_users
- users(new_users, approved_users)
37. Fill in the blank: Determining that you need to use string slicing and a for loop to extract information from items in a list is part of creating a(n) _____.
- index
- regular expression
- append
- algorithm