Exam 1 Flashcards

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

A

a = a +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

A

8

22
Q

True or False: The / operator sometimes returns an integer

A

False

23
Q

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

A

True

24
Q

What does modulo do?

A

Returns the remainder after division is performed

25
Q

Which operation should be performed first in the following expression?

7 / 2 + (5 % 2 * 5) ** 2 - 3

A

5 % 2

26
Q

Which operators can be used with strings?

A
  • and +
27
Q

Which data type is used by a conditional to know which part of the code should be executed?

A

Boolean

28
Q

Why might you want to use a compound conditional?

A

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
Q

Why might you NOT want to use a compound conditional?

A

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
Q

Create a compound conditional that prints Hello World. Assume the variable color has already been declared and has the value red

A

A compound conditional requires two boolean expressions joined by either and or or.

if color == ‘red’ or color == ‘pink’:
print(“Hello World”)

31
Q

What is the purpose of the random.seed() function

A

To set up a predictable sequence of pseudo-random numbers

32
Q

What is the main difference between a loop and a conditional?

A

A loop repeats the block of code as long as its condition is true

33
Q

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)

A

n
n > 0
n = n - 1

34
Q

What will cause an infinite loop?

A

Not updating the loop control variable in the body of the loop

35
Q

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

A

7

36
Q

What is the difference between a definite loop and an indefinite loop?

A

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
Q

What purpose does a break statement have on a loop?

A

A break statement is used to exit a loop

38
Q
A