1) for (int i = 0; i < 5; i++) Console.Write(i + “\\t”); Given the code snippet
ID: 3812022 • Letter: 1
Question
1) for (int i = 0; i < 5; i++)
Console.Write(i + “ ”);
Given the code snippet above, how many times will the loop body be executed?
2) for (int outer = 0; outer < 2; outer++)
{
for (int inner = 0; inner < 3; inner++)
{
Console.WriteLine("Outer: {0} Inner: {1}", outer, inner);
}
}
How many lines will be printed for the above nested loop?
3)
for (int outer = 0; outer < 2; outer++)
{
for (int inner = 0; inner < 3; inner++)
{
Console.WriteLine("Outer: {0} Inner: {1}", outer, inner);
}
}
During the last iteration through the nested loop, what numbers are printed?
Outer: 2 Inner 3
Outer: 1 Inner: 2
Outer: 1 Inner 0
Outer: 3 Inner 0
4)
int counter = 0;
while (counter < 100)
{
Console.WriteLine(counter);
counter++;
}
Looking above, if the loop body was changed from counter++; to counter += 5;, how many times would the loop body be executed?
19
20
99
100
5)
int counter = 0;
while (counter < 100)
{
Console.WriteLine(counter);
counter++;
}
The first value printed with the program segment above is ____.
0
1
99
100
a.Outer: 2 Inner 3
b.Outer: 1 Inner: 2
c.Outer: 1 Inner 0
d.Outer: 3 Inner 0
Explanation / Answer
1) for (int i = 0; i < 5; i++)
Console.Write(i + “ ”);
Given the code snippet above, how many times will the loop body be executed?
Answer: 5 times. When i reaches 5 value loop will terminate.
for (int outer = 0; outer < 2; outer++)
{
for (int inner = 0; inner < 3; inner++)
{
Console.WriteLine("Outer: {0} Inner: {1}", outer, inner);
}
}
How many lines will be printed for the above nested loop?
Answer: 6 lines. Becuase outside loop is running 2 times ans inside loop is running 3 times. So 2 * 3 = 6 times.
for (int outer = 0; outer < 2; outer++)
{
for (int inner = 0; inner < 3; inner++)
{
Console.WriteLine("Outer: {0} Inner: {1}", outer, inner);
}
}
During the last iteration through the nested loop, what numbers are printed?
Answer: b. Outer: 1 Inner: 2
int counter = 0;
while (counter < 100)
{
Console.WriteLine(counter);
counter++;
}
Looking above, if the loop body was changed from counter++; to counter += 5;, how many times would the loop body be executed?
Answer: b. 20
5)
int counter = 0;
while (counter < 100)
{
Console.WriteLine(counter);
counter++;
}
The first value printed with the program segment above is
Answer: d. 100
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.