A loop that searches for a particular value in input is called a(n) : correct answers sentinel loop
final int LASTVAL = -99;
Scanner cin = new Scanner(System.in);
int entry = cin.nextInt();
while (entry != LASTVAL)
{
int triple = entry * 3;
System.out.println(triple);
entry = cin.nextInt();
}
The above code is an example of a(n) ____ while loop. correct answers sentinel-controlled
Which executes first in a do...while loop? correct answers statement
What is the output of the following Java code?
int num = 0;
while (num < 5)
{
System.out.print(num + " ");
num = num + 1;
}
System.out.println(); correct answers 0 1 2 3 4
Suppose sum and num are int variables, and the input is
18 25 61 6 -1
What is the output of the following code?
Scanner cin = new Scanner(System.in);
sum = 0;
num = cin.nextInt();
while (num != -1)
{
sum = sum + num;
num = cin.nextInt();
}
System.out.println(sum); correct answers 110
int x = 5;
int y = 30;
do
x = x * 2;
while (x < y);
, If y = 0,
How many times would the loop above execute? correct answers 1
What is the output of the following Java code?
int x = 1;
do
{
System.out.print(x + " ");
x--;
}
while (x > 0);
System.out.println(); correct answers 1
Which of the following is not a function of the break statement? correct answers To ignore
certain values for variables and continue with the next iteration of a loop
Suppose that the input is
5 3 4 -6 8
What is the output of following Java code fragment?
Scanner cin = new Scanner(System.in);
int sum = 0;
int num;
for (int j = 1; j <= 5; j++)
{
num = cin.nextInt();
if (num < 0)
continue;
sum = sum + num;
}
System.out.println(sum); correct answers 20
The hasNext method returns ____ when trying to read past the end of the input file. correct
answers false
Which of the following about Java arrays is true?
(i) All components must be of the same type.
(ii) The array index and array element must be of the same type. correct answers Only (i)
You can create an array of three doubles by writing : correct answers double ar[] = { 1.1, 2.2, 3.3
};