What does a evaluate to in control flow?
Click to see answer
True or False.
Click to see question
What does a evaluate to in control flow?
True or False.
How should range tests like a ≤ x < b be written in Python?
As (a <= x) and (x < b).
What happens if the is True?
Expressions in that block are evaluated.
What is a common mistake when using assignment and comparison operators?
Mixing '=' with '=='.
What is the purpose of using logical operators in programming?
To simplify some program code.
Are the two provided programs the same?
No, they are not the same.
What does the condition 'mark >= 70 and mark < 75' check?
It checks if the mark is between 70 and 75.
What should the program do when the user inputs an even number?
Print 'Even'.
What will happen if 'count' is zero in the second program?
It will give an error.
What does the pass statement indicate in an if-statement?
That no actions should be done.
What output is produced if the mark is between 70 and 75?
You get a B+.
What data type represents true or false values in Python?
bool
What can you use to force the evaluation order in expressions?
Parentheses.
What is the correct way to add parentheses to show the precedence in the expression '17 x + 3 >= 9 or z != x * 2 and not(y / 2 < 3)'?
(17 x + 3 >= 9) or (z != (x * 2) and not(y / 2 < 3))
What should the program do when the user inputs an odd number?
Print 'Odd'.
What is the purpose of indentation in Python?
To determine when one line of code is connected to the line above it.
What is the condition checked in both programs?
Whether 'scores / count' is less than 60 and 'count' is not zero.
Can the body of an if-statement be empty?
No, it can't be empty.
How can the code be simplified using logical operators?
By combining conditions into a single if statement.
What is a potential problem in the provided code examples?
Dangling 'else' problem.
What should the test expression (condition) evaluate to?
Either True or False.
What logical operator has higher precedence in the expression provided?
The 'and' operator has higher precedence than 'or'.
Which operators have the highest precedence?
Logical NOT.
What does indentation refer to in Python?
The spaces at the beginning of a code line.
What is the purpose of an if/else statement?
To allow the program to choose only one of two alternatives.
What is the main condition being checked in the program?
Whether the number is even or odd.
What is the condition for a year to be a leap year?
A leap year is divisible by 4, but not divisible by 100, unless it is divisible by 400.
What is the significance of short-circuit evaluation in the first program?
It prevents division by zero by checking 'count != 0' first.
How many spaces are recommended for each indentation level in Python?
Four spaces.
What happens if y is equal to 0 in the provided code?
The pass statement is executed, and no action is taken.
What is the result of 'not a' when a is True?
False.
Why is indentation important in Python?
It determines the block of code that belongs to a conditional statement.
What is an expression in programming?
An expression has a value and a type.
What does the 'not' operator do in the expression?
It negates the result of the expression that follows it.
What is the order of arithmetic operators in terms of precedence?
*, /, %, //, +, −.
What is a block of code in Python?
A composed set of several statements to be executed when a certain condition is met.
What happens when y is not equal to 0 in the provided code?
The program calculates and prints the value of x divided by y.
What is the purpose of a test expression in control flow?
To determine whether a statement should be executed based on a condition.
What does the program prompt the user to input?
The year to be checked.
What types of data can be used to form a test expression?
int, float, string.
What type of input does the program ask from the user?
A number.
What is calculated when y is not equal to 0 in the provided code?
The value of x divided by y.
Why might template files use fewer spaces for indentation compared to Python files?
Template files tend to have more levels of nesting.
What is short-circuit evaluation?
Sub-expressions in a test expression are not evaluated if the value of the entire expression is already known.
What does the first if statement check in the provided code?
If x is greater than 0.
What is the result of 'not a' when a is False?
True.
What does the variable 'isZero' represent in the expression 'isZero = (y == 0)'?
It checks if 'y' is equal to 0.
In the expression 'y / 2 < 3', what is being compared?
The result of 'y / 2' is being compared to 3.
How does Python use indentation?
To indicate a block of code.
What are the relational operators listed?
<, <=, >, >=, ==, !=.
What does the program print when y equals 0?
It prints 'Y is zero.'
What happens if the condition in a branching statement is false?
The statement is skipped.
What is the output if the year is a leap year?
The year is a leap year!
What logical operations can be used to form a test expression?
Logical operations on bools.
What is the result of 'a and b' when both a and b are True?
True.
What does the second if statement check in the provided code?
If x is even (x % 2 == 0).
In the expression (expr1 and expr2), when is expr2 not evaluated?
When expr1 is false, making the whole expression false.
What is the recommended maximum line length in Python code?
Less than 80 characters.
What is the main characteristic of sequential programs?
All statements are executed one-by-one.
What type of input is expected for x and y in the code?
Integer input.
What are the only two possible values of the bool type?
True or False.
How many if-statement bodies are executed regardless of the value of y?
Only one if-statement body is executed.
Which logical operator represents AND?
and.
What constitutes a block of code in Python?
Code written at the same indentation level.
What does the test expression 'y != 0' check?
Whether y is not equal to 0.
What is a block statement in an if-statement?
A block of code that executes multiple actions when the test expression is true.
What is the output if the year is not a leap year?
The year isn't a leap year!
In the expression 'if (a and b)', what are 'a' and 'b'?
Variable names with Boolean values.
What is the result of 'a and b' when either a or b is False?
False.
What should be avoided regarding blank lines in Python programs?
Excessive use of blank lines.
What will be printed if x is a positive odd number?
It is a positive odd number.
How does short-circuit evaluation simplify program code?
By avoiding unnecessary evaluations of sub-expressions.
Why do we use branching in programming?
To execute statements only if certain criteria are satisfied.
What type of input does the program expect for x and y?
Integer input.
Which logical operator represents OR?
or.
What is the purpose of an if-statement?
To perform branching based on a test expression.
What are relational operators?
Operators used to compare values, such as ==, !=, <, >, <=, and >=.
What Python function is used to get user input in the program?
input()
What is composed of several statements in an if-statement?
A block of code.
What is a test expression?
An expression formed by comparison or logical operations.
What is the result of 'a or b' when both a and b are False?
False.
What will be printed if x is a positive even number?
It is a positive even number.
What should be considered when using short-circuit evaluation?
The order of the sub-expressions.
What is the value of 'pset_time' in the example?
15
What is the operator for exponentiation?
**.
What is an example of a condition that can cause a run-time error?
If the variable y is zero.
What does an if-statement consist of?
A test expression and a body.
What will the code 'val = x / y' execute?
It will execute if y is not equal to 0.
What logical operators are used in the leap year condition?
and, or
How does Python indicate a block of code?
By using indentation.
What are the three main logical operators mentioned?
and (AND), or (OR), and not (NOT).
What is the result of 'a or b' when either a or b is True?
True.
What do comparison operations evaluate to?
Boolean (bool), either True or False.
What is the value of 'sleep_time' in the example?
8
What will happen if y is zero in the provided program?
A run-time error will occur.
When is the body of an if-statement executed?
Only if the test expression evaluates to true.
What is the output of the code if y equals 0?
The code will not execute the division statement.
What does indentation refer to in Python?
The spaces at the beginning of a code line.
What type of values do logical operators take as input?
Boolean values (bool).
What is the result of the comparison 'i > j'?
Evaluates to True if i is greater than j, otherwise False.
What does 'print(sleep_time > pset_time)' evaluate to?
False
What is the purpose of checking if y is zero in the program?
To prevent a run-time error during division.
How does Python identify the body of an if-statement?
By relying on indentation (the number of white spaces at the beginning of a line).
What is the condition to check if three lengths can form a triangle?
a + b > c, b + c > a, and c + a > b.
What must be consistent in the body of an if-statement?
The indentation must be the same.
What is the result type of a logical operator?
Boolean (bool).
What does 'i >= j' check?
Checks if i is greater than or equal to j.
What is the value of 'derive' in the example?
True
What is the syntax structure of an if-statement in Python?
if test_expression: followed by the body.
What type of numbers does the challenge extension require as input?
6 floating point numbers corresponding to the (x, y) coordinates of the triangle's corners.
What will you receive if you fail in the quiz, exam, or do not hand in assignments?
A warning letter.
What are the two alternatives in an if/else statement?
The input number y is either equal to zero (y == 0 is true) or not equal to zero (y != 0 is true).
What does 'i < j' evaluate?
Evaluates to True if i is less than j, otherwise False.
What is the value of 'drink' in the example?
False
What output is given when the lengths 3, 4, and 5 are input?
This is a triangle!
What grade will you receive if your exam score is at least 70 and less than 75?
B+.
How does an if/else statement represent alternatives?
It represents the idea more naturally and clearly.
What does 'i <= j' signify?
Checks if i is less than or equal to j.
What does 'both = derive and drink' evaluate to?
False
What is branching in programming?
Branching allows the program to take different paths based on conditions.
What output is given when the lengths 3, 4, and 15 are input?
This is NOT a triangle!
What is a test expression in the context of logical operators?
An expression that can consist of sub-expressions combined using logical operators.
What is the basic syntax of an if/else statement?
if test_expression_TE: body_to_be_executed_if_TE_is_true else: body_to_be_executed_if_TE_is_false.
What does 'i == j' test for?
Tests for equality; True if i is the same as j.
What will 'print(both)' output?
False
What is the correct way to add parentheses to the expression '18 x + 3 >= 9 or z != x * 2 and not(y / 2 < 3)'?
(((x + 3) >= 9) or ((z != (x * 2)) and (not((y / 2) < 3))))
What does control flow refer to in programming?
Control flow determines the order in which individual statements, instructions, or function calls are executed.
What is the purpose of the program described in Exercise 2?
To check if three given lengths can form a triangle.
What does the 'if' part of an if/else statement do?
Executes the body if the test expression is true.
What does 'i != j' indicate?
Indicates inequality; True if i is not the same as j.
What operator has the highest precedence in the given expression?
not
What is the boolean data type?
The boolean data type represents two values: True and False.
What does the 'else' part of an if/else statement do?
Executes the body if the test expression is false.
Which operator has lower precedence than 'and' but higher than 'or'?
= and !=
What are logical operators used for?
Logical operators are used to combine conditional statements.
In the expression, what does 'not(y / 2 < 3)' evaluate?
It evaluates to the negation of whether y divided by 2 is less than 3.
What is short-circuit evaluation?
Short-circuit evaluation is a programming technique where the second argument is evaluated only if the first argument does not suffice to determine the value of the expression.
What is the role of parentheses in logical expressions?
They clarify the order of operations and precedence.
What is an if-statement?
An if-statement executes a block of code if its condition evaluates to True.
What is the purpose of an if/else statement?
An if/else statement allows the program to execute one block of code if the condition is True and another block if it is False.
Why is indentation important in Python?
Indentation is used to define the scope of loops, functions, and conditionals in Python.