Here is a listing of online C++ test questions on “Statements” along with answers, explanations and/or solutions:
1. How are many sequences of statements present in c++?
a) 4
b) 3
c) 5
d) 6
Answer: c
Clarification: There are five sequences of statements. They are Preprocessor directives, Comments, Declarations, Function Declarations, Executable statements.
2. The if..else statement can be replaced by which operator?
a) Bitwise operator
b) Conditional operator
c) Multiplicative operator
d) Addition operator
Answer: b
Clarification: In the conditional operator, it will predicate the output using the given condition.
3. The switch statement is also called as?
a) choosing structure
b) selective structure
c) certain structure
d) bitwise structure
Answer: b
Clarification: The switch statement is used to choose the certain code to execute, So it is also called as selective structure.
4. The destination statement for the goto label is identified by what label?
a) $
b) @
c) *
d) :
Answer: d
Clarification: : colon is used at the end of labels of goto statements.
5. What will be the output of the following C++ code?
-
#include -
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
Clarification: In this program, We are printing the numbers in reverse order but by using break statement we stopped printing on 3.
Output:
$ g++ stat.cpp $ a.out 543
6. What will be the output of the following C++ code?
-
#include -
using namespace std;
-
int main()
-
{ -
int a = 10;
-
if (a < 15)
-
{ -
time:
-
cout << a;
-
goto time;
-
} -
break;
-
return 0;
-
}
a) 1010
b) 10
c) infinitely print 10
d) compile time error
Answer: d
Clarification: Because the break statement need to be presented inside a loop or a switch statement.
7. What will be the output of the following C++ code?
-
#include -
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
Clarification: There is not a condition in the for loop, So it will loop continuously.
8. What will be the output of the following C++ code?
-
#include -
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
Clarification: 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.
Output:
$ g++ stat2.cpp $ a.out 10
