Exam 1 Flashcards

(38 cards)

1
Q

What is wrong with the code snippet below?

print(Hello world)

A

Missing “ “ around Hello world

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: Python automatically inserts a newline character when you use the print command

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Single line comment

A

#

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Multiple line comment

A

’’’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the print statement do?

A

Sends your output to the screen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which statement will print Hi! without a newline character?

A

print(“Hi!” , end= ‘ ‘)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How many arguments are in each of the following calls to the print function?

print(end= ‘ ‘)
print()
print(“Hi”,”there”)
print(“Hi there”)
print(“The quick brown fox” , “jumps” , “over the lazy” , “dog”, end= ‘\n’)

A

10215

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False: the two names below refer to the same variable

My_Variable
my_variable

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Declare the variable my_variable and assign it the value of “red” and assign another variable to overwrite this with “blue”

A

my_variable = “red”
my_variable = “blue”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the rule regarding boolean values in Python?

A

Boolean values in Python are case sensitive. They must start with a capital letter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Strings

A

Collections of letters, numbers, and symbols

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Boolean

A

True or False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Ints

A

Integers (no decimals)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Floats

A

Numbers with a decimal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Select the ways that a string can be declared

my_string = “I am a string”
my_string = “I am a string’
my_string = ‘“I am a string”’
my_string = ‘I am a string’

A

my_string = “I am a string”
my_string = ‘“I am a string”’
my_string = ‘I am a string’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the rules for variable names in Python?

A

You cannot use a keyword

The variable name must start with a letter or an underscore

The rest of the variable name can only be letters, numbers, or an underscore

17
Q

Declare a variable my_boolean and assign it the boolean value of true. Print the variable

A

my_boolean = True
print(my_boolean)

18
Q

Use two print statements to write the following string:
Okay, it is time to learn about operators

A

print(“Okay, “, end= ‘ ‘)
print(“it is time to learn about operators.”)

19
Q

What happens when you add an int and a float?

A

You get a float

20
Q

Select the line of code below that increments the variable by 1

21
Q

In the following line of code, what value is stored in variable b when the program ends?

a = 5
b = 3
c = a + b
a = 2
b = c

22
Q

True or False: The / operator sometimes returns an integer

23
Q

True or False: The // operator always returns a whole number value

24
Q

What does modulo do?

A

Returns the remainder after division is performed

25
Which operation should be performed first in the following expression? 7 / 2 + (5 % 2 * 5) ** 2 - 3
5 % 2
26
Which operators can be used with strings?
* and +
27
Which data type is used by a conditional to know which part of the code should be executed?
Boolean
28
Why might you want to use a compound conditional?
Compound conditionals allow you to test two or more things to be true, and they can make your code more concise and easy to read
29
Why might you NOT want to use a compound conditional?
When you have conditions that depend on the outcome of other conditions and with several variables, constructing a single boolean expression that is correct is difficult and hard to read and understand.
30
Create a compound conditional that prints Hello World. Assume the variable color has already been declared and has the value red
A compound conditional requires two boolean expressions joined by either and or or. if color == 'red' or color == 'pink': print("Hello World")
31
What is the purpose of the random.seed() function
To set up a predictable sequence of pseudo-random numbers
32
What is the main difference between a loop and a conditional?
A loop repeats the block of code as long as its condition is true
33
In the following code, identify the loop control variable, the looping condition, and the update to the loop control. n = int(input("Enter a number: ")) sum = 0 while n > 0: sum = sum + n n = n - 1 print("The sum of the first n numbers is", sum)
n n > 0 n = n - 1
34
What will cause an infinite loop?
Not updating the loop control variable in the body of the loop
35
How many times is the following while loop going to check it’s condition? i = 0 while i <= 5: print (i+1) i = i + 1
7
36
What is the difference between a definite loop and an indefinite loop?
A definite loop will execute a predictable number of times while an indefinite loop will execute an unpredictable number of times which depends on circumstances.
37
What purpose does a break statement have on a loop?
A break statement is used to exit a loop
38