C Interview 1
C Interview 1
C Interview 1
com
C INTERVIEW QUESTIONS
Question: What are the advantages of the functions? Answer : Debugging is easier It is easier to understand the logic involved in the program Testing is easier Recursive call is possible Irrelevant details in the user point of view are hidden in functions Functions are helpful in generalizing the program Question: What is the purpose of main( ) function? Answer :The function main( ) invokes other functions within it.It is the first function to be called when the program starts execution. It is the starting function It returns an int value to the environment that called the program Recursive call is allowed for main( ) also. It is a user-defined function Program execution ends when the closing brace of the function main( ) is reached. It has two arguments 1)argument count and 2) argument vector (represents strings passed). Any user-defined name can also be used as parameters for main( ) instead of argc and argv Question: What is storage class and what are storage variable? Answer: A storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage. There are five types of storage classes 1) auto 2) static 3) extern 4) register 5) typedef Question: Which expression always return true? Which always return false? Answer: expression if (a=0) always return false expression if (a=1) always return true. Question: Write the equivalent expression for x%8? Answer:x&7
www.fresherschoice.com
www.fresherschoice.com Question: What is the purpose of realloc ( )? Answer: The function realloc (ptr,n) uses two arguments. The first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument n specifies the new size. The size may be increased or decreased. If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc ( ) may create a new region and all the old data are moved to the new region.
Question: What is static memory allocation and dynamic memory allocation? Answer: Static memory allocation: The compiler allocates the required memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variable has static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time. Dynamic memory allocation: It uses functions such as malloc ( ) or calloc ( ) to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these functions are assigned to pointer variables, such assignments are known as dynamic memory allocation. Memory is assigned during run time.
Question: How are pointer variables initialized? Answer: Pointer variable are initialized by one of the following two ways Static memory allocation Dynamic memory allocation
Question: What is a pointer variable? Answer: A pointer variable is a variable that may contain the address of another variable or any valid address in the memory.
Question: What is a pointer value and address? Answer: A pointer value is a data object that refers to a memory location. Each memory location is numbered in the memory. The number attached to a memory location is called the address of the location. www.fresherschoice.com
www.fresherschoice.com
Question: What are the characteristics of arrays in C? Answer :1) An array holds elements that have the same data type 2) Array elements are stored in subsequent memory locations 3) Two-dimensional array elements are stored row by row in subsequent memory locations. 4) Array name represents the address of the starting element 5) Array size should be mentioned in the declaration. Array size must be a constant expression and not a variable.
Question: Differentiate between a linker and linkage? Answer: A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.
Question: What are the advantages of auto variables? Answer : 1)The same auto variable name can be used in different blocks 2)There is no side effect by changing the values in the blocks 3)The memory is economically used 4)Auto variables have inherent protection because of local scope.
Question: why n++ executes faster than n+1? Answer: The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas; n+1 requires more instructions to carry out this operation.
www.fresherschoice.com
www.fresherschoice.com
Question: what is a modulus operator? What are the restrictions of a modulus operator? Answer: A Modulus operator gives the remainder value. The result of x%y is obtained by (x-(x/y)*y). This operator is applied only to integral operands and cannot be applied to float or double.
Question: Can the sizeof operator be used to tell the size of an array passed to a function? Answer: No. Theres no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to the first element.
Question: Is using exit () the same as using return? Answer: No. The exit () function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main () function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit () function are similar.
Question Why should I prototype a function? Answer: A function prototype tells the compiler what kind of arguments a function is looking to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and that no erroneous type conversions are taking place. Question: How do you print an address? Answer: The safest way is to use printf () (or fprintf() or sprintf()) with the %P specification. That prints a void pointer (void*). Different compilers might print a pointer with different formats. Your compiler will pick a format thats right for your environment. If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*: printf (%Pn, (void*) buffer); www.fresherschoice.com
www.fresherschoice.com Question: Can math operations be performed on a void pointer? Answer: No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you dont know what its pointing to, so you dont know the size of what its pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers.
Question: How can you determine the size of an allocated portion of memory? Answer: You cant, really free() can , but theres no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, theres no guarantee the trick wont change with the next release of the compiler. Question: What is the difference between NULL and NUL? Answer: NULL is a macro defined in <stddef.h> for the null pointer. NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. Theres no standard macro NUL in C, but some people like to define it. The digit 0 corresponds to a value of 80, decimal. Dont confuse the digit 0 with the value of (NUL)! NULL can be defined as ((void*)0), NUL as . Question: When would you use a pointer to a function? Answer : Pointers to functions are interesting when you pass them to other functions. A function that takes function pointers says, in effect, Part of what I do can be customized. Give me a pointer to a function, and Ill call it when that part of the job needs to be done. That function can do its part for me. This is known as a callback. Its used a lot in graphical user interface libraries, in which the style of a display is built into the library but the contents of the display are part of the application. As a simpler example, say you have an array of character pointers (char*s), and you want to sort it by the value of the strings the character pointers point to. The standard qsort() function uses function pointers to perform that task. qsort() takes four arguments, a pointer to the beginning of the array, the number of elements in the array, the size of each array element, and a comparison function, and returns an int.
www.fresherschoice.com
www.fresherschoice.com Question; How can I convert a number to a string? Answer: The standard C library provides several functions for converting numbers of all formats (integers, longs, floats, and so on) to strings and vice versa The following functions can be used to convert integers to strings: Function Name Purpose iota() Converts an integer value to a string. ltoa () Converts a long integer value to a string. ultoa () Converts an unsigned long integer value to a string. The following functions can be used to convert floating-point values to strings: Function Name Purpose ecvt() Converts a double-precision floating-point value to a string without an embedded decimal point. fcvt() Same as ecvt(), but forces the precision to a specified number of digits. gcvt() Converts a double-precision floating-point value to a string with an embedded decimal point.
strtod() Converts a string to a double-precision floating-point value and reports any leftover numbers that could not be converted. strtol() Converts a string to a long integer and reports any leftover numbers that could not be converted. strtoul() Converts a string to an unsigned long integer and reports any leftover numbers that could not be converted.
Question: What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used? Answer: The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character () has been moved. On the other hand, the memcpy () function is designed to work with any type of data. Because not all data ends with a null character, you must provide the memcpy () function with the number of bytes you want to copy from the source to the destination. www.fresherschoice.com
www.fresherschoice.com Question: How can you check to see whether a symbol is defined? Answer: You can use the #ifdef and #ifndef preprocessor directives to check whether a symbol has been defined (#ifdef) or whether it has not been defined (#ifndef).
Question: How do you override a defined macro? Answer: You can use the #undef preprocessor directive to undefine (override) a previously defined macro. Question: What is #line used for? Answer: The #line preprocessor directive is used to reset the values of the _ _LINE_ _ and _ _FILE_ _ symbols, respectively. This directive is commonly used in fourth-generation languages that generate C language source files.
Question: What is a pragma? Answer :The #pragma preprocessor directive allows each compiler to implement compiler-specific features that can be turned on and off with the #pragma statement. For instance, your compiler might support a feature called loop optimization. This feature can be invoked as a command-line option or as a #pragma directive. To implement this option using the #pragma directive, you would put the following line into your code: #pragma loop_opt(on) Conversely, you can turn off loop optimization by inserting the following line into your code: Question: What does it mean when a pointer is used in an if statement? Answer: Any time a pointer is used as a condition, it means Is this a non-null pointer? A pointer can be used in an if, while, for, or do/while statement, or in a conditional expression.
www.fresherschoice.com
www.fresherschoice.com Question: Is NULL always defined as 0? Answer: NULL is defined as either 0 or (void*)0. These values are almost identical; either a literal zero or a void pointer is converted automatically to any kind of pointer, as necessary, whenever a pointer is needed (although the compiler cant always tell when a pointer is needed).
Question: How are portions of a program disabled in demo versions? Answer :If you are distributing a demo version of your program, the preprocessor can be used to enable or disable portions of your program. The following portion of code shows how this task is accomplished, using the preprocessor directives #if and #endif: int save document(char* doc_name) { #if DEMO_VERSION printf(Sorry! You cant save documents using the DEMO version of this program!n); return(0); #endif ... Question: What is a null pointer? Answer: There are times when its necessary to have a pointer that doesnt point to anything. The macro NULL, defined in <stddef.h>, has a value thats guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather than NULL. The null pointer is used in three ways: 1) To stop indirection in a recursive data structure 2) As an error value 3) As a sentinel value
www.fresherschoice.com
www.fresherschoice.com Question :How many levels of pointers can you have? Answer: The answer depends on what you mean by levels of pointers. If you mean How many levels of indirection can you have in a single declaration? the answer is At least 12. int i = 0; int *ip01 = & i; int **ip02 = & ip01; int ***ip03 = & ip02; int ****ip04 = & ip03; int *****ip05 = & ip04; int ******ip06 = & ip05; int *******ip07 = & ip06; int ********ip08 = & ip07; int *********ip09 = & ip08; int **********ip10 = & ip09; int ***********ip11 = & ip10; int ************ip12 = & ip11; ************ip12 = 1; /* i = 1 */ The ANSI C standard says all compilers must handle at least 12 levels. Your compiler might support more.
Question: What is indirection? Answer: If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable or any other object in memory, you have an indirect reference to its value. Question: How can I search for data in a linked list? Answer :Unfortunately, the only way to search a linked list is with a linear search, because the only way a linked lists members can be accessed is sequentially. Sometimes it is quicker to take the data from a linked list and store it in a different data structure so that searches can be more efficient.
Question: How can I sort a linked list? Answer: Both the merge sort and the radix sort are good sorting algorithms to use for linked lists.
www.fresherschoice.com
www.fresherschoice.com Question: What is the quickest searching method to use? Answer :A binary search, such as bsearch() performs, is much faster than a linear search. A hashing algorithm can provide even faster searching. One particularly interesting and fast method for searching is to keep the data in a digital trie. A digital trie offers the prospect of being able to search for an item in essentially a constant amount of time, independent of how many items are in the data set.
A digital trie combines aspects of binary searching, radix searching, and hashing. The term digital trie refers to the data structure used to hold the items to be searched. It is a multilevel data structure that branches N ways at each level.
Question: Whats is structure padding?Say a given structure Struct{ int a; char c; float d; } the size of structure is 7 here. But structure padding is done what will be the size of the struct?Will it change and how?How to avoid this?is it necessary? Answer : Integers and floats :compilers will try to place these variables at addresses which are in multiples of 2 or 4(in 16-bit system) now in this case 1 byte can be padded...we can store integers and floats at start to avoid padding
Question :How to type a string without using printf function? Answer : //printing a string without printf#includeint main(){ char *str="shobhit"; while((*str)!=NULL) { putchar(*str); str++; } return 0;}
Question: How to write a C program to find the power of 2 in a normal way and in single step? Answer : U can take logarithm base 2, and check the result is in interger form or floating point form, u can check whether it is power of 2 or not.
www.fresherschoice.com
www.fresherschoice.com Question :How to break cycle in circular single link list? Answer :we can delete an intermediate one Question :What does it meana[i]=i+i Answer :a[i]=i+i; its just simple... an assignment statement. an i'th element of array a (i.e.,) a[i] is going to have a value i+i; eg; lets i=3 means a[3]=3+3; a[3]=6;
Question: Between a long pointer and a char pointer, which one consumes more memory? Explain Answer: Both will consume same amount of memory. why because they means long or char pointer always stores the address of the character or long integer .
Question: What is wrong with the following c prog?? char *s1 = "hello"; char *s2 = "world"; char *s3 = strcat(s1, s2); Please provide me explanations?? Answer: Since what is present in memory beyond "United" is not known and we are attaching "Front" at the end of "United", thereby overwriting something, which is an unsafe thing to do.
www.fresherschoice.com
www.fresherschoice.com Question: How can we open a image file through C program Answer :In C, generally we can open files having text format... other types of files can be opened in binary format only using file *fp; fp=fopen("filename","rb+");// where b stands for binary format
Question :How can you calculate number of nodes in a circular Linked List? Answer :struct node { int data; struct node *next; }; i write just function here int count(struct node *pp) { struct node *start; int count=0; start=pp->next; while(start->next!=pp) { start=start->next; count++; } return count; }
www.fresherschoice.com
www.fresherschoice.com Question: What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro? Answer: #define NULL 0 #define NULL_PTR (void *)0 NULL_PTR has pointer context, while NULL is a normal value.
Question :Can we use string in switch statement? Answer : We cannot use a string in switch statement nor can we use a floating point in switch statement. all we can use in a switch statement is a character and integer. Also we cannot use statements like i<10 in case statements.This is the reason why switch can't replace IF statements although it allows us to make several choices depending upon the condition. Question: Output of this Programme please?? main() { int a[]={2,4,6,8,10}; int i; change(a,5); for(int i=0;i<=4;i++) printf("\n %d", a[i]); } change(int *b,int n) { int i; for(i=0;i *(b+i) = *(b+i)+5; } sytaxis correct?? was asked i a test
www.fresherschoice.com
www.fresherschoice.com Answer :I think the syntax is incorect in the for loop of the change function, it sould have atleast a closing ')'. Secondly, if the function defination is given after the function calling, then the proper prototype of the function must be declared before the calling of the function, other wise compiler declares a default prototype by considering each parameters as well as the return type as integer, which leads into the compiler error "type mismatch in redeclaration of the function".
Question: How to swap the content of two variables without a temporary variable Answer: void swap (int a, int b) { a =a+b; b=a-b; a=a-b; }
Question: How do you write a C program which can calculate lines of code but not counting comments? Answer :Using file concept with Command line arguments.declare a variable (lcnt) used to count the no of lines.Open a file in read made and then using while loop check the condition for not equal to EOF.Later using if condition check check for new line and increment the variable for counting the lines. Then using while,check for the character '/','*' (as the comments start with these characters) and end with ('*' and '/').if condition of this is true then break and come out of the block else increment the line.
www.fresherschoice.com
www.fresherschoice.com Question: main(int x)............. explaination on arguments passed thr' main Answer : The main function can have the command linre arguments like in this syntax main(int x)...... the main function can have two arguments 1. int x : tell the number of arguments in the main function that are given on the command line 2. char *array[] :it is an array of pointers to the string and specify the file names thay you want to pass on the command line. Question :What will be the output of the following program in UNIX OS with CC compiler and TC compiler? int main() { int i=5; printf("\n%d",++i + ++i + ++i + ++i + ++i ); } If any difference then Why it is difference? Answer : Output: 41. For different compiler ther will be same output. Expression will be evaluated in following manner. (((++i + ++i) + ++i) + ++i) + ++i 6 7 7 + 7 = 14 8 14 + 8 = 22 9 22 + 9 = 31 10 31 + 10 = 41.
www.fresherschoice.com
www.fresherschoice.com Question: main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); }
Question: main() { printf("%d, %d", sizeof('c'), sizeof(100)); } Answer :The answer is 2,2 coz sizeof return the memory occupied and "c" uses 1 byte to store character 'c' and the other byte to store NULL to indicate the end of string. 100 being stored as an integer would take 2 bytes. Question main() { int i = 100; clrscr(); printf("%d", sizeof(sizeof(i))); }
Answer : Internal sizeof(i) gives output 2.2 is integer so outer sizeof()again gives output 2. Question: main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%dn", x--); } }
www.fresherschoice.com
Answer : 1 Question main() { signed int bit=512, i=5; for(;i;i--) { printf("%dn", bit = (bit >> (i - (i -1)))); } }
Answer :because the will b terminated after i=0;bit>>(i-(i-1)))means first it assignsi=5.the first value for bit is512>>(5-(5-1))=512>>1=512/2/1=256
Question :main() { if (!(1&&0)) { printf("OK I am done."); } else { printf("OK I am gone."); } } Answer : OK I am done
www.fresherschoice.com
www.fresherschoice.com Question: In c , main() is a function . and where is defined main() in c. bcz every function has three parts. 1>. decleration 2>. definition. 3>. Calling Answer: Declaration is not needed if method is defined before calling. main() method is called by the OS when the program is run. So, it has only a definition.. Question: Fibonacci series program Answer :#include <iostream> using namespace std; int main () { int fOne = 1; int fTwo = 1; int fThree = 2; long fN; long fNN; cout << "How many n terms do you want in the sequence: "; cin >> fN; for ( fN = 1 ; fN >= 3 ; fN++ ) { for (fNN = 1; fNN >= fN; fNN++) fNN = (fN - 1) + (fN - 2); cout << fN; Question What is the difference between #include< > and #include" " Answer: General Convention for this notation is: # include < > ---> Specifically used for built in header files. # include " " ----->Specifically used for used for user defined/created n header file www.fresherschoice.com
www.fresherschoice.com Question: When function say abc() calls another function say xyz(), what happens in stack? Answer: When some function xyz() calls function abc(). all the local variables, static links, dynamic links and function return value goes on the top of all elements of function xyz() in the stack. when abc() exit it's return value has been assigned to xyz().
Question: Difference between arrays and pointers? Answer: Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them Arrays use subscripted variables to access and manipulate data. Array variables can be equivalently written using pointer expression.
Question: How will you print % character? Answer: printf (%%) will print %
Question: const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } Answer :Ans will be two not 4 as that value perplexed is const variable and a const can not be changed
www.fresherschoice.com