Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How Do You Use A Pointer To A Function?

Answer»

The hardest part about using a pointer-to-function is declaring it. Consider an example. You want to CREATE a pointer, pf, that points to the strcmp() function. The strcmp() function is declared as SHOWN below
int strcmp( const char *, const char * )
To set up “pf” to point to the strcmp() function, you want a DECLARATION that LOOKS just like the strcmp() function's declaration, but that has *pf rather than strcmp:
int (*pf)( const char *, const char * );
Notice that you need to PUT parentheses around *pf.

The hardest part about using a pointer-to-function is declaring it. Consider an example. You want to create a pointer, pf, that points to the strcmp() function. The strcmp() function is declared as shown below
int strcmp( const char *, const char * )
To set up “pf” to point to the strcmp() function, you want a declaration that looks just like the strcmp() function's declaration, but that has *pf rather than strcmp:
int (*pf)( const char *, const char * );
Notice that you need to put parentheses around *pf.

2.

What Is Size Of Void Pointer?

Answer»

Size of all pointers are same in C, REGARDLESS of their type because pointers variable holds a memory LOCATION. And for a given system, this size is constant. The type of pointer is used to know the size of the DATA that the pointer is pointer is POINTING to.

Size of all pointers are same in C, regardless of their type because pointers variable holds a memory location. And for a given system, this size is constant. The type of pointer is used to know the size of the data that the pointer is pointer is pointing to.

3.

Write A C Program To Find Size Of Structure Without Using Sizeof Operator?

Answer»

STRUCT XXX
{
int x;
float y;
CHAR z;
};
int main()
{
struct XXX *PTR=(struct XXX *)0;
ptr++;
printf("SIZE of STRUCTURE is: %d",*ptr);
return 0;
}

struct XXX
{
int x;
float y;
char z;
};
int main()
{
struct XXX *ptr=(struct XXX *)0;
ptr++;
printf("Size of structure is: %d",*ptr);
return 0;
}

4.

What Is The Meaning Of Prototype Of A Function?

Answer»

Declaration of function is known as prototype of a function. Prototype SAYS the NAME, parameter list and return TYPE of a function but not the definition, this is same as declaring some VARIABLE but not defining it. For example,
int PRINTF(const char *, int/);

Declaration of function is known as prototype of a function. Prototype says the name, parameter list and return type of a function but not the definition, this is same as declaring some variable but not defining it. For example,
int printf(const char *, int/);

5.

What Is Wild Pointer In C?

Answer»

A pointer is known as wild pointer c, if it has not been initialized. For Example:
int main()
{
int *PTR;
printf("%un",ptr);
printf("%d",*ptr);
return 0;
}
Output: Any ADDRESS, GARBAGE value.
Here ptr is wild pointer, because it has not been initialized. Wild pointer is not the same as NULL pointer. Because, NULL pointer doesn't point to any location, but a wild pointer points to a specific memory location but the memory MAY not be available for CURRENT application, which is very fatal.

A pointer is known as wild pointer c, if it has not been initialized. For Example:
int main()
{
int *ptr;
printf("%un",ptr);
printf("%d",*ptr);
return 0;
}
Output: Any address, Garbage value.
Here ptr is wild pointer, because it has not been initialized. Wild pointer is not the same as NULL pointer. Because, NULL pointer doesn't point to any location, but a wild pointer points to a specific memory location but the memory may not be available for current application, which is very fatal.

6.

What Is Dangling Pointer In C?

Answer»

If any POINTER is pointing at the memory location/address of any variable, but if the variable is deleted or does not EXIST in the current scope of code, while pointer is STILL pointing to that memory location, then the pointer is called dangling pointer. For example,
#include<stdio.h>
INT *func();
int MAIN()
{
int *ptr;
ptr=func();
printf("%d",*ptr);
return 0;
}
int * func()
{
int x=18;
return &x;
}
Output is Garbage value, since the variable x has been freed as soon as the function func() returned

