InterviewSolution
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 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 |
|
| 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 struct XXX |
|
| 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, 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, |
|
| 5. |
What Is Wild Pointer In C? |
|
Answer» A pointer is known as wild pointer c, if it has not been initialized. For Example: A pointer is known as wild pointer c, if it has not been initialized. For Example: |
|
| 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, 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, |
|
| 7. |
Write A C Program To Swap Two Variables Without Using Third Variable ? |
|
Answer» #include<stdio.h> #include<stdio.h> |
|
| 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. 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. |
|
| 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: 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: |
|
| 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, 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, |
|
| 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: 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: |
|
| 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]); printf("%-20.20s", data[d]); |
|
| 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. 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. |
|
| 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 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 |
|
| 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. 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. |
|
| 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, The #undef preprocessor can be used to reset a macro. For example, |
|
| 32. |
Explain Recursive Functions? Also Explain The Advantages And Disadvantages Of Recursive Algorithms? |
|
Answer» A recursive function is a function which calls itself.
The disadvantages of Recursive functions:
A recursive function is a function which calls itself. 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”. 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”. |
|
| 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 num1 = 6 / 4; |
|
| 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:
Malloc:
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). 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). |
|
| 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.] 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.] |
|
| 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. 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. |
|
| 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* 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(). |
|
| 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. 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. |
|
| 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. 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. |
|
| 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. 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. |
|