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?
-
#include -
using namespace std;
-
int main()
-
{ -
/* this is comment* -
cout << "hello world"; -
return 0; -
}
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?
-
#include -
using namespace std;
-
long factorial (long a)
-
{ -
if (a > 1)
-
return (a * factorial (a + 1));
-
else -
return (1);
-
} -
int main ()
-
{ -
long num = 3;
-
cout << num << "! = " << factorial ( num );
-
return 0;
-
}
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?
-
#include -
using namespace std;
-
void square (int *x)
-
{ -
*x = (*x + 1) * (*x);
-
} -
int main ( )
-
{ -
int num = 10;
-
square(&num);
-
cout << num;
-
return 0;
-
}
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
