Java Chapter 6 – Loops Questions and
Answers| Latest Update
What is the sentinel value in the following code snippet?
public static void main(String[] args)
{
int age = 0;
int sumOfAges = 0;
int stop = 1;
Scanner reader = new Scanner(System.in);
System.out.println("Enter an age (-1 to stop): ");
age = reader.nextInt();
while (age != -1)
{
sumOfAges = sumOfAges + age;
System.out.println("Enter an age (-1 to stop): ");
age = reader.nextInt();
}
System.out.println("Sum of ages " + sumOfAges);
return 0;
} ✔️✔️-1
What is the outcome of the following code snippet?
boolean val1 = true;
boolean val2 = false;
while (val1)
{
, if (val1)
{
System.out.println("Hello");
}
val1 = val2;
} ✔️✔️"Hello" will be displayed only once.
Suppose that a program asks a user to enter multiple integers, either positive or negative, to do
some calculation. The data entry will stop when the user enters a certain value to indicate the
end of the data. What suggested value should the code use as the sentinel? ✔️✔️An alphabetic
character
Which of the following statements is correct about a sentinel? ✔️✔️A sentinel is a value that
indicates the end of an input sequence.
Which statement is correct about the execution of the loop in the following code fragment?
double num;
int incr = 0;
Scanner reader = new Scanner(System.in);
do
{
incr = incr + 1;
System.out.println("Please enter a number (0 when done): ");
num = reader.nextDouble();
}
while (num != 0);
System.out.println("" + incr); ✔️✔️The loop will execute at least once even if the user has
entered the sentinel value.
Answers| Latest Update
What is the sentinel value in the following code snippet?
public static void main(String[] args)
{
int age = 0;
int sumOfAges = 0;
int stop = 1;
Scanner reader = new Scanner(System.in);
System.out.println("Enter an age (-1 to stop): ");
age = reader.nextInt();
while (age != -1)
{
sumOfAges = sumOfAges + age;
System.out.println("Enter an age (-1 to stop): ");
age = reader.nextInt();
}
System.out.println("Sum of ages " + sumOfAges);
return 0;
} ✔️✔️-1
What is the outcome of the following code snippet?
boolean val1 = true;
boolean val2 = false;
while (val1)
{
, if (val1)
{
System.out.println("Hello");
}
val1 = val2;
} ✔️✔️"Hello" will be displayed only once.
Suppose that a program asks a user to enter multiple integers, either positive or negative, to do
some calculation. The data entry will stop when the user enters a certain value to indicate the
end of the data. What suggested value should the code use as the sentinel? ✔️✔️An alphabetic
character
Which of the following statements is correct about a sentinel? ✔️✔️A sentinel is a value that
indicates the end of an input sequence.
Which statement is correct about the execution of the loop in the following code fragment?
double num;
int incr = 0;
Scanner reader = new Scanner(System.in);
do
{
incr = incr + 1;
System.out.println("Please enter a number (0 when done): ");
num = reader.nextDouble();
}
while (num != 0);
System.out.println("" + incr); ✔️✔️The loop will execute at least once even if the user has
entered the sentinel value.