Quiz 2 Flashcards

1
Q

What is a Boolean Expression?

A

A bool is a data type. An expression that is tested to see whether it is true or false by an if statement in Python.

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

What is a control structure?

A

A control structure allows us to control the order in which a set of statements executes

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

What is a sequence structure?

A

a set of statements that execute in the order that they appear.

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

What is a dual alternative decision structure? How does it work? What statment do you use in Python to write a dual alternative decision structure?

A

if else clause. One taken if the condition is true and the other if the condition is false.

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

Write a snippet of code that will compare two strings. The program will print “They are the same” if the strings are identical. The program will print “They are not the same.” if the strings are different.

A
P = "something"
Q = "something"
if (P == Q):
       print ("They are the same.")
else:
       print("They are the same.")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a compound Boolean expression?

A

( P==Q and R==S)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What will the program display?
s1 = "Dillon"
s2= "Butte"
if s1 > s2:
     print (s2)
     print (s1)
else:
    print (s1)
    print (s2)
A

Butte

Dillon

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

Simplify:

Not (num >0 and temp <=0)

A

num <= 0 or temp >0

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

Simplify:

not (sum<5 or not (num>0)

A

sum >=5 and num>0

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

simplify:

not(num>0 and sum >0)

A

num<= 0 and sum <=0

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

simplify:

not( X and not Y) or (X and Y)

A

(not X or Y) and (X or Y)

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

What is a decision structure:

A

specific action(s) performed only if a condition exists

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

write an if statement that assigns 20 to the variable y and assigns 40 to the variable z if the variable x is greater than 100

A

if x > 100:
y = 20
Z = 40

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

What is a single alternative decision structure:

A

provides only one alternative path of execution

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

What is a relational operator?

A

determines whether a specific relationship exists between two values Example: greater than (>)

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

What are logical operators?

A

operators that can be used to create complex Boolean expressions

17
Q

Write an if-else statement that determines whether the points variable is outside the range 9 to 51. if the variable’s value is outside this range it should display “invalid points.” Otherwise, it should display “Valid points.”

A
points = 100
if points <  9 or points > 51:
        print("Invalid points:)
else:
        print("Valid points")
points = 100
if 9 <= points <= 51:
       print("valid points")
else:
       print("invalid points")
18
Q
Convert the nested if-else statements into an if-elif-else statement:
if num == 1:
     print("one")
else:
     if num == 2:
           print("two")
     else:
            if num == 3:
                 print("three")
           else:
                  print("unknown")
A
if num == 1:
     print ("one")
elif num == 2:
     print ("two")
elif num == 3:
     print ("three")
else:
     print("unknown")
19
Q

The area of a rectangle is the rectangle’s length times its width. Write a program that asks for the length and width of two rectangles. The Program should tell the user which rectangle has the greater are, or the areas are the same.

A
length1 = float(input("Enter the length of the first rectangle: "))
width1 = float(input("Enter the width of the first rectangle: "))
length2 = float(input("Enter the length of the second rectangle: "))
width2 = float(input("Enter the width of the second rectangle: "))

if ((length1width1) == (length2width2)):
print(“They have the same area”)
elif ((length1width1) > (length2width2)):
print(“The area of rectangle 1 is greater than rectangle 2”)
else:
print(“The area of rectangle2 is greater than rectangle 1.”)