C++ Questions and Answers Part-23

1. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
main()
{
double a = 21.09399;
float b = 10.20;
int c ,d;
c = (int) a;
d = (int) b;
cout << c <<' '<< d;
return 0;
}
a) 20 10
b) 10 21
c) 21 10
d) 10 20

Answer: c
Explanation: In this program, we are casting the operator to integer, So it is printing as 21 and 10.
Output:
21 10

2. How are many sequences of statements present in c++?
a) 4
b) 3
c) 5
d) 6

Answer: c
Explanation: There are five sequences of statements. They are Preprocessor directives, Comments, Declarations, Function Declarations, Executable statements.

3. The if..else statement can be replaced by which operator?
a) Bitwise operator
b) Conditional operator
c) Multiplicative operator
d) Addition operator

Answer: b
Explanation: In the conditional operator, it will predicate the output using the given condition.

4. The switch statement is also called as?
a) choosing structure
b) selective structure
c) certain structure
d) bitwise structure

Answer: b
Explanation: The switch statement is used to choose the certain code to execute, So it is also called as selective structure.

5. 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.

6. 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.
Output:
543

7. 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;
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.

8. 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.

9. 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.
Output:
10

10. 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.