If any pointer is pointing at the memory location/address of any variable, but if the variable is deleted or does not exist in the current scope of code, while pointer is still pointing to that memory location, then the pointer is called dangling pointer. For example,
#include<stdio.h>
int *func();
int main()
{
int *ptr;
ptr=func();
printf("%d",*ptr);
return 0;
}
int * func()
{
int x=18;
return &x;
}
Output is Garbage value, since the variable x has been freed as soon as the function func() returned

7.

Write A C Program To Swap Two Variables Without Using Third Variable ?

Answer»

#include&LT;stdio.h>
INT MAIN()
{
int a=5,b=10;
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
}

#include<stdio.h>
int main()
{
int a=5,b=10;
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
}

8.

In C, What Is The Difference Between A Static Variable And Global Variable?

Answer»

A static variable ia DECLARED outside of any function and it is accessible only to all the FUNCTIONS defined in the same FILE (as the static variable). In case of global variable, it can be accessed by any function (including the ones from different FILES).

A static variable ia declared outside of any function and it is accessible only to all the functions defined in the same file (as the static variable). In case of global variable, it can be accessed by any function (including the ones from different files).

9.

Is Using Exit() The Same As Using Return?

Answer»

No. They are not the same. Return statement returns control to the CALLER function, that is, it EXITS from the lowest level of the call stack. Where as, EXIT statement make the program returns to the system from where the application was STARTED. So, exit always exits from the highest level of call stack. Eventually, if there is only one level of function call then they both do the same.

No. They are not the same. Return statement returns control to the caller function, that is, it exits from the lowest level of the call stack. Where as, exit statement make the program returns to the system from where the application was started. So, exit always exits from the highest level of call stack. Eventually, if there is only one level of function call then they both do the same.

10.

Why Does Pascal Matter? Is There Any Benefit To Using Pascal Functions?

Answer»

The main REASON behind using PASCAL is that, in the left-to-right parsing the efficiency of switching INCREASES in C.

The main reason behind using PASCAL is that, in the left-to-right parsing the efficiency of switching increases in C.

11.

What Does A Function Declared As Pascal Do Differently?

Answer»

In C, when some function is called, the parameters are put at the top of the stack. Now the order in which they are put is the order in which the parameters are parsed. Normally, the order is right to left. That is, the right most is parsed FIRST and the left most parameter is parsed at last.
If you want to alter this PARADIGM, then you have to define the function with PASCAL as FOLLOWING:
int PASCAL pascal_func(int, CHAR*, long);
Here, the left most parameter(int) will be parsed first, then char* and then long.

In C, when some function is called, the parameters are put at the top of the stack. Now the order in which they are put is the order in which the parameters are parsed. Normally, the order is right to left. That is, the right most is parsed first and the left most parameter is parsed at last.
If you want to alter this paradigm, then you have to define the function with PASCAL as following:
int PASCAL pascal_func(int, char*, long);
Here, the left most parameter(int) will be parsed first, then char* and then long.

12.

Is It Possible To Execute Code Even After The Program Exits The Main() Function?

Answer»

There is a standard C function NAMED atexit() for this purpose that can be used to perform some operations when your program exiting. You can register some functions with atexit() to be EXECUTED at the time of termination. Here's an example:
#include <stdio.h>
#include <stdlib.h>
VOID _some_FUNC_(void);
INT main(int argc, char** argv)
{
...
atexit(_some_FUNC_);
….
}

There is a standard C function named atexit() for this purpose that can be used to perform some operations when your program exiting. You can register some functions with atexit() to be executed at the time of termination. Here's an example:
#include <stdio.h>
#include <stdlib.h>
void _some_FUNC_(void);
int main(int argc, char** argv)
{
...
atexit(_some_FUNC_);
….
}

13.

How Can You Pass An Array To A Function By Value?

Answer»

An ARRAY can be passed to a function by value, by keeping a PARAMETER with an array TAG with empty square brackets(like []). From the caller function, just pass the array tag. For instance,
VOID func(int i[]) {..} /* parameter */
...
int k[10];
func(k); /* caller function */

An array can be passed to a function by value, by keeping a parameter with an array tag with empty square brackets(like []). From the caller function, just pass the array tag. For instance,
void func(int i[]) {..} /* parameter */
...
int k[10];
func(k); /* caller function */

14.

