SlideShare a Scribd company logo
If you are interested in all the chapters, email
lauriewest24@gmail.com
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1
Chapter 3
Flow of Control
1  Multiple Choice
1) An if selection statement executes if and only if:
(a) the Boolean condition evaluates to false.
(b) the Boolean condition evaluates to true.
(c) the Boolean condition is short-circuited.
(d) none of the above.
Answer: B
2) A compound statement is enclosed between:
(a) [ ]
(b) { }
(c) ( )
(d) < >
Answer: B
3) A multi-way if-else statement
(a) allows you to choose one course of action.
(b) always executes the else statement.
(c) allows you to choose among alternative courses of action.
(d) executes all Boolean conditions that evaluate to true.
Answer: C
2 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
4) The controlling expression for a switch statement includes all of the following types except:
(a) char
(b) int
(c) byte
(d) double
Answer: D
5) To compare two strings lexicographically the String method ____________ should be used.
(a) equals
(b) equalsIgnoreCase
(c) compareTo
(d) ==
Answer: C
6) When using a compound Boolean expression joined by an && (AND) in an if statement:
(a) Both expressions must evaluate to true for the statement to execute.
(b) The first expression must evaluate to true and the second expression must evaluate to false for
the statement to execute.
(c) The first expression must evaluate to false and the second expression must evaluate to true for
the statement to execute.
(d) Both expressions must evaluate to false for the statement to execute.
Answer: A
7) The OR operator in Java is represented by:
(a) !
(b) &&
(c) | |
(d) None of the above
Answer: C
8) The negation operator in Java is represented by:
(a) !
(b) &&
(c) | |
(d) None of the above
Answer: A
Chapter 3 Flow of Control 3
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9) The ____________ operator has the highest precedence.
(a) *
(b) dot
(c) +=
(d) decrement
Answer: B
10) The association of operands with operators is called ______________.
(a) assignment
(b) precedence
(c) binding
(d) lexicographic ordering
Answer: C
11) The looping mechanism that always executes at least once is the _____________ statement.
(a) if…else
(b) do…while
(c) while
(d) for
Answer: B
12) A mixture of programming language and human language is known as:
(a) Algorithms
(b) Recipes
(c) Directions
(d) Pseudocode
Answer: D
13) When the number of repetitions are known in advance,you should use a ___________ statement.
(a) while
(b) do…while
(c) for
(d) None of the above
Answer: C
14) A ___________ statement terminates the current iteration of a loop.
(a) Break
(b) Continue
(c) Switch
(d) Assert
Answer: B
4 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
15) Common loop errors are:
(a) Off-by-one
(b) Infinite loops
(c) Both a and b
(d) None of the above
Answer: C
16) To terminate a program, use the Java statement:
(a) System.quit(0);
(b) System.end(0);
(c) System.abort(0);
(d) System.exit(0);
Answer: D
17) Good debugging techniques include:
(a) Inserting output statements in your program.
(b) Tracing variables
(c) Using an IDE debugger
(d) All of the above
Answer: D
2  True/False
1) An if-else statement chooses between two alternative statements based on the value of a Boolean
expression.
Answer: True
2) You may omit the else part of an if-else statement if no alternative action is required.
Answer: True
3) In a switch statement, the choice of which branch to execute is determined by an expression given in
parentheses after the keyword switch.
Answer: True
4) In a switch statement, the default case is always executed.
Answer: False
5) Not including the break statements within a switch statement results in a syntax error.
Answer: False
6) The equality operator (==) may be used to test if two string objects contain the same value.
Answer: False
Chapter 3 Flow of Control 5
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
7) Boolean expressions are used to control branch and loop statements.
Answer: True
8) The for statement, do…while statement and while statement are examples of branching mechanisms.
Answer: False
9) When choosing a sentinel value, you should choose a value that would never be encountered in the
input of the program.
Answer: True
10) An algorithm is a step-by-step method of solution.
Answer: True
11) The three expressions at the start of a for statement are separated by two commas.
Answer: False
12) An empty statement is defined as a loop that runs forever.
Answer: False
3  ShortAnswer/Essay
1) What output will be produced by the following code?
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!");
}
}
6 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Answer: The condition evaluated to true!
2) What would be the output of the code in #1 if number was originally initialized to 25?
Answer: The condition evaluated to false!
3) 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!
Answer:
if(weight <= 115)
System.out.println("Eat 5 banana splits!");
else if(weight <= 130)
System.out.println("Eat a banana split!");
else if(weight <=200)
System.out.println("Perfect!");
else
System.out.println("Plenty of banana splits have been consumed!");
4) Name and describe three types of branching mechanisms and give an example of each.
Answer: Three types of branching mechanisms include the if-else statement,the multi-way if-else
statement, and the switch statement. An if-else statement chooses between two alternative
statements based on the value of a Boolean expression. If you only need to perform an
action if a certain condition is true, then the else part of an if-else statement can be
omitted. A multi-way if-else statement allows several conditions to be evaluated. The
first condition that evaluates to true, is the branch that is executed. The switch statement is
another multi-selection branching mechanism. A switch evaluates a controlling
expression and executes the statements in the matching case label.
Answer: If – else example: if ( taxableIncome > 30000)
Answer: taxRate = .135;
Answer: else
Answer: taxRate = .075;
Answer:
Answer: Multi-way if-else example:
Answer: If(taxableIncome < 10000)
Answer: taxRate = .0425;
Chapter 3 Flow of Control 7
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Answer: else if(taxableIncome < 30000)
Answer: taxRate = .075;
Answer: else
Answer: taxRate = .135;
Answer:
Answer: Switch example: char myChar = ‘b’;
Answer: switch(myChar)
Answer: {
Answer: case ‘a’:
Answer: System.out.println(“It is an A”);
Answer: break;
Answer:
Answer: case ‘b’:
Answer: System.out.println(“It is a B”);
Answer: break;
Answer: case default:
Answer: System.out.println(“Default case”);
Answer: break;
Answer: }
Answer:
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.
Answer:
8 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
if(costOfPurchase < 20)
shippingCost = 5.99;
else if((costOfPurchase > 20)&&(costOfPurchase <= 65))
shippingCost = 10.99;
else
shippingCost = 15.99;
6) Evaluate the Boolean equation: !( ( 6 < 5) && (4 < 3))
Answer: True
7) 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.
8) Discuss the rules Java follows when evaluating expressions.
Answer: First, Java binds operands with operators by fully parenthesizes the expression using
precedence and associativity rules.
Answer: Next, Java evaluates expressions from left to right.
Answer: Finally, if an operator is waiting for other operands to be evaluated, then that operator is
evaluated as soon as its operands have been evaluated.
9) Write Java code that uses a do…while loop that prints even numbers from 2 through 10.
Answer:
int evenNumber = 2;
do
{
System.out.println(evenNumber);
evenNumber += 2;
}while(evenNumber <= 10);
Chapter 3 Flow of Control 9
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10) Write Java code that uses a while loop to print even numbers from 2 through 10.
Answer:
int evenNumber = 2;
while(evenNumber <= 10)
{
System.out.println(evenNumber);
evenNumber += 2;
}
11) What is a sentinel value and how is it used?
Answer: A sentinel value is a value that should never be encountered in the input of the program. It
is used to control looping mechanisms when the number of repetitions is unknown. When
the sentinel value is encountered, the loop terminates. A sentinel value is commonly used
with while and do….while statements.
12) Write Java code that uses a for statement to sum the numbers from 1 through 50. Display the total
sum to the console.
Answer:
int sum = 0;
for(int i=1; i <= 50; i++)
{
sum += i;
}
System.out.println("The total is: " + sum);
13) 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);
10 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
while(x > 0)
{
x++;
}
System.out.println("The value of x is:" + x);
}
Answer: The value of x is 5. The program then enters an infinite loop because the value of x is
always greater than 0.
14) Discuss the differences between the break and the continue statements when used in looping
mechanisms.
Answer: When the break statement is encountered within a looping mechanism, the loop
immediately terminates. When the continue statement is encountered within a looping
mechanism, the current iteration is terminated, and execution continues with the next
iteration of the loop.
15) 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.
Answer:
import java.util.Scanner;
public class FindMin
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int smallest = 9999999;
String userInput;
Chapter 3 Flow of Control 11
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
boolean quit = false;
System.out.println("This program finds the smallest number"
+ " in a series of numbers");
System.out.println("When you want to exit, type Q");
while(quit != true)
{
System.out.print("Enter a number: ");
userInput = keyboard.next();
if(userInput.equals("Q") || userInput.equals("q"))
{
quit = true;
}
else
{
int userNumber = Integer.parseInt(userInput);
if(userNumber < smallest)
smallest = userNumber;
}
}
System.out.println("The smallest number is " + smallest);
12 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
System.exit(0);
}
}
16) 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.
public class sumEvenOdd
{
public static void main(String[] args)
{
int evenSum = 0;
int oddSum = 0;
//loop through the numbers
for(int i=1; i <= 25; i++)
{
if(i % 2 == 0)
{
//even number
evenSum += i;
}
else
{
oddSum += i;
}
}
Chapter 3 Flow of Control 13
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
//Output the results
System.out.println("Even sum = " + evenSum);
System.out.println("Odd sum = " + oddSum);
}
}

