Intro to python exam 1

Ace your homework & exams now with Quizwiz!

Dual alternative decision structure

two possible paths of execution - One is taken if the condition is true, and the other if the condition is false

Python has three types of loops:

- While - For - Nested

What will be displayed by the following program? my_value = 99 my_value = 0 print(my_value)

0

What would the following display? a = 5 b = 2 c = 3 result = a + b * c print(result)

11 First, the multiplication operation b * c (2 * 3) is performed, resulting in 6. Then, the addition operation a + 6 (5 + 6) is performed, resulting in 11. Finally, the value of 11 is printed to the console.

What will the following code display? total = 0 for count in range(1, 6): total = total + count print(total)

15 On the first iteration, count is 1 and total is 0. The expression total + count evaluates to 0 + 1 = 1. The new value of total becomes 1. On the second iteration, count is 2 and total is 1. The expression total + count evaluates to 1 + 2 = 3. The new value of total becomes 3. On the third iteration, count is 3 and total is 3. The expression total + count evaluates to 3 + 3 = 6. The new value of total becomes 6. On the fourth iteration, count is 4 and total is 6. The expression total + count evaluates to 6 + 4 = 10. The new value of total becomes 10. On the fifth and final iteration, count is 5 and total is 10. The expression total + count evaluates to 10 + 5 = 15. The new value of total becomes 15.

What would the following display? num = 99 num = 5 print(num)

5 This is because the variable num is first assigned the value 99, but then immediately reassigned the value 5. When the print statement is executed, it displays the current value of num, which is 5.

Which of the following are illegal variable names in Python, and why? x 99bottles july2009 theSalesFigureForFiscalYear r&d grade_report

99bottles: This variable name starts with a digit, which is not allowed in Python. r&d: This variable name contains a special character (&) which is not allowed in variable names in Python.

or a loop structure, causes one or more statements to repeat

A repetition

A sentinel

A sentinel is a special value that marks the end of a sequence of values.

\\

Causes a backslash character to be printed

\"

Causes a double quote mark to be printed.

\

Causes a single quote mark to be printed

\n

Causes output to be advanced to the next line.

\t

Causes output to skip over to the next horizontal tab position.

A program can be made of only one type of control structure. You cannot combine structures.

False

Programmers must be careful not to make syntax errors when writing pseudocode programs.

False

Variable names can have spaces in them.

False

A condition-controlled loop always repeats a specific number of times.

False A condition-controlled loop (e.g., while loop) repeats the loop statements as long as the specified condition evaluates to true. The condition can be based on a counter variable or any other expression, so the number of iterations of the loop is determined by the condition. It can repeat a specific number of times if the condition is based on a counter variable that is incremented or decremented in each iteration, but it can also repeat an unknown or variable number of times based on the condition.

If you print a variable that has not been assigned a value, the number 0 will be displayed.

False. If a variable has not been assigned a value, trying to access its value will result in an error.

What will the following code display? name = 'Karlie' print(f'Hello {name}')

Hello Karlie

Nested

In ______ loops, loops can go inside other loops

(n) __________ makes a variable reference a value in the computer's memory. a. variable declaration b. assignment statement c. math expression d. string literal

b

A __________ is a name that represents a value that does not change during the program's execution. a. named literal b. named constant c. variable signature d. key term

b

A __________ is any hypothetical person using a program and providing input for it. a. designer b. user c. guinea pig d. test subject

b

A compound Boolean expression created with the _________ operator is true if either of its subexpressions is true. a. and b. or c. not d. either

b

A magic number is ________________. a. a number that is mathematically undefined b. an unexplained value that appears in a program's code c. a number that cannot be divided by 1 d. a number that causes computers to crash

b

An informal language that has no syntax rules and is not meant to be compiled or executed is called __________. a. faux code b. pseudocode c. Python d. a flowchart

b

Suppose the following statement is in a program: price = 99.0. After this statement executes, the price variable will reference a value of which data type? a. int b. float c. currency d. str

b

Which built-in function can be used to convert an int value to a float? a. int_to_float() b. float() c. convert() d. int()

b

Which of the following statements will cause an error? a. x = 17 b. 17 = x c. x = 99999 d. x = '17'

b

