Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • C++ Interview Questions and Answers Part 2

    • 0
    • 4
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 536
    Comment on it

    In the previous part C++ Interview Questions and Answers Part 1 I have explained some basic questions of C++.This part is in continuation with the previous C++ Interview Questions and Answers tutorial. In this part of the tutorial we are proceeding with more questions based on further concepts of C++ and trying to guide you with the best possible explanations.

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

    We can initialize variables in c++ in two ways:

    1. The traditional C++ initialization using "=" operator and
    2. Using the constructor notation.
    3. Traditional C++ initilization:
    4.  int i = 10; 

      variable i will get initialized to 10.

    5. Using C++ constructor notation:
    6. int i(10);
    1. What is implicit conversion/coercion in c++?

    Implicit conversions are performed whenever an expression of some type T1 is used in context that does not accept that type, but accepts some other type T2

    There are two basic types of implicit type conversion: promotions and conversions.

    • Numeric promotion:
    • Whenever a value from one type is converted into a value of a larger similar data type, this is called a numeric promotion.For example, an int can be widened into a long, or a float promoted into a double:

      1. long l(64); // widen the integer 64 into a long
      1. double d(0.12f); // promote the float 0.12 into a double
    • Numeric conversions:
    • When we convert a value from a larger type to a similar smaller type, or between different types, this is called a numeric conversion. For example:

      1. double d = 3; // convert integer 3 to a double
    1. What are C++ inline functions?

    The inline functions are a C++ enhancement feature to increase the execution time of a program.At the time of declaration or definition, function name is preceded by word inline.

    When inline functions are used, the overhead of function call is eliminated. Instead, the executable statements of the function are copied at the place of each function call. This is done by the compiler.

     Inline int sqr(int x)
    { int y;
    y=x*x;
    return y;
    }
    int main()
    {int a=3,b;
    b=sqr(a);
    cout<< b;
    return 0;
    } 
    

    Here, the statement b = sqr(a) is a function call to sqr(). But since we have declared it as inline, the compiler replaces the statement with the executable statement of the function (b = a *a).

    Also, small functions which are defined inside a class (ie their code is written inside the class) are taken as inline by the compiler even if we dont explicitly declare them so. They are called auto inline functions.

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

    A translation unit is the basic unit of compilation in C++. We organize our C++ programs into different source files (.cpp, .cxx etc).It consist of these files along with the contents of any header files directly or indirectly included by it,but excluding those lines that were ignored using conditional preprocessing statements.

    A single translation unit can be compiled into an object file, library, or executable program. The notion of a translation unit is most often mentioned in the contexts of the One Definition Rule, and templates.

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

    Internal linkage refers to everything only in scope of a translation unit. External linkage refers to things that exist beyond a particular translation unit. In other words, accessable through the whole program, which is the combination of all translation units (or object files).

    1. What do you mean by storage classes?

    Storage classes are used to specify the lifetime and scope of variables. How storage is allocated for variables and how variable is treated by complier depends on these storage classes.

    These are basically divided into 5 different types :

    1. Global variables
    2. Local variables
    3. Register variables
    4. Static variables
    5. Extern variables

    Global Variables:

    These are defined at the starting , before all function bodies and are available throughout the program.

    int var1;  // Global variable
    void func();
    int main()
    { .............}

    Local variables:

    It has scope only within the function block where it is defined.They are also called Automatic variables.

    The keyword auto is used, but by default all local variables are auto, so we don't have to explicitly add keyword auto before variable dedaration. Default value of such variable is garbage.

    Register variables:

    This is also a type of local variable. This keyword is used to tell the compiler to make access to this variable as fast as possible. Variables are stored in registers to increase the access speed.

    But you can never use or compute address of register variable and also , a register variable can be declared only within a block, that means, you cannot have global or static register variables.

    Static Variables:

    Static variables are the variables which are initialized & allocated storage only once at the beginning of program execution, no matter how many times they are used and called in the program. A static variable retains its value until the end of program.

    void func()
    { static int i = 10;
    i++;
    cout<< i;
    }
    int main()
    { fun();      // Output = 11
    func();     // Output = 12
    func();     // Output = 13
    }

    As i is static,hence it will retain its value through function calls, and is initialized only once at the beginning.

    Extern Variables:

    This keyword is used to access variable in a file which is declared & defined in some other file, that is the existence of a global variable in one file is declared using extern keyword in another file.

    In the next session , we will continue with other important C++ questions asked by Interviewer.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: