Selections Flashcards

1
Q

A Boolean expression is an expression that evaluates to a ______ value _____ or ______.

A

A Boolean expression is an expression that evaluates to a Boolean value True or False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
The word True is \_\_\_\_\_\_\_\_.
 A. a Python keyword
 B. a Boolean literal
 C. same as value 1
 D. same as value 0
A

A. a Python keyword

B. a Boolean literal

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

What does the randint(a, b) function do?

A

The randint(a, b) function can be used to generate a random integer between a and b, inclusively.

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

How do you generate a random integer i such that 0 <= i < 20?

A

random.randrange(0, 20) or random.randint(0, 19)

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

How do you generate a random integer i such that 10 <= i < 20?

A

random.randrange(10, 20) or random.randint(10, 19)

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

How do you generate a random integer i such that 10 <= i <= 50?

A

random.randrange(10, 50 + 1) or random.randint(10, 50)

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

How do you generate a random integer 0 or 1?

A

random.randrange(0, 2) or random.randint(0, 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
To generate a random integer between 0 and 5, use \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.
 A. random.randint(0, 5)
 B. random.randint(0, 6)
 C. random.randrange(0, 5)
 D. random.randrange(0, 6)
A

A. random.randint(0, 5)
D. random.randrange(0, 6)

Hint: In A, randint(0, 5) returns a sequence from 0, 1, 2, 3, 4, 5. In B, randint(0, 6) returns a sequence from 0, 1, 2, 3, 4, 5, 6. In C, randrange(0, 5) returns a sequence from 0, 1, 2, 3, 4. In D, randrange(0, 6) returns a sequence from 0, 1, 2, 3, 4, 5. A and D are correct.

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

random.random() returns ____________.
A. a float number i such that 0 < i < 1.0
B. a float number i such that 0 <= i < 1.0
C. a float number i such that 0 <= i <= 1.0
D. a float number i such that 0 < i < 2.0

A

B. a float number i such that 0 <= i < 1.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
 A. if isPrime = True:
 B. if isPrime == True:
 C. if isPrime:
 D. if not isPrime = False:
 E. if not isPrime == False:
A

B. if isPrime == True:
C. if isPrime:

Both correct, but C is better.

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

Write an if statement that increases pay by 3% if score is greater than 90, otherwise it increases pay by 1%.

A

if score > 90:
pay *= 1.03
else:
pay *= 1.01

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

What is the outpout of the code in (a) and (b) if number is 30 and 35?
(a)
if number % 2 == 0:
print(number, “is even.”)

print(number, "is odd.")
(b)
if number % 2 == 0: 
    print(number, "is even.")
else: 
    print(number, "is odd.")
A

If number is 30,
(a) displays
30 is even
30 is odd

(b) displays
30 is even

If number is 35,
(a) displays
35 is odd

(b) displays
35 is odd

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

Are the following statements correct? Which one is better?
(a)
if age < 16:
print(“Cannot get a driver’s license”)
if age >= 16:
print(“Can get a driver’s license”)

(b)            
if age < 16:
    print("Cannot get a driver's license") 
else: 
    print("Can get a driver's license")
A

Both are correct. (b) is better. Both conditions in (a) are tested. In (b) the condition is tested only once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Rewrite the following statement using a Boolean expression:
if count % 10 == 0:
    newLine = True
else:
    newLine = False
A

newLine = (count % 10 == 0)

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

Analyze the following code:

even = False
if even = True:
print(“It is even!”)
A. The program has a syntax error in line 1 (even = False)
B. The program has a syntax error in line 2 if even = True is not a correct condition. It should be replaced by if even == True: or simply better using if even:.
C. The program runs, but displays nothing.
D. The program runs and displays It is even!.

A

B. The program has a syntax error in line 2 if even = True is not a correct condition. It should be replaced by if even == True: or simply better using if even:.

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

Analyze the following code.

even = False
if even:
print(“It is even!”)
A. The code displays It is even!
B. The code displays nothing.
C. The code is wrong. You should replace if even: with if even == True:
D. The code is wrong. You should replace if even: with if even = True:

A

B. The code displays nothing.

Hint: even is False. Nothing is displayed.

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

Analyze the following code:

Code 1:

if number % 2 == 0:
even = True
else:
even = False

Code 2:

even = number % 2 == 0
A. Code 1 has compile errors.
B. Code 2 has compile errors.
C. Both Code 1 and Code 2 have compile errors.
D. Both Code 1 and Code 2 are correct, but Code 2 is better.

A

D. Both Code 1 and Code 2 are correct, but Code 2 is better.

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

Suppose income is 4001, what will be displayed by the following code?

if income > 3000:
    print("Income is greater than 3000")
elif income > 4000:
    print("Income is greater than 4000")
 A. none
 B. Income is greater than 3000
 C. Income is greater than 3000 followed by Income is greater than 4000
 D. Income is greater than 4000
 E. Income is greater than 4000 followed by Income is greater than 3000
A

B. Income is greater than 3000

Hint: Since income is 4001, the condition (income > 3000) is true. So statement for the true case is executed. The elif clause is not executed in this case.

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

The __________ function immediately terminates the program.

A

sys.exit()

20
Q
Assuming that x is 1, show the result of the following Boolean expressions.
True and (3 > 4)
not (x > 0) and (x > 0)
    (x > 0) or (x < 0)
    (x != 0) or (x == 0)
    (x >= 0) or (x < 0)
    (x != 1) == not (x == 1)
A

(true) and (3 > 4)
False

not(x > 0) and (x > 0)
False

(x > 0) or (x < 0)
True

(x != 0) or (x == 0)
True

(x >= 0) or (x < 0)
True

(x != 1) == not (x == 1)
True

21
Q

(a) Write a Boolean expression that evaluates to True if variable num is between 1 and 100. (b) Write a Boolean expression that evaluates to True if variable num is between 1 and 100 or the number is negative.

A

(a) (x > 1) and (x < 100)

b) (x > 1 and x < 100) or (x < 0

22
Q

(a) Write a Boolean expression for |x - 5| < 4.5. (b) Write a Boolean expression for |x - 5| > 4.5.

A

(a) (-4.5 <= x - 5 < 4.5)

b) (x - 5 > 4.5 and x - 5 <= -4.5

23
Q
Assuming x = 4 and y = 5, show the result of the following Boolean expressions.
x >= y >= 0
x <= y >= 0
x != y == 5
(x != 0) or (x == 0)
A

x >= y >= 0 False
x <= y >= 0 True
x != y == 5 True
(x != 0) or (x == 0) True

24
Q

Are the following expressions equivalent?

a. (x >= 1) and (x < 10)
b. (1 <= x < 10)

A

Yes

25
Q

What is the value of the expression 50 <= x <= 100 if x is 45, 67, or 101?

A

It is False for x is 45.
It is True for x is 67.
It is False for x is 101.

26
Q

Write a Boolean expression that evaluates to true if weight is greater than 50 and height is greater than 160.

A

weight > 50 and height > 160.

27
Q

Write a Boolean expression that evaluates to true if either weight is greater than 50 or height is greater than 160, but not both.

A

(weight > 50 or height > 160) and not (weight > 50 and height > 160)

28
Q
Which of the Boolean expressions below is incorrect?
 A. True and 3 => 4
 B. !(x > 0) and (x > 0)
 C. (x > 0) or (x < 0)
 D. (x != 0) or (x = 0)
 E. (-10 < x < 0)
A

A. True and 3 => 4
B. !(x > 0) and (x > 0)
D. (x != 0) or (x = 0)

Hint: a: (3 => 4) should be (3 >= 4), b: !(x > 0) should be not (x > 0) d: (x = 0) should be (x == 0)

29
Q
Which of the following is the correct expression that evaluates to True if the number x is between 1 and 100 or the number is negative?
 A. 1 < x < 100 and x < 0
 B. (x < 100 and x > 1) or (x < 0)
 C. (x < 100 and x > 1) and (x < 0)
 D. (1 > x > 100) or (x < 0)
A

The correct answer is ABCD.

They are all correct.

30
Q
To check whether a char variable ch is an uppercase letter, you write \_\_\_\_\_\_\_\_\_\_\_.
 A. (ch >= 'A' and ch >= 'Z')
 B. (ch >= 'A' and ch <= 'Z')
 C. (ch >= 'A' or ch <= 'Z')
 D. ('A' <= ch <= 'Z')
A

B. (ch >= ‘A’ and ch <= ‘Z’)
D. (‘A’ <= ch <= ‘Z’)

B and D are correct.

31
Q
Given |x| <= 4, Which of the following is true?
 A. x <= 4 and x >= 4
 B. x <= 4 and x > -4
 C. x <= 4 and x >= -4
 D. x <= 4 or x >= -4
A

C. x <= 4 and x >= -4

The correct answer is C.

Hint: |x| <= 4 means -4 <= x <= 4. That is x <= 4 and x >= -4. |x| <= 4 is true for x being -4, -3, 0, 2, 4, etc. So the correct answer is C.

32
Q
Given |x| >= 4, Which of the following is true?
 A. x >= 4 and x <= -4
 B. x >= 4 or x <= -4
 C. x >= 4 and x < -4
 D. x >= 4 or x <= -4
A

B. x >= 4 or x <= -4

The correct answer is B.

Hint: |x| >= 4 means x >= 4 or x <= -4. |x| >= 4 is true for x being -4, -5, -6, 4, 5, 6, etc. So B is correct.

33
Q
Assume x = 14 and y = 15, Which of the following is true?
 A. x % 2 == 0 and y % 2 == 0
 B. x % 2 == 0 and y % 2 == 1
 C. x % 2 == 0 or y % 2 == 0
 D. x % 2 != 0 and y % 2 != 0
A

The correct answer is BC.

34
Q
Which of the following is equivalent to x != y?
 A. not (x == y)
 B. x > y and x < y
 C. x > y or x < y
 D. x >= y or x <= y
A

The correct answer is AC.

35
Q

What will be displayed by the following code?

ch = ‘F’
if ch >= ‘A’ and ch <= ‘Z’:
print(ch)

A. F
B. f
C. nothing
D. F f

A

A. F

36
Q

What happens if you enter an integer as 05?

A

It will be the same as entering 5.

37
Q
Rewrite the following if statements using a conditional expression.
(a)            
if ages >= 16:
    ticketPrice = 20
else:
    ticketPrice = 10
(b)
if count % 10 == 0:
    print(count, end = "*")
else:
    print(count, end = "#")
A

(a) ticketPrice = 20 if (ages >= 16) else 10

b) print(count, end = “*” if count % 10 == 0 else “#”

38
Q

Rewrite the following conditional expressions using if/else statements.

a. score = 3 * scale if x > 10 else 4 * scale
b. tax = income * 0.2 if income > 10000 else income * 0.17 + 1000
c. print(i if number % 3 == 0 else j)

A
A:
if x > 10:
    score = 3 * scale
else: 
    score = 4 * scale
B:
if income > 10000:
    tax = income * 0.2
else: 
    tax = income * 0.17 + 1000
C:
if number % 3 == 0:
    print(i)
else: 
    print(j)
39
Q

Rewrite conditional expression that returns -1 or 1 randomly.

A

-1 if random.randint(0, 1) == 0 else 1

40
Q

What is tax if income is 1000 and 20000, respectively?

a. tax = income * 0.2 if income > 10000 else 0.1
b. tax = income * (0.2 if income > 10000 else 0.1)

A

For income 1000, a. 0.1 and b. 100.0

For income 20000, a. 4000.0 and b. 4000.0

41
Q

Analyze the following code fragments that assign a boolean value to the variable even.

Code 1: 
if number % 2 == 0:
    even = True
else: 
    even = False

Code 2:
even = True if number % 2 == 0 else False

Code 3:
even = number % 2 == 0

A. Code 2 has a syntax error, because you cannot have True and False literals in the conditional expression.
B. Code 3 has a syntax error, because you attempt to assign number to even.
C. All three are correct, but Code 1 is preferred.
D. All three are correct, but Code 2 is preferred.
E. All three are correct, but Code 3 is preferred.

A

E. All three are correct, but Code 3 is preferred.

42
Q
The order of the precedence (from high to low) of the operators +, *, and, or is:
 A. and, or, *, +
 B. *, +, and, or
 C. *, +, and, or
 D. *, +, or, and
 E. or, and, *, +
A

C. *, +, and, or

The correct answer is C.

43
Q
Which of the following operators are right-associative.
 A. *
 B. +
 C. %
 D. and
 E. =
A

E. =

44
Q

Which of the following statements are True?
A. (x > 0 and x < 10) is same as (x > 0 and x < 10)
B. (x > 0 or x < 10) is same as (0 < x < 10)
C. (x > 0 or x < 10 and y < 0) is same as (x > 0 or (x < 10 and y < 0))
D. (x > 0 or x < 10 and y < 0) is same as ((x > 0 or x < 10) and y < 0)

A

The correct answer is AC.

45
Q

A Boolean type variable can store a ____ or _____ value

A

True or False