1) Which of the loops below produces the same number of loop iterations as the f
ID: 3555423 • Letter: 1
Question
1) Which of the loops below produces the same number of loop iterations as the following loop?
D) a and b above
E) a, b, and c above
2) What is the value of i after this loop finishes executing?
3) What is the value of n after the following statements have executed?
4) What is the value of y after the following statements have executed?
A) for (count = 10; count >= 1; count--) DoSomething();
B) for (count = 0; count < 10; count++) DoSomething();
C) for (count = 10; count >= 0; count--) DoSomething();
D) a and b above
E) a, b, and c above
2) What is the value of i after this loop finishes executing?
int i; for (i = 1; i <= 15; i++){ cout << "iteration number: " + i; } 3) What is the value of n after the following statements have executed?
int n = 0; int a = 4; int b = 5; for (int i = 1; i <= a; i++) { for (int j = 1; j <= b; j++) { n--; } } 4) What is the value of y after the following statements have executed?
int y = 5; for (int i = 0; i < 10; i++) { y += 2; }
Explanation / Answer
Dear user,
1) D) a and b above
2) 16
Reason:Inititally taken i=1, next for loop checked upto i <=15, then increased i value, that is i=16.
3)n=-20
Complete code with explanation:
//Headers file section
#include <iostream>
using namespace std;
//main class
int main()
{
//Initially n value is 0, a=4 and b=5
int n = 0;
int a = 4;
int b = 5;
//This loops repeats 20 times then
//n value decrease 20 times, the result is -20
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= b; j++)
{
n--;
}
}
cout<<"n="<<n<<endl;
//Pause the system for a while
system("PAUSE");
return 0;
}
4) y=25
Complete code with explanation:
//Headers file section
#include <iostream>
using namespace std;
//main class
int main()
{
//Initialized y value is 5
int y = 5;
//This loop repeats 10 times
for (int i = 0; i < 10; i++)
{
//Add y value to 2 and store in y
y += 2;
}
//Display output
cout<<"y="<<y<<endl;
//Pause the system for a while
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.