You use a(n) __________ statement to write a single alternative decision structure. a. test-jump b. if c. if-else d. if-call

b

and, or, and not are __________ operators. a. relational b. logical c. conditional d. ternary

b

The -= operator is an example of a(n) __________ operator. a. relational b. augmented assignment c. complex assignment d. reverse assignment

b The -= operator is an example of an augmented assignment operator in Python. Augmented assignment operators combine a binary operator (such as +, -, *, /, %, etc.) with an assignment operator (such as =) to create a shorthand way of updating a variable's value.

11. The ___________ operator takes a Boolean expression as its operand and reverses its logical value. a. and b. or c. not d. either

c

16. This is an operator that raises a number to a power. a. % b. * c. ** d. /

c

A __________ is a sequence of characters. a. char sequence b. character collection c. string d. text block

c

Logical operators:

operators that can be used to create complex Boolean expressions

In the following statement, what is the purpose of the number 10 in the format specifier? print(f'{name:10}')

In the following statement, the number 10 in the format specifier is specifying the width of the field to be printed. In this case, the format specifier :10 is indicating that the output should be formatted as a string with a fixed width of 10 characters. If the length of the string assigned to name is less than 10 characters, the output will be padded with spaces to fill the remaining characters. For example, if name has a value of "Karlie", the output will be "Karlie ", with six spaces added after the "e" to fill up the 10-character width. If name has a value of "Kloss", the output will be "Kloss ", with five spaces added after the "s".

In the following statement, what is the purpose of the number 8 in the format specifier? print(f'{number:8,.2f}')

In the format specifier {number:8,.2f}, the number 8 specifies the minimum width of the field in characters. The field will be at least 8 characters wide, and if the formatted number is less than 8 characters, it will be padded with spaces to the left.

In the following statement, what is the purpose of the ^ character in the format specifier? print(f'{number:^12d}')

In the given statement, the ^ character is used as a part of the format specifier to center the output value within a field of a specified width. Here, ^12d means that the output value of the variable "number" will be displayed in a field that is 12 characters wide, and the value will be centered within the field.

In the following statement, what is the purpose of the < character in the format specifier? print(f'{number:<12d}')

The < character in the format specifier indicates that the value of the variable number should be left-justified within the available space of 12 characters.

In the following statement, what is the purpose of the > character in the format specifier? print(f'{number:>12d}')

The > character in the format specifier is used for right justification. It tells Python to align the variable value to the right of the specified field width, which is 12 in this case. If the variable value has fewer digits than the field width, the remaining spaces are filled with blank spaces.

What will the following code display? name = 'Karlie' print('Hello {name}')

The following code will output the string "Hello {name}" to the console. The reason for this is that the print() function is printing the literal string "Hello {name}", including the curly braces, rather than substituting the value of the variable name into the string.

In the following statement, what is the purpose of the number 15 in the format specifier? print(f'{number:15,d}')

The purpose of the number 15 in the format specifier is to allocate a minimum field width of 15 characters for the output, with the number being right-justified within that field. If the number does not take up all 15 characters, the remaining characters will be filled with spaces. In this case, the 'd' format specifier is used to format the number as an integer with comma separators for thousands.

What will the following code display? value = 99 print(f'The value is {value + 1}')

The value is 100 The following code will display the string "The value is 100" to the console. This is because an f-string is used to create a formatted string that includes the value of the value variable plus one. When the f-string is evaluated, the expression {value + 1} is replaced with the value of value plus one, which is 100. So, the final output of the print() function is the string "The value is 100".

What will the following code display? value = 65.4321 print(f'The value is {value:.2f}')

The value is 65.43

What will the following code display? value = 9876543210 print(f'The value is {value:,d}')

The value is 9,876,543,210

What will the following code display? value = 987654.129 print(f'The value is {value:,.2f}')

The value is 987,654.13 This is because an f-string is used to create a formatted string that includes the value of the value variable with two decimal places and a comma separator for thousands. The :,.2f format specifier is used to format the value of value. The comma indicates that the number should be formatted with a thousands separator, and the .2f specifies that the number should be displayed with two decimal places. So, the final output of the print() function is the string "The value is 987,654.13", with the value of value formatted according to the specified format specifier.

A compound Boolean expression created with the and operator is true only when both subexpressions are true.

