• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

C Interview Questions Answers

Question - 141 : - What is a const pointer?

Answer - 141 : - The access modifier keyword const is a promise the programmer makes to the compiler that the value of a variable will not be changed after it is initialized. The compiler will enforce that promise as best it can by not enabling the programmer to write code which modifies a variable that has been declared const. A const pointer, or more correctly, a pointer to const, is a pointer which points to data that is const (constant, or unchanging). A pointer to const is declared by putting the word const at the beginning of the pointer declaration. This declares a pointer which points to data that can’t be modified. The pointer itself can be modified. The following example illustrates some legal and illegal uses of a const pointer: const char *str = hello; char c = *str /* legal */ str++; /* legal */ *str = ‘a’; /* illegal */ str[1] = ‘b’; /* illegal */

Question - 142 : - Is it better to use a macro or a function?

Answer - 142 : - The answer depends on the situation you are writing code for. Macros have the distinct advantage of being more efficient (and faster) than functions, because their corresponding code is inserted directly into your source code at the point where the macro is called. There is no overhead involved in using a macro like there is in placing a call to a function. However, macros are generally small and cannot handle large, complex coding constructs. A function is more suited for this type of situation. Additionally, macros are expanded inline, which means that the code is replicated for each occurrence of a macro. Your code therefore could be somewhat larger when you use macros than if you were to use functions. Thus, the choice between using a macro and using a function is one of deciding between the tradeoff of faster program speed versus smaller program size. Generally, you should use macros to replace small, repeatable code sections, and you should use functions for larger coding tasks that might require several lines of code.

Question - 143 : - Linked Lists -- Can you tell me how to check whether a linked list is circular?

Answer - 143 : - Create two pointers, and set both to the start of the list. Update each as follows: while (pointer1) { pointer1 = pointer1->next; pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next; if (pointer1 == pointer2) { print ("circular"); } } If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.

Question - 144 : - What is the easiest searching method to use?

Answer - 144 : - Just as qsort() was the easiest sorting method, because it is part of the standard library, bsearch() is the easiest searching method to use. If the given array is in the sorted order bsearch() is the best method. Following is the prototype for bsearch(): void *bsearch(const void *key, const void *buf, size_t num, size_t size, int (*comp)(const void *, const void*)); Another simple searching method is a linear search. A linear search is not as fast as bsearch() for searching among a large number of items, but it is adequate for many purposes. A linear search might be the only method available, if the data isn’t sorted or can’t be accessed randomly. A linear search starts at the beginning and sequentially compares the key to each element in the data set.

Question - 145 : - Which bit wise operator is suitable for checking whether a particular bit is on or off?

Answer - 145 : - The bitwise AND operator. Here is an example:enum { KBit0 = 1, KBit1, … KBit31, }; if ( some_int & KBit24 ) printf ( “Bit number 24 is ON\n” ); else printf ( “Bit number 24 is OFF\n” );

Question - 146 : - When is a switch statement better than multiple if statements?

Answer - 146 : - A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type.

Question - 147 : - How many levels of pointers can you have?

Answer - 147 : - 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 - 148 : - What will be the result of the following code? #define TRUE 0 // some code while(TRUE) { // some code }

Answer - 148 : - This will not go into the loop as TRUE is defined as 0.

Question - 149 : - What is #line used for?

Answer - 149 : - 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 - 150 : - What will be printed as the result of the operation below: main() { char s1[]=“exam”; char s2[]= “material”; printf(“%s”,s1) ; }

Answer - 150 : - exam

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners