c++ interview Q&A a4academics part 3

​13. How many storage classes are available in C++?

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.

Following storage classes are available in C++


It’s the default storage class for local variables. They can be accessed only from with in the declaration scope. auto variables are allocated at the beginning of enclosing block and deallocated at the end of enclosing block.

1. void changeValue( void )

2. {

3. auto int i = 1 ;

4. i++;

5. printf ( “%d ” , i ) ;


7. }

8. int main()

9. {

10. changeValue();

11. changeValue();

12. changeValue();

13. changeValue();

14. return 0;

15. }


17. Output:-

18. 2 2 2 2

In the above example, every time the method changeValue is invoked, memory is allocated for i and de allocated at the end of the method. So it’s output will be same.


It’s similar to auto variables. Difference is that register variables might be stored on the processor register instead of RAM, that means the maximum size of register variable should be the size of CPU register ( like 16bit, 32bit or 64bit). This is normally used for frequently accessed variables like counters, to improve performance. But note that, declaring a variable as register does not mean that they will be stored in the register. It depends on the hardware and implementation.

1. int main()

2. {

3. register int i;

4. int array[ 10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};


6. for (i= 0;i< 10;i++)

7. {

8. printf( “%d ” , array [i]);

9. }

10. return 0;

11. }


13. Output:-

14. 0 1 2 3 4 5 6 7 8 9

The variable i might be stored on the CPU register and due to which the access of i in the loop will be faster.


A static variable will be kept in existence till the end of the program unlike creating and destroying each time they move into and out of the scope. This helps to maintain their value even if control goes out of the scope. When static is used with global variables, they will have internal linkage, that means it cannot be accessed by other source files. When static is used in case of a class member, it will be shared by all the objects of a class instead of creating separate copies for each object.

1. void changeValue( void )

2. {

3. static int i = 1 ;

4. i++;

5. printf ( “%d ” , i ) ;


7. }


9. int main()

10. {

11. changeValue();

12. changeValue();

13. changeValue();

14. changeValue();

15. return 0;

16. }


18. Output:-

19. 2 3 4 5

Since static variable will be kept in existence till the end of program, variable i will retain it’s value across the method invocations.


extern is used to tell compiler that the symbol is defined in another translation unit (or in a way, source files) and not in the current one. Which means the symbol is linked externally. extern symbols have static storage duration, that is accessible through out the life of program. Since no storage is allocated for extern variable as part of declaration, they cannot be initialized while declaring.

1. int x = 10 ;

2. int main( )

3. {

4. extern int y ;

5. printf( “x: %d ” , x );

6. printf( “y: %d” , y);

7. return 0;

8. }

9. int y = 70 ;


11. Output:-

12. x: 10 y: 70

extern variable is like global variable, it’s scope is through out the program. It can be defined anywhere in the c++ program.


mutable storage class can be used only on non static non const data a member of a class. Mutable data member of a class can be modified even is it’s part of an object which is declared as const.

1. class Test

2. {

3. public:

4. Test (): x( 1), y( 1) {};

5. mutable int x;

6. int y;

7. };


9. int main()

10. {

11. const Test object;

12. object.x = 123;

13. //object.y = 123;

14. /*

15. * The above line if uncommented, will create compilation error.

16. */

17. cout<< “X:” << object.x << “, Y:” << object.y;

18. return 0;

19. }


21. Output:-

22. X: 123 , Y: 1

In the above example, we are able to change the value of member variable x though it’s part of an object which is declared as const. This is because the variable x is declared as mutable. But if you try to modify the value of member variable y, compiler will throw error.

You can find the summary of c++ storage class specifiers below

C++ Storage Specifier

Storage Location

Scope Of Variable

Life Time

auto Memory (RAM) Local With in function

static Memory (RAM) Local

Life time is from when the flow reaches the first declaration to the termination of program.

register CPU register Local With in function

extern Memory (RAM) Global Till the end of main program

14. What is ‘Copy Constructor’ and when it is called?

This is a frequent c++ interview question. Copy constructor is a special constructor of a class which is used to create copy of an object. Compiler will give a default copy constructor if you don’t define one. This implicit constructor will copy all the members of source object to target object.

Implicit copy constructors are not recommended, because if the source object contains pointers they will be copied to target object, and it may cause heap corruption when both the objects with pointers referring to the same location does an update to the memory location. In this case its better to define a custom copy constructor and do a deep copy of the object.

1. class SampleClass {

2. public:

3. int * ptr;

4. SampleClass ();

5. // Copy constructor declaration

6. SampleClass ( SampleClass &obj);

7. };


9. SampleClass :: SampleClass (){

10. ptr = new int();

11. *ptr = 5;

12. }


14. // Copy constructor definition

15. SampleClass :: SampleClass ( SampleClass &obj){

16. //create a new object for the pointer

17. ptr = new int();

18. // Now manually assign the value

19. *ptr = *(obj.ptr);

20. cout<< “Copy constructor…\n” ;

21. }

15. What is realloc() and free()? What is difference between them?

void* realloc (void* ptr, size_t size)

This function is used to change the size of memory object pointed by address ptr to the size given by size . If ptr is a null pointer, then realloc will behave like malloc(). If the ptr is an invalid pointer, then defined behaviour may occur depending the implementation. Undefined behaviour may occur if the ptr has previously been deallocated by free(), or dealloc() or ptr do not match a pointer returned by an malloc(), calloc() or realloc().

void free (void* ptr)

This function is used to deallocate a block of memory that was allocated using malloc(), calloc() or realloc(). If ptr is null, this function does not doe anything.

16. What is difference between shallow copy and deep copy? Which is default?

[This question can be expected in any interviews, not just c++ interviews. This is a usual question in most of the java interviews.]

When you do a shallow copy, all the fields of the source object is copied to target object as it is. That means, if there is a dynamically created field in the source object, shallow copy will copy the same pointer to target object. So you will have two objects with fields that are pointing to same memory location which is not what you usually want.

In case of deep copy, instead of copying the pointer, the object itself is copied to target. In this case if you modify the target object, it will not affect the source. By default copy constructors and assignment operators do shallow copy. To make it as deep copy, you need to create a custom copy constructor and override assignment operator.

17. What do you mean by persistent and non persistent objects?

[This question may be asked in many ways during c++ interviews, like how to send an object to a remote computer or how to save the your program state across application restarts. All these are related to serialization.]

Persistent objects are the ones which we can be serialized and written to disk, or any other stream. So before stopping your application, you can serialize the object and on restart you can deserialize it. [ Drawing applications usually use serializations.]

Objects that can not be serialized are called non persistent objects. [ Usually database objects are not serialized because connection and session will not be existing when you restart the application. ]

18. Is it possible to get the source code back from binary file?

Technically it is possible to generate the source code from binary. It is called reverse engineering. There are lot of reverse engineering tools available. But, in actual case most of them will not re generate the exact source code back because many information will be lost due to compiler optimization and other interpretations.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s