Download Chapter3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 3
Flow of Control
1
 Multiple Choice
1)
2)
3)
4)
5)
6)
7)
8)
9)
An if selection statement executes if and only if:
the Boolean condition evaluates to false.
the Boolean condition evaluates to true.
the Boolean condition is short-circuited.
none of the above.
A compound statement is enclosed between:
[ ]
{ }
( )
< >
A multi-way if-else statement
allows you to choose one course of action.
always executes the else statement.
allows you to choose among alternative courses of action.
executes all Boolean conditions that evaluate to true.
The controlling expression for a switch statement includes all of the following types except:
char
int
byte
double
To compare two strings lexicographically the String method ____________ should be used.
equals
equalsIgnoreCase
compareTo
==
When using a compound Boolean expression joined by an && (AND) in an if statement:
Both expressions must evaluate to true for the statement to execute.
The first expression must evaluate to true and the second expression must evaluate to false for the
statement to execute.
The first expression must evaluate to false and the second expression must evaluate to true for the
statement to execute.
Both expressions must evaluate to false for the statement to execute.
The OR operator in Java is represented by:
!
&&
||
None of the above
The negation operator in Java is represented by:
!
&&
||
None of the above
The ____________ operator has the highest precedence.
*
dot
3
10)
11)
12)
13)
14)
15)
16)
17)
+=
decrement
The association of operands with operators is called ______________.
assignment
precedence
binding
lexicographic ordering
The looping mechanism that always executes at least once is the _____________ statement.
if…else
do…while
while
for
A mixture of programming language and human language is known as:
Algorithms
Recipes
Directions
Pseudocode
When the number of repetitions are known in advance, you should use a ___________ statement.
while
do…while
for
None of the above
A ___________ statement terminates the current iteration of a loop.
Break
Continue
Switch
Assert
Common loop errors are:
Off-by-one
Infinite loops
Both a and b
None of the above
To terminate a program, use the Java statement:
System.quit(0);
System.end(0);
System.abort(0);
System.exit(0);
Good debugging techniques include:
Inserting output statements in your program.
Tracing variables
Using an IDE debugger
All of the above

True/False
1)
10)
11)
12)
An if-else statement chooses between two alternative statements based on the value of a Boolean
expression.
You may omit the else part of an if-else statement if no alternative action is required.
In a switch statement, the choice of which branch to execute is determined by an expression given in
parentheses after the keyword switch.
In a switch statement, the default case is always executed.
Not including the break statements within a switch statement results in a syntax error.
The equality operator (==) may be used to test if two string objects contain the same value.
Boolean expressions are used to control branch and loop statements.
The for statement, do…while statement and while statement are examples of branching mechanisms.
When choosing a sentinel value, you should choose a value that would never be encountered in the
input of the program.
An algorithm is a step-by-step method of solution.
The three expressions at the start of a for statement are separated by two commas.
An empty statement is defined as a loop that runs forever.

Short Answer/Essay
1)
What output will be produced by the following code?
2)
3)
4)
5)
6)
7)
8)
9)
public class SelectionStatements
{
public static void main(String[] args)
{
int number = 24;
if(number % 2 == 0)
System.out.print("The condition evaluated to true!");
else
System.out.print("The condition evaluated to false!");
}
}
5
2)
3)
What would be the output of the code in #1 if number was originally initialized to 25?
Write a multi-way if-else statement that evaluates a persons weight on the following criteria: A
weight less than 115 pounds, output: Eat 5 banana splits! A weight between 116 pounds and 130
pounds, output: Eat a banana split! A weight between 131 pounds and 200 pounds, output: Perfect!
A weight greater than 200 pounds, output: Plenty of banana splits have been consumed!
4)
Name and describe three types of branching mechanisms and give an example of each.
5)
Write an if-else statement to compute the amount of shipping due on an online sale. If the cost of
the purchase is less than $20, the shipping cost is $5.99. If the cost of the purchase over $20 and at
most $65, the shipping cost is $10.99. If the cost of the purchase is over $65, the shipping cost is
$15.99.
Evaluate the Boolean equation: !( ( 6 < 5) && (4 < 3))
What is short-circuit evaluation of Boolean expressions?
Answer: Short-circuit evaluation is a mechanism in Java that allows the early termination of the
evaluation of a compound Boolean expression. This occurs when two Boolean expressions are
joined by the && operator when the first expression evaluates to false. The evaluation short-circuits
because no matter what the value of the second expression is, the expression will evaluate to false.
When two Boolean expressions are joined by the || operator, if the first expression evaluates to true,
then the evaluation will short circuit because the expression will always evaluate to true no matter
what the second expression evaluates to.
6)
7)
8)
9)
10)
11)
12)
13)
Discuss the rules Java follows when evaluating expressions.
Write Java code that uses a do…while loop that prints even numbers from 2 through 10.
Write Java code that uses a while loop to print even numbers from 2 through 10.
What is a sentinel value and how is it used?
Write Java code that uses a for statement to sum the numbers from 1 through 50. Display the total
sum to the console.
What is the output of the following code segment?
public static void main(String[] args)
{
int x = 5;
System.out.println("The value of x is:" + x);
while(x > 0)
{
x++;
}
System.out.println("The value of x is:" + x);
}
14)
15)
16)
Discuss the differences between the break and the continue statements when used in looping
mechanisms.
Write a complete Java program that prompts the user for a series of numbers to determine the
smallest value entered. Before the program terminates, display the smallest value.
Write a complete Java program that uses a for loop to compute the sum of the even numbers and the
sum of the odd numbers between 1 and 25.