C++ Programming Questions and Answers – Statements

This section on C++ interview questions and answers focuses on “Statements”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

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

4. The destination statement for the goto label is identified by what label?
a) $
b) @
c) *
d) :
View Answer

Answer: d
Explanation: : colon is used at the end of labels of goto statements.

5. What will be the output of the following C++ code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main ()
  4.     {
  5.         int n;
  6.         for (n = 5; n > 0; n--)
  7.         {
  8.             cout << n;
  9.             if (n == 3)
  10.                 break;
  11.         }
  12.         return 0;
  13.     }

a) 543
b) 54
c) 5432
d) 53
View Answer

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:

advertisement
$ g++ stat.cpp
$ a.out
543

6. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 10;
  6.         if (a < 15)
  7.         {
  8.             time:
  9.             cout << a;
  10.             goto time;
  11.         }
  12.         break;
  13.         return 0;
  14.     }

a) 1010
b) 10
c) infinitely print 10
d) compile time error
View Answer

Answer: d
Explanation: 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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int n = 15;
  6.         for ( ; ;)
  7.         cout << n;
  8.         return 0;
  9.     }

a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
View Answer

Answer: c
Explanation: 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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int i;
  6.         for (i = 0; i < 10; i++);
  7.         {
  8.             cout << i;
  9.         }
  10.         return 0;
  11.     }

a) 0123456789
b) 10
c) 012345678910
d) compile time error
View Answer

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:

$ g++ stat2.cpp
$ a.out
10

9. How many types of loops are there in C++?
a) 4
b) 2
c) 3
d) 1
View Answer

Answer: a
Explanation: There are four types of loop. They are the while, do while, nested, for the loop.

10. Which looping process is best used when the number of iterations is known?
a) for
b) while
c) do-while
d) all looping processes require that the iterations be known
View Answer

Answer: a
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.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.