Saturday, 12 December 2020

C interview questions - Part 2

Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post. 

code snippets are hosted on ideone.com. you can run code there as well.


1. What will be new average given ( old average + number of samples ) and new sample value :

New average = ( old average ) * ( old number of samples ) + new sample / ( old number of samples + 1 ) 


2. Read some details about C standard libraries : 

https://www.tutorialspoint.com/c_standard_library/ >


3. Remeber that strings are terminated with null character "\0" (zero) , this is how we find end of the string. In C language there is no seperate string data type , strings are created using arrays. Where as in C++ , strings are a seperate class.

also '\0'( ASCII ) is 0x00 in hex.

'0'( ASCII ) is 0x30 in hex.

one interesting string question < https://stackoverflow.com/questions/18688971/c-char-array-initialization/18688992#18688992 >

Consider below example of "strlen" vs "sizeof" below.



4. What is use of Assert for error handling in C language ???

C assert() statement is a runtime assert statement i.e it won't give you a compile time error , but it will result into runtime error when assert() statement fails.

Compile time asserts can be also written , using preprocessor directives like #if , #warning, #error or using if...else. Always remember preprocessor is invoked before compiler and linker.

Please play around with below example code , I would recommend to install codeblocks + MinGW in yout machine and try there.


5. What is use of Cygwin ( note : windows bash is going to replace Cygwin ).

https://www.quora.com/For-what-purposes-is-cygwin-exactly-used >


6. Overflow of smaller data type:

Remeber that when you assign a "larger value" to a "smaller data type variable" overflow occurs.

Also remember that range of signed char is -128 to 127 ( negative side is more ) and unsigned char range is 0 to 255.

https://stackoverflow.com/questions/21667971/what-happens-when-a-char-is-assigned-a-value-too-large-to-fit-in-a-byte >

consider following example :


7. Rules of integer promotion  and type casting in C :

This is excellent article.

https://www.geeksforgeeks.org/integer-promotions-in-c/ >

also try this code as well:


Is integer promotion always needed ? 

https://stackoverflow.com/questions/12807953/do-integer-promotions-always-happen-when-evaluating-an-expression >


8. Types of Error in C :

There are 3 types of errors : compile time , link time , run time , logical/design errors.

compile time error can be syntex error or semantic error.

It's very difficult to detect design errors.

https://www.geeksforgeeks.org/errors-in-cc/ >

example of sematic error :

int main(void) {

    volatile unsigned int temp;

    10 = temp;

}

error: lvalue required as left operand of assignment.

9. what is lvalue and rvalue ??

https://www.geeksforgeeks.org/lvalue-and-rvalue-in-c-language/ >

10. How would you avoid invalid use of assignment operator instead of comparison operator ??

consider below nasty situation and try to guess output :

to avoid this you can make rule to write VALUES/rvalue on left hand side of comparision operator. as writng rvalue on left hand side of assignment operator will result in simantic error.

You should write following way.

11. Write a code to convert big Endian to little endian and vice versa :



12. operator precedence in c :

while it is better to us brackets to isolate statements in C , knowledge of operator precedence is necessary to avoid unnecessary use of brackets.



13. Dividend ÷ Divisor = Quotient ( and reminder ) , numerators and denominators.









remember that % will take the sign of dividend ( this is reminder ) 

14. Duff's device:


15. coroutine vs subroutine vs threads :



16. write a program to find a number is power of 2 :

here the trick is number 2 , note that if a given number is power of 2 then that number will have only 1 bit set in it's binanry representation. 



17. Write a generic program to find if  given number "pow" is power of number "num":

This cannot be achived by the technique above. And you have to go traditional way of dividing numbers or multiplying the numbers and then comparing them.


18. Write a C program to swap all nibbles in individual byte in an 32-bit integer.



19. Write a macro to define a register variable in C.


20. what is stack overflow and heap overflow ?


21. does malloc always returns contiguous memory ?

Answer is YES.


Others :
I would strongly recommend to read SO anweres of this account.

Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post. 

code snippets are hosted on ideone.com. you can run code there as well.


1 comment:

  1. Really useful to read these before interviews. Good collection of challenging questions. ~NT

    ReplyDelete