Question
Consider the following code segment. int k = 35; while (k >= 0) { System.out.println(X); k -= 5; } How many times will the string
Consider the following code segment.
int k = 35;
while (k >= 0)
{
System.out.println("X");
k -= 5;
}
How many times will the string "X" be printed as a result of executing the code segment?
-
1
A
-
7
B
-
8
C
-
35
D
-
More than 35 times, because the code segment will cause an infinite loop.
E
-
______________________________________________________________________________________
-
Consider the following code segment, which is intended to store the sum of all multiples of 10 between 10 and 100, inclusive (10 + 20 + ... + 100), in the variable total.
int x = 100;
int total = 0;
while( /* missing code */ )
{
total = total + x;
x = x - 10;
}
Which of the following can be used as a replacement for /* missing code */ so that the code segment works as intended?
-
x < 100
A
-
x <= 100
B
-
x > 10
C
-
x >= 10
D
-
x != 10
E
-
____________________________________________________________________________________________________________________________
-
Consider the following code segment.
for (int i = 0; i < 5; i++) // Line 1
{
for (int j = 0; j < 5; j++)
{
int k = i + j;
System.out.print(k + " ");
}
}
Which of the following best describes the result of changing i < 5 to i > 5 in line 1?
-
The numbers will be printed in the reverse order as they were in the original code segment because the outer loop will occur in reverse order.
A
-
Five additional values will be printed because the outer for loop will iterate one additional time.
B
-
An infinite loop will occur because the termination condition of the loop will never be reached.
C
-
There will be no change to the program output.
D
-
Nothing will be printed because the body of the outer for loop will not execute at all.
E
-
________________________________________________________________________________________________
-
Consider the following code segment.
int a = 1;
while (a <= 2)
{
int c = 1;
while (/* missing condition */)
{
System.out.print("*");
c++;
}
a++;
}
The code segment is intended to print "******". Which of the following can be used to replace /* missing condition */ so that the code segment works as intended?
-
c <= 2
A
-
c < 3
B
-
c <= 3
C
-
c > 2
D
-
c >= 3
E
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started