Home  | 

 

 ONLINE SESSION   B  

Covering Gaddis chapter 4



Answer the following multiple choice questions for chapter 4

Multiple Choice  ch 4 

1.    What will the println statement in the following program segment display?

int x = 5;

System.out.println(x++);

a.    5

b.    6

c.    0

d.    None of these

 

 

2.    What will the println statement in the following program segment display?

int x = 5;

System.out.println(++x);

a.    5

b.    6

c.    0

d.    None of these

 

 

 

 

3.    In the expression number++, the ++ operator is in what mode?

a.    prefix

b.    pretest

c.    postfix

d.    posttest

4.    What is each repetition of a loop known as?

a.    cycle

b.    revolution

c.    orbit

d.    iteration

5.    This is a variable that controls the number of iterations performed by a loop.

a.    loop control variable

b.    accumulator

c.    iteration register variable

d.    repetition meter

6.    The while loop is this type of loop.

a.    pretest

b.    posttest

c.    prefix

d.    postfix

7.    The do-while loop is this type of loop.

a.    pretest

b.    posttest

c.    prefix

d.    postfix

 

8.    The for loop is this type of loop.

a.    pretest

b.    posttest

c.    prefix

d.    postfix

9.    This type of loop has no way of ending and repeats until the program is interrupted.

a.    indeterminate

b.    interminable

c.    infinite

d.    timeless

10. This type of loop always executes at least once.

a.    while

b.    do-while

c.    for

d.    any of these

 

11. This expression is executed by the for loop only once, regardless of the number of iterations.

a.    initialization expression

b.    test expression

c.    update expression

d.    pre-increment expression

12. This is a variable that keeps a running total.

a.    sentinel

b.    sum

c.    total

d.    accumulator

13. This is a special value that signals when there are no more items from a list of items to be processed. This value cannot be mistaken as an item from the list.

a.    sentinel

b.    flag

c.    signal

d.    accumulator

 

DEFINE THE FOLLOWING TERMS

 

while loop,

do-while loop,

for loop

infinite loop

pre-test loop,

post-test loop

sentinel

accumulator

 

REPRESENTATIVE SAMPLE PROGRAMS FOR CHAPTER 4

SIMILAR PROGRAMS  are in the chapter examples that you downloaded

optional - not to be turned in

type in your name and RUN THEM  ALL  using JGrasp

 

 

THE FIRST USES FOR

/**
This program demonstrates the for loop.
*/

public class Squares
{
public static void main(String [] args)
{
int number; // Loop control variable

System.out.println("Number Number Squared");
System.out.println("-----------------------");

for (number = 1; number <= 10; number++)
{
System.out.println(number + "\t\t" +
number * number);
}
}
}

THE SECOND USES WHILE

**
This program demonstrates the while loop.
*/

public class WhileLoop
{
public static void main(String [] args)
{
int number = 1;

while (number <= 5)
{
System.out.println("Hello");
number++;
}

System.out.println("That's all!");
}
}

 

THE THIRD USES DO-WHILE

import java.util.Scanner; // Needed for the Scanner class

/**
This program demonstrates a user controlled loop.
*/

public class TestAverage1
{
public static void main(String[] args)
{
int score1, score2, score3; // Three test scores
double average; // Average test score
char repeat; // To hold 'y' or 'n'
String input; // To hold input

System.out.println("This program calculates the " +
"average of three test scores.");

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get as many sets of test scores as the user wants.
do
{
// Get the first test score in this set.
System.out.print("Enter score #1: ");
score1 = keyboard.nextInt();

// Get the second test score in this set.
System.out.print("Enter score #2: ");
score2 = keyboard.nextInt();

// Get the third test score in this set.
System.out.print("Enter score #3: ");
score3 = keyboard.nextInt();

// Consume the remaining newline.
keyboard.nextLine();

// Calculate and print the average test score.
average = (score1 + score2 + score3) / 3.0;
System.out.println("The average is " + average);
System.out.println(); // Prints a blank line

// Does the user want to average another set?
System.out.println("Would you like to average " +
"another set of test scores?");
System.out.print("Enter Y for yes or N for no: ");
input = keyboard.nextLine(); // Read a line.
repeat = input.charAt(0); // Get the first char.

} while (repeat == 'Y' || repeat == 'y');
}
}

 

HINTS FOR CHAPTER 4

Read the following hints for correcting your JAVA programs

 

The following list describes several errors that are commonly committed when learning this chapter’s topics.

·         Using the increment or decrement operator in the wrong mode. When the increment or decrement operator is placed in front of (to the left of) its operand, it is used in prefix mode. When either of these operators is placed behind (to the right of) its operand, it is used in postfix mode.

·         Forgetting to enclose the boolean expression in a while loop or a do-while loop inside parentheses.

·         Placing a semicolon at the end of a while or for loop’s header. When you write a semicolon at the end of a while or for loop’s header, Java assumes that the conditionally executed statement is a null or empty statement. This usually results in an infinite loop.

·         Forgetting to write the semicolon at the end of the do-while loop. The do-while loop must be terminated with a semicolon.

·         Forgetting to enclose multiple statements in the body of a loop in braces. Normally a loop conditionally executes only one statement. To conditionally execute more than one statement, you must place the statements in braces.

·         Using commas instead of semicolons to separate the initialization, test, and update expressions in a for loop.

·         Forgetting to write code in the body of a while or do-while loop that modifies the loop control variable. If a while or do-while loop’s boolean expression never becomes false, the loop will repeat indefinitely. You must have code in the body of the loop that modifies the loop control variable so that the boolean expression will at some point become false.

·         Using a sentinel value that can also be a valid data value. Remember, a sentinel is a special value that cannot be mistaken as a member of a list of data items and signals that there are no more data items from the list to be processed. If you choose as a sentinel a value that might also appear in the list, the loop will prematurely terminate if it encounters the value in the list.

·         Forgetting to initialize an accumulator to zero. In order for an accumulator to keep a correct running total, it must be initialized to zero before any values are added to it.


ANSWERS TO MC

Chapter 4

Multiple Choice

 

1. a

2. b

3. c

4. d

5. a

6. a

7. b

8. a

9. c

10. b

11. a

12. d

13. a