Automate Cybersecurity Tasks with Python | Week 2
Week 2 – Write Effective Python Code
Course Name: Automate Cybersecurity Tasks with Python
Course Link: Automate Cybersecurity Tasks with Python
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Test your knowledge: Introduction to functions
Q1. In Python, what is a function?
- A section of code that can be reused in a program
- A section of code that contains a conditional
- A section of code that contains an iterative statement
- A section of code that exists directly in Python
Answer: A section of code that can be reused in a program
Q2. Which of the following keywords is essential when defining a function?
- if
- def
- for
- while
Answer: def
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q3. 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
Answer: def status_check():
Q4. 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?
- def alert():
print(“Security issue detected”)
alert() - def alert():
print(“Security issue detected”)
alert() - def alert():
print(“Security issue detected.”)
alert() - def alert():
print(“Security issue detected.”)
alert()
Answer:
def alert():
print(“Security issue detected.”)
alert()
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Test your knowledge: Arguments, parameters, and return statements
Q1. Fill in the blank: In the following code, the integers 5 and 12 are _______:
for i in range(5, 12):
print(i)
- arguments
- parameters
- return statements
- functions
Answer: arguments
Q2. 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):
Answer: def addition(num1, num2):
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q3. Which of the following lines of code has correct syntax for printing the data type of the string “elarson”?
- type(print(“elarson”))
- print(type(“elarson”))
- print(type, “elarson”)
- print(“elarson”, type)
Answer: print(type(“elarson”))
Q4. 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
return result - def doubles(num):
result = num * 2
return “result” - def doubles(num):
result = num * 2
result return
Answer:
def doubles(num):
result = num * 2
return result
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Test your knowledge: Learn from the Python community
Q1. Which of these is not included in the Python Standard Library?
- NumPy
- csv
- time
- re
Answer: NumPy
Q2. Which of the following resources provides recommendations about including comments in your code?
- Python Standard Library
- re
- PEP 8
- csv
Answer: PEP 8
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q3. Which of the following code blocks have correct indentation?
if username == "elarson":
print("Welcome, elarson!")
if username == "elarson":
print("Welcome, elarson!")
if username == "elarson":
print("Welcome, elarson!")
if username == "elarson":
print("Welcome, elarson!")
Answer:
if username == "elarson":
print("Welcome, elarson!")
Q4. What is a Python module?
- A Python function that exists within Python and can be called directly
- A text file that contains cybersecurity-related data
- 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
Answer: A Python file that contains additional functions, variables, and any kind of runnable code
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Module 2 challenge
Q1. 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
Answer: The name of the function
The parameters used in a function
The keyword def
Q2. 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]
Answer: type([55, 81, 17])
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q3. 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.
Answer: It is a parameter because it is included in the function definition.
Q4. 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
Answer: part of the Python Standard Library
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q5. What does this line of code return? print(max(1,3,7))
- 1
- 3
- 7
- 11
Answer: 7
Q6. 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)
- 1
- 2
- 3
- 5
Answer: 5
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q7. Which of the following choices is a resource that provides stylistic guidelines for programmers working in Python?
- re
- Python Standard Library
- PEP 8
- glob
Answer: PEP 8
Q8. 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.
Answer: Keep them up-to-date.
Make them clear.
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q9. 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
Answer: A reusable section of code
Q10. 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
Answer: module
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q11. 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))
Answer: def remove_user(username):
Q12. 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
Answer: library
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q13. What does this line of code return?
print(type(“h32rb17”))
- int
- “h32rb17”
- str
- h32rb17
Answer: str
Q14. 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”
Answer: “John”
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q15. 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
Answer: Making it easier for other programmers to understand your code
Making your code more consistent
Q16. 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.
Answer: They explain the code to other programmers.
They make debugging easier later on.
They provide insight on what the code does.
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q17. 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
Answer: Functions that exist with Python and can be called directly
Q18. 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
Answer: Additional functions, variables, classes, and other kinds of runnable code
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q19. 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():
Answer: sorted([532, 73, 85])
Q20. 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
Answer: username
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q21. 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
Answer: A collection of modules that provide code users can access in their programs
Q22. 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
Answer: 2
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q23. 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.
Answer: 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.
Q24. 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.
Answer: When functions are updated, the changes are applied everywhere they are used.
Functions are useful for automation.
Functions can be reused throughout a program.
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
Q25. What does this line of code return?
print(sorted([“h32rb17”, “p52jb81”, “k11ry83”]))
- [“h32rb17”, “k11ry83”, “p52jb81”]
- [“p52jb81”]
- [“p52jb81”, “k11ry83”, “h32rb17”]
- [“h32rb17”]
Answer: [“h32rb17”, “k11ry83”, “p52jb81”]
Q26. What is returned from the following user-defined function if you pass it the argument 9?
def subtract(num):
total = 100 – num
return total
subtract(9)
- 9
- 100
- 91
- 9.0
Answer: 91
These are Automate Cybersecurity Tasks with Python Answers Week 2 Coursera Quiz
More Weeks of this course: Click Here
More Coursera Courses: http://progiez.com/coursera