Java Programming 8e Joyce Farrell - Test Bank

Java Programming 8e Joyce Farrell - Test Bank   Instant Download - Complete Test Bank With Answers     Sample Questions Are Posted Below   True / False   1. You write pseudocode in everyday language, not the syntax used in a programming language.   a. True   b. False   ANSWER:   True POINTS:   1 REFERENCES:   …

$19.99

Java Programming 8e Joyce Farrell – Test Bank

 

Instant Download – Complete Test Bank With Answers

 

 

Sample Questions Are Posted Below

 

True / False

 

1. You write pseudocode in everyday language, not the syntax used in a programming language.

  a. True
  b. False

 

ANSWER:   True
POINTS:   1
REFERENCES:   246

 

2. An alternative to using a Boolean expression, such as someVariable == 10, is to store the Boolean expression’s value in a Boolean variable.

  a. True
  b. False

 

ANSWER:   True
POINTS:   1
REFERENCES:   249

 

3. When you create a block, you must place multiple statements within it.

  a. True
  b. False

 

ANSWER:   False
POINTS:   1
REFERENCES:   255

 

4. When you use nested if statements, you must pay careful attention to placement of any else clauses.

  a. True
  b. False

 

ANSWER:   True
POINTS:   1
REFERENCES:   261

 

5. In the switch structure, break is followed by one of the possible values for the test expression and a colon.

  a. True
  b. False

 

ANSWER:   False
POINTS:   1
REFERENCES:   276

 

6. Computers contain switches that are set to on or off.

  a. True
  b. False

 

ANSWER:   True
POINTS:   1
REFERENCES:   247

 

7. When writing a statement with the two-line format, you must be sure to type a semicolon at the end of the first line in order to ensure accurate results.

  a. True
  b. False

 

ANSWER:   False
POINTS:   1
REFERENCES:   249

 

8. Although it is possible to block statements that depend on an if, you cannot likewise block statements that depend on an else.

  a. True
  b. False

 

ANSWER:   False
POINTS:   1
REFERENCES:   257

 

9. Although not required, it is common procedure to align the keyword if with the keyword else.

  a. True
  b. False

 

ANSWER:   True
POINTS:   1
REFERENCES:   251-252

 

10. You use the NOT operator, which is written as the exclamation point (!), to negate the result of any Boolean expression.

  a. True
  b. False

 

ANSWER:   True
POINTS:   1
REFERENCES:   281

 

Multiple Choice

 

11. A ____ consists of written steps in diagram form, as a series of shapes connected by arrows.

  a. pseudocode chart b. flowchart
  c. sequence structure d. decision structure

 

ANSWER:   b
POINTS:   1
REFERENCES:   246

 

12. A logical structure called a(n) ____ structure is when one step follows another unconditionally.

  a. straight b. decision
  c. sequence d. unconditional

 

ANSWER:   c
POINTS:   1
REFERENCES:   246

 

13. The simplest statement you can use to make a decision is the ____ statement.

  a. this b. Boolean
  c. true false d. if

 

ANSWER:   d
POINTS:   1
REFERENCES:   248

 

14. When an expression containing a ____ is part of an if statement, the assignment is illegal.

  a. single equal sign b. greater than sign
  c. double equal sign d. Boolean value

 

ANSWER:   a
POINTS:   1
REFERENCES:   250

 

15. A(n) ____ statement is the decision structure you use when you need to take one or the other of two possible courses of action.

  a. Boolean b. dual-alternative if
  c. single-alternative if d. if…else

 

ANSWER:   b
POINTS:   1
REFERENCES:   251

 

16. When you execute an if…else statement, only one of the resulting actions takes place depending on the evaluation of the ____ following the if.

  a. Boolean expression b. keyword
  c. else statement d. independent statement

 

ANSWER:   a
POINTS:   1
REFERENCES:   252

 

17. When you place a block within an if statement, it is crucial to place the ____ correctly.

  a. periods b. angle brackets
  c. commas d. curly braces

 

ANSWER:   d
POINTS:   1
REFERENCES:   255

 

18. Just as you can block statements that depend on an if, you can also block statements that depend on a(n) ____.

  a. Boolean expression b. else
  c. constant d. operator

 

ANSWER:   b
POINTS:   1
REFERENCES:   257

 

19. When you block statements, you must remember that any ____ you declare within a block is local to that block.

  a. method b. expression
  c. variable d. decision

 

ANSWER:   c
POINTS:   1
REFERENCES:   258

 

20. Statements in which an if structure is contained inside another if structure are commonly called ____ if statements.

  a. nested b. logical
  c. blocked d. inside

 