Should A Function Contain A Return Statement If It Does Not Return A Value?

Answer»

In C, VOID functions does not return anything. So it is useless to put a return statement at the end of the function, where the control will any way return to the caller function. But, if you WANT to omit some PORTION of the function depending upon the SCENARIO, then this return statement is PERFECT to avoid further execution of that void function.

In C, void functions does not return anything. So it is useless to put a return statement at the end of the function, where the control will any way return to the caller function. But, if you want to omit some portion of the function depending upon the scenario, then this return statement is perfect to avoid further execution of that void function.

15.

What Is A Static Function?

Answer»

STATIC function is a SPECIAL type of function whose scope is limited to the source file where the function is defined and can not be used other than that file. This feature HELPS you to hide some functions and to provide some standard interface or wrapper over that LOCAL function.

Static function is a special type of function whose scope is limited to the source file where the function is defined and can not be used other than that file. This feature helps you to hide some functions and to provide some standard interface or wrapper over that local function.

16.

What Is The Benefit Of Using Const For Declaring Constants Over #define?

Answer»

The BASIC difference between them is that, a CONST variable is a real variable which has a datatype and it EXISTS at run TIME, and it can't be altered. But a macro is not a real variable, but it carries a constant value which replaces all the OCCURRENCES of that macro at the time of pre-processing.

The basic difference between them is that, a const variable is a real variable which has a datatype and it exists at run time, and it can't be altered. But a macro is not a real variable, but it carries a constant value which replaces all the occurrences of that macro at the time of pre-processing.

17.

Can Static Variables Be Declared In A Header File?

Answer»

You can't DECLARE a static variable without DEFINITION (this is because they are mutually EXCLUSIVE STORAGE classes). A static variable can be defined in a header file, but then EVERY source file with in that scope will have their own copy of this variable, which is intended.

You can't declare a static variable without definition (this is because they are mutually exclusive storage classes). A static variable can be defined in a header file, but then every source file with in that scope will have their own copy of this variable, which is intended.

18.

What Is The Difference Between Declaring A Variable And Defining A Variable?

Answer»

Declaration is done to tell COMPILER the data type of the variable, and it INHERENTLY meant that somewhere in this scope, this variable is DEFINED or will be defined. And defining a variable MEANS to allocate SPACE for that variable and register it in the stack memory. For example:
extern int decl1; /* this is a declaration */
int def2; /* this is a definition */

Declaration is done to tell compiler the data type of the variable, and it inherently meant that somewhere in this scope, this variable is defined or will be defined. And defining a variable means to allocate space for that variable and register it in the stack memory. For example:
extern int decl1; /* this is a declaration */
int def2; /* this is a definition */

19.

Char *p="sampletext" , *q ="sampletext"; Are These Two Pointers Equal ? If Yes , Then Explain?

Answer»

In C, strings(not array of characters) are immutable. This means that a STRING once CREATED cannot be modified. Only FLUSHING the buffer can remove it. Next point is, when a string is created it is stored in buffer. Next time, when a new string is created, it will CHECK whether that string is PRESENT in buffer or not. If present, that address is assigned. Otherwise, new address stores the new string and this new address is assigned.

In C, strings(not array of characters) are immutable. This means that a string once created cannot be modified. Only flushing the buffer can remove it. Next point is, when a string is created it is stored in buffer. Next time, when a new string is created, it will check whether that string is present in buffer or not. If present, that address is assigned. Otherwise, new address stores the new string and this new address is assigned.

20.

What Does Const Keyword Do?

Answer»

The access MODIFIER keyword “const” tells COMPILER that the value of this VARIABLE is not going to be changed after it is initialized. The compiler will enforce it throughout the LIFETIME of the variable.

The access modifier keyword “const” tells compiler that the value of this variable is not going to be changed after it is initialized. The compiler will enforce it throughout the lifetime of the variable.

21.

How Can I Pad A String To A Known Length?

Answer»

printf("%-20.20s", data[d]);
The "%-20.20s" argument TELLS the printf() FUNCTION that you are printing a string and you want to force it to be 20 characters long. By default, the string is right justified, but by including the minus SIGN (-) before the first 20, you TELL the printf() function to left-justify your string. This action forces the printf() function to PAD the string with spaces to make it 20 characters long.

printf("%-20.20s", data[d]);
The "%-20.20s" argument tells the printf() function that you are printing a string and you want to force it to be 20 characters long. By default, the string is right justified, but by including the minus sign (-) before the first 20, you tell the printf() function to left-justify your string. This action forces the printf() function to pad the string with spaces to make it 20 characters long.

22.

What Is The Difference Between A String Copy (strcpy) And A Memory Copy (memcpy)?

Answer»

Generally SPEAKING, they both copy a number of bytes from a source POINTER to a destination pointer. But the basic difference is that the strcpy() is specifically designed to copy strings, hence it stops COPYING after getting the first ''(NULL) character. But memcpy() is designed to work with all data TYPES. So you need to SPECIFY the length of the data to be copied, starting from the source pointer.

Generally speaking, they both copy a number of bytes from a source pointer to a destination pointer. But the basic difference is that the strcpy() is specifically designed to copy strings, hence it stops copying after getting the first ''(NULL) character. But memcpy() is designed to work with all data types. So you need to specify the length of the data to be copied, starting from the source pointer.

23.

Which One To Use, A Stream Function Or A System Calls?

Answer»

Stream files are generally better to use, since they provide sufficient amount of BUFFER for read and write. That is why it is more efficient.
But in a MULTIUSER ENVIRONMENT, files can be shared among users. These shared files are secured with lock, where only one user will be ABLE to write at a TIME. In this scenario, buffering will not be efficient, since the file content will change continuously and it will be slower.
So, normally it is good to use stream functions, but for shared files system calls are better.

Stream files are generally better to use, since they provide sufficient amount of buffer for read and write. That is why it is more efficient.
But in a multiuser environment, files can be shared among users. These shared files are secured with lock, where only one user will be able to write at a time. In this scenario, buffering will not be efficient, since the file content will change continuously and it will be slower.
So, normally it is good to use stream functions, but for shared files system calls are better.

24.

What Are Text And Binary Modes?

Answer»

Streams can be classified into TWO types: text streams and binary streams. The text streams are INTERPRETED as per the ASCII values starting from 0 to 255. Binary streams are RAW bytes which C can't interpret, but APPLICATION has to interpret it itself. Text modes are used to handle, generally text file where as binary modes can be used for all files. But they won't give you the content of a file, rather they will give you the file properties and content in raw binary format.

Streams can be classified into two types: text streams and binary streams. The text streams are interpreted as per the ASCII values starting from 0 to 255. Binary streams are raw bytes which C can't interpret, but application has to interpret it itself. Text modes are used to handle, generally text file where as binary modes can be used for all files. But they won't give you the content of a file, rather they will give you the file properties and content in raw binary format.

25.

Can You Change The Value Of An Array Tag?

Answer»

No. An array tag can't be used as a storage, because it is not an LVALUE. It can be thought as a pointer to the datatype of the array which is constant and which can't be CHANGED or ASSIGNED dynamically.

No. An array tag can't be used as a storage, because it is not an Lvalue. It can be thought as a pointer to the datatype of the array which is constant and which can't be changed or assigned dynamically.

26.

What Happens When We Use Incremental Operator In A Pointer?

Answer»

<P>It DEPENDS upon the type of the pointer. It gets incremented by the size of the data type, the pointer is POINTING to. For example
char p; p++; /* here p increments by 1*/
int p; p++;/* here p increments by 4(for 32 bit system)*/

It depends upon the type of the pointer. It gets incremented by the size of the data type, the pointer is pointing to. For example
char p; p++; /* here p increments by 1*/
int p; p++;/* here p increments by 4(for 32 bit system)*/

27.

What Is The Difference Between ++x And X++?

Answer»

The ++ OPERATOR is called the incremental operator. When the operator is placed before, the VARIABLE is INCREMENTED by 1 before it is used in the statement. When the operator is placed after the variable, then the expression is EVALUATED first and then the variable is incremented by 1.

The ++ operator is called the incremental operator. When the operator is placed before, the variable is incremented by 1 before it is used in the statement. When the operator is placed after the variable, then the expression is evaluated first and then the variable is incremented by 1.

