C++ Decision making & Loops | Set 3
21. The destination statement for the goto label is identified by what label?
a) $
b) @
c) *
d) :
Answer: D
Explanation: : colon is used at the end of labels of goto statements.
22. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int n ;
for (n = 5; n < 0; n--)
{
cout << n;
if (n == 3)
break;
}
return 0;
}
a) 543
b) 54
c) 5432
d) 53
Answer: A
Explanation: In this program, We are printing the numbers in reverse order but by using break statement we stopped printing on 3.
23. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int a = 10;
if (a = 15)
{
time;
cout << a;
if (n == 3)
goto time;
}
break;
return 0;
}
a) 1010
b) 10
c) infinitely print 10
d) compile time error
Answer: D
Explanation: Because the break statement need to be presented inside a loop or a switch statement.
24. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int n = 15;
for (; ;)
cout << n;
return 0;
}
a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
Answer: C
Explanation: There is not a condition in the for loop, So it will loop continuously.
25. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int i ;
for (i = 0; i < 10; i++);
{
cout << i;
}
return 0;
}
a) 0123456789
b) 10
c) 012345678910
d) compile time error
Answer: B
Explanation: 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.
26. How many types of loops are there in C++?
a) 4
b) 2
c) 3
d) 1
Answer: A
Explanation: There are four types of loop. They are the while, do while, nested, for the loop.
27. Which looping process is best used when the number of iterations is known?
a) While loop
b) For loop
c) Do while loop
d) all looping processes require that the iterations be known
Answer: B
Explanation: Because in for loop we are allowed to provide starting and ending conditions of loops, hence fixing the number of iterations of loops, whereas no such things are provided by other loops.
28. Which of the following must be present in switch construct?
a) Expression in ( ) after switch
b) default
c) case followed by value
d) All of the above
Answer: A
Explanation: No explanation is given for this question.
29. What is the effect of writing a break statement inside a loop?
a) It cancels remaining iterations.
b) It skips a particular iteration.
c) The program terminates immediately.
d) Loop counter is reset.
Answer: A
Explanation: No explanation is given for this question.
30. The break statement causes an exit
a) fromthe innermost loop only.
b) only from the innermost switch.
c) from all loops & switches.
d) from the innermost loop or switch.
Answer: D
Explanation: No explanation is given for this question.