automate cybersecurity tasks with python coursera weekly challenge 2 answers

Test your knowledge: Introduction to functions

1. In Python, what is a function?

  • A section of code that contains a conditional
  • A section of code that exists directly in Python
  • A section of code that can be reused in a program
  • A section of code that contains an iterative statement

2. Which of the following keywords is essential when defining a function?

  • while
  • if
  • def
  • for

3. You want to define a function that performs a status check. Which of the following is a valid header for the function definition?

  • def status_check()
  • def status_check
  • def status_check:
  • def status_check():

4. You are responsible for defining a function alert() that prints out the statement "Security issue detected." Which of the following blocks of code represent the correct indentation for defining and then calling the function?

Test your knowledge: Arguments, parameters, and return statements

5. Fill in the blank: In the following code, the integers 5 and 12 are _____:

for i in range(5, 12):

print(i)

  • functions
  • parameters
  • return statements
  • arguments

6. What is the correct way to define the function addition() if it requires the two parameters num1 and num2?

  • def addition(num1 num2):
  • def addition(num1 and num2):
  • def addition(num1)(num2):
  • def addition(num1, num2):

7. Which of the following lines of code has correct syntax for printing the data type of the string "elarson"?

  • print(type, “elarson”)
  • print(type(“elarson”))
  • type(print(“elarson”))
  • print(“elarson”, type)

8. Which function definition includes the correct syntax for returning the value of the result variable from the doubles() function?

  • def doubles(num):

    result = num * 2

    return result

  • def doubles(num):

    result = num * 2

    result return

  • def doubles(num):

    result = num * 2

    return = result

  • def doubles(num):

    result = num * 2

    return “result”

Test your knowledge: Learn from the Python community

9. Which of these is not included in the Python Standard Library?

  • time
  • NumPy
  • re
  • csv

10. Which of the following resources provides recommendations about including comments in your code?

  • csv
  • re
  • Python Standard Library
  • PEP 8

11. Which of the following code blocks have correct indentation?

12. What is a Python module?

  • A Python file that contains additional functions, variables, and any kind of runnable code
  • A resource that provides stylistic guidelines for programmers working in Python
  • A text file that contains cybersecurity-related data
  • A Python function that exists within Python and can be called directly

Weekly challenge 2

13. Which of the following components are part of the header in a function definition? Select all that apply.

  • The keyword return
  • The name of the function
  • The parameters used in a function
  • The keyword def

14. Which of the following calls to the type() function uses correct syntax?

  • type([55, 81, 17])
  • type([17, 81]):
  • type[(81, 17)]
  • type[81, 55, 17]

15. Review the following code. Which of these statements accurately describes name?

def echo(name):

return name * 3

  • It is a parameter because it is used in a return statement.
  • It is a parameter because it is included in the function definition.
  • It is an argument because it is included in the function call.
  • It is an argument because it is used in a return statement.

16. Fill in the blank: The re, csv, glob, and time modules are all _____.

  • keywords in a function header
  • built-in functions
  • part of the Python Standard Library
  • part of PEP 8

17. What does this line of code return? print(max(1,3,7))

  • 1
  • 7
  • 3
  • 11

18. What is returned from the following user-defined function if you pass it the arguments 2 and 3?

def add(num1, num2):
result = num1 + num2
return result
add(2, 3)

  • 2
  • 5
  • 3
  • 1

19. Which of the following choices is a resource that provides stylistic guidelines for programmers working in Python?

  • re
  • Python Standard Library
  • PEP 8
  • glob

20. What should you do when writing comments? Select all that apply.

  • Keep them up-to-date.
  • Make them clear.
  • Only place them at the beginning of a program.
  • Place them before every line of code.

21. What is a function?

  • A Python file that contains runnable code
  • A reusable section of code
  • A set of stylistic guidelines for working in Python
  • A downloadable resource with code instructions

22. Fill in the blank: A Python file that contains additional functions, variables, classes, and any kind of runnable code is called a _____.

  • parameter
  • library
  • module
  • built-in function

23. Which of the following choices is a valid header in a function definition?

  • def remove_user(username):
  • def remove_user(username)
  • remove_user(username):
  • def (remove_user(username))

24. Fill in the blank: A collection of modules that users can access in their programs is a _____.

  • style guide
  • built-in function
  • library
  • user-defined function

25. What does this line of code return?

print(type("h32rb17"))

  • int
  • “h32rb17”
  • str
  • h32rb17

26. What is returned from the following user-defined function if you pass it the argument "John"?

def greet(name):
greeting = "Hello"
return name
greet("John")

  • “John”
  • name
  • “Hello, John”
  • “Hello John”

27. What can a style guide help you with when working with Python? Select two answers.

  • Making it easier for other programmers to understand your code
  • Finding new modules you can incorporate into your code
  • Finding ways to make your code more complex
  • Making your code more consistent

28. Why are comments useful? Select three answers.

  • They explain the code to other programmers.
  • They make debugging easier later on.
  • They provide insight on what the code does.
  • They make the code run faster.

29. What are built-in functions?

  • Functions that return information
  • Functions that exist with Python and can be called directly
  • Functions that take parameters
  • Functions that a programmer builds for their specific needs

30. You imported a Python module, what do you now have access to in Python?

  • A manual that informs the writing, formatting, and design of documents
  • A function that exists within Python and can be called directly
  • A list of comments that you have included in previous code
  • Additional functions, variables, classes, and other kinds of runnable code

31. Which of the following calls to the sorted() function uses correct syntax?

  • sorted([532, 73, 85])
  • sorted[73, 85, 532]
  • sorted[(85, 523, 73)]
  • sorted():

32. In the following code, what is the argument?

def welcome_user(name):

print("Welcome," name)

username="elarson"

welcome_user(username)

  • welcome_user
  • def
  • name
  • username

33. When working in Python, what is a library?

  • A collection of modules that provide code users can access in their programs
  • A module that allows you to work with a particular type of file
  • A Python file that contains additional functions, variables, classes, and any kind of runnable code
  • A collection of stylistic guidelines for working with Python

34. What is returned from the following user-defined function if you pass it the argument of 2?

def multiples(num):
multiple = num * 3
return num
multiples(2)

  • multiples
  • 6
  • num
  • 2

35. What is an advantage of including this comment in the following code? Select all that apply.

# For loop iterates to print an alert message 5 times

for i in range(5):

print("alert")

  • It ensures the loop will function when the code is run in Python.
  • It can help other programmers understand the purpose of this loop.
  • It can help you understand the code if you revisit it in the future.
  • It is displayed in the output when the code is run in Python.

36. Which of the following statements accurately describe functions? Select all that apply.

  • Functions can be used no more than 10 times from within a single program.
  • When functions are updated, the changes are applied everywhere they are used.
  • Functions are useful for automation.
  • Functions can be reused throughout a program.

37. What does this line of code return?

print(sorted(["h32rb17", "p52jb81", "k11ry83"]))

  • [“h32rb17”, “k11ry83”, “p52jb81”]
  • [“p52jb81”]
  • [“p52jb81”, “k11ry83”, “h32rb17”]
  • [“h32rb17”]

Leave a Reply