1/99
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
what is the addition reassignment operator?
when you are reassigning a variable relative to it's current value, rather than saying i = i + 1, you can say i += 1
is return a function call?
no
why aren't return and print the same?
print is a function call. With return you are sending the function back to where it was called
The while statement allows you to repeat a block of statements in your program. T/F
t
The while statement is called a loop, because the control of your program jumps back up to a point earlier in the program than the end of the repeat block. T/F
t
You can name a variable while in your program without Python confusing your variable's name and the while keyword. T/F
f
The condition in the while statement's syntax must be a bool expression. T/F
t
If the condition in a while statement evaluates to False, control jumps over the repeat block and onto the next statement at the same level of indentation as the while keyword. T/F
t
The while loop keeps going until the condition becomes (True/False)
False
If the condition in a while statement evaluates to True, control jumps into the repeat block and evaluates the first statement of the repeat block. T/F
t
After the final statement of the repeat block is evaluated, control jumps back up to the condition of the while statement and evaluates it again. T/F
t
You can write any statements you'd like inside of the repeat block, such as other print statements, variable declaration and assignment statements, conditional if-else statements, while loop statements, and so on. T/F
t
To avoid an infinite loop which of the follow should be true, choose all that apply:
A: Something must change in the repeat block that causes the while loop's condition to change
B: Forward progress must be made toward the while loop condition becoming True
C: Forward progress must be made toward the while loop condition becoming False
a, b
An iteration of a loop is one evaluation of the repeat block and it should be making incremental progress toward the goal of the loop. T/F
t
The number of iterations a loop runs for is the number of times the repeat block is evaluated. T/F
t
The then block of an if statement and the repeat block of a while statement are semantically the same, including what happens after each block completes. T/F
f
The phrase "if loop" is incorrect and should not be said. The phrase "while loop" is correct. T/F
t
You can use a while loop to iterate through each item in a collection. For example, a str is a collection of characters, and you can use a while loop to iterate through each character one-by-one. T/F
t
The variable i is commonly used as a counter variable when writing while loops. T/F
t
What is the result of the following boolean expression?
not True
False
The answer to the condition tells you if you should enter the loop, and is a boolean value. T/F
t
In a while loop, if the condition is True you enter the ____ block
repeat
What is the result of the following boolean expression?
not not True
True
What is the result of the following boolean expression?
not True and not True
False
What does the not operator do?
it inverts the boolean (True becomes False and vice versa)
What does the and operator do?
Evaluates to True if and only if both bools on left and right evaluate to True. Evaluates to False in all other scenarios
What does the or operator do?
Evaluates to True if and only if either left or right is True or both are True. Evaluates to False if both sides are False
What is the order of precedence for the 3 boolean operators in order from first to last?
not, and, or
What is the result of the following boolean expression?
not True or not False
True
What is the result of the following boolean expression?
not False or not False
True
What is the result of the following boolean expression?
True and False or False and not False
False
Consider the following expression:
True and False or False and not False
Which operator got evaluated first?
not
Which operator evaluates last?
or
A string is a sequence of characters. T/F
t
All characters are letters, but not all letters are characters. T/F
f
Every individual character in a str has a corresponding int value. T/F
t
What does the following expression evaluate to?
ord("C")
67
What does the ord( ) function do?
it takes a single-character string (like "A") and returns the int representation on the character's binary code
What does the following expression evaluate to?
ord("C") > ord("A")
True
What does the following expression evaluate to?
"C" > "A"
True
Do these two expressions evaluate to the same thing?
ord("C") > ord("A")
"C" > "A"
yes
How many digits are there in hexidecimal ("hex") numbers?
16
what does the chr ( ) function do?
it converts an int to a character, so the opposite of the ord ( ) function
The largest hex digit is F. What is its value in the decimal system and why?
15. A-F corresponds to the values 10-15 since 0-9 is already covered
The following string uses an escape sequence that causes it to span multiple lines when printed? T/F
"Hello\nworld\n!!!"
t
What does /n do and what is it?
it is an escape sequence and gives a new line
What will "The computer said, \"Hello, world.\"" evaluate to?
The computer said "Hello, world."
What would be printed from this code snippet?
age: int = 21
msg: str = f"You are {age}!"
print(msg)
You are 21!
Functions are used for which of the following:
A. Process abstraction
B. Defining new data types
C. Breaking larger programs into smaller sub-programs
D. Modeling data structures
a, b, c, d
Function calls are expressions that evaluate to a specific data type. T/F
t
When you define a function you are invoking its call. T/F
f
A function definition can be thought of as a sub-program that specifies the instructions which will be carried out when the function definition is called. T/F
t
Function definitions are found in which of the following ways:
A: Built-in functions automatically available
B: Imported from libraries
C: Defined in the same python file/module
a, b, c
Functions in programming are exactly equivalent to algebraic functions. T/F
f
What are the syntax requirements of a valid function call expression?
A. the def keyword
B. the name of the function
C. an argument list
D. a parameter list
E. a function body
b, c,
Each argument is an expression. T/F
t
A function definition is made of which of the following high-level elements?
A. Signature / "Contract" Line
B. Docstring for documentation purposes
C. Function body block
D. Argument list
a, b, c,
The syntax of the signature line of a function definition includes which of the following elements?
A. the def keyword
B. the name of the function
C. an argument list
D. a parameter list
E. a return type preceded by ->
F. ends with a :
a, b, d, e, f
what is a function call?
when we call a function, we are invoking its definition
what is a function definition?
sub-programs that specify what actually happens when a function is called
The signature line is tells anyone who wants to use a function what it needs in order to be called and what you can expect returned back as a result. T/F
t
Parameters are found in function definitions. Arguments are found in function calls. T/F
t
A parameter list is made of a pair of matching parentheses with a list of "variable declaration" statements separated by commas found between them, which specify the parameters of the function. T/F
t
the argument list is made up of (expressions/parameters)
expressions
What does the signature line/"contract" do?
explains the requirements for anyone who wants to use the function definition
what is the format of the signature/contract line? And give an example
def function name (parameter list) --> return type. def my_max(a: int) -> int
can you call a function before its' definition has been evaluated?
no
difference between function call and signature?
function signature has def, lists the parameters, and gives the return type. The call gives the function and arguments inside parenthesis
When tracing a program to produce a memory diagram, much like the Python interpreter processes a program, and a function definition statement is encountered, what happens in our memory diagram? Choose all that apply.
A. The name of the function is entered into the current (for now Globals) frame of the stack
B. A function object, represented as a box with the line numbers in which the function definition is found, is added to the heap
C. An arrow binds the name of the function on the stack to the definition on the heap
D. Choice 4 of 5:The statements inside the function definition body are evaluated immediately
E. A value is returned
a, b, c
What steps are needed to prepare for a function call when tracing a program?
A. Check for the name of the function being defined
B. Fully evaluate the argument expressions of the function call
C. Ensure agreement between arguments of call and parameters of definition
a, b, c
When function call expressions are evaluated, new frames are added to which area of the memory diagram?
A. stack
B. heap
C. output
a
The "Stack" area of a memory diagram is short for "Call Stack" because each time a function call is encountered a new frame is established on the stack. T/F
t
What happens when establishing a frame for a function call expression?
A. A new space in memory, called a frame, is added to the stack
B. The name of the function labels the frame
C. A Return Address ("RA"), which is the line the function call was encountered on is added to the frame
D. The parameters of the function definition are established as variable names within the stack frame
E. The fully evaluated values of argument expressions are assigned to the parameters in the function call frame
F. The Return Value ("RV") is added to the function call frame
a, b, c, d, e
The expression following the return keyword must evaluate to a value whose data type matches the return type specified in the signature line of its function definition. T/F
t
What is improper about the following function definition?
def hello_n(n: int) -> int:
"""A silly example function."""
return "hello " + str(n)
print(hello_n(3))
A. The parameter list is invalid
B. The definition is missing a docstring
C. The return statement's type does not match the return type of the signature
D. The argument of the function call does not match the parameter of the function definition
c
Each function with a return type must reach a return statement when called. T/F
t
When a return statement is evaluated, the function call process is completed. T/F
t
In the following Python program, how many times is a return statement evaluated when processing the function call?
def my_max(a: int, b: int) -> int:
"""Returns the largest argument."""
if a >= b:
return a
return b
print(my_max(10, 0))
1
what is the different between a parameter and an argument?
a parameter is in a function definition and is a placeholder for a value. An argument is a value passed during function call
what is our current frame?
the lowest frame on our stack that has yet to return
where are return statements found?
inside function definitions
Which of the following is a list type?
A. list [str]
B. names [0]
C. ["kris", "kaki", "marc"]
D. list ( )
a
Which of the following is a list access via the subscription operator?
A. list [str]
B. names [0]
C. ["kris", "kaki", "marc"]
D. list ( )
b
Which of the following constructs an empty list?
A. list [str]
B. names [0]
C. ["kris", "kaki", "marc"]
D. list ( )
d
How do you find the number of items in a list named names?
A. names.length
B. len (names)
C. length (names)
D. items (names)
b
What type of expression is expected in a while statement's test?
A. a str expression
B. an int expression
C. a bool expression
c
Which of the following methods adds an item to the end of a list?
A. push
B. pop
C. append
D. add
c
Given a list with at least one element in it, which expression accesses the first item of the list?
A. foo[0]
B. foo[1]
C. foo[2]
D. foo[len(foo) - 1]
E. foo[len(foo)]
a
Given a list with at least one element in it, which expression accesses the second item of the list?
A. foo[0]
B. foo[1]
C. foo[2]
D. foo[len(foo) - 1]
E. foo[len(foo)]
b
Given a list with at least one element in it named foo, which expression accesses the last item of the list?
A. foo[0]
B. foo[1]
C. foo[2]
D. foo[len(foo) - 1]
E. foo[len(foo)]
d
If you attempt to access an index that is invalid (such as an index that is greater than or equal to the length of the list), what kind of error is encountered?
A. IndexError
B. NameError
C. ListError
D. No error, a value of None is evaluated
a
Which of the following is a list literal expression?
A. list ( )
B. list [str]
C. names [0]
D. ["kris", "kaki", "marc"]
d
Which of the following methods allows you to remove an item from a list?
A. push
B. pop
C. append
D. add
b
It is possible to write a while loop with a fixed number of lines of code that will process an arbitrary number of items in a list. T/F
t
The implications of square brackets in the following three snippets of code each has a different meaning: (T/F)
list[int]
rolls[0]
[1, 2, 3]
t
It is possible to reassign items in a list using subscription notation on the left-hand side of the assignment operator T/F
t
It is NOT possible to have duplicate values in a list. T/F
f
How would you express a list of integers?
list [int]
how do you append a value to a list
rolls.append (expression [ T ] )
how do you access items in a list?
rolls [int expr]