Lec6: Intro to Branching + If statements Flashcards
A boolean expression is one that is either:
true or false
This type of operator determines whether a specific relationship exists between two values:
Relational
Which of the following expressions will determine whether x is less than or equal to y?
x <= y
What will be the value of x after the following code is executed?
int x = 75;
int y = 60;
if (x>y)
x = x - y;
15
What will be the value of ans after the following code has been executed?
int ans = 10;
int x = 65;
int y = 55;
if (x >= y)
ans = x + y;
120
A block of code is enclosed in a set of:
braces { }
What will be the value of ans, x and y after the following statements are executed?
int ans = 35, x = 50, y = 50;
if x >= y)
{
ans = x + 10;
x -= y;
}
else
{
ans = y + 10;
y += x;
}
ans = 60, x = 0, y = 50
The expression tested by an if statement must evaluate to:
true or false
If you prematurely terminate an if statement with a semicolon, the compiler will:
Assume you are placing a null statement there
Not display an error message (all of the above)
What is the value of x after the following code has been executed?
int x = 75;
int y = 90;
if (x != y)
x += y;
165
What is the value of ans after the following code has been executed?
int x = 40;
int y = 40;
int ans = 0;
if (x = y)
ans = x + 10;
No value, this is a syntax error.
What is the value of ans after the following code has been executed?
int x = 35;
int y = 20, ans = 80;
if (x < y);
ans += y;
80
An important style rule you should follow when writing if statements is to line up the conditionally executed statement with the if statement.
False
Unicode is an international encoding system that is extensive enough to represent ALL the characters of ALL the world’s alphabets.
True
What would be the output generated to the screen upon running the code fragment?
int a = 8, b = 10;
if (b < a)
if (a + 2 >= b)
System.out.println(“Joe);
else
System.out.println(“Jane”);
else
if (a + 2 <= b)
System.out.println(“John”);
John