28.

What Is The Order Of Operator Precedence, Left To Right Or Right To Left ?

Answer»

None of them is standard. C does not ALWAYS start EVALUATING LEFT to right or right to left. Normally, FUNCTION calls are done FIRST, followed by complex expressions and then simple expressions. That is why it is best to use parenthesis in all expressions, without depending on precedence.

None of them is standard. C does not always start evaluating left to right or right to left. Normally, function calls are done first, followed by complex expressions and then simple expressions. That is why it is best to use parenthesis in all expressions, without depending on precedence.

29.

How To Assign One Array To Another?

Answer»

You can't assign an array to other. Arrays are not lvalue, because they don't refer to one variable, RATHER a set of variables. So they can't be PLACED on the left hand side of an assignment statement. For EXAMPLE the FOLLOWING statement will generate compilation error.
int x[5], y[5];
x = y;

You can't assign an array to other. Arrays are not lvalue, because they don't refer to one variable, rather a set of variables. So they can't be placed on the left hand side of an assignment statement. For example the following statement will generate compilation error.
int x[5], y[5];
x = y;

30.

What Is An Lvalue?

Answer»

An LVALUE is an expression to which a value can be assigned. The lvalue expression is the one which is located on the left side a STATEMENT, WHEREAS an rvalue is located on the right side of a statement. Each assignment must have a valid lvalue and rvalue. The lvalue expression must refer to a storage where something can be stored. It can't be a constant.

An lvalue is an expression to which a value can be assigned. The lvalue expression is the one which is located on the left side a statement, whereas an rvalue is located on the right side of a statement. Each assignment must have a valid lvalue and rvalue. The lvalue expression must refer to a storage where something can be stored. It can't be a constant.

31.

How To Redefined Macro With Different Value?

Answer»

The #undef preprocessor can be USED to reset a macro. For example,
#ifdef SAMPLE /* Checking if SAMPLE is defined */
#undef SAMPLE /* If so, then reset it */
#endif
#define SAMPLE 0 /* Then redefine with INTENDED value */

The #undef preprocessor can be used to reset a macro. For example,
#ifdef SAMPLE /* Checking if SAMPLE is defined */
#undef SAMPLE /* If so, then reset it */
#endif
#define SAMPLE 0 /* Then redefine with intended value */

32.

Explain Recursive Functions? Also Explain The Advantages And Disadvantages Of Recursive Algorithms?

Answer»

A recursive function is a function which calls itself.
The advantages of recursive functions are:

  • A SUBSTITUTE for very complex iteration. For example, a recursive function is best to reduce the code size for Tower of HANOI application.
  • Unnecessary calling of functions can be AVOIDED.

The disadvantages of Recursive functions:

  • The exit point must be explicitly coded ,otherwise stack OVERFLOW may happen
  • A recursive function is often confusing. It is difficult to trace the logic of the function. HENCE, it is difficult to debug a recursive function.

A recursive function is a function which calls itself.
The advantages of recursive functions are:

The disadvantages of Recursive functions:

33.

How To Print An Address?

Answer»

The best WAY is to use "%p" in printf() or fprintf. The “%p” will TELL compiler to use the best type to use, while printing the ADDRESS ACCORDING to the ENVIRONMENT, since the size of a pointer changes from system to system.

The best way is to use "%p" in printf() or fprintf. The “%p” will tell compiler to use the best type to use, while printing the address according to the environment, since the size of a pointer changes from system to system.

34.

How To Restrict A Header File From Including More Than Once?

Answer»

In C, to avoid double INCLUSION, we use a include guard also known as macro guard. It is #ifndef - #ENDIF pair. "ifndef" is an indication of “if not DEFINED”.
#ifndef FAMILY_H
#DEFINE FAMILY_H
struct Example
{
int member;
};
#endif /* FAMILY _H */

In C, to avoid double inclusion, we use a include guard also known as macro guard. It is #ifndef - #endif pair. "ifndef" is an indication of “if not defined”.
#ifndef FAMILY_H
#define FAMILY_H
struct Example
{
int member;
};
#endif /* FAMILY _H */

