Docsity

Python Conditionals and Boolean Logic: Practice Questions, Exams of Advanced Education

Toggle sidebar
bg1
TECHSMART PYTHON UNIT 2; PART 1 TEST
QUESTIONS AND ANSWERS
In the following code, identify the condition that includes the elif-clause.
if x == 1:
print("X is 1")
if y == 2:
print("y is 2")
elif x == w:
print("X is w")
elif x == 5:
print("X is 5")
if x < 53:
print("x is less than 53") - ANSWER if y == 2:
print("y is 2")
elif x == w:
print("X is w")
elif x == 5:
print("X is 5")
Remember that a conditional starts with an if-clause.
Which of the following is not a type of conditional clause?
if, elif, then, or else - ANSWER then
What data type can only be represented as Yes or No and True or False? -
ANSWER boolean
Example of a boolean expression - ANSWER a == b;
The three basic boolean operators are: - ANSWER AND, OR, and NOT.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Python Conditionals and Boolean Logic: Practice Questions and more Exams Advanced Education in PDF only on Docsity!

TECHSMART PYTHON UNIT 2; PART 1 TEST

QUESTIONS AND ANSWERS

In the following code, identify the condition that includes the elif-clause. if x == 1: print("X is 1") if y == 2: print("y is 2") elif x == w: print("X is w") elif x == 5: print("X is 5") if x < 53: print("x is less than 53") - ANSWER if y == 2: print("y is 2") elif x == w: print("X is w") elif x == 5: print("X is 5") Remember that a conditional starts with an if-clause. Which of the following is not a type of conditional clause? if, elif, then, or else - ANSWER then What data type can only be represented as Yes or No and True or False? - ANSWER boolean Example of a boolean expression - ANSWER a == b; The three basic boolean operators are: - ANSWER AND, OR, and NOT.

How many times will the following code print the statement "Print me"? x = 0 while x < 1000: print("Print me") x += 100 - ANSWER 10 Identify the if-clause in the following code. if x < y: print("x is smaller") print("y is bigger") - ANSWER if x < y: Identify the condition in the following code. if x < y: print("x is smaller") print("y is bigger") - ANSWER x < y What happens if the condition in a conditional is False? - ANSWER The program skips the then-block In the following code, the then-block will never execute. Why? if 100 < 10: print("We executed!") print("Congratulations!") print("The program finished.") - ANSWER The condition will always be false. Add a symbol to the conditional so it is formatted correctly. x = 10 y = 5 if x > y print("x is bigger than y") - ANSWER add a colon after if x > y. Should be written if x > y:

Tell whether the result of this boolean expression is True or False. not 0 < 10 - ANSWER False If the word yes prints below, what is the value of the variable x? if x == y: if y == 100: print("yes") else: print("no") - ANSWER 100 Identify the error in the following conditional. if x > 100 print("x is greater than 100") - ANSWER if x > 100: print("x is greater than 100") What will print when this code executes? x = 0 y = 0 if x == y: print("x and y are equal") if x == 0: print("x is 0") - ANSWER x and y are equal, x is 0 (on separate lines) What will print when this code executes? x = 0 y = 0 if x == y: print("x and y are equal") elif x == 0: print("x is 0") - ANSWER x and y are equal What is the maximum number of else-clauses in a single conditional? - ANSWER 1 How would you fix the comparison operator in the following conditional so the print statement is accurate. if people > 50:

print("There are fewer than 50 people.") - ANSWER if people < 50: Why will the code below cause an error? if x == 100: print("100") elif: print("x is not 100") - ANSWER elif must have a condition What is the fewest lines that can be in a then-block? - ANSWER 1 Which logical operator gives the opposite of a boolean value? - ANSWER not Identify the expressions in the following code that produce a boolean value. There are 2. weather = sleet or snow outside = not weather print("I should go outside: " + str(outside)) - ANSWER sleet or snow not weather Identify the expression in the following code that produces a boolean value. print("Check if the values are the same: ") same = x == y print(same) - ANSWER x == y Which of the following single clauses could replace both the nested clauses while keeping the same meaning? if x == y: if x == 0: print("y is 0") - ANSWER if x == y and x == 0: What is the value inside the variable z after this code executes? x = True y = True z = x or y - ANSWER True Which of these is NOT a benefit of using loops?

What is the name of the library used in this code? import random x = 0 y = 0 if x == y: print("x and y are equal") elif x == 0: print("x is 0") - ANSWER random If you encounter a error in your code, what is the most likely cause to prevent your code from running? - ANSWER syntax error What is the most common cause for a runtime error? - ANSWER Not typecasting a variable correctly. What symbol is used to separate multiple arguments while using a function call?

  • ANSWER , or comma How many times will the following code print the statement "Print me"? import random loops = random.randint(1, 200) while loops > 0: loops - = 1 print("Print me") - ANSWER 200 What is one use for a dot operator in Phyton? - ANSWER Call a function from a library What two arguments does the random.randint function require? - ANSWER minimum and maximum If you experience errors in your code, how might you find a logic error? - ANSWER Insert print statements If you experience errors in your code, how might you find a runtime error? - ANSWER Use error messages or print statements

If you experience errors in your code, how might you find a syntax error? - ANSWER Look at the error message that pops up