True

A decision structure can be nested inside another decision structure

True

A single alternative decision structure tests a condition and then takes one path if the condition is true, or another path if the condition is false.

True

In Python, the first character of a variable name cannot be a number.

True

In a math expression, multiplication and division take place before addition and subtraction.

True

You can write any program using only sequence structures.

True

0 1 2 3 4

What is the output? for i in range (5):..print(i, end = " ")

time time time

What is the output? for x in range (3):..print("time", end = " ")

12

What is the output? j = 0 while j < 10: j+=3 print(j)

s e p t e

What is the output? word = "september" for let in word: ..if let != "m":.... print(let,) ..else: ....break

0 1 2 3 4

What is the output? x = 0 while(x<5): ..print(x) ..x+=1

condition

What is the term for x called? while(x)

19. Which built-in function can be used to read input that has been typed on the keyboard? a. input() b. get_input() c. read_input() d. keyboard()

a

A __________ is a diagram that graphically depicts the steps that take place in a program. a. flowchart b. step chart c. code graph d. program graph

a

A __________ is a name that references a value in the computer's memory. a. variable b. register c. RAM slot d. byte

a

A ___________ is a Boolean variable that signals when some condition exists in the program. a. flag b. signal c. sentinel d. siren

a

A compound Boolean expression created with the __________ operator is true only if both of its subexpressions are true. a. and b. or c. not d. both

a

In the expression 12 + 7, the values on the right and left of the + symbol are called __________. a. operands b. operators c. arguments d. math expressions

a

Short notes placed in different parts of a program explaining how those parts of the program work are called __________. a. comments b. reference manuals c. tutorials d. external documentation

a

This operator performs division, but instead of returning the quotient it returns the remainder. a. % b. * c. ** d. /

a

This operator performs integer division. a. // b. % c. ** d. /

a

A __________ -controlled loop uses a true/false condition to control the number of times that it repeats. a. Boolean b. condition c. decision d. count

a Boolean-controlled loop uses a true/false condition to control the number of times that it repeats.

The while loop is a __________ type of loop. a. pretest b. no-test c. prequalified d. post-iterative

a The while loop is a pretest loop, which means that the loop condition is evaluated before the loop body is executed. The loop statements are executed only if the condition is true, and the loop continues to execute as long as the condition remains true. If the condition is false from the beginning, the loop body will not be executed at all.

The symbols >, <, and == are all __________ operators. a. relational b. logical c. conditional d. ternary

a. relational

A(n) _________ structure tests a condition and then takes one path if the condition is true, or another path if the condition is false. a. if statement b. single alternative decision c. dual alternative decision d. sequence

c

The integrity of a program's output is only as good as the integrity of the program's . a. compiler b. programming language c. input d. debugger

c

You use a(n) __________ statement to write a dual alternative decision structure. a. test-jump b. if c. if-else d. if-call

c

A __________ -controlled loop repeats a specific number of times. a. Boolean b. condition c. decision d. count

d

A string literal in Python must be enclosed in __________. a. parentheses. b. single-quotes. c. double-quotes. d. either single-quotes or double-quotes.

d

A(n) __________ expression has a value of either True or False. a. binary b. decision c. unconditional d. Boolean

d

Each repetition of a loop is known as a(n) __________. a. cycle b. revolution c. orbit d. iteration

d

This symbol marks the beginning of a comment in Python. a. & b. * c. ** d. #

d

How many times will 'Hello World' be printed in the following program? count = 10 while count < 1: print('Hello World')

he 'Hello World' message will not be printed at all because the condition in the while loop is false, and the code inside the loop will not be executed. The initial value of the variable count is 10, and the condition count < 1 is false. Therefore, the code inside the loop will not be executed even once. If we change the condition to count > 1, the loop will execute at least once, and 'Hello World' will be printed once. However, the loop will not continue because count will not be updated within the loop. Therefore, the message will be printed only once.

Each repetition of the loop is called an

iteration


Related study sets

AUD 1 M7- emphasis of matter, other-matter, and explain paragraphs

View Set

NURS 10 Med Admin 1, Med Admin 2 & Med Admin 3, Safe Dosage Post test & Knowledge and Clinical Judgment Beginning Post tests

View Set