250+ TOP MCQs on Comments and Indentation and Answers

This section on C++ interview questions and answers on “Comments and Indentation”. 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++ interview questions on “Comments and Indentation” along with answers, explanations and/or solutions:

1. How many types of comments are there in c++?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two types of comments in C++. Single line comments uses double slash //. Multiple line comments uses /* comment inside */.

2. What is a comment in c++?
a) comments are parts of the source code disregarded by the compiler
b) comments are executed by the compiler to find the meaning of the comment
c) comments are executable
d) comments are executed by the compiler
Answer: a
Clarification: Comments are used to add meaning to the program.

3. What type of comments does c++ support?
a) single line
b) multiline
c) single line and multi-line
d) reusable line
Answer: c
Clarification: C++ provides two types of comments in programs. They are single line(using //) or multiple line (using /*…… */) comments.

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

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         /* this is comment*
  6.         cout << "hello world";
  7.         return 0;
  8.     }

a) hello world
b) hello
c) compile time error
d) hellohello
Answer: c
Clarification: Because the slash should need to be forward not backward.

5. What is used to write multi line comment in c++?
a) /* …. */
b) /$ …. $/
c) //
d) /$ …. */
Answer: a
Clarification: The /* is used to write the multi line comment.

6. What is the use of the indentation in c++?
a) distinguishes between comments and code
b) r distinguishes between comments and outer data
c) distinguishes between comments and outer data
d) r distinguishes between comments and inner data
Answer: a
Clarification: To distinguish between different parts of the program like comments, codes, etc.

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

  1.     #include 
  2.     using namespace std;
  3.     long factorial (long a)
  4.     {
  5.         if (a > 1)
  6.             return (a * factorial (a + 1));
  7.         else
  8.             return (1);
  9.     }
  10.     int main ()
  11.     {
  12.         long num = 3;
  13.         cout << num << "! = " << factorial ( num );
  14.         return 0;
  15.     }

a) 6
b) 24
c) segmentation fault
d) compile time error
Answer: c
Clarification: As we have given in the function as a+1, it will exceed the size and so it arises the segmentation fault.
Output:

$ g++ arg3.cpp
$ a.out
segmentation fault

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

  1.     #include 
  2.     using namespace std;
  3.     void square (int *x)
  4.     {
  5. 	*x = (*x + 1) * (*x);
  6.     }
  7.     int main ( )
  8.     {
  9. 	int num = 10;
  10.         square(&num);
  11. 	cout << num;
  12. 	return 0;
  13.     }

a) 100
b) compile time error
c) 144
d) 110
Answer: d
Clarification: We have increased the x value in operand as x + 1, so it will return as 110.
Output:

$ g++ arg2.cpp
$ a.out
110

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

  1.     #include 
  2.     using namespace std;
  3.     int add(int a, int b);
  4.     int main()
  5.     {
  6.         int i = 5, j = 6;
  7.         cout << add(i, j) << endl;
  8.         return 0;
  9.     }
  10.     int add(int a, int b )
  11.     {
  12.         int sum = a + b;
  13.         a = 7;
  14.         return a + b;
  15.     }

a) 11
b) 12
c) 13
d) compile time error
Answer: c
Clarification: The value of a has been changed to 7, So it returns as 13.
Output:

$ g++ arg1.cpp
$ a.out
13

10. What will happen when we use void in argument passing?
a) It will not return value to its caller
b) It will return value to its caller
c) May or may not depend on the declared return type of the function, the passed arguments are different than the function return type
d) It will return value
Answer: a
Clarification: As void is not having any return value, it will not return the value to the caller.

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

  1.     #include 
  2.     using namespace std;
  3.     void Sum(int a, int b, int & c)
  4.     {
  5.         a = b + c;
  6.         b = a + c;
  7.         c = a + b;
  8.     }
  9.     int main()
  10.     {
  11.         int x = 2, y =3;
  12.         Sum(x, y, y);
  13.         cout << x << " " << y;
  14.         return 0;
  15.     }

a) 2 3
b) 6 9
c) 2 15
d) compile time error
Answer: c
Clarification: We have passed three values and it will manipulate according to the given condition and yield the result as 2 15.
Output:

$ g++ arg.cpp
$ a.out
2 15
  250+ TOP MCQs on Dereferencing and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top