Intro to Python for Security
Which of the following operators in Python will produce a modulo of numbers that are provided? A. += B. ** C. // D. %D. %
D. %
What will the output of the following code? counter = 0 for i in range(0, 10, 4): break counter += 1 print(counter) A. 10 B. 2 C. 3 D. 0
D. 0
What opens a file in Append mode A. APP B. WA C. Add D. A
D. A
_______Runs whether the code failed or succeeded A. Raise B. Else C. Break D. Finally
D. Finally (Raise prints custom error message) (Else runs only if no exceptions occurred)
You wrote a script that dumps txt files from a website. However, the code crashes with an error. What's the solution? A. Implement an if & else B. Implement a try-exit statement C. Implement classes for code handling D. Implement a try-except statement
D. Implement a try-except statement
What type of variable is the 'x' in the following code? x = 5 A. String B. Float C. Boolean D. Interger
D. Interger
What will happen when the following code is executed? if True: print("Hello") A. It will print the word Hello. B. It won't run because "if" wasn't invoked. C. It won't run because it can't understand "True". D. It will display an indentation error.
D. It will display an indentation error.
What opens a file in Read-Only mode A. Read B. +R C. RO D. R
D. R
while loop example
counter = 0 while counter <= 6: counter += 1 print(counter)
Dictionary symbols are?
curly brackets{ }. can be updated
What is a modulo?
%... Division with the remainder
Code that is read from left to right and top to bottom while checking and executing each line in the process uses which of the following methods? A. Interpretation B. Compilation C. Encapsulation D. Obfuscation
A. Interpretation PY-01-P1 Slide 9
What type of variable is the 'x' in the following code? x = "5.5" A. String B. Float C. Boolean D. Interger
A. String
What type of variable is the 'x' in the following code? x = "TEST" A. String B. Float C. Boolean D. Interger
A. String
Which of the following is NOT a Python data structure? A. String B. Tuple C. List D. Dict
A. String
What should be added to the following function to print the value 20? x = 10 def func(): x = 20 func() print(x) A. The command "global x" B. The command "main x" C. The command "self x" D. The command "pass x"
A. The command "global x"
Not closing a file may cause a program to behave unexpectedly. A. True B. False.
A. True
You need to know loops for the final exam? A. True B. False
A. True
Which line reads from the file called myfile.txt? A. myfile.read() B. file.readline() C. file.read() D. myfile.write()
A. myfile.read()
Explain "for i in range(1,10,2):" A. starts at 1 and then skips every 2 numbers up until the number 10 B. starts at 1 and them skips every number up until the number 10 C. starts at 10 and then skips every 2 numbers up until the number 1 D. starts at 1 and then skips every 10 numbers up until the number 20
A. starts at 1 and then skips every 2 numbers up until the number 10
The command x = input("Enter a number") saves data in what format? A. x is a string B. x is a boolean C. x is an integer D. x is a float
A. x is a string
with open("File.txt", "w") as file: file.write("Hello World") A. An error occurs- "File.txt" does not exist B. "Hello World" is written inside "File.txt" C. An error occurs- The file opens but does not close D. "Hello World" is written inside "file"
B. "Hello World" is written inside "File.txt"
Which of the following operators in Python will produce a modulo of numbers that are provided? A. += B. ** C. // D. %D. %
B. **
What will the value of X be? X = 10 if True and False: X += 10 print(X) A. 20 B. 10 C. The code is erroneous D. X cannot be a variable
B. 10
What type of variable is the 'x' in the following code? x = 5.5 A. String B. Float C. Boolean D. Interger
B. Float
Which of the following should be used to iterate over a list and print its items separately? A. While loop B. For loop C. If condition D. Dictionary
B. For loop PY-03-P1 slide 6
Of the following, which is an advantage of an interpreter compared to a compiler? A. Speed B. Large, community supported set of a code-repositories C. Faster D. Runs first and then executes code
B. Large, community supported set of a code-repositories
Running socket.accept() needs to have what? A. The accept() command was not assigned with several variables B. The accept() command was not assigned with two variables C. The accept() command was not assigned with three variables D. The accept command was not assigned with two pair variables
B. The accept() command was not assigned with two variables
If you see the word error in a question, the answer is? A. Try and Exit B. Try and Except C. Catch and Exit D. Try and Catch
B. Try and Except
What opens a file in Write mode A. Write B. W C. W+R D. A
B. W
Dividing by zero generates what? A. GeneralDivisionError B. ZeroDivisionError C. Exception D. TypeError
B. ZeroDivisionError
Which if the following demonstrates a List structure? A. animal = ("dog", "cat") B. animal = ["dog", "cat] C. animal = {"dog", "cat"} D. animal = {"dog":maxx, "cat",:meow}
B. animal = ["dog", "cat]
The ____ function saves a file. A. close() B. flush() C. save() D. exit()
B. flush()
Loops
Blocks of code that run as long as a condition is true
What are Loops?
Blocks of code that run as long as a condition is true They allow repeating code for as long as the condition is true
What type of variable is the 'x' in the following code? x = True A. String B. Float C. Boolean D. Interger
C. Boolean
Taking a string to a float is an example of what? A. Converting B. Can not be done C. Casting D. multicast
C. Casting
When sending text via the socket library to a server, why should the text be encoded? A. The data must be encoded to ASCII. B. The data must be encoded with SHA. C. It must be passed as a byte object. D. Because it's code.
C. It must be passed as a byte object.
What does a function return by default? A. Answer - is a variable B. Response - is the sum of all numbers C. None - which is empty data D. None - returns all number
C. None - which is empty data
What will happen when you run the following code? with open("MyFile.txt", "w") as file: file.write("Hello World") A. "Hello World" will be appended to the file. B. An error will occur because "w" allows reading the file. C. The file will be overwritten with "Hello World". D. An error will occur because an exception was not added.
C. The file will be overwritten with "Hello World".
What is the purpose of the following expression: If __name__ == "__main__" A. To check the name of the file. B. To make sure the first function in the file is executed. C. To check if the file is the main file in the project. D. To check if the file is imported or directly executed.
C. To check if the file is the main file in the project.
Which commands would you use to gracefully exit in case of an error? A. Try and Exit B. Try and Exception C. Try and Except D. If and Except
C. Try and Except
The ___ function saves and closes a file. A. save() B. flush() C. close() D. exit()
C. close()
What is needed if you use the following code "Myfile = open("file.txt","a")" A. myfile.closed() B. myfile.write() C. myfile.close() D. file.close()
C. myfile.close()
What is Casting?
Casting is the process of converting a value from one data type to another.
When accepting data within communication, what is the meaning of recv(1028)? A. Receiving time in seconds B. The limit for the amount of words to accept C. The max for the amount of bytes to accept D. The limit for the amount of bytes to accept
D. The limit for the amount of bytes to accept
When creating client-server communication scripts, which one should be executed first? A. Both must be executed at the same time. B. The client. C. It doesn't matter. D. The server.
D. The server.
What does "if name == 'Fen': break" do? A. sets name equaled to fen B. asks if name equals fen C. sets name equaled to fen, and if true then breaks D. asks if name equals fen, and if true then breaks
D. asks if name equals fen, and if true then breaks
The ______ function can be used to read the number of bytes from the line. A. read() B. readline{bytes} C. readline() D. readline([bytes])
D. readline([bytes])
Tuple symbols are?
Parentheses ( )... like items that dont normally change
Opens a file for appending
a
range() function
accepts an integer and returns the range of the object
while loops
perform blocks of code while a condition is true if the condition is false, the while loop will not be executed while loops do not have iteration capability like for loops
For loop
performs blocks of code if the condition is true provides iterating capabilities Can iterate over lists, defined ranges and more
Opens a file for reading only
r
len() function
returns the number of items of an object
For Loops
runs if a condition is true. provides iterating capabilities will execute w/o a pre-check to validate the condition for "variable" in list do stuff
While Loops
runs while a condition is true. doesn't have iteration capabilities like For Loops first checks if the condition is valid while "condition" do stuff
List symbols are?
squared brackets[ ]...
Try and Except
try will execute the code except will execute part of the code when a problem arises
Opens a file for writing only and overwrites the existing file
w
For loop example:
word = "test" num = (1,2,3,4) for i in word: print(i) print() for n in num: print(n)