C++ interview questions and answers focuses 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 . 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.
C++ interview questions on “Comments and Indentation” along with answers, explanations and/or solutions:
Q 1. How many types of comments are there in c++?
A. 1
B. 2
C. 3
D. 4
Show Answer
Answer:-B. 2Explanation
There are two types of comments in C++. Single line comments uses double slash //. Multiple line comments uses /* comment inside */.Q 2. What is a comment in c++?
A. comments are executable
B. comments are executed by the compiler to find the meaning of the comment
C. comments are parts of the source code disregarded by the compiler
D. comments are executed by the compiler
Show Answer
Answer:-C. comments are parts of the source code disregarded by the compilerExplanation
Comments are used to add meaning to the program.Q 3. What type of comments does c++ support?
A. single line
B. reusable line
C. single line and multi-line
D. multiline
Show Answer
Answer:-B. reusable lineExplanation
C++ provides two types of comments in programs. They are single line(using //) or multiple line (using /*…… */) comments.Q 4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
/* this is comment*
cout << “hello world”;
return 0;
}
A. hello world
B. hello
C. compile time error
D. hellohello
Show Answer
Answer:-C. compile time errorExplanation
Because the slash should need to be forward not backward.Q 5. What is used to write multi line comment in c++?
A. /* …. */
B. /$ …. $/
C. //
D. /$ …. */
Show Answer
Answer:-A. /* …. */Explanation
The /* is used to write the multi line comment.Q 6. What is the use of the indentation in c++?
A. distinguishes between comments and outer data
B. r distinguishes between comments and outer data
C. distinguishes between comments and code
D. r distinguishes between comments and inner data
Show Answer
Answer:-C. distinguishes between comments and codeExplanation
To distinguish between different parts of the program like comments, codes, etc.Q 7. What will be the output of the following C++ code?
#include <iostream>
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
Show Answer
Answer:-C. segmentation faultExplanation
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 faultQ 8. What will be the output of the following C++ code?
#include <iostream>
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
Show Answer
Answer:-D. 110Explanation
We have increased the x value in operand as x + 1, so it will return as 110. Output: $ g++ arg2.cpp $ a.out 110Q 9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int add(int a, int b);
int main()
{
int i = 5, j = 6;
cout << add(i, j) << endl;
return 0;
}
int add(int a, int b )
{
int sum = a + b;
a = 7;
return a + b;
}
A. 11
B. 12
C. 13
D. compile time error
Show Answer
Answer:-C. 13Explanation
The value of a has been changed to 7, So it returns as 13. Output: $ g++ arg1.cpp $ a.out 13Q 10. What will happen when we use void in argument passing?
A. It will return value to its caller
B. It will not 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
Show Answer
Answer:-B. It will not return value to its callerExplanation
As void is not having any return value, it will not return the value to the caller.Q 11. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void Sum(int a, int b, int & c)
{
a = b + c;
b = a + c;
c = a + b;
}
int main()
{
int x = 2, y =3;
Sum(x, y, y);
cout << x << ” ” << y;
return 0;
}
A. 2 15
B. 6 9
C. 2 3 2 15
D. compile time error
Leave a Reply