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