Decision and Loops & Control Statements
QUE.1 What is the output of this program ?
CPP
OPTION
a) 543
b) 54
c) 5432
d) 53
CPP
OPTION
a) 1010
b) 10
c) infinitely print 10
d) compile time error
CPP
OPTION
a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
CPP
OPTION
a) 0123456789
b) 10
c) 012345678910
d) compile time error
CPP
#include <iostream>
using namespace std;
int main ()
{
int n;
for (n = 5; n > 0; n--)
{
cout << n;
if (n == 3)
break;
}
return 0;
}
Answer: aExplanation : In this program, we are printing the numbers in reverse order and by using break statement we stopped printing on 3. QUE.2 What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
int a = 10;
if (a < 15)
{
time:
cout << a;
goto time;
}
break;
return 0;
}
Answer: dExplanation: Because the break statement needs to be presented inside a loop or a switch statement. QUE. 3 What is the output of this program ?
#include <iostream>
using namespace std;
int main()
{
int n = 15;
for ( ; ; )
cout << n;
return 0;
}
Answer: cExplanation: There is no condition in the for loop, So it will loop continuously. QUE.4 What is the output?
#include <iostream>
using namespace std;
int main()
{
int i;
for (i = 0; i < 10; i++);
{
cout << i;
}
return 0;
}
Answer: bExplanation: for loop with a semicolon is called as body less for loop. It is used only for incrementing the variable values. So, in this program the value is incremented and printed as 10. QUE.5 what is output of this program?
#include <iostream>
int i = 30;
using namespace std;
int main()
{
int i = 10;
for (i = 0; i < 5; i++)
{
int i = 20;
cout << i <<" ";
}
return 0;
}
OPTION
a) 0 1 2 3 4
b) 10 10 10 10 10
c) 20 20 20 20 20
d) Compile Errors
Answer: cExplanation: int i = 20 is initialized in the body of loop so it considers the value i = 20 inside loop only. For loop control, the i declared outside is used and loop runs 5 times and print 20 five times.