Lec6: Intro to Branching + If statements Flashcards

1
Q

A boolean expression is one that is either:

A

true or false

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

This type of operator determines whether a specific relationship exists between two values:

A

Relational

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

Which of the following expressions will determine whether x is less than or equal to y?

A

x <= y

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

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;

A

15

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

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;

A

120

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

A block of code is enclosed in a set of:

A

braces { }

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

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;
}

A

ans = 60, x = 0, y = 50

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

The expression tested by an if statement must evaluate to:

A

true or false

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

If you prematurely terminate an if statement with a semicolon, the compiler will:

A

Assume you are placing a null statement there

Not display an error message (all of the above)

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

What is the value of x after the following code has been executed?

int x = 75;
int y = 90;
if (x != y)
x += y;

A

165

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

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;

A

No value, this is a syntax error.

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

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;

A

80

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

An important style rule you should follow when writing if statements is to line up the conditionally executed statement with the if statement.

A

False

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

Unicode is an international encoding system that is extensive enough to represent ALL the characters of ALL the world’s alphabets.

A

True

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

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”);

A

John

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

Java’s if statement executes the statement inside its body if a specific _____ is ______.

A

condition, true

17
Q

Which statement is false:

A. If the condition in an if statement is met, the statement in the body of the if statement is executed.
B. The equality operators have a higher level of precedence than the relational operators.
C. Executable Java statements either perform actions or make decisions.
D. All the relational operators have the same level of precedence.

A

B. The equality operators have a higher level of precedence than the relational operators.

18
Q

Structured design advocates the use of software modules. Which of the following items are advantages of this approach?

modules are easier to modify
all of the above
modules can be reused
modules cost less to develop

A

All of the above

19
Q

Which of the following is not a restriction of structured programming?

A

Each module has nested modules

20
Q

In structured programming’s _____ structure, the logic flow branches depending on certain conditions being met.

A

Decision

21
Q

What will be the value of the variable coupons after running the code fragment?

int books = 2, coupons;

if (books < 1)
coupons = 0;
else if (books < 3)
coupons = 1;
else if (books < 5)
coupons = 2;
else
coupons = 3;

A

1

22
Q

What will be printed after running the code fragment?

int y = 0, x = 2;

if (x <= 1)
y = 3;
if (x <= 2)
y = 5;
if (x == 3)
y = 7;
else
y = 9;

System.out.println ( y );

A

9

23
Q

Which of the following is typically used in a flowchart to indicate a decision?

A

Diamond

24
Q

In Java, the value of (4 > 7) is _____.

A

False

25
Q

Two truths and a lie: Two of the following statements are true, and one of them is FALSE. Identify the FALSE statement.

A. Pseudocode and flowcharts are both tools that are used to check the syntax of computer programs.
B. In a decision structure, alternative courses of action are chosen based on a Boolean value.
C. In a sequence structure, one step follows another unconditionally.

A

A. Pseudocode and flowcharts are both tools that are used to check the syntax of computer programs.

26
Q

What will be the boolean expression needed in order to check if the value is an odd number or not? In other words, what boolean expression is the appropriate choice to fill in the blank?

int value = 11;

if (_______________)
System.out.println(“The value is an odd number.”);
else
System.out.println(“The value is an even number.”);

A

x % 2 == 1

27
Q

Which of the following statements is true.

  1. for every ‘if’ there should be an ‘else’.
  2. for every ‘else’ there should be an ‘if’.
  3. ‘if’ block can exist without an ‘else’
  4. ‘if’ condition can be used inside another ‘if’ condition.
A

Only 2, 3, and 4 are true.