35.

How Does Free() Method Know About How Much Memory To Release?

Answer»

There's no concrete way. Most systems, keeps a TRACK of each MEMORY block as linked lists. When memory is allocated, all the blocks that are given to that PARTICULAR call are put into a linked list and the SIZE, block number and serial number are WRITTEN in the head node. There is no assurance, though. But in some way or other, the system keeps track of each block to know the size of each allocated portion of the heap.

There's no concrete way. Most systems, keeps a track of each memory block as linked lists. When memory is allocated, all the blocks that are given to that particular call are put into a linked list and the size, block number and serial number are written in the head node. There is no assurance, though. But in some way or other, the system keeps track of each block to know the size of each allocated portion of the heap.

36.

What Happens If You Free A Pointer Twice?

Answer»

It is really DANGEROUS to FREE the same memory twice. If the memory has not been reallocated in between, it will GENERATE a “DOUBLE free” ERROR, since the memory location has already been freed.

It is really dangerous to free the same memory twice. If the memory has not been reallocated in between, it will generate a “double free” error, since the memory location has already been freed.

37.

What Will Be The Output Of The Following Code Snippet?

Answer»

float NUM1 = 6 / 4;
float num2 = 6 / 4.0;
printf("6/4 == %f or %FN", num1, num2);
OUTPUT will be : 6/4 == 1.000000 or 1. 500000. This is a case of operator promotion. The variable num1 is set to “6/4”. Because, both 3 and 4 are integers. So integer division is performed on them and the result is the integer 0. The variable num2 is set to “6/4.0”. Because 4.0 is a float, the number 6 is converted to a float as well, and the result will be floating value 1.5.

float num1 = 6 / 4;
float num2 = 6 / 4.0;
printf("6/4 == %f or %fn", num1, num2);
Output will be : 6/4 == 1.000000 or 1. 500000. This is a case of operator promotion. The variable num1 is set to “6/4”. Because, both 3 and 4 are integers. So integer division is performed on them and the result is the integer 0. The variable num2 is set to “6/4.0”. Because 4.0 is a float, the number 6 is converted to a float as well, and the result will be floating value 1.5.

38.

What Is The Heap In Memory?

Answer»

The HEAP is where MALLOC(), calloc(), and realloc() get memory. The allocation of memory from the heap is much slower than the stack. But, the heap is much more flexible about memory allocation than the stack. Memory can be allocated and deallocated in any time and order. This heap memory isn't deallocated by itself, method FREE() has to be called in order to do so.

The heap is where malloc(), calloc(), and realloc() get memory. The allocation of memory from the heap is much slower than the stack. But, the heap is much more flexible about memory allocation than the stack. Memory can be allocated and deallocated in any time and order. This heap memory isn't deallocated by itself, method free() has to be called in order to do so.

39.

What Is The Difference Between Malloc() And Calloc()?

Answer»

Calloc:

  • Allocates a REGION of memory LARGE enough to hold "N” elements of "size" bytes each.
  • Calloc initializes the whole memory with 0

Malloc:

  • Allocates "size" bytes of memory.
  • Malloc does not change the EXISTING memory. So it returns a memory chunk with garbage VALUE

Calloc:

Malloc:

40.

Can The Size Of An Array Be Declared At Runtime?

Answer»

No. The SIZE of an ARRAY must be STATED at the time of compilation. Alternate way is to use dynamic allocation by CALLOC or MALLOC.

No. The size of an array must be stated at the time of compilation. Alternate way is to use dynamic allocation by calloc or malloc.

41.

What Is The Value Of Null?

Answer»

The VALUE of NULL is 0 or (VOID*)0. Whenever NULL has to be compared with some VARIABLE or assigned to a variable, depending upon the type of that variable, the value of NULL will be decided.

The value of NULL is 0 or (void*)0. Whenever NULL has to be compared with some variable or assigned to a variable, depending upon the type of that variable, the value of NULL will be decided.

42.

Can #include Handle Other File Formats Than .h?

Answer»

Yes. Irrespective of the FILE type, PREPROCESSOR will do its job and will INCLUDE any file like test.z.

