COMP 110 Quiz 0 Gradescope 8-14

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/99

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

100 Terms

1
New cards

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

2
New cards

is return a function call?

no

3
New cards

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

4
New cards

The while statement allows you to repeat a block of statements in your program. T/F

t

5
New cards

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

6
New cards

You can name a variable while in your program without Python confusing your variable's name and the while keyword. T/F

f

7
New cards

The condition in the while statement's syntax must be a bool expression. T/F

t

8
New cards

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

9
New cards

The while loop keeps going until the condition becomes (True/False)

False

10
New cards

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

11
New cards

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

12
New cards

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

13
New cards

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

14
New cards

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

15
New cards

The number of iterations a loop runs for is the number of times the repeat block is evaluated. T/F

t

16
New cards

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

17
New cards

The phrase "if loop" is incorrect and should not be said. The phrase "while loop" is correct. T/F

t

18
New cards

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

19
New cards

The variable i is commonly used as a counter variable when writing while loops. T/F

t

20
New cards

What is the result of the following boolean expression?

not True

False

21
New cards

The answer to the condition tells you if you should enter the loop, and is a boolean value. T/F

t

22
New cards

In a while loop, if the condition is True you enter the ____ block

repeat

23
New cards

What is the result of the following boolean expression?

not not True

True

24
New cards

What is the result of the following boolean expression?

not True and not True

False

25
New cards

What does the not operator do?

it inverts the boolean (True becomes False and vice versa)

26
New cards

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

27
New cards

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

28
New cards

What is the order of precedence for the 3 boolean operators in order from first to last?

not, and, or

29
New cards

What is the result of the following boolean expression?

not True or not False

True

30
New cards

What is the result of the following boolean expression?

not False or not False

True

31
New cards

What is the result of the following boolean expression?

True and False or False and not False

False

32
New cards

Consider the following expression:

True and False or False and not False

Which operator got evaluated first?

not

33
New cards

Which operator evaluates last?

or

34
New cards

A string is a sequence of characters. T/F

t

35
New cards

All characters are letters, but not all letters are characters. T/F

f

36
New cards

Every individual character in a str has a corresponding int value. T/F

t

37
New cards

What does the following expression evaluate to?

ord("C")

67

38
New cards

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

39
New cards

What does the following expression evaluate to?

ord("C") > ord("A")

True

40
New cards

What does the following expression evaluate to?

"C" > "A"

True

41
New cards

Do these two expressions evaluate to the same thing?

ord("C") > ord("A")

"C" > "A"

yes

42
New cards

How many digits are there in hexidecimal ("hex") numbers?

16

43
New cards

what does the chr ( ) function do?

it converts an int to a character, so the opposite of the ord ( ) function

44
New cards

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

45
New cards

The following string uses an escape sequence that causes it to span multiple lines when printed? T/F

"Hello\nworld\n!!!"

t

46
New cards

What does /n do and what is it?

it is an escape sequence and gives a new line

47
New cards

What will "The computer said, \"Hello, world.\"" evaluate to?

The computer said "Hello, world."

48
New cards

What would be printed from this code snippet?

age: int = 21

msg: str = f"You are {age}!"

print(msg)

You are 21!

49
New cards

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

50
New cards

Function calls are expressions that evaluate to a specific data type. T/F

t

51
New cards

When you define a function you are invoking its call. T/F

f

52
New cards

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

53
New cards

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

54
New cards

Functions in programming are exactly equivalent to algebraic functions. T/F

f

55
New cards

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,

56
New cards

Each argument is an expression. T/F

t

57
New cards

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,

58
New cards

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

59
New cards

what is a function call?

when we call a function, we are invoking its definition

60
New cards

what is a function definition?

sub-programs that specify what actually happens when a function is called

61
New cards

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

62
New cards

Parameters are found in function definitions. Arguments are found in function calls. T/F

t

63
New cards

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

64
New cards

the argument list is made up of (expressions/parameters)

expressions

65
New cards

What does the signature line/"contract" do?

explains the requirements for anyone who wants to use the function definition

66
New cards

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

67
New cards

can you call a function before its' definition has been evaluated?

no

68
New cards

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

69
New cards

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

70
New cards

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

71
New cards

When function call expressions are evaluated, new frames are added to which area of the memory diagram?

A. stack

B. heap

C. output

a

72
New cards

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

73
New cards

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

74
New cards

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

75
New cards

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

76
New cards

Each function with a return type must reach a return statement when called. T/F

t

77
New cards

When a return statement is evaluated, the function call process is completed. T/F

t

78
New cards

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

79
New cards

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

80
New cards

what is our current frame?

the lowest frame on our stack that has yet to return

81
New cards

where are return statements found?

inside function definitions

82
New cards

Which of the following is a list type?

A. list [str]

B. names [0]

C. ["kris", "kaki", "marc"]

D. list ( )

a

83
New cards

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

84
New cards

Which of the following constructs an empty list?

A. list [str]

B. names [0]

C. ["kris", "kaki", "marc"]

D. list ( )

d

85
New cards

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

86
New cards

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

87
New cards

Which of the following methods adds an item to the end of a list?

A. push

B. pop

C. append

D. add

c

88
New cards

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

89
New cards

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

90
New cards

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

91
New cards

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

92
New cards

Which of the following is a list literal expression?

A. list ( )

B. list [str]

C. names [0]

D. ["kris", "kaki", "marc"]

d

93
New cards

Which of the following methods allows you to remove an item from a list?

A. push

B. pop

C. append

D. add

b

94
New cards

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

95
New cards

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

96
New cards

It is possible to reassign items in a list using subscription notation on the left-hand side of the assignment operator T/F

t

97
New cards

It is NOT possible to have duplicate values in a list. T/F

f

98
New cards

How would you express a list of integers?

list [int]

99
New cards

how do you append a value to a list

rolls.append (expression [ T ] )

100
New cards

how do you access items in a list?

rolls [int expr]