Module 1: Introduction to Python

Q: Fill in the blank: Automation is _____.

  • the use of technology to reduce human and manual effort to perform common and repetitive tasks
  • the replacement of existing technology (CORRECT)
  • the use of human and manual effort to reduce technological power consumption
  • the combination of technology and manual effort to complete a task
Explanation: Automation refers to the use of technology to lessen the amount of human and physical labor required to carry out routine and repetitive operations. Within the context of this definition, automation is defined as the use of technology to simplify operations, boost efficiency, and reduce the amount of dependence on human involvement for regular activities.

Q: What is wrong with the following code?

for username in failed_login:

print(username)

  • The line with print(username) is not indented. 
  • The line with for username in failed_login: is not indented.
  • Both lines are not indented.
  • The first line should be split in two, and in failed_login: should be indented on the new line.
Explanation: The problem is that the line that contains the for loop header does not have the appropriate amount of indentation beneath it. To make it clear that the statements included inside the for loop (or any other block such as if, while, etc.) are a part of that loop, Python requires that they be indented.

Q: Fill in the blank: String data _____.

  • must be placed in brackets
  • must include a decimal point
  • must be placed in quotation marks 
  • must be placed in parentheses
Explanation: Enclosing text inside single (') or double (") quote marks is the standard method for representing strings in the majority of computer languages, including Python. Through the use of this syntax, the interpreter or compiler is informed that the material that is included inside the quotation marks ought to be handled as a string literal.

Q: Which data type always has a value of either True or False?

  • Boolean 
  • Float
  • List
  • String
Explanation: When it comes to computer languages, Boolean data types represent two states: true and false. It is standard practice to use them in conditional statements, logical expressions, and boolean algebra operations to regulate the flow of a program depending on logical circumstances.

Q: Which line of code assigns the string “dtanaka” to a variable called username?

  • “dtanaka” = username
  • username(“dtanaka”)
  • username = dtanaka
  • username = “dtanaka” (CORRECT)
Explanation: It is possible to assign a value to a variable in Python, as well as in a great number of other programming languages, by using the assignment operator =. Therefore, you may assign the string "dtanaka" to the variable username by using the syntax username = "Tanaka."

Q: What will this code do when you run it?

var2 = ["a","b","c"]

var2_type = type(var2)

print(var2_type)

  • Change the data type of var2
  • Output the characters “a”, “b”, and “c” to the screen
  • Indicate that var2 contains list data 
  • Print the string “var2_type” to the screen
Explanation: With the help of the type() function, this line places the type of var2, which is a list, into the variable var2_type.This line will display the type of var2 on the screen. In the above scenario, the result will be, which signifies that the variable var2 includes data from a list.

Q: You are checking whether the string stored in a device_id variable matches to the correct device ID, the string “15hgu3769”. When it matches, you want to print, “Login successful!”. Which conditional statement has the correct syntax needed to do this?

 

if device_id == "15hgu3769":

    print("Login successful!") (CORRECT)

if "device_id" = "15hgu3769"

    print("Login successful!")

if device_id != "15hgu3769"

    print("Login successful!")

if "device_id == 15hgu3769"

    print("Login successful!")

Q: Fill in the blank: An else statement _____.

  • contains its own unique condition
  • executes when the condition in the if statement preceding it evaluates to False 
  • executes when the condition in the if statement preceding it evaluates to True
  • is required after every if statement
Explanation: The else statement is used in combination with an if statement in the majority of programming languages, including Python, to indicate a block of code that should be executed if the condition of the if statement evaluates to False. If the required condition of the if statement is not satisfied, it offers an alternate route of execution.

Q: What iterative statement should you use if you want to print the numbers 1, 2, and 3?

 

for i in [1,3]:

    print(i)

for i in range(0,3):

    print(i)

for i in [1, 2, 3]:

    print(i) (CORRECT)

for i in range(1,3):

    print(i)

Q: You want to print all even numbers between 0 and 10 (in other words, 0, 2, 4, 6, 8, and 10). What should your next line of code be?

count = 0

while count <= 10:

    print(count)

  • count = count + 1
  • if count < 10:
  • count = count + 2 
  • count = 1

Q: Which of these are string data? Select all that apply.

  •  [100, 200, 300]
  • “100” 
  • 100
  • user1”

Q: What are possible values for the Boolean data type? Select all that apply.

  • True 
  • !=
  • False 

Q: You are implementing security measures on a server. If a user has more than 3 failed login attempts, the program should print “locked out”. The number of failed login attempts is stored in a variable called failed_attempts. Which conditional statement has the correct syntax needed to do this?

if failed_attempts >= 3

    print(“locked out”)

if failed_attempts >= 3

    print("locked out")

if failed_attempts < 3

    print("locked out")

if failed_attempts > 3:

    print("locked out") (CORRECT)

if failed_attempts <= 3:

    print("locked out")

Q: In a cybersecurity setting, which of these tasks would it be common to apply Python to? Select all that apply.

  • Reducing the effort needed to manage an access control list 
  • Automating several tasks from a playbook into one workstream 
  • Automating how a log is read when responding to an incident 
  • Manually checking individual timestamps in a log
Explanation: Python is capable of automating and simplifying the administration of access control lists (ACLs), which makes it simpler to add, delete, or alter permissions depending on established criteria or rules. Python can automate workflows and combine various jobs into coherent processes, which may improve the speed and consistency of incident response and other cybersecurity operations. The ability of Python to automate the parsing, analysis, and extraction of essential information from logs is a significant benefit to cybersecurity experts, as it enables them to swiftly detect and react to security events.

Q: What data type requires quotation marks (” “)?

  • String 
  • Boolean
  • Integer
  • Float
Explanation: Sequences of characters that are wrapped inside single (' ') or double (" ") quote marks are referred to as strings in the field of computer programming. In contrast to other data types, such as integers, floats, and booleans, which do not make use of quotation marks for their literals, this syntax differentiates strings from these other data types.

Q: Which line of Python code would create a Boolean value of True?

  • print(25<24)
  • print(“True”)
  • print(10<100) 
  • print([“Boolean”])
Explanation: The evaluation of this comparison (10 < 100) occurs as True since 10 is, in fact, smaller than 100. Depending on whether the condition is true or false, comparisons in Python such as <, >, <=, >=, ==, and!= provide Boolean values (True or False) as the response.

Q: What are the variables in the following code? Select all that apply.

username = "kcarter"

attempts = 5

print(username)

print(attempts)

print("locked")

  • attempts 
  • username 
  • “kcarter”
  • “locked”

Q: Fill in the blank: If you ran the following code, the output would _____.

var1 = 9.5

var1_type = type(var1)

print(var1_type)

  • reassign var1 as float data
  • indicate that var1 contains float data 
  • output 9.5 to the screen
  • reassign var1 as string data
Explanation: It is a floating-point integer (float) that the code uses to assign the value 9.5 to the variable var1, which stores the data. The data type of var1 is returned by the type(var1) function call, which is subsequently shown on the screen with the command "print." Hence, the result would be , which would indicate that the variable var1 contains data that is of the float type.

Q: You wrote the following code:

if attempts >= 5:

    print("locked")

else:

    print("try again")

If the value in the attempts variable is 3, what will Python do?

  • First output the message “try again” and then output the message “locked”
  • Output the message “try again”
  • First output the message “locked” and then output the message “try again”
  • Output the message “locked”
Explanation: The implementation of conditional logic in the code is what determines this behavior. In this particular scenario, Python will carry out the execution of the specified print statement if there is an if statement that checks the value of attempts and prints "locked" when the condition is true (in this example, attempts == 3). Without knowing the specific code context, this is the result that is most likely to occur based on the available information.

Post a Comment

Previous Post Next Post