Yes. Irrespective of the file type, Preprocessor will do its job and will include any file like test.z.

43.

What Is The Difference Between #include And #include "file" ?

Answer»

We use # include to include a file. The difference between two ways of file inclusion lies in the order in which preprocessor searches for the file specified. When the preprocessor ENCOUNTERS #include statement, it looks for the file specified in the angled brackets in the default location (PATH defined in INCLUDE environment variable of the system).
When # include followed by file NAME in double quotation marks is encountered by the preprocessor, it looks for the file in the current DIRECTORY. If the file is not found in the current directory, it will look for the file in the default location.

We use # include to include a file. The difference between two ways of file inclusion lies in the order in which preprocessor searches for the file specified. When the preprocessor encounters #include statement, it looks for the file specified in the angled brackets in the default location (Path defined in INCLUDE environment variable of the system).
When # include followed by file name in double quotation marks is encountered by the preprocessor, it looks for the file in the current directory. If the file is not found in the current directory, it will look for the file in the default location.

44.

Is It Possible To Get The Source Code Back From Binary File?

Answer»

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.

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.

45.

What Do You Mean By Persistent And Non Persistent Objects?

Answer»

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. ]

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. ]

46.

What Is Difference Between Shallow Copy And Deep Copy? Which Is Default?

Answer»

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.

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.

47.

What Is Realloc() And Free()? What Is Difference Between Them?

Answer»

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.

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.

48.

What Is 'copy Constructor' And When It Is Called?

Answer»

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.
class SampleClass{
public:
int* ptr;
SampleClass();
// Copy constructor declaration
SampleClass(SampleClass &obj);
};
SampleClass::SampleClass(){
ptr = new int();
*ptr = 5;
}
// Copy constructor definition
SampleClass::SampleClass(SampleClass &obj){
//create a new object for the pointer
ptr = new int();
// Now manually ASSIGN the value
*ptr = *(obj.ptr);
cout<<"Copy constructor...n";
}

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.
class SampleClass{
public:
int* ptr;
SampleClass();
// Copy constructor declaration
SampleClass(SampleClass &obj);
};
SampleClass::SampleClass(){
ptr = new int();
*ptr = 5;
}
// Copy constructor definition
SampleClass::SampleClass(SampleClass &obj){
//create a new object for the pointer
ptr = new int();
// Now manually assign the value
*ptr = *(obj.ptr);
cout<<"Copy constructor...n";
}

49.

How Many Storage Classes Are Available In C++?

Answer»

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++
auto : 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.
register : 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.
static : 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.
extern :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.
mutable : 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.

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++
auto : 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.
register : 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.
static : 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.
extern :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.
mutable : 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.

50.

What Are Virtual Functions And What Is Its Use?

Answer»

Virtual functions are member functions of class which is declared using keyword 'virtual'. When a base class type REFERENCE is initialized using object of sub class type and an overridden method which is declared as virtual is invoked using the base reference, the method in CHILD class object will get invoked.
class Base
{
int a; 
public:
Base()
{
a = 1;
}
virtual void method()
{
cout << a;
}
};
class Child: public Base
{
int b;
public: 
Child()
{
b = 2
}
virtual void method()

cout << b;
}
}; 
int main()
{
Base *pBase; 
Child oChild;
pBase = &oChild;
pBase->method(); 
return 0;
}
In the above EXAMPLE even though the method in invoked on Base class reference, method of the child will get invoked since its declared as virtual.

Virtual functions are member functions of class which is declared using keyword 'virtual'. When a base class type reference is initialized using object of sub class type and an overridden method which is declared as virtual is invoked using the base reference, the method in child class object will get invoked.
class Base
{
int a; 
public:
Base()
{
a = 1;
}
virtual void method()
{
cout << a;
}
};
class Child: public Base
{
int b;
public: 
Child()
{
b = 2; 
}
virtual void method()

cout << b;
}
}; 
int main()
{
Base *pBase; 
Child oChild;
pBase = &oChild;
pBase->method(); 
return 0;
}
In the above example even though the method in invoked on Base class reference, method of the child will get invoked since its declared as virtual.