Programming Questions and Answers
Our C Interview questions come with detailed explanation of the answers which helps in better understanding of C concepts.
#1: Predict the output of the following C snippets
int main() { int main = 56; printf("%d", main); return 0; }
a) Compiler Error
b) Depends on the compiler
c) 56
d) none of above
#2: What is the output of this C code?
#include<stdio.h> int main() { float a = 3.14, *fptr; fptr = &a; printf("Size of Float Pointer : %d", sizeof(fptr)); return (0); }
a) Size of Float Pointer : 2
b) Size of Float Pointer : 4
c) Size of Float Pointer : 6
d) Size of Float Pointer : 8
#3: What is the output of the below c code?
#include<stdio.h> int main() { char ch; if(ch = printf("")) printf("It matters\n"); else printf("It doesn't matters\n"); return 0; }
a) It matters
b) It doesn’t matters
c) Run time error
d) Nothing
#4: How many times is Hello world printed ?
int main() { fork(); fork(); printf("Hello world\n"); }
a) 1
b) 2
c) 4
d) 8
#5: What is the output of this C code?
void main() { int i, j; for(i=0,j=0;i<10,j<20;i++,j++){ printf("i=%d \t j=%d\n", i, j); } }
a) print i and j till 19
b) print i till 9 and j till 19
c) print i and j till 9
d) Runtime error
#6: What is the output of this C code?
#include<stdio.h> int main() { int x=1, y=0,z=5; int a=x&&y||++z; printf("%d",z++); }
a) 1
b) 5
c) 6
d) 7
#7: What is the output of this C code?
#include<stdio.h> int main() { int y = 2; int z = y +(y = 10); printf("%d\n", z); }
a) 2
b) 4
c) 20
d) Compile time error
#8: What is the output of this C code?
#define max(a) a int main() { int x = 1; switch (x) { case max(2): printf("yes\n"); case max(1): printf("no\n"); break; } }
a) yes
b) no
c) Runtime error
d) Compile time error
#9: Which of the following is not a standard C Library?
a) errno.h
b) setjmp.h
c) signal.h
d) retarg.h
a) errno.h
b) setjmp.h
c) signal.h
d) retarg.h
#10: What is the output of this C code?
#include<stdio.h> int main() { int x=35; printf("%d %d %d",x==35,x=50,x>40); return 0; }
a) 1 50 1
b) 0 50 0
c) Runtime error
d) Compile time error
I believe the answer for second question is the size of the pointer, it will never tell the size of reference object….
Explanations can even be given better
Question 2 answer is 4
eg:
sizeof(char) = 1 bytes
sizeof(*char_ptr) = 1 bytes
sizeof(char_ptr) = 4 bytes
Question 2 : answer is sizeof(fptr) is 2
Explanation: Because fptr is a pointer of float type variable
fptr=&a;
it means fptr stores an integer value(address can not be in floating point numbers) and to store an integer number 2 byte of space is required. Therefor size of fptr is of 2 bytes.
question no:6 will give 5 as an answer