More Related Content

Similar to Absolute Java 5e Savitch Test Bank

Ch05.pdf
Ch05.pdfCh05.pdf
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
Syed Afaq Shah MACS CP
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
yasir_cesc
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
jhtp9_ch04.ppt
jhtp9_ch04.pptjhtp9_ch04.ppt
jhtp9_ch04.ppt
DrCMeenakshiVISTAS
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
Anjali127411
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
PRN USM
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
ShivamChaturvedi67
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
It Academy
 
07 flow control
07   flow control07   flow control
07 flow control
dhrubo kayal
 
Ch04
Ch04Ch04
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and Selection
Ahmed Nobi
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
SzeChingChen
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
Anil Dutt
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
dplunkett
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
sshhzap
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
Ashita Agrawal
 

Similar to Absolute Java 5e Savitch Test Bank (20)

Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
jhtp9_ch04.ppt
jhtp9_ch04.pptjhtp9_ch04.ppt
jhtp9_ch04.ppt
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptx
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
07 flow control
07   flow control07   flow control
07 flow control
 
Ch04
Ch04Ch04
Ch04
 
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and Selection
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 

Recently uploaded

Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
Amin Marwan
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 

Recently uploaded (20)

Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 

Absolute Java 5e Savitch Test Bank

  • 1. If you are interested in all the chapters, email lauriewest24@gmail.com ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1 Chapter 3 Flow of Control 1  Multiple Choice 1) An if selection statement executes if and only if: (a) the Boolean condition evaluates to false. (b) the Boolean condition evaluates to true. (c) the Boolean condition is short-circuited. (d) none of the above. Answer: B 2) A compound statement is enclosed between: (a) [ ] (b) { } (c) ( ) (d) < > Answer: B 3) A multi-way if-else statement (a) allows you to choose one course of action. (b) always executes the else statement. (c) allows you to choose among alternative courses of action. (d) executes all Boolean conditions that evaluate to true. Answer: C
  • 2. 2 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4) The controlling expression for a switch statement includes all of the following types except: (a) char (b) int (c) byte (d) double Answer: D 5) To compare two strings lexicographically the String method ____________ should be used. (a) equals (b) equalsIgnoreCase (c) compareTo (d) == Answer: C 6) When using a compound Boolean expression joined by an && (AND) in an if statement: (a) Both expressions must evaluate to true for the statement to execute. (b) The first expression must evaluate to true and the second expression must evaluate to false for the statement to execute. (c) The first expression must evaluate to false and the second expression must evaluate to true for the statement to execute. (d) Both expressions must evaluate to false for the statement to execute. Answer: A 7) The OR operator in Java is represented by: (a) ! (b) && (c) | | (d) None of the above Answer: C 8) The negation operator in Java is represented by: (a) ! (b) && (c) | | (d) None of the above Answer: A
  • 3. Chapter 3 Flow of Control 3 ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 9) The ____________ operator has the highest precedence. (a) * (b) dot (c) += (d) decrement Answer: B 10) The association of operands with operators is called ______________. (a) assignment (b) precedence (c) binding (d) lexicographic ordering Answer: C 11) The looping mechanism that always executes at least once is the _____________ statement. (a) if…else (b) do…while (c) while (d) for Answer: B 12) A mixture of programming language and human language is known as: (a) Algorithms (b) Recipes (c) Directions (d) Pseudocode Answer: D 13) When the number of repetitions are known in advance,you should use a ___________ statement. (a) while (b) do…while (c) for (d) None of the above Answer: C 14) A ___________ statement terminates the current iteration of a loop. (a) Break (b) Continue (c) Switch (d) Assert Answer: B
  • 4. 4 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15) Common loop errors are: (a) Off-by-one (b) Infinite loops (c) Both a and b (d) None of the above Answer: C 16) To terminate a program, use the Java statement: (a) System.quit(0); (b) System.end(0); (c) System.abort(0); (d) System.exit(0); Answer: D 17) Good debugging techniques include: (a) Inserting output statements in your program. (b) Tracing variables (c) Using an IDE debugger (d) All of the above Answer: D 2  True/False 1) An if-else statement chooses between two alternative statements based on the value of a Boolean expression. Answer: True 2) You may omit the else part of an if-else statement if no alternative action is required. Answer: True 3) In a switch statement, the choice of which branch to execute is determined by an expression given in parentheses after the keyword switch. Answer: True 4) In a switch statement, the default case is always executed. Answer: False 5) Not including the break statements within a switch statement results in a syntax error. Answer: False 6) The equality operator (==) may be used to test if two string objects contain the same value. Answer: False
  • 5. Chapter 3 Flow of Control 5 ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7) Boolean expressions are used to control branch and loop statements. Answer: True 8) The for statement, do…while statement and while statement are examples of branching mechanisms. Answer: False 9) When choosing a sentinel value, you should choose a value that would never be encountered in the input of the program. Answer: True 10) An algorithm is a step-by-step method of solution. Answer: True 11) The three expressions at the start of a for statement are separated by two commas. Answer: False 12) An empty statement is defined as a loop that runs forever. Answer: False 3  ShortAnswer/Essay 1) What output will be produced by the following code? 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!"); } }
  • 6. 6 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Answer: The condition evaluated to true! 2) What would be the output of the code in #1 if number was originally initialized to 25? Answer: The condition evaluated to false! 3) 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! Answer: if(weight <= 115) System.out.println("Eat 5 banana splits!"); else if(weight <= 130) System.out.println("Eat a banana split!"); else if(weight <=200) System.out.println("Perfect!"); else System.out.println("Plenty of banana splits have been consumed!"); 4) Name and describe three types of branching mechanisms and give an example of each. Answer: Three types of branching mechanisms include the if-else statement,the multi-way if-else statement, and the switch statement. An if-else statement chooses between two alternative statements based on the value of a Boolean expression. If you only need to perform an action if a certain condition is true, then the else part of an if-else statement can be omitted. A multi-way if-else statement allows several conditions to be evaluated. The first condition that evaluates to true, is the branch that is executed. The switch statement is another multi-selection branching mechanism. A switch evaluates a controlling expression and executes the statements in the matching case label. Answer: If – else example: if ( taxableIncome > 30000) Answer: taxRate = .135; Answer: else Answer: taxRate = .075; Answer: Answer: Multi-way if-else example: Answer: If(taxableIncome < 10000) Answer: taxRate = .0425;
  • 7. Chapter 3 Flow of Control 7 ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Answer: else if(taxableIncome < 30000) Answer: taxRate = .075; Answer: else Answer: taxRate = .135; Answer: Answer: Switch example: char myChar = ‘b’; Answer: switch(myChar) Answer: { Answer: case ‘a’: Answer: System.out.println(“It is an A”); Answer: break; Answer: Answer: case ‘b’: Answer: System.out.println(“It is a B”); Answer: break; Answer: case default: Answer: System.out.println(“Default case”); Answer: break; Answer: } Answer: 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. Answer:
  • 8. 8 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. if(costOfPurchase < 20) shippingCost = 5.99; else if((costOfPurchase > 20)&&(costOfPurchase <= 65)) shippingCost = 10.99; else shippingCost = 15.99; 6) Evaluate the Boolean equation: !( ( 6 < 5) && (4 < 3)) Answer: True 7) 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. 8) Discuss the rules Java follows when evaluating expressions. Answer: First, Java binds operands with operators by fully parenthesizes the expression using precedence and associativity rules. Answer: Next, Java evaluates expressions from left to right. Answer: Finally, if an operator is waiting for other operands to be evaluated, then that operator is evaluated as soon as its operands have been evaluated. 9) Write Java code that uses a do…while loop that prints even numbers from 2 through 10. Answer: int evenNumber = 2; do { System.out.println(evenNumber); evenNumber += 2; }while(evenNumber <= 10);
  • 9. Chapter 3 Flow of Control 9 ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10) Write Java code that uses a while loop to print even numbers from 2 through 10. Answer: int evenNumber = 2; while(evenNumber <= 10) { System.out.println(evenNumber); evenNumber += 2; } 11) What is a sentinel value and how is it used? Answer: A sentinel value is a value that should never be encountered in the input of the program. It is used to control looping mechanisms when the number of repetitions is unknown. When the sentinel value is encountered, the loop terminates. A sentinel value is commonly used with while and do….while statements. 12) Write Java code that uses a for statement to sum the numbers from 1 through 50. Display the total sum to the console. Answer: int sum = 0; for(int i=1; i <= 50; i++) { sum += i; } System.out.println("The total is: " + sum); 13) 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);
  • 10. 10 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. while(x > 0) { x++; } System.out.println("The value of x is:" + x); } Answer: The value of x is 5. The program then enters an infinite loop because the value of x is always greater than 0. 14) Discuss the differences between the break and the continue statements when used in looping mechanisms. Answer: When the break statement is encountered within a looping mechanism, the loop immediately terminates. When the continue statement is encountered within a looping mechanism, the current iteration is terminated, and execution continues with the next iteration of the loop. 15) 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. Answer: import java.util.Scanner; public class FindMin { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int smallest = 9999999; String userInput;
  • 11. Chapter 3 Flow of Control 11 ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. boolean quit = false; System.out.println("This program finds the smallest number" + " in a series of numbers"); System.out.println("When you want to exit, type Q"); while(quit != true) { System.out.print("Enter a number: "); userInput = keyboard.next(); if(userInput.equals("Q") || userInput.equals("q")) { quit = true; } else { int userNumber = Integer.parseInt(userInput); if(userNumber < smallest) smallest = userNumber; } } System.out.println("The smallest number is " + smallest);
  • 12. 12 Walter Savitch • Absolute Java 5/e: Chapter 3 Test Bank ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. System.exit(0); } } 16) 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. public class sumEvenOdd { public static void main(String[] args) { int evenSum = 0; int oddSum = 0; //loop through the numbers for(int i=1; i <= 25; i++) { if(i % 2 == 0) { //even number evenSum += i; } else { oddSum += i; } }
  • 13. Chapter 3 Flow of Control 13 ©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. //Output the results System.out.println("Even sum = " + evenSum); System.out.println("Odd sum = " + oddSum); } }