ANSWER:   a
POINTS:   1
REFERENCES:   260

 

21. The compiler does not take indentation into account when compiling code, but consistent indentation can help readers understand a program’s ____.

  a. machine language b. class
  c. decision d. logic

 

ANSWER:   d
POINTS:   1
REFERENCES:   262

 

22. The AND operator is written as two ____.

  a. plus signs b. equal signs
  c. ampersands d. asterisks

 

ANSWER:   c
POINTS:   1
REFERENCES:   264

 

23. When you use the && operator, you must include a complete _____ on each side.

  a. mathematical expression b. Boolean expression
  c. variable d. operator

 

ANSWER:   b
POINTS:   1
REFERENCES:   264

 

24. You can use the ____, which is written as ||, if you want some action to occur when at least one of two conditions is true.

  a. conditional OR operator b. logical AND operator
  c. range check d. switch statement

 

ANSWER:   a
POINTS:   1
REFERENCES:   265

 

25. A(n) ____ is a series of if statements that determine whether a value falls within a specified range.

  a. scope check b. if check
  c. range test d. range check

 

ANSWER:   d
POINTS:   1
REFERENCES:   270

 

26. The ____ statement is useful when you need to test a single variable against a series of exact integer, character, or string values.

  a. switch b. if
  c. else d. break

 

ANSWER:   a
POINTS:   1
REFERENCES:   275

 

27. You can leave out the ____ statements in a switch structure.

  a. break b. switch
  c. if d. case

 

ANSWER:   a
POINTS:   1
REFERENCES:   276

 

28. The ____ requires three expressions separated with a question mark and a colon.

  a. flowchart b. conditional operator
  c. sequence structure d. conditional statement

 

ANSWER:   b
POINTS:   1
REFERENCES:   280

 

29. The ____ operator is written as the exclamation point ( ! ).

  a. equality b. AND
  c. assignment d. NOT

 

ANSWER:   d
POINTS:   1
REFERENCES:   281

 

30. The ____ operator is always evaluated before the OR operator.

  a. Assignment b. AND
  c. Conditional d. Logical

 

ANSWER:   b
POINTS:   1
REFERENCES:   282-283

 

31. When working with logical operators, you can always use ____ to change precedence.

  a. conditional operators b. break statements
  c. parentheses d. semicolons

 

ANSWER:   c
POINTS:   1
REFERENCES:   284

 

32. A ____ checks a value, and based on the result performs one of two actions.

  a. short-circuit structure b. sequence structure
  c. decision structure d. block structure

 

ANSWER:   c
POINTS:   1
REFERENCES:   246

 

33.
Using the flowchart above, which decision statement will correctly check that hoursWorked is greater than or equal to the FULL_WEEK constant?

  a. hoursWorked == FULL_WEEK b. hoursWorked > FULL_WEEK
  c. hoursWorked >= FULL_WEEK d. hoursWorked != FULL_WEEK

 

ANSWER:   c
POINTS:   1
REFERENCES:   255 – 256

 

34. if (quotaAmt > 100 || sales > 100 && productCode == “C”)

bonusAmt = 50;

When the above code is executed, which operator is evaluated first?

  a. && b. ?:
  c. || d. =

 

ANSWER:   a
POINTS:   1
REFERENCES:   283

 

35. When using equals and not equals for comparisons with objects, you compare the objects’ ____ instead of actual values.

  a. reference fields b. primitive data types
  c. Boolean values d. memory addresses

 

ANSWER:   d
POINTS:   1
REFERENCES:   251

 

Completion

 

36. ____________________ is a tool that helps programmers plan a program’s logic by writing plain English statements.

ANSWER:   Pseudocode
POINTS:   1
REFERENCES:   246

 

37. A(n) ____________________ structure is one that involves choosing between alternative courses of action based on some value within a program.

ANSWER:   decision
POINTS:   1
REFERENCES:   246

 

38. ____________________ statements are particularly useful when two conditions must be met before some action is taken.

ANSWER:   Nested if
POINTS:   1
REFERENCES:   260

 

39. Both & and | are valid Java operators, but a single & or | with integer operands operate on individual ____________________ rather than making comparisons in logical conditions as && and || do.

ANSWER:   bits
POINTS:   1
REFERENCES:   273

 

40. You are never required to use a switch structure; you can always achieve the same results with nested ____________________ statements.

ANSWER:   if
POINTS:   1
REFERENCES:   278

 

Matching

 

Match each term with the correct statement below.

a. relational operator
b. conditional operator
c. Boolean values
d. equality
e. switch statement
f. pipes
g. dual-alternative
h. !
i. logical AND operator

 

REFERENCES:   249
280
247
248
275
265
251
281
264

 

41. true and false

ANSWER:   c
POINTS:   1

 

42. A double equal sign

ANSWER:   d
POINTS:   1

 

43. Two possible courses of action

ANSWER:   g
POINTS:   1

 

44. Two ampersands

ANSWER:   i
POINTS:   1

 

45. Used in the OR operator

ANSWER:   f
POINTS:   1

 

46. > or <

ANSWER:   a
POINTS:   1

 

47. An alternative to using a series of nested if statements

ANSWER:   e
POINTS:   1

 

48. Separated with a question mark and a colon

ANSWER:   b
POINTS:   1

 

49. The logical NOT operator

ANSWER:   h
POINTS:   1

 

Subjective Short Answer

 

50. Why do programmers use pseudocode?

ANSWER:   When computer programmers write programs, they rarely just sit down at a keyboard and begin typing. Programmers must plan the complex portions of programs using paper and pencil. Programmers often use pseudocode, a tool that helps them plan a program’s logic by writing plain English statements. Using pseudocode requires that you write down the steps needed to accomplish a given task. You write pseudocode in everyday language, not the syntax used in a programming language.
POINTS:   1
REFERENCES:   246

 

51. Describe how a sequence structure works.

ANSWER:   In a sequence structure, one step follows another unconditionally. A sequence structure might contain any number of steps in which one task follows another with no chance to branch away or skip a step.
POINTS:   1
REFERENCES:   246

 

52. An alternative to using a Boolean expression, such as quizScore == 10, is to store the Boolean expression’s value in a Boolean variable. How could you express this in Java?

ANSWER:   For example, if isPerfectScore is a Boolean variable, then the following statement compares quizScore to 10 and stores true or false in isPerfectScore:

isPerfectScore = (quizScore == 10);

Then, you can write the if as:

if(isPerfectScore)

System.out.println(“The score is perfect”);

POINTS:   1
REFERENCES:   250

 

53. Why would a programmer execute more than one statement that depends on the evaluation of a Boolean expression, and how could he or she do it in Java?

ANSWER:   Often, you want to take more than one action following the evaluation of a Boolean expression within an if statement. For example, you might want to display several separate lines of output or perform several mathematical calculations. To execute more than one statement that depends on the evaluation of a Boolean expression, you use a pair of curly braces to place the dependent statements within a block.
POINTS:   1
REFERENCES:   254

 

54. Within an if or an else statement, how can you code as many dependent if statements as you need and why is this useful?

ANSWER:  

Within an if or an else clause, you can code as many dependent statements as you need, including other if and else statements. Statements in which a decision is contained inside either the if or else clause of another decision are nested if statements. Nested if statements are particularly useful when two or more conditions must be met before some action is taken.

POINTS:   1
REFERENCES:   260

 

55. Why would a programmer use an && operator when using nested if statements?

ANSWER:   You are never required to use the && operator because using nested if statements always achieves the same result, but using the && operator often makes your code more concise, less error-prone, and easier to understand.
POINTS:   1
REFERENCES:   265

 

56. What is a range check?  Provide an example of a range check using a series of if..else statements using the values defined below.

 

final double HIGH_LIM = 1000.00;
final double HIGH_RATE = 0.08;
final double MED_LIM = 500.00;
final double MED_RATE = 0.06;
final double LOW_RATE = 0.05;

ANSWER:   A range check is a series of statements that determine to which of several consecutive series of values another value falls.

if(saleAmount >= HIGH_LIM)
commissionRate = HIGH_RATE;
else
if(saleAmount >= MED_LIM)
commissionRate = MED_RATE;
else
commissionRate = LOW_RATE;

Or, perhaps  somewhat more efficient:

if(saleAmount < LOW_LIM)
commissionRate = LOW_RATE;
else
if(saleAmount < MED_LIM)
commissionRate = MED_RATE;
else
commissionRate = HIGH_RATE;

POINTS:   1
REFERENCES:   270, 272

 

57. What is wrong with the following statement? How could you correct it?
if(payRate < 5.85 && payRate > 60)
System.out.println(“Error in pay rate”);

ANSWER:   As a single variable, no payRate value can ever be both below 5.85 and over 60 at the same time. Therefore, the output statement can never execute, no matter what value the payRate has. In this case, you must write the following code to print the error message under the correct circumstances:
if(payRate < 5.85 || payRate > 60)
System.out.println(“Error in pay rate”);
POINTS:   1
REFERENCES:   273

 

58. List and explain the keywords used with the switch statement.

ANSWER:   switch starts the statement and is followed immediately by a test expression enclosed in parentheses.

case is followed by one of the possible values for the test expression and a colon.

break optionally terminates a switch structure at the end of each case.

default is optionally used prior to any action that should occur if the test variable does not match any case.

POINTS:   1
REFERENCES:   275

 

59. What is a conditional operator? Show an example of an expression that uses a conditional operator.

ANSWER:  

The conditional operator requires three expressions separated with a question mark and a colon and is used as an abbreviated version of the if…else statement.

 

testExpression ? trueResult : falseResult;

The first expression, testExpression, is a Boolean expression that is evaluated as true or false. If it is true, the entire conditional expression takes on the value of the expression following the question mark (trueResult). If the value of the testExpression is false, the entire expression takes on the value of falseResult.

POINTS:   1
REFERENCES:   280

 

60. Explain the use of the NOT operator.

ANSWER:   You use the NOT operator, which is written as the exclamation point ( ! ), to negate the result of any Boolean expression. Any expression that evaluates as true becomes false when preceded by the NOT operator; and accordingly, any false expression preceded by the NOT operator becomes true.
POINTS:   1
REFERENCES:   281

 

61. In Boolean expressions, when you want to execute more than one statement based on the results of the expression, you place statements within a block. What is a block and why is it important in decision statements? Why are curly braces crucial when blocking an if statement?

ANSWER:   To execute more than one statement that depends on the evaluation of a Boolean expression, you use a pair of curly braces to place the dependent statements within a block. When you place a block within an if statement, it is crucial to place the curly braces correctly. Curly braces are required if multiple statements must be treated as a block. When you create a block, you do not have to place multiple statements within it. It is perfectly legal to place curly braces around a single statement. For clarity, some programmers always use curly braces to surround the actions in an if statement, even when there is only one statement in the block.
POINTS:   1
REFERENCES:   254-255

 

62. Create an if statement that will check if the variable salesAmt is greater than the constant SALES_QUOTA. If true, create a block statement that will calculate salesAmt * BONUS_PERCENT and store the result in a variable named bonusPay. The block statement will then compute totalPay by adding salesAmt to bonusPay. Outside of the if structure, add a println statement that will output the totalPay value. Be sure to use curly braces and semicolons appropriately.

ANSWER:   if(salesAmt > SALES_QUOTA)

{

bonusPay = salesAmt * BONUS_PERCENT;

totalPay = salesAmt + bonusPay;

}

System.out.println(“Total pay is ” + totalPay);

POINTS:   1
REFERENCES:   256-258

 

63. Create an if statement that checks if the variable salesAmt is greater than or equal to the QUOTA_AMT constant. Use the logical OR operator to also check if salesAmt is greater than or equal to the HIGH_SALES constant. If either expression is true, assign the value of the LARGE_QUOTA constant to the variable bonusAmt.

ANSWER:   if(salesAmt >= QUOTA_AMT || salesAmt >= HIGH_SALES)
bonusAmt = LARGE_QUOTA;
POINTS:   1
REFERENCES:   265

 

64. public class Student
{
private int studentNum;
private int studentScore;
public int MAX_NUM = 500;
public int MAX_SCORE = 100;
Student(int num, int score)
{

}

Decision making can be used to control the allowed values in an object’s fields. In the above code, the Student class contains two fields that hold a student number and a score. A constructor accepts values for these fields as parameters. Write the code between the curly brackets that will determine whether the value of num is less than the MAX_NUM constant. If true, assign the value of num to studentNum. Otherwise, assign the value of MAX_NUM to the studentNum. Then check if the value of score is less than or equal to MAX_SCORE. If true, assign the value of score to studentScore. Otherwise, assign 0 to studentScore.

ANSWER:   if(num <= MAX_NUM)
studentNum = num;
else
studentNum = MAX_NUM;
if(score <= MAX_SCORE)
studentScore = score;
else
studentScore = 0;
POINTS:   1
REFERENCES:   285

 

65. ​

Using the above flowchart and code, why will the above statements execute but provide unintended results? Write the statement correctly.

ANSWER:   There should be no semicolon at the end of the first line of the if statement following the parentheses because the statement does not end there. The statement should end after the println() call, so that is where you type the semicolon.

if(quizScore == 10)
System.out.println(“The score is perfect”);

POINTS:   1
REFERENCES:   249

 

66. Write a single-alternative if statement that will check if a variable named maxValue is equal to 100. If true, use a println statement to output “You have reached the limit”.

ANSWER:   if (maxValue == 100)
System.out.println (“You have reached the limit”);

(Be sure there is no semicolon at the end of the first line.)

POINTS:   1
REFERENCES:   249

 

67. if (maxValue = 100)

System.out.println (“Your limit has been reached”);

Why is the above if statement illegal? How would you fix it?

ANSWER:   A common programming error occurs when a programmer uses a single equal sign rather than a double equal sign when attempting to determine equivalency. The expression maxValue = 100 does not compare maxValue to 100. Instead, it attempts to assign the value 100 to maxValue. When the expression maxValue = 100 is used in the if statement, the assignment is illegal because only Boolean expressions are allowed.

Then, if statement should be written as:

 

if(maxValue == 100)
System.out.println(“Your limit has been reached”);

POINTS:   1
REFERENCES:   250

 

68. Create an if…else statement that will check if the maxValue variable is equal to 100. If true, create a println statement that will output “Max reached”. If false, create a println statement to output “Keep trying”.

ANSWER:   if(maxValue == 100)

System.out.println(“Max reached”);
else
System.out.println(“Keep trying”);

POINTS:   1
REFERENCES:   252

 

69. if(qtySold > QUOTA)

bonusPay = 50;

totalPay = regPay + bonusPay;

System.out.println(“Your totalPay amount is ” + totalPay);

Why is the above code incorrect? Write the code that will provide the correct result.

ANSWER:   When you place a block within an if statement, it is crucial to place the curly braces correctly. In the above code, the curly braces have been omitted. When qtySold > QUOTA is true, bonusPay is calculated and the if expression ends. The next statement that computes totalPay executes every time the program runs, no matter what value is stored in qtySold.

if(qtySold > QUOTA)

{

bonusPay = 50;

totalPay = regPay + bonusPay;

}

System.out.println(“Your totalPay amount is ” + totalPay);

 

POINTS:   1
REFERENCES:   255-256

 

70. if(firstValue == secondValue)
{
int total = firstValue + secondVaue;
System.out.println(“The values are equal”);
}
System.out.println(“The total is ” + total);

Why will the above println() statement cause an error?

ANSWER:   When blocking statements, it is crucial to remember that any variable declared within the block is local to that block. In the above code, a variable named total is local to the block following the if. The ending println statement causes an error because the total variable is not recognized.
POINTS:   1
REFERENCES:   258

 

71. Write a nested if statement that will check if the variable qtySold is greater than the QTY_QUOTA constant. If true, evaluate a second expression that will check if the variable totalSales is greater than the constant SALES_QUOTA. If the second expression is also true, create an assignment statement that will assign the constant BONUSAMT to the variable bonus. After BONUSAMT is assigned, the if structure ends.

ANSWER:   if(qtySold > QTY_QUOTA)
if(totalSales > SALES_QUOTA)
bonus = BONUSAMT;

(The semicolon will only be placed after the final statement.)

POINTS:   1
REFERENCES:   260-261

 

72.

Using the above flowchart, write the nested if…else statements that will correctly choose among alternative courses of action based on the values of itemsSold and totalValue.

ANSWER:  

if(itemsSold >= MIN_ITEMS)
if(totalValue >= MIN_VALUE)
bonus = LARGE_BONUS;
else
bonus = MEDIUM_BONUS;
else
bonus = SMALL_BONUS;

POINTS:   1
REFERENCES:   262

 

73.

Use the above flowchart to create the code using the logical && operator to determine if a SALES_BONUS will be assigned to the variable bonus.

ANSWER:  

if(itemsSold > MIN_ITEMS && totalValue >= MIN_VALUE)
bonus = SALES_BONUS;

POINTS:   1
REFERENCES:   264

 

74. Create a switch statement that will test the value of a variable named score. If score has a value of 5, create a println statement that will output “Excellent”. If score has a value of 4, create a println statement that will output “Great”. If score has a value of 3, create a println statement that will output “Good”. If score has a value of 2, create a println statement that will output “Fair”. If score has a value of 1, create a println statement that will output “Needs improvement”. If score does not contain a matching value for any of the case statements, create a println statement that will output “Retake test”.

ANSWER:   switch(score)

{

case 5:

System.out.println(“Excellent”);

break;

case 4:

System.out.println(“Great”);

break;

case 3:

System.out.println(“Good”);

break;

case 2:

System.out.println(“Fair”);

break;

case 1:

System.out.println(“Needs improvement”);

break;

default:

System.out.println(“Retake test”);

}

POINTS:   1
REFERENCES:   275-276

 

 

Additional information

Add Review

Your email address will not be published. Required fields are marked *