Deck 4: Loops
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/100
Play
Full screen (f)
Deck 4: Loops
1
A loop inside another loop is called:
A) A sentinel loop
B) A nested loop
C) A parallel loop
D) A do/while loop
A) A sentinel loop
B) A nested loop
C) A parallel loop
D) A do/while loop
B
2
What is the output of the code fragment given below?
Int i = 0;
Int j = 0;
While (i < 27)
{
I = i + 2;
J++;
}
System.out.println("j=" + j);
A) j=27
B) j=12
C) j=13
D) j=14
Int i = 0;
Int j = 0;
While (i < 27)
{
I = i + 2;
J++;
}
System.out.println("j=" + j);
A) j=27
B) j=12
C) j=13
D) j=14
D
3
What is the output of the following code snippet?
Int i = 1;
While (i < 10)
{
System.out.print(i + " ");
I = i + 2;
If (i == 5)
{
I = 9;
}
}
A) 1 3 5
B) 1 3 9
C) 1 3 5 7 9
D) 1 3 5 9
Int i = 1;
While (i < 10)
{
System.out.print(i + " ");
I = i + 2;
If (i == 5)
{
I = 9;
}
}
A) 1 3 5
B) 1 3 9
C) 1 3 5 7 9
D) 1 3 5 9
B
4
The code snippet below checks whether a given number is a prime number. What will be the result of executing it?
Public static void main(String[] args)
{
Int j = 2;
Int result = 0;
Int number = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Please enter a number: ");
Number = reader.nextInt();
While (j <= number / 2)
{
If (number % j == 0)
{
Result = 1;
}
J++;
}
If (result == 1)
{
System.out.println("Number: " + number + " is Not Prime.");
}
Else
{
System.out.println("Number: " + number + " is Prime. ");
}
}
A) The code snippet will not compile.
B) The code snippet will display the desired result.
C) The code snippet will display an incorrect result.
D) The code snippet will loop forever.
Public static void main(String[] args)
{
Int j = 2;
Int result = 0;
Int number = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Please enter a number: ");
Number = reader.nextInt();
While (j <= number / 2)
{
If (number % j == 0)
{
Result = 1;
}
J++;
}
If (result == 1)
{
System.out.println("Number: " + number + " is Not Prime.");
}
Else
{
System.out.println("Number: " + number + " is Prime. ");
}
}
A) The code snippet will not compile.
B) The code snippet will display the desired result.
C) The code snippet will display an incorrect result.
D) The code snippet will loop forever.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
5
How many times does the following loop run?
Int i = 0;
Int j = 1;
Do
{
System.out.println("" + i + ";" + j);
I++;
If (i % 2 == 0)
{
J--;
}
}
While (j >= 1);
A) 0 times
B) 1 times
C) 2 times
D) 4 times
Int i = 0;
Int j = 1;
Do
{
System.out.println("" + i + ";" + j);
I++;
If (i % 2 == 0)
{
J--;
}
}
While (j >= 1);
A) 0 times
B) 1 times
C) 2 times
D) 4 times
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
6
What is the output of the following code fragment?
Int i = 1;
Int sum = 0;
While (i <= 11)
{
Sum = sum + i;
I++;
}
System.out.println("The value of sum is " + sum);
A) The value of sum is 65
B) The value of sum is 66
C) The value of sum is 55
D) The value of sum is 56
Int i = 1;
Int sum = 0;
While (i <= 11)
{
Sum = sum + i;
I++;
}
System.out.println("The value of sum is " + sum);
A) The value of sum is 65
B) The value of sum is 66
C) The value of sum is 55
D) The value of sum is 56
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
7
How many times will the following loop run?
Int i = 0;
While (i < 10)
{
System.out.println(i);
I++;
}
A) 0
B) 8
C) 9
D) 10
Int i = 0;
While (i < 10)
{
System.out.println(i);
I++;
}
A) 0
B) 8
C) 9
D) 10
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
8
Which error type does the "off-by-one" error belong to?
A) Syntax error
B) Compile-time error
C) Run-time error
D) Infinite loop
A) Syntax error
B) Compile-time error
C) Run-time error
D) Infinite loop
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
9
How many times does the loop execute in the following code fragment?
Int i;
For (i = 0; i < 50; i = i + 4)
{
System.out.println(i);
}
A) 11
B) 12
C) 13
D) 14
Int i;
For (i = 0; i < 50; i = i + 4)
{
System.out.println(i);
}
A) 11
B) 12
C) 13
D) 14
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following loops executes the statements inside the loop before checking the condition?
A) for
B) while
C) do
D) do-for
A) for
B) while
C) do
D) do-for
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
11
Which loop does not check a condition at the beginning of the loop?
I) The do loop
II) The while loop
III) The for loop
A) I and II
B) I and III
C) I only
D) III only
I) The do loop
II) The while loop
III) The for loop
A) I and II
B) I and III
C) I only
D) III only
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
12
What is the output of the code snippet given below?
Int i = 0;
While (i != 9)
{
System.out.println("" + i);
I = i + 2;
}
A) No output
B) 0 2 4 6 8
C) 10 12 14 16 18 …. (infinite loop)
D) 0 2 4 6 8 10 12 14 …. (infinite loop)
Int i = 0;
While (i != 9)
{
System.out.println("" + i);
I = i + 2;
}
A) No output
B) 0 2 4 6 8
C) 10 12 14 16 18 …. (infinite loop)
D) 0 2 4 6 8 10 12 14 …. (infinite loop)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
13
How many times will the output line be printed in the following code snippet?
For (int num2 = 1; num2 <= 3; num2++)
{
For (int num1 = 0; num1 <= 2; num1++)
{
System.out.println("" + num2 + " " + num1);
}
}
A) 3 times
B) 6 times
C) 9 times
D) 12 times
For (int num2 = 1; num2 <= 3; num2++)
{
For (int num1 = 0; num1 <= 2; num1++)
{
System.out.println("" + num2 + " " + num1);
}
}
A) 3 times
B) 6 times
C) 9 times
D) 12 times
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
14
How many times does the following code fragment display "Hi"?
Int i = 10;
While (i >= 0)
{
System.out.println("Hi");
I--;
}
A) 9 times
B) 10 times
C) 11 times
D) 12 times
Int i = 10;
While (i >= 0)
{
System.out.println("Hi");
I--;
}
A) 9 times
B) 10 times
C) 11 times
D) 12 times
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
15
What are the values of i and j after the following code fragment runs?
Int i = 60;
Int j = 50;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println("i=" + i + ", j=" + j);
A) i = 1951, j = 0
B) i = 1951, j = 45
C) i = 65, j = 1
D) i = 65, j = 45
Int i = 60;
Int j = 50;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println("i=" + i + ", j=" + j);
A) i = 1951, j = 0
B) i = 1951, j = 45
C) i = 65, j = 1
D) i = 65, j = 45
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
16
What is the output of the code snippet given below?
Int i;
Int j = 0;
For (i = 0; i < 5; i++)
{
If (i % 2 == 0)
{
I = i + 2;
J++;
}
Else
{
I++;
J = j + 2;
}
J++;
}
System.out.println("i=" + i + ", j=" + j);
A) i=7, j =7
B) i =7, j =6
C) i =6, j =7
D) i =5, j =5
Int i;
Int j = 0;
For (i = 0; i < 5; i++)
{
If (i % 2 == 0)
{
I = i + 2;
J++;
}
Else
{
I++;
J = j + 2;
}
J++;
}
System.out.println("i=" + i + ", j=" + j);
A) i=7, j =7
B) i =7, j =6
C) i =6, j =7
D) i =5, j =5
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
17
How many times does the following code snippet display "Loop Execution"?
For (int i = 0; i < 10; i++);
{
System.out.println("Loop Execution");
}
A) Ten times.
B) The code snippet does not run because of a compile error.
C) Infinite loop.
D) Only one time.
For (int i = 0; i < 10; i++);
{
System.out.println("Loop Execution");
}
A) Ten times.
B) The code snippet does not run because of a compile error.
C) Infinite loop.
D) Only one time.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
18
How many times does the code snippet given below display "Loop Execution"?
Int i = 1;
While (i != 10)
{
System.out.println ("Loop Execution");
I++;
}
A) Infinite times
B) 8 times
C) 9 times
D) 10 times
Int i = 1;
While (i != 10)
{
System.out.println ("Loop Execution");
I++;
}
A) Infinite times
B) 8 times
C) 9 times
D) 10 times
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
19
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;
}
A) 0
B) 1
C) 2
D) -1
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;
}
A) 0
B) 1
C) 2
D) -1
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
20
What is the first and last value of i to be displayed by the following code snippet?
Int n = 20;
For (int i = 0; i <= n; i++)
{
For (int j = 0; j <= i; j++)
{
System.out.println("" + i);
}
}
A) 0 and 20
B) 1 and 20
C) 0 and 19
D) 1 and 19
Int n = 20;
For (int i = 0; i <= n; i++)
{
For (int j = 0; j <= i; j++)
{
System.out.println("" + i);
}
}
A) 0 and 20
B) 1 and 20
C) 0 and 19
D) 1 and 19
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
21
What will be the output of the following code snippet?
Boolean token = false;
Do
{
System.out.println("Hello");
}
While (token);
A) "Hello" will be displayed infinite times.
B) No output because of compilation error.
C) No output after successful compilation.
D) "Hello" will be displayed only once.
Boolean token = false;
Do
{
System.out.println("Hello");
}
While (token);
A) "Hello" will be displayed infinite times.
B) No output because of compilation error.
C) No output after successful compilation.
D) "Hello" will be displayed only once.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
22
What is the output of the code snippet given below?
String s = "12345";
Int i = 1;
Do
{
If (i > 1)
{
System.out.print);
}
}
While (i < 5);
A) No output
B) No output (infinite loop)
C) 12345
D) 2345
String s = "12345";
Int i = 1;
Do
{
If (i > 1)
{
System.out.print);
}
}
While (i < 5);
A) No output
B) No output (infinite loop)
C) 12345
D) 2345
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
23
How many times does the following loop execute?
Int upperCaseLetters = 0;
String str = "abcdEfghI";
Boolean found = false;
For (int i = 0; i < str.length() && !found; i++)
{
Char ch = str.charAt(i);
If (Character.isUpperCase(ch))
{
Found = true;
}
}
A) 9 times
B) 8 times
C) 5 times
D) 1 time
Int upperCaseLetters = 0;
String str = "abcdEfghI";
Boolean found = false;
For (int i = 0; i < str.length() && !found; i++)
{
Char ch = str.charAt(i);
If (Character.isUpperCase(ch))
{
Found = true;
}
}
A) 9 times
B) 8 times
C) 5 times
D) 1 time
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
24
What is the result when the following code is run?
Double x = 1;
Double y = 1;
Int i = 0;
Do
{
Y = x / 2;
X = x + y;
I = i + 1;
}
While (x < 2.5);
System.out.print(i + " ");
A) 1
B) 2
C) 3
D) 4
Double x = 1;
Double y = 1;
Int i = 0;
Do
{
Y = x / 2;
X = x + y;
I = i + 1;
}
While (x < 2.5);
System.out.print(i + " ");
A) 1
B) 2
C) 3
D) 4
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following is the correct code snippet for throwing a pair of dice to get a sum of the numbers on two dice between 2 and 12 with different probabilities?
A) (int) (Math.random() * 6 + 1)
B) (int) (Math.random() * 12 + 1)
C) (int) (Math.random() * 6 + 1) + (int) (Math.random() * 6 + 1)
D) (int) (Math.random() * 12 - 2)
A) (int) (Math.random() * 6 + 1)
B) (int) (Math.random() * 12 + 1)
C) (int) (Math.random() * 6 + 1) + (int) (Math.random() * 6 + 1)
D) (int) (Math.random() * 12 - 2)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
26
What is the output of the following code snippet?
Int i = 1;
While (i <= 10)
{
System.out.println("Inside the while loop");
I = i + 10;
}
A) No output because of compilation error.
B) "Inside the while loop" will be displayed 10 times.
C) No output after successful compilation.
D) "Inside the while loop" will be displayed only once.
Int i = 1;
While (i <= 10)
{
System.out.println("Inside the while loop");
I = i + 10;
}
A) No output because of compilation error.
B) "Inside the while loop" will be displayed 10 times.
C) No output after successful compilation.
D) "Inside the while loop" will be displayed only once.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
27
Given the following code snippet, what should we change to have 26 alphabet characters in the string str?
String str = "";
For (char c = 'A'; c < 'Z'; c++)
{
Str = str + c;
}
A) int c = 'A'
B) str = c + str;
C) c <= 'Z'
D) Must change to use a do loop
String str = "";
For (char c = 'A'; c < 'Z'; c++)
{
Str = str + c;
}
A) int c = 'A'
B) str = c + str;
C) c <= 'Z'
D) Must change to use a do loop
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
28
Which of the following activities can be simulated using a computer?
I) Waiting time in a line at a restaurant
II) Tossing a coin
III) Shuffling cards for a card game
A) I only
B) II only
C) I and II only
D) I, II, and III
I) Waiting time in a line at a restaurant
II) Tossing a coin
III) Shuffling cards for a card game
A) I only
B) II only
C) I and II only
D) I, II, and III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
29
What will be the range of the random numbers generated by the following code snippet?
Int r1 = (int) (Math.random() * 50) + 1;
A) Between 1 and 49
B) Between 0 and 50
C) Between 0 and 49
D) Between 1 and 50
Int r1 = (int) (Math.random() * 50) + 1;
A) Between 1 and 49
B) Between 0 and 50
C) Between 0 and 49
D) Between 1 and 50
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
30
What is the output of the following code snippet?
Int i = 1;
While (i < 20)
{
System.out.print(i + " ");
I = i + 2;
If (i == 15)
{
I = 19;
}
}
A) 1 3 5 7 9 11 13 15 17 19
B) 1 3 5 7 9 11 13 19
C) 1 3 5 7 9 11 13 15 17
D) 1 3 5 7 9 11 13 17 19
Int i = 1;
While (i < 20)
{
System.out.print(i + " ");
I = i + 2;
If (i == 15)
{
I = 19;
}
}
A) 1 3 5 7 9 11 13 15 17 19
B) 1 3 5 7 9 11 13 19
C) 1 3 5 7 9 11 13 15 17
D) 1 3 5 7 9 11 13 17 19
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
31
What is the output of the code fragment given below?
Int i = 0;
Int j = 0;
While (i < 125)
{
I = i + 2;
J++;
}
System.out.println(j);
A) 0
B) 62
C) 63
D) The code fragment displays no output because it does not compile.
Int i = 0;
Int j = 0;
While (i < 125)
{
I = i + 2;
J++;
}
System.out.println(j);
A) 0
B) 62
C) 63
D) The code fragment displays no output because it does not compile.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
32
What is the data type of the number generated by the Math.random() function?
A) double
B) float
C) int
D) String
A) double
B) float
C) int
D) String
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
33
What is the output of the code snippet given below?
String s = "abcde";
Int i = 1;
Do
{
If (i > 1)
{
System.out.print);
}
}
While (i < 5);
A) No output
B) No output (infinite loop)
C) abcde
D) bcde
String s = "abcde";
Int i = 1;
Do
{
If (i > 1)
{
System.out.print);
}
}
While (i < 5);
A) No output
B) No output (infinite loop)
C) abcde
D) bcde
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
34
What is the output of the code snippet given below?
String s = "abcde";
Int i = 1;
While (i < 5)
{
System.out.print);
I++;
}
A) No output
B) abcd
C) abcde
D) bcde
String s = "abcde";
Int i = 1;
While (i < 5)
{
System.out.print);
I++;
}
A) No output
B) abcd
C) abcde
D) bcde
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
35
In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop?
Int i;
Int j;
For (i = 0; i <= 9; i++)
{
For (j = 1; j < 5; j++)
{
System.out.println("Hello");
If (j == 2)
{
J = 6;
}
}
}
A) When the value of j becomes 6
B) When the program executes completely
C) When the condition for the outer loop is met
D) When the value of i is incremented
Int i;
Int j;
For (i = 0; i <= 9; i++)
{
For (j = 1; j < 5; j++)
{
System.out.println("Hello");
If (j == 2)
{
J = 6;
}
}
}
A) When the value of j becomes 6
B) When the program executes completely
C) When the condition for the outer loop is met
D) When the value of i is incremented
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
36
What will be the final output of the following code snippet when a user enters input values in the order 10, 20, 30, 40, 50, and -1?
Public static void main(String[] args)
{
Double sum = 0;
Int count = 0;
Double salary = 0;
Double average = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Enter salaries (-1 to stop): ");
While (salary != -1)
{
Salary = reader.nextInt();
If (salary != -1)
{
Sum = sum + salary;
Count++;
}
}
If (count > 0)
{
Average = sum / count;
System.out.println("The Average Salary: " + average);
}
Else
{
System.out.println ("No data!");
}
Return 0;
}
A) The Average Salary: 0
B) The Average Salary: 30
C) The Average Salary: 24.83333
D) There will be no output as the code snippet will not compile.
Public static void main(String[] args)
{
Double sum = 0;
Int count = 0;
Double salary = 0;
Double average = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Enter salaries (-1 to stop): ");
While (salary != -1)
{
Salary = reader.nextInt();
If (salary != -1)
{
Sum = sum + salary;
Count++;
}
}
If (count > 0)
{
Average = sum / count;
System.out.println("The Average Salary: " + average);
}
Else
{
System.out.println ("No data!");
}
Return 0;
}
A) The Average Salary: 0
B) The Average Salary: 30
C) The Average Salary: 24.83333
D) There will be no output as the code snippet will not compile.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
37
What is the output of the code snippet given below?
String s = "12345";
Int i = 1;
While (i < 5)
{
System.out.print);
I++;
}
A) No output
B) 1234
C) 12345
D) 2345
String s = "12345";
Int i = 1;
While (i < 5)
{
System.out.print);
I++;
}
A) No output
B) 1234
C) 12345
D) 2345
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
38
What will be the output of the following code snippet?
Boolean token = false;
While (token)
{
System.out.println("Hello");
}
A) "Hello" will be displayed infinite times.
B) No output because of compilation error.
C) No output after successful compilation.
D) "Hello" will be displayed only once.
Boolean token = false;
While (token)
{
System.out.println("Hello");
}
A) "Hello" will be displayed infinite times.
B) No output because of compilation error.
C) No output after successful compilation.
D) "Hello" will be displayed only once.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
39
How many times does the code snippet below display "Hello"?
Int i = 0;
While (i != 15)
{
System.out.println("Hello");
I++;
}
A) Infinite times
B) 14 times
C) 15 times
D) 16 times
Int i = 0;
While (i != 15)
{
System.out.println("Hello");
I++;
}
A) Infinite times
B) 14 times
C) 15 times
D) 16 times
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
40
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;
}
A) No output will be displayed because of compilation error.
B) "Hello" will be displayed only once.
C) "Hello" will be displayed infinite times.
D) No output will be displayed even after successful compilation of the code snippet.
Boolean val1 = true;
Boolean val2 = false;
While (val1)
{
If (val1)
{
System.out.println("Hello");
}
Val1 = val2;
}
A) No output will be displayed because of compilation error.
B) "Hello" will be displayed only once.
C) "Hello" will be displayed infinite times.
D) No output will be displayed even after successful compilation of the code snippet.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
41
Which of the following loops executes exactly 10 times?
A) for (int i = 0; i <= 10; i++) { }
B) int i = 0;
Boolean found = false;
Do
{
I++;
If (i % 10 == 0)
{
Found = true;
}
}
While (i < 10 && !found);
C) int i = 0;
While (i <= 10)
{
I++;
}
D) for (int i = 1; i <= 10; i++)
A) for (int i = 0; i <= 10; i++) { }
B) int i = 0;
Boolean found = false;
Do
{
I++;
If (i % 10 == 0)
{
Found = true;
}
}
While (i < 10 && !found);
C) int i = 0;
While (i <= 10)
{
I++;
}
D) for (int i = 1; i <= 10; i++)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
42
What are the values of i and j after the following code snippet is run?
Int i = 10;
Int j = 20;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println("i = " + i + ", j = " + j);
A) i = 45, j = 1
B) i = 351, j = 0
C) i = 351, j = 2
D) i = 1311, j = 35
Int i = 10;
Int j = 20;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println("i = " + i + ", j = " + j);
A) i = 45, j = 1
B) i = 351, j = 0
C) i = 351, j = 2
D) i = 1311, j = 35
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
43
What is the output of this loop?
Int i = 0;
Boolean found;
While (i < 20 && !found)
{
Int sum = i * 2 + i * 3;
System.out.print(sum + " ");
If (sum > 50)
{
Found = true;
}
I++;
}
A) 0 5 10 15 20 25 30 35 40 45 50 55
B) 0
C) No output, compilation error
D) 0 5 10
Int i = 0;
Boolean found;
While (i < 20 && !found)
{
Int sum = i * 2 + i * 3;
System.out.print(sum + " ");
If (sum > 50)
{
Found = true;
}
I++;
}
A) 0 5 10 15 20 25 30 35 40 45 50 55
B) 0
C) No output, compilation error
D) 0 5 10
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
44
What is the last output line of the code snippet given below?
Int i = 0;
While (i < 10)
{
Int num = 1;
For (int j = i; j > 1; j--)
{
System.out.print(j + " ");
Num = num * 2;
}
System.out.println("***");
I++;
}
A) 3 2 ***
B) 9 8 7 6 5 4 3 2 ***
C) 8 7 6 5 4 3 2 ***
D) 2 ***
Int i = 0;
While (i < 10)
{
Int num = 1;
For (int j = i; j > 1; j--)
{
System.out.print(j + " ");
Num = num * 2;
}
System.out.println("***");
I++;
}
A) 3 2 ***
B) 9 8 7 6 5 4 3 2 ***
C) 8 7 6 5 4 3 2 ***
D) 2 ***
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
45
What is the output of the following code snippet?
Double a = 2;
Int n = 16;
Double r = 1;
Double b = a;
Int i = n;
While (i > 0)
{
If (i % 2 == 0) // n is even
{
B = b * b;
I = i / 2;
}
Else
{
R = r * b;
I--;
}
}
System.out.println("r = " + r);
A) 16.0
B) 128.0
C) 4096.0
D) 65536.0
Double a = 2;
Int n = 16;
Double r = 1;
Double b = a;
Int i = n;
While (i > 0)
{
If (i % 2 == 0) // n is even
{
B = b * b;
I = i / 2;
}
Else
{
R = r * b;
I--;
}
}
System.out.println("r = " + r);
A) 16.0
B) 128.0
C) 4096.0
D) 65536.0
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
46
What is the output of the code snippet given below?
String s = "abcdefghijkl";
Int i = 1;
Do
{
If (i > 2)
{
System.out.print(s.substring (1, i));
}
I++;
}
While (i < 5);
A) abcd
B) bcde
C) bcbcd
D) cdef
String s = "abcdefghijkl";
Int i = 1;
Do
{
If (i > 2)
{
System.out.print(s.substring (1, i));
}
I++;
}
While (i < 5);
A) abcd
B) bcde
C) bcbcd
D) cdef
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
47
What is the output of the following code fragment?
Int i = 1;
Int sum = 0;
While (i <= 15)
{
Sum = sum + i;
I++;
}
System.out.println("The value of sum is " + sum);
A) The value of sum is 0
B) The value of sum is 105
C) The value of sum is 120
D) The value of sum is 136
Int i = 1;
Int sum = 0;
While (i <= 15)
{
Sum = sum + i;
I++;
}
System.out.println("The value of sum is " + sum);
A) The value of sum is 0
B) The value of sum is 105
C) The value of sum is 120
D) The value of sum is 136
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
48
What does the following code snippet display?
Char ch1 = '\ u0000';
Char ch2 = '\ uffff';
For (int i = 0; i < 1000; i++)
{
If (i % 50 == 0)
{
System.out.println();
}
System.out.print((char)(ch1 + Math.random() * (ch2 - ch1 + 1)));
}
A) It displays random Unicode characters.
B) It displays random ASCII characters.
C) Nothing because it has compilation error.
D) It displays the hexadecimal characters between '0' and 'F'.
Char ch1 = '\ u0000';
Char ch2 = '\ uffff';
For (int i = 0; i < 1000; i++)
{
If (i % 50 == 0)
{
System.out.println();
}
System.out.print((char)(ch1 + Math.random() * (ch2 - ch1 + 1)));
}
A) It displays random Unicode characters.
B) It displays random ASCII characters.
C) Nothing because it has compilation error.
D) It displays the hexadecimal characters between '0' and 'F'.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
49
What does the following code do?
Int sum = 0;
Final int count = 1000;
For (int i = 1; i <= count; i++)
{
Sum = sum + (int) (Math.random() * 101);
}
System.out.println(sum / count);
A) It simulates the outcome of throwing a coin.
B) It calculates the average of 1000 random numbers between 0 and 100.
C) It performs Monte Carlo simulation of fluid dynamics.
D) It calculates the average of 1000 random numbers between 1 and 101.
Int sum = 0;
Final int count = 1000;
For (int i = 1; i <= count; i++)
{
Sum = sum + (int) (Math.random() * 101);
}
System.out.println(sum / count);
A) It simulates the outcome of throwing a coin.
B) It calculates the average of 1000 random numbers between 0 and 100.
C) It performs Monte Carlo simulation of fluid dynamics.
D) It calculates the average of 1000 random numbers between 1 and 101.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
50
Which of the following for loops is illegal?
A) for (int i = 0; ; ) { }
B) for (int i = 0) { }
C) for (int i = 0, k = 1; ; i++) { }
D) for ( ; ; )
A) for (int i = 0; ; ) { }
B) for (int i = 0) { }
C) for (int i = 0, k = 1; ; i++) { }
D) for ( ; ; )
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
51
How many times does the following loop execute?
Double d;
Double x = Math.random() * 100;
Do
{
D = Math.sqrt(x) * Math.sqrt(x) - x;
System.out.println(d);
X = Math.random() * 10001;
}
While (d != 0);
A) Exactly once
B) Exactly twice
C) Can't be determined
D) Always infinite loop
Double d;
Double x = Math.random() * 100;
Do
{
D = Math.sqrt(x) * Math.sqrt(x) - x;
System.out.println(d);
X = Math.random() * 10001;
}
While (d != 0);
A) Exactly once
B) Exactly twice
C) Can't be determined
D) Always infinite loop
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
52
In the __________ loop header, you can include multiple update expressions, separated by commas, but it is not recommended.
A) do
B) while
C) for
D) do
A) do
B) while
C) for
D) do
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
53
What is the output of the following code snippet?
Int counter = 1;
For (double i = 0.01; i <= 1.0; i = i + 0.01)
{
Counter++;
}
System.out.println(counter);
A) 100
B) 49.50
C) 60.5
D) 10
Int counter = 1;
For (double i = 0.01; i <= 1.0; i = i + 0.01)
{
Counter++;
}
System.out.println(counter);
A) 100
B) 49.50
C) 60.5
D) 10
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
54
Choose the loop that is equivalent to this loop.
Int n = 1;
Double x = 0;
Double s;
Do
{
S = 1.0 / (n * n);
X = x + s;
N++;
}
While (s > 0.01);
A) double x = 0;
Double s = 1;
For (int k = 1; s > 0.01; k++)
{
S = 1.0 / (k * k);
X = x + s;
}
B) double x = 0;
Double s = 1;
For (int k = 1; k < 100; k++)
{
S = 1.0 / (k * k);
X = x + s;
}
C) double x = 0;
Double s = 1;
Int k = 10;
While (s > 0.01)
{
S = 1.0 / (k * k);
X = x + s;
K++;
}
D) double x = 0;
Double s = 10;
Int k = 1;
While (s > 0.01)
{
S = 1.0 / (k * k);
X = x + s;
K++;
Int n = 1;
Double x = 0;
Double s;
Do
{
S = 1.0 / (n * n);
X = x + s;
N++;
}
While (s > 0.01);
A) double x = 0;
Double s = 1;
For (int k = 1; s > 0.01; k++)
{
S = 1.0 / (k * k);
X = x + s;
}
B) double x = 0;
Double s = 1;
For (int k = 1; k < 100; k++)
{
S = 1.0 / (k * k);
X = x + s;
}
C) double x = 0;
Double s = 1;
Int k = 10;
While (s > 0.01)
{
S = 1.0 / (k * k);
X = x + s;
K++;
}
D) double x = 0;
Double s = 10;
Int k = 1;
While (s > 0.01)
{
S = 1.0 / (k * k);
X = x + s;
K++;
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
55
Insert a statement that will correctly terminate this loop when the end of input is reached.
Boolean done = false;
While (!done)
{
String input = in.next();
If (input.equalsIgnoreCase("Q"))
{
__________
}
Else
{
Double x = Double.parseDouble(input);
Data.add(x);
}
}
A) stop;
B) done = 1;
C) exit;
D) done = true;
Boolean done = false;
While (!done)
{
String input = in.next();
If (input.equalsIgnoreCase("Q"))
{
__________
}
Else
{
Double x = Double.parseDouble(input);
Data.add(x);
}
}
A) stop;
B) done = 1;
C) exit;
D) done = true;
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
56
What is the output of the following code snippet?
Int f1 = 0;
Int f2 = 1;
Int fRes;
System.out.print(f1 + " ");
System.out.print(f2 + " ");
For (int i = 1; i < 10; i++)
{
FRes = f1 + f2;
System.out.print(fRes + " ");
F1 = f2;
F2 = fRes;
}
System.out.println();
A) 0 1 5 7 9 11 13 15 17 19
B) 0 1 1 2 3 5 8 13 21 34
C) 0 1 4 6 8 10 12 14 16 18
D) 0 1 6 7 9 12 14 17 19 21
Int f1 = 0;
Int f2 = 1;
Int fRes;
System.out.print(f1 + " ");
System.out.print(f2 + " ");
For (int i = 1; i < 10; i++)
{
FRes = f1 + f2;
System.out.print(fRes + " ");
F1 = f2;
F2 = fRes;
}
System.out.println();
A) 0 1 5 7 9 11 13 15 17 19
B) 0 1 1 2 3 5 8 13 21 34
C) 0 1 4 6 8 10 12 14 16 18
D) 0 1 6 7 9 12 14 17 19 21
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
57
What values does counter variable i assume when this loop executes?
For (int i = 20; i >= 2; i = i - 6)
{
System.out.print(i + ",");
}
A) 20, 14, 8, 2
B) 20, 14, 8, 2, -4
C) 20, 14, 8
D) 14, 8, 2
For (int i = 20; i >= 2; i = i - 6)
{
System.out.print(i + ",");
}
A) 20, 14, 8, 2
B) 20, 14, 8, 2, -4
C) 20, 14, 8
D) 14, 8, 2
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
58
What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1 5?
Double total = 0;
Boolean hasValidNumber = true;
Scanner in = new Scanner(System.in);
While (in.hasNextDouble() && hasValidNumber)
{
Double input = in.nextDouble();
If (input < 0)
{
HasValidNumber = false;
}
Else
{
Total = total + input;
}
}
System.out.println(total);
A) 15.0
B) 14.0
C) 12.0
D) 10.0
Double total = 0;
Boolean hasValidNumber = true;
Scanner in = new Scanner(System.in);
While (in.hasNextDouble() && hasValidNumber)
{
Double input = in.nextDouble();
If (input < 0)
{
HasValidNumber = false;
}
Else
{
Total = total + input;
}
}
System.out.println(total);
A) 15.0
B) 14.0
C) 12.0
D) 10.0
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
59
What does the following code snippet print?
Int a = 120;
Int b = 90;
Int n1 = Math.abs(a);
Int n2 = Math.abs(b);
Int result = 1;
For (int k = 1; k <= n1 && k <= n2; k++)
{
If (n1 % k == 0 && n2 % k == 0)
{
Result = k;
}
}
System.out.println(result);
A) 10
B) 20
C) 30
D) 40
Int a = 120;
Int b = 90;
Int n1 = Math.abs(a);
Int n2 = Math.abs(b);
Int result = 1;
For (int k = 1; k <= n1 && k <= n2; k++)
{
If (n1 % k == 0 && n2 % k == 0)
{
Result = k;
}
}
System.out.println(result);
A) 10
B) 20
C) 30
D) 40
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
60
Which of the following is considered a loop with a bound that could be problematic?
A) for (i = 1; i != n; i++)
B) for (years = n; years < 0; years--)
C) for (i = 1; i <= n; i++)
D) for (int i = 1; i <= 10; i++)
A) for (i = 1; i != n; i++)
B) for (years = n; years < 0; years--)
C) for (i = 1; i <= n; i++)
D) for (int i = 1; i <= 10; i++)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
61
Which of the following loop(s) could possibly not enter the loop body at all?
I) for loop
II) while loop
III) do loop
A) I only
B) I and II only
C) II and III only
D) I and III only
I) for loop
II) while loop
III) do loop
A) I only
B) I and II only
C) II and III only
D) I and III only
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
62
How many times does the following loop execute?
Int i = 0;
Boolean found = false;
While (i < 100 && !found)
{
I++;
System.out.print(i + " ");
Int j = i * i;
If ((i * i * i) % j == j)
{
Found = true;
}
}
A) 10 times
B) 20 times
C) 100 times
D) An infinite number of times
Int i = 0;
Boolean found = false;
While (i < 100 && !found)
{
I++;
System.out.print(i + " ");
Int j = i * i;
If ((i * i * i) % j == j)
{
Found = true;
}
}
A) 10 times
B) 20 times
C) 100 times
D) An infinite number of times
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
63
Which statement about storyboards is true?
A) A storyboard can help prevent potential user confusion early in the design process.
B) Storyboards are used primarily to understand how implemented programs work.
C) The storyboard helps to train users about how to use software.
D) Storyboards have no relationship to the structure of an actual working program.
A) A storyboard can help prevent potential user confusion early in the design process.
B) Storyboards are used primarily to understand how implemented programs work.
C) The storyboard helps to train users about how to use software.
D) Storyboards have no relationship to the structure of an actual working program.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
64
What changes do you need to make in the following code snippet to display "Let us learn Java" exactly 10 times?
Int i = 0;
While (i <= 10)
{
System.out.println("Let us learn Java");
I++;
}
A) while (i < 9)
B) while (i < 11)
C) while (i < 12)
D) int i = 1;
Int i = 0;
While (i <= 10)
{
System.out.println("Let us learn Java");
I++;
}
A) while (i < 9)
B) while (i < 11)
C) while (i < 12)
D) int i = 1;
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
65
Which of the following statements expresses why the following code is considered bad form?
For (rate = 5; years-- > 0; System.out.println(balance))
) . .
I) Unrelated expressions in loop header
II) Doesn't match expected for loop idiom
III) Loop iteration is not clear
A) II and III only
B) I and II only
C) I and III only
D) I, II, and III
For (rate = 5; years-- > 0; System.out.println(balance))
) . .
I) Unrelated expressions in loop header
II) Doesn't match expected for loop idiom
III) Loop iteration is not clear
A) II and III only
B) I and II only
C) I and III only
D) I, II, and III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
66
Which of the following code snippets displays the output exactly 10 times?
A) int i = 0;
While (i < 10);
{
System.out.println("This is example 1.");
I++;
}
B) int i = 0;
While (i < 10)
{
System.out.println("This is example 2.");
I++;
}
C) int i = 0;
While (i < 10)
{
System.out.println("This is example 3.");
}
D) int i = 1;
While (i < 10)
{
System.out.println("This is example 4.");
I++;
A) int i = 0;
While (i < 10);
{
System.out.println("This is example 1.");
I++;
}
B) int i = 0;
While (i < 10)
{
System.out.println("This is example 2.");
I++;
}
C) int i = 0;
While (i < 10)
{
System.out.println("This is example 3.");
}
D) int i = 1;
While (i < 10)
{
System.out.println("This is example 4.");
I++;
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
67
Which of the following is considered a loop with a problematic condition?
A) for(i = 1; i != n; i++)
B) for (years = n; years > 0; years++)
C) for(i = 1; i <= n; i++)
D) for (int i = 20; i >= 10; i--)
A) for(i = 1; i != n; i++)
B) for (years = n; years > 0; years++)
C) for(i = 1; i <= n; i++)
D) for (int i = 20; i >= 10; i--)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
68
Which of the following is correct for simulating the toss of a pair of coins to get 0 (head) or 1 (tail) with different probabilities?
A) (int) (Math.random() * 0 + 1)
B) (int) (Math.random() * 1 + 1)
C) (int) (Math.random() * 2)
D) (int) (Math.random() * 2) + (int) (Math.random() * 2)
A) (int) (Math.random() * 0 + 1)
B) (int) (Math.random() * 1 + 1)
C) (int) (Math.random() * 2)
D) (int) (Math.random() * 2) + (int) (Math.random() * 2)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
69
What is the output of the following loop?
Int s = 1;
Int n = 1;
Do
{
S = s + n;
N++;
}
While (s < 10 * n);
System.out.println(s);
A) 211
B) 210
C) 120
D) 123
Int s = 1;
Int n = 1;
Do
{
S = s + n;
N++;
}
While (s < 10 * n);
System.out.println(s);
A) 211
B) 210
C) 120
D) 123
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
70
Which code snippet produces the sum of the first n even numbers? (0 is not considered an even number.)
A) int sum = 0;
For (int i = 1; i <= n; i++)
{
If (i % 2 == 0)
{
Sum = sum + i;
}
}
B) int sum = 0;
For (int i = 1; i <= n; i++)
{
Sum = sum + i * 2;
}
C) int sum = 0;
For (int i = 0; i < n; i++)
{
If (i % 2 == 0)
{
Sum = sum + i;
}
}
D) int sum;
For (int i = 1; i <= n; i++)
{
Sum = sum + i * 2;
A) int sum = 0;
For (int i = 1; i <= n; i++)
{
If (i % 2 == 0)
{
Sum = sum + i;
}
}
B) int sum = 0;
For (int i = 1; i <= n; i++)
{
Sum = sum + i * 2;
}
C) int sum = 0;
For (int i = 0; i < n; i++)
{
If (i % 2 == 0)
{
Sum = sum + i;
}
}
D) int sum;
For (int i = 1; i <= n; i++)
{
Sum = sum + i * 2;
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
71
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 value should the code use as the sentinel?
A) 0
B) -1
C) 999
D) An alphabetic character
A) 0
B) -1
C) 999
D) An alphabetic character
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
72
When hand-tracing the loop in the code snippet below, which variables are important to evaluate?
Int i = 10;
Int j = 5;
Int k = -10;
Int sum = 0;
While (i > 0)
{
Sum = sum + i + j;
I--;
System.out.println("Iteration: " + i);
}
A) The variables i and j
B) The variables i and sum
C) The variables i, j, and k
D) The variables j and k
Int i = 10;
Int j = 5;
Int k = -10;
Int sum = 0;
While (i > 0)
{
Sum = sum + i + j;
I--;
System.out.println("Iteration: " + i);
}
A) The variables i and j
B) The variables i and sum
C) The variables i, j, and k
D) The variables j and k
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
73
What will be the result of running the following code fragment?
Int year = 0;
Double rate = 5;
Double principal = 10000;
Double interest = 0;
While (year < 10)
{
Interest = (principal * year * rate) / 100;
System.out.println("Interest " + interest);
}
A) The code fragment will display the interest calculated for nine years.
B) The code fragment will continue to display the calculated interest forever because the loop will never end.
C) The code fragment will not display the calculated interest and halt abruptly.
D) The code fragment will not display any output because it will not compile.
Int year = 0;
Double rate = 5;
Double principal = 10000;
Double interest = 0;
While (year < 10)
{
Interest = (principal * year * rate) / 100;
System.out.println("Interest " + interest);
}
A) The code fragment will display the interest calculated for nine years.
B) The code fragment will continue to display the calculated interest forever because the loop will never end.
C) The code fragment will not display the calculated interest and halt abruptly.
D) The code fragment will not display any output because it will not compile.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
74
What is the output of this code snippet?
String str = "ABCabc";
Char ch;
Int i = 0;
While (i < str.length())
{
Ch = str.charAt(i);
If (Character.isLowerCase(ch))
{
System.out.print(i + " ");
}
Else
{
I++;
}
}
A) 3 4 5
B) 3
C) 3 3 3 3 3 ... (infinite loop)
D) 0 1 2
String str = "ABCabc";
Char ch;
Int i = 0;
While (i < str.length())
{
Ch = str.charAt(i);
If (Character.isLowerCase(ch))
{
System.out.print(i + " ");
}
Else
{
I++;
}
}
A) 3 4 5
B) 3
C) 3 3 3 3 3 ... (infinite loop)
D) 0 1 2
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
75
What is the output of the following code snippet?
Int i = 1;
While (i != 9)
{
System.out.print(i + " ");
I++;
If (i == 9)
{
System.out.println("End");
}
}
A) 1 End
B) 1 End (infinite loop)
C) 1 2 3 4 5 6 7 8 End
D) 1 2 3 4 5 6 7 8 End (infinite loop)
Int i = 1;
While (i != 9)
{
System.out.print(i + " ");
I++;
If (i == 9)
{
System.out.println("End");
}
}
A) 1 End
B) 1 End (infinite loop)
C) 1 2 3 4 5 6 7 8 End
D) 1 2 3 4 5 6 7 8 End (infinite loop)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
76
Which of the following loop(s) should be used when you need to ask a user to enter one data item and repeat the prompt if the data is invalid?
I) for loop
II) while loop
III) do loop
A) I only
B) I and II
C) III only
D) II only
I) for loop
II) while loop
III) do loop
A) I only
B) I and II
C) III only
D) II only
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
77
Which of the loop(s) test the condition after the loop body executes?
I) for loop
II) while loop
III) do loop
A) I only
B) II only
C) III only
D) II and III
I) for loop
II) while loop
III) do loop
A) I only
B) II only
C) III only
D) II and III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
78
How do you fix this code snippet to make it print out the sum when the user enters Q?
System.out.print("Enter a value, Q to quit: ");
Double sum = 0;
Scanner in = new Scanner(System.in);
Boolean hasData = true;
Do
{
Double value = in.nextDouble();
Sum = sum + value;
System.out.print("Enter a value, Q to quit: ");
}
While (in.hasNext());
System.out.println("sum " + sum);
A) while (in.hasData());
B) while (!in.hasEnded());
C) while (in.hasNextDouble());
D) while (hasData);
System.out.print("Enter a value, Q to quit: ");
Double sum = 0;
Scanner in = new Scanner(System.in);
Boolean hasData = true;
Do
{
Double value = in.nextDouble();
Sum = sum + value;
System.out.print("Enter a value, Q to quit: ");
}
While (in.hasNext());
System.out.println("sum " + sum);
A) while (in.hasData());
B) while (!in.hasEnded());
C) while (in.hasNextDouble());
D) while (hasData);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
79
How many times does the following loop execute?
For (double d = 1; d != 10; d++)
{
D = d / 3;
System.out.print(d + " ");
}
A) 10
B) 9
C) 8
D) An infinite number of times
For (double d = 1; d != 10; d++)
{
D = d / 3;
System.out.print(d + " ");
}
A) 10
B) 9
C) 8
D) An infinite number of times
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
80
Suppose that the chance to hit the jackpot in a lottery is one in one million. Which of the following code snippets simulates that random number?
A) (long) (Math.random() * 1000000 + 1)
B) (long) (Math.random() * 100000 + 1)
C) (long) (Math.random() * 9999990 + 1)
D) (long) (Math.random() * 1000001 + 1)
A) (long) (Math.random() * 1000000 + 1)
B) (long) (Math.random() * 100000 + 1)
C) (long) (Math.random() * 9999990 + 1)
D) (long) (Math.random() * 1000001 + 1)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck