C++ Interview Questions and Answers for Freshers, Experienced

    174 Votes

7. In how many ways we can initialize an int variable in C++?

In c++, variables can be initialized in two ways, the traditional C++ initialization using "=" operator and second using the constructor notation.
  • Traditional C++ initilization
int i = 10;
variable i will get initialized to 10.
  • Using C++ constructor notation
int i(10);

8. What is implicit conversion/coercion in c++?

Implicit conversions are performed when a type (say T) is used in a context where a compatible type (Say F) is expected so that the type T will be promoted to type F.
short a = 2000 + 20;
In the above example, variable a will get automatically promoted from short to int. This is called implicit conversion/coercion in c++.

9. What are C++ inline functions?

C++ inline functions are special functions, for which the compiler replaces the function call with body/definition of function. Inline functions makes the program execute faster than the normal functions, since the overhead involved in saving current state to stack on the function call is avoided. By giving developer the control of making a function as inline, he can further optimize the code based on application logic. But actually, it's the compiler that decides whether to make a function inline or not regardless of it's declaration. Compiler may choose to make a non inline function inline and vice versa. Declaring a function as inline is in effect a request to the compiler to make it inline, which compiler may ignore. So, please note this point for the interview that, it is upto the compiler to make a function inline or not.
inline int min(int a, int b)
{
   return (a < b)? a : b;
}

int main( )
{
   cout << "min (20,10): " << min(20,10) << endl;
   cout << "min (0,200): " << min(0,200) << endl;
   cout << "min (100,1010): " << min(100,1010) << endl;
   return 0;
}
If the complier decides to make the function min as inline, then the above code will internally look as if it was written like
int main( )
{
   cout << "min (20,10): " << ((20 < 10)? 20 : 10) << endl;
   cout << "min (0,200): " << ((0 < 200)? 0 : 200) << endl;
   cout << "min (100,1010): " << ((100 < 1010)? 100 : 1010) << endl;
   return 0;
}

10. What do you mean by translation unit in c++?

We organize our C++ programs into different source files (.cpp, .cxx etc). When you consider a source file, at the preprocessing stage, some extra content may get added to the source code ( for example, the contents of header files included) and some content may get removed ( for example, the part of the code in the #ifdef of #ifndef block which resolve to false/0 based on the symbols defined). This effective content is called a translation unit. In other words, a translation unit consists of
  • Contents of source file
  • Plus contents of files included directly or indirectly
  • Minus source code lines ignored by any conditional pre processing directives ( the lines ignored by #ifdef,#ifndef etc)

11. What do you mean by internal linking and external linking in c++?

[This interview question is related to questions on "translation unit" and "storage classes"]
A symbol is said to be linked internally when it can be accessed only from with-in the scope of a single translation unit. By external linking a symbol can be accessed from other translation units as well. This linkage can be controlled by using static and extern keywords.

12. What do you mean by storage classes?

Storage class are used to specify the visibility/scope and life time of symbols(functions and variables). That means, storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of program.
Page 2 of 4

Related Articles

Advanced C Interview Questions and Answers for Experienced
Embedded C Interview Questions and Answers
C Interview Questions and Answers

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know