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

Leave a Reply