C++ interview question

1) What does static variable mean ?
There are 3 main uses for the static.
1. If you declare within a function:
It retains the value between function calls.

#include 

int t = 10;  

main(){

    int x = 0; 
    void funct1();
    funct1();            
    printf("After first call \n");
    funct1();            
    printf("After second call \n");
    funct1();            
    printf("After third call \n");

}
void funct1()
{
    static int y = 0;  
    int z = 10;             
    printf("value of y %d z %d",y,z);
    y=y+10;
}
OUTPUT
value of y 0 z 10 After first call
      value of y 10 z 10 After second call
      value of y 20 z 10 After third call

2.If it is declared for a function name:
By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files.
 Eg:
file abc.cpp

- - - -  - - - 
class A {

static foo () {}
}

Now this can be accessible to other files with out creating the object of this which means
file def.cpp

- - - - - - -
include abc.h

Now i can access foo in this file by A::foo() (can
be invocked by classname::functionname)

3. Static for global variables:
By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file.
Eg:
File abc.cpp


- - - - -  -
static foo() {}  //The scope of this function is in file level means it can be used in the same file

2) What are the differences between structures and arrays ?
Array structure
same data typediff datatype
Static memory allocationDynamic memory allocation
3) What are the differences between malloc() and calloc()?
malloc calloc
takes one argumenttakes two arguments
default value of malloc is garvagedefault value is "0"
char *malloc(sizeof(datatype) )char *calloc(sizeof(datatype), num of elements)
4) what is the difference between arrays and linked list ?
differences between the two are:
1) arrays are RANDOM ACCESS structures, where you can access
elements in random/indexed manner, whereas list is a
sequential access structure. This makes such algorithms like
heap sort or binary search to work much faster on arrays
2) arrays are static/fixed size whereas lists are dynamic
size structures.
5)Constructor initialize list
The Constructor initialize list need to initialize always for following

1) Refrence Variables,

2) Const Variables 
3) base class constructor(having variable number of arguments). 
 
6) Difference between class template and function template?
 
C++ Function templates are those functions which can handle different data 
types without separate code for each of them. For a similar operation on several 
kinds of data types, a programmer need not write different versions by overloading 
a function. It is enough if we writes a C++ template based function
 
template 
T Add(T a, T b) //C++ function template sample
{
return a+b;
}
now t can be int, foat or any other data type.
so using functionn template we can work with many datatypes in one function.
we can call the function by using int x=Add(2,4)
 
Class Template
a class template provides a specification for generating classes based on 
parameters.For example, the C++ standard library has a list container called list, 
which is a template. The statement list designates or instantiates a 
linked-list of type int. The statement list designates or instantiates a 
linked-list of type string.



Reversing a String
Logic: The approach here is to reverse the string using the pointers. Reversing the string includes the reversing the each and every words in it. After accepting a string from user, it calls a function “strev” with two string pointer arguments, the source and destination. It has an iterative loop, which traces from the EOL through the beginning. Each time it copies the current letter to the destination. Finally it displays the resultant string.

The earlier program implements the same by direct method, i.e. without pointers.
#include
#include
void strev(char *str1, char *str2);
void main()
{
        char *str1, *str2;
        clrscr();
        printf("\n\n\t ENTER A STRING...: ");
        gets(str1);
        strev(str1,str2);
        printf("\n\t THE REVERSED STRING IS...: ");
        puts(str2);
        getch();
}

void strev(char *str1, char *str2)
{
        int i = 0, len = 0, r = 0;
        while(*(str1+len)!='\0')
                len++;
        for(i=len-1; i>=0; i--)
        {
                *(str2+r) = *(str1+i);
                r++;
        }
        *(str2+r) = '\0';
} 
 

Why virtual template functions possible ?

The reason is because as the compiler scans class declarations, it builds a list of
virtual function declarations that have been encountered. The first one is numbered
0 and is located at the 0th entry in the vtable. The next one is the 1st entry in
the table and so forth. When the compiler wants to call a virtual function, the code
it generates is to read the Nth index of the vtable which contains a pointer to the
function to actually call, where N is computed simply by looking at compile time at
the virtual function being called and find its index in the symbol table.

template functions can have many instantiations, and those aren't known at the
time of parsing the class declaration, therefore the compiler cannot possibly build
the vtable since it does not know how many different ways the template function
will be instantiated.

The differences between a static member function and non-static member functions are as follows.

  • A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.
     
  • A static member function can be called, even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.
     
  • A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual
     
  • A static member function cannot have access to the 'this' pointer of the class.