(150 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''1. What is the output of this program?'''
+
<metadesc>C coding questions for understanding the concepts and features of C language</metadesc>
<syntaxhighlight lang="c">
+
__FORCETOC__
#include<stdio.h>
+
<!--
 +
==C Objective Questions==
 +
 
 +
<quiz display="simple">
 +
{'''What is the output of this program?'''
 +
<syntaxhighlight lang="c" >
 +
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 9: Line 15:
 
     ptr += 4;
 
     ptr += 4;
 
     printf("%s",ptr);
 
     printf("%s",ptr);
   
 
 
     return 0;
 
     return 0;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' How are you?
+
|type="()"
 +
/}
 +
-'''(a)''' How are you?
  
'''(b)''' are you?
+
+'''(b)''' are you?
  
'''(c)''' are
+
-'''(c)''' are
  
'''(d)''' No output
+
-'''(d)''' No output
 +
||ptr points to the start of the string and hence ptr+4 will point to the 5th char in the string. So, %s will print the characters from ptr+4 till \0 is encountered. ("How are you?" is a string literal and a \0 is implicitly added at the end of it by the compiler)
  
  
'''2. Which of the following will print the value 2 for the above code?'''
+
{''' Which of the following will print the value 2 for the above code?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 33: Line 41:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' printf("%d",*(((a+5)+2)+1));
+
|type="()"
 +
/}
 +
-'''(a)''' printf("%d",*(((a+5)+2)+1));
  
'''(b)''' printf("%d",***((a+5)+2)+1);
+
-'''(b)''' printf("%d",***((a+5)+2)+1);
  
'''(c)''' printf("%d",*(*(*(a+5)+2)+1));
+
+'''(c)''' printf("%d",*(*(*(a+5)+2)+1));
  
'''(d)''' None of these
+
-'''(d)''' None of these
 +
||* is the dereferencing operator and to access an element in a 3D array we have to dereference 3 times as in (c)
  
 
+
{''' What is the output of the following program?'''
'''3.  What is the output of the following program?'''
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 53: Line 63:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)'''25
+
|type="()"
 
+
/}
'''(b)''' 30
+
-'''(a)'''25
  
'''(c)''' 36
+
-'''(b)''' 30
  
'''(d)''' Undefined Behavior
+
-'''(c)''' 36
  
 +
+'''(d)''' Undefined Behavior
 +
||In C language if we modify a variable more than once between two sequence points, undefined behavior results (output can be anything on any compiler) and arithmetic operators doesn't form sequence points
  
'''4. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 84: Line 96:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 5
+
|type="()"
 
+
/}
'''(b)''' 4
+
+'''(a)''' 5
  
'''(c)''' 3
+
-'''(b)''' 4
  
'''(d)''' None of these
+
-'''(c)''' 3
  
 +
-'''(d)''' None of these
 +
||In switch, a jump happens to the matching case (or else to default) and all statements below that are executed unless there is a break.
  
'''5. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 106: Line 120:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 5 2
+
|type="()"
 +
/}
 +
-'''(a)''' 5 2
  
'''(b)''' 2 5
+
-'''(b)''' 2 5
  
'''(c)''' 7 7
+
-'''(c)''' 7 7
  
'''(d)''' 7 2
+
+'''(d)''' 7 2
 +
|| ^ does the XOR operation. a^b = 010 ^ 101 = 111 = 7. Now, b^a = 101 ^ 111 = 010 = 2
  
 
+
{'''What is the output of the following program?'''
'''6. What is the output of the following program?'''
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 129: Line 145:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 2 3 5 6
+
|type="()"
 +
/}
 +
+'''(a)''' 2 3 5 6
  
'''(b)''' 2 3 4 5
+
-'''(b)''' 2 3 4 5
  
'''(c)''' 4 5 0 0
+
-'''(c)''' 4 5 0 0
  
'''(d)''' none of the above
+
-'''(d)''' none of the above
  
  
'''7. What is the output of the following program? '''
+
{'''What is the output of the following program? '''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 157: Line 175:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' ab
+
|type="()"
 +
/}
 +
-'''(a)''' ab
  
'''(b)''' cd
+
-'''(b)''' cd
  
'''(c)''' ef
+
-'''(c)''' ef
  
'''(d)''' gh
+
+'''(d)''' gh
  
  
'''8. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdarg.h>
 
#include <stdarg.h>
Line 176: Line 196:
 
     va_list p;
 
     va_list p;
 
     k = 0;
 
     k = 0;
     j = 1;
+
     j = 0;
 
     va_start(p, n);
 
     va_start(p, n);
 
     for (; j < n; ++j)
 
     for (; j < n; ++j)
Line 189: Line 209:
 
int main()
 
int main()
 
{
 
{
     printf("%d\n", ripple(3, 5, 7));
+
     printf("%d\n", ripple(2, 5, 7));
  
 
     return 0;
 
     return 0;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 12
+
|type="()"
 +
/}
 +
+'''(a)''' 12
  
'''(b)''' 5
+
-'''(b)''' 5
  
'''(c)''' 7
+
-'''(c)''' 7
  
'''(d)''' 15
+
-'''(d)''' 15
  
  
'''9. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 224: Line 246:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 10
+
|type="()"
 +
/}
 +
-'''(a)''' 10
  
'''(b)''' 15
+
+'''(b)''' 15
  
'''(c)''' 6
+
-'''(c)''' 6
  
'''(d)''' 7
+
-'''(d)''' 7
  
  
'''10.  What is the output of the following program?'''
+
{''' What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 247: Line 271:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 5
+
|type="()"
 +
/}
 +
-'''(a)''' 5
  
'''(b)''' 10
+
-'''(b)''' 10
  
'''(c)''' Compile Error
+
+'''(c)''' Compile Error
  
'''(d)''' Garbage value
+
-'''(d)''' Garbage value
  
  
'''11. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
#define x 4+1
 
#define x 4+1
 
int main()
 
int main()
Line 269: Line 295:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 125
+
|type="()"
 +
/}
 +
-'''(a)''' 125
  
'''(b)''' 13
+
+'''(b)''' 13
  
'''(c)''' 17
+
-'''(c)''' 17
  
'''(d)''' None of above
+
-'''(d)''' None of above
  
  
'''12. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 290: Line 318:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 135
+
|type="()"
 +
/}
 +
-'''(a)''' 135
  
'''(b)''' +INF
+
-'''(b)''' +INF
  
'''(c)''' -121
+
+'''(c)''' -121
  
'''(d)''' -8
+
-'''(d)''' -8
  
  
'''13. What is the output of the following program?'''
+
{''' What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
  
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 317: Line 347:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' Equal
+
|type="()"
 +
/}
 +
-'''(a)''' Equal
  
'''(b)''' Greater  
+
-'''(b)''' Greater  
  
'''(c)''' Lesser  
+
-'''(c)''' Lesser  
  
'''(d)''' Compile Error
+
+'''(d)''' Compile Error
  
  
'''14. Consider the following code segment:'''
+
{'''Consider the following code segment:'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdlib.h>
 
#include <stdlib.h>
Line 349: Line 381:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
'''Which of these functions uses pointers incorrectly?'''
 
'''Which of these functions uses pointers incorrectly?'''
 +
|type="()"
 +
/}
 +
+'''(a)''' f3 only
  
'''(a)''' f3 only
+
-'''(b)''' f1 and f3
 
 
'''(b)''' f1 and f3
 
  
'''(c)''' f1 and f2
+
-'''(c)''' f1 and f2
  
'''(d)''' f1, f2, and f3
+
-'''(d)''' f1, f2, and f3
  
  
'''15. What is the output of the following program?'''
+
{''' What is the output of the following program? (Assume sizeof (int) is 4)'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 372: Line 405:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' i=4 j=4
+
|type="()"
 +
/}
 +
-'''(a)''' i=4 j=4
  
'''(b)''' i=3 j=4
+
+'''(b)''' i=3 j=4
  
'''(c)''' i=5 j=4
+
-'''(c)''' i=5 j=4
  
'''(d)''' the behavior is undefined
+
-'''(d)''' the behavior is undefined
  
  
'''16. What is the output of the following program?'''
+
{''' What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 415: Line 450:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 5 5 5 5
+
|type="()"
 +
/}
 +
+'''(a)''' 5 5 5 5
  
'''(b)''' 3 5 3 5
+
-'''(b)''' 3 5 3 5
  
'''(c)''' 5 3 3 5
+
-'''(c)''' 5 3 3 5
  
'''(d)''' none of the above
+
-'''(d)''' none of the above
  
  
'''17. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 447: Line 484:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)'''0 1 2 0
+
|type="()"
 +
/}
 +
+'''(a)'''0 1 2 0
  
'''(b)''' 0 1 2 1
+
-'''(b)''' 0 1 2 1
  
'''(c)''' 1 2 0 1
+
-'''(c)''' 1 2 0 1
  
'''(d)''' 0 2 1 1
+
-'''(d)''' 0 2 1 1
  
  
'''18. Consider the following code segment:'''
+
{''' Consider the following code segment:'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
typedef int (*test)(float*, float*);
 
typedef int (*test)(float*, float*);
Line 462: Line 501:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
'''What is the type of tmp?'''
 
'''What is the type of tmp?'''
 +
|type="()"
  
 +
/}
 +
-'''(a)''' function taking two pointer-to-float arguments and returning pointer to int
  
'''(a)''' function taking two pointer-to-float arguments and returning pointer to int
+
-'''(b)''' pointer to int
  
'''(b)''' pointer to int
+
+'''(c)''' pointer to function taking two pointer-to-float arguments and returning int
  
'''(c)''' pointer to function taking two pointer-to-float arguments and returning int
+
-'''(d)''' none of the above
  
'''(d)''' none of the above
 
  
 
+
{''' What is the output of the following program?'''
'''19. What is the output of the following program?'''
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 487: Line 527:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 5
+
|type="()"
 +
/}
 +
-'''(a)''' 5
  
'''(b)''' 6
+
-'''(b)''' 6
  
'''(c)''' 9
+
+'''(c)''' 9
  
'''(d)''' none of the above
+
-'''(d)''' none of the above
  
  
'''20. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 514: Line 556:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 3
+
|type="()"
 +
/}
 +
-'''(a)''' 3
  
'''(b)''' 5
+
+'''(b)''' 5
  
'''(c)''' 6
+
-'''(c)''' 6
  
'''(d)''' 7
+
-'''(d)''' 7
  
  
'''21. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 535: Line 579:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' Compile Error
+
|type="()"
 +
/}
 +
-'''(a)''' Compile Error
  
'''(b)''' 2 1
+
-'''(b)''' 2 1
  
'''(c)''' 2 5
+
+'''(c)''' 2 5
  
'''(d)''' none of the above
+
-'''(d)''' none of the above
  
  
'''22. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 564: Line 610:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 8
+
|type="()"
 +
/}
 +
-'''(a)''' 8
  
'''(b)''' 9
+
+'''(b)''' 9
  
'''(c)''' 7
+
-'''(c)''' 7
  
'''(d)''' none of the above
+
-'''(d)''' none of the above
  
  
'''23. Consider the following function:'''
+
{'''Consider the following function:'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
int foo(int x, int n)
 
int foo(int x, int n)
Line 587: Line 635:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
'''What function of x and n is computed by foo?'''
 
'''What function of x and n is computed by foo?'''
 +
|type="()"
 +
/}
 +
+'''(a)''' $x^n$
  
'''(a)''' x^n
+
-'''(b)''' $x×n$
  
'''(b)''' x×n
+
-'''(c)''' $n^x$
  
'''(c)''' nx
+
-'''(d)''' none of the above
  
'''(d)''' none of the above
+
{'''What is the output of the following program?'''
 
 
'''24. What is the output of the following program?'''
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
  
 
int main()
 
int main()
Line 620: Line 670:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 5
+
|type="()"
 +
/}
 +
-'''(a)''' 5
  
'''(b)''' 4
+
-'''(b)''' 4
  
'''(c)''' 3
+
+'''(c)''' 3
  
'''(d)''' 0
+
-'''(d)''' 0
  
  
'''25. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 643: Line 695:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' Hello
+
|type="()"
 +
/}
 +
-'''(a)''' Hello
  
'''(b)''' World
+
-'''(b)''' World
  
'''(c)''' Hello World
+
+'''(c)''' HelloWorld
  
'''(d)''' Compile Error
+
-'''(d)''' Compile Error
  
  
'''26. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 667: Line 721:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' Equal
+
|type="()"
 +
/}
 +
-'''(a)''' Equal
  
'''(b)''' Not Equal
+
-'''(b)''' Not Equal
  
'''(c)''' Compiler Dependent
+
-'''(c)''' Compiler Dependent
  
'''(d)''' Compile Error
+
+'''(d)''' Compile Error
  
  
'''27. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
void foo(char *);
 
void foo(char *);
  
Line 699: Line 755:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' Hello
+
|type="()"
 +
/}
 +
-'''(a)''' Hello
  
'''(b)''' Ifmmp
+
-'''(b)''' Ifmmp
  
'''(c)''' Compile Error
+
-'''(c)''' Compile Error
  
'''(d)''' Segmentation fault
+
+'''(d)''' Segmentation fault
  
  
'''28. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
#include<stdlib.h>
+
#include <stdlib.h>
 
int main()
 
int main()
 
{
 
{
     char s[] = "Opendays2012";
+
     char s[] = "Hello World";
 
     int i = 0;
 
     int i = 0;
 
     while(*(s++))
 
     while(*(s++))
Line 723: Line 781:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' Segmentation Fault
+
|type="()"
 +
/}
 +
-'''(a)''' Segmentation Fault
  
'''(b)''' Compile Error  
+
+'''(b)''' Compile Error  
  
'''(c)''' 12
+
-'''(c)''' 12
  
'''(d)''' 0
+
-'''(d)''' 0
  
  
'''29. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int a = 10;
 
int a = 10;
 
int main()
 
int main()
Line 753: Line 813:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' 1 2
+
|type="()"
 +
/}
 +
+'''(a)''' 1 2
  
'''(b)''' 1 1
+
-'''(b)''' 1 1
  
'''(c)''' 10 11
+
-'''(c)''' 10 11
  
'''(d)''' 10 10
+
-'''(d)''' 10 10
  
  
'''30. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
Line 774: Line 836:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''(a)''' Hello
+
|type="()"
 +
/}
 +
+'''(a)''' Hello
  
'''(b)''' Link error
+
-'''(b)''' Link error
  
'''(c)''' Segmentation fault
+
-'''(c)''' Segmentation fault
  
'''(d)''' Compiler error
+
-'''(d)''' Compiler error
 +
</quiz>
 +
-->
 +
==Some Questions==
 +
<quiz display="simple">
  
 
+
{'''Consider the following program:'''
'''31. Consider the following program:'''
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 795: Line 862:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
'''What is the output of this program?'''
 
'''What is the output of this program?'''
'''Ans''':
+
|type="{}"
 +
/}
 +
{ 1 }
  
  
'''32. Consider the following program:'''
+
{'''Consider the following program:'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 813: Line 882:
 
'''What is the output of this program?'''
 
'''What is the output of this program?'''
 
(You may ignore compiler warnings)
 
(You may ignore compiler warnings)
 +
|type="{}"
 +
/}
 +
{ 600 }
  
'''Ans''':
 
  
  
'''33. What is the output of the following program?'''
+
{''' What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
#include<stdlib.h>
+
#include <stdlib.h>
 
int* fun();
 
int* fun();
  
Line 837: Line 908:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
|type="{}"
 +
/}
 +
{ 10 }
  
'''Ans''':
 
  
  
'''34. What is the output of the following program?'''
+
{''' What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
 
     int *a = fun();
 
     int *a = fun();
     printf("%d",*a);
+
     printf("%d",a);
  
 
     return 0;
 
     return 0;
Line 857: Line 930:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
|type="{}"
 +
/}
 +
{ 10 }
  
'''Ans''':
 
  
  
'''35. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
#include<string.h>
+
#include <string.h>
  
 
int main()
 
int main()
 
{
 
{
 
     char string[] = "Hello";
 
     char string[] = "Hello";
     printf("%lu %lu",sizeof(string),strlen(string));
+
     printf("%zu %zu",sizeof(string),strlen(string));
  
 
     return 0;
 
     return 0;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
|type="{}"
 +
/}
 +
{ 6 5 }
  
'''Ans''':
 
  
  
'''36. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 892: Line 969:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
|type="{}"
 +
/}
 +
{ Yes }
  
'''Ans''':
 
  
  
'''37. What is the output of the following program?'''
+
{'''What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
#include<string.h>
+
#include <string.h>
 
void foo(char *);
 
void foo(char *);
  
Line 905: Line 984:
 
{
 
{
 
     char a[100] = {0};
 
     char a[100] = {0};
     printf("%lu %lu",sizeof(a),strlen(a));
+
     printf("%zu %zu",sizeof(a),strlen(a));
  
 
     return 0;
 
     return 0;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
|type="{}"
'''Ans''':
+
/}
 +
{ 100 0 }
  
  
  
 
 
'''38. What is the output of the following program?'''
+
{'''What is the output of the following program?''' (Assume input is 10)
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 927: Line 1,007:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
|type="{}"
 +
/}
 +
{ 1 }
  
  
'''Ans''':
+
{'''If the binary equivalent of 5.375 in normalised form is 01000000 10101100 00000000 00000000, what will be the output of the following program?'''
 
 
'''39. If the binary equivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000
 
0000 0000 0000, what will be the output of the following program?'''
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
#include<math.h>
+
#include <math.h>
  
 
int main()
 
int main()
Line 944: Line 1,024:
 
     p = (char*)&a;
 
     p = (char*)&a;
 
     for(i=0; i<2; i++)
 
     for(i=0; i<2; i++)
         printf("%02x ", (unsigned char)(p[i]^p[3-i]));
+
         printf("%x", (unsigned char)(p[i]^p[3-i]));
  
 
     return 0;
 
     return 0;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
'''Ans''':
+
|type="{}"
 +
/}
 +
{ 40ac }
  
  
'''40. What is the output of the following program?'''
+
{''' What is the output of the following program?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 964: Line 1,046:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
|type="{}"
 +
/}
 +
{ Abc }
  
'''Ans''':
+
</quiz>
+
==Questions from Datatypes==
 +
{{:C_Questions_on_Datatypes}}
 +
 
 +
 
 +
==Questions from Pointers==
 +
{{:C_Questions_on_Pointers}}
 +
 
 +
 
 +
==Some codes==
  
'''41. What is the following function doing?'''
+
'''1. What is the following function doing?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
int foo(int n)
 
int foo(int n)
Line 985: Line 1,078:
  
  
'''42. What is the following function doing?'''
+
'''2. What is the following function doing?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
int foo(int a, int b)
 
int foo(int a, int b)
Line 1,004: Line 1,097:
  
  
'''43. What is the following function doing?'''
+
'''3. What is the following function doing?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
int foo( int a, int b)
 
int foo( int a, int b)
Line 1,017: Line 1,110:
  
  
'''44. What is the following function doing?'''
+
'''4. What is the following function doing?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
unsigned fun(unsigned a, unsigned b)
 
unsigned fun(unsigned a, unsigned b)
Line 1,041: Line 1,134:
  
  
'''45. What is the following function doing?'''
+
'''5. What is the following function doing?'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
unsigned fun(unsigned int a)
 
unsigned fun(unsigned int a)
Line 1,070: Line 1,163:
  
 
 
'''46. Write the code to dynamically allocate a 2-D array of size m x n.'''
+
'''6. Write the code to dynamically allocate a 2-D array of size m x n.'''
  
 
'''Ans''':
 
'''Ans''':
Line 1,077: Line 1,170:
  
  
'''47. Declare a pointer to a function accepting an integer and returning void.'''
+
'''7. Declare a pointer to a function accepting an integer and returning void.'''
  
 
'''Ans''':
 
'''Ans''':
Line 1,083: Line 1,176:
  
  
'''48. Write the condition so that the below code outputs $``$Hello World<math>$''"$</math>.'''
+
'''8. Write the condition so that the below code outputs <math>\unicode{x201C}</math>Hello World<math>\unicode{x201D}</math>.'''
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include<stdio.h>
 
#include<stdio.h>
Line 1,103: Line 1,196:
  
  
'''49. Write a one line code to check if a number is a power of 2.'''
+
'''9. Write a one line code to check if a number is a power of 2.'''
  
 
'''Ans''':
 
'''Ans''':
Line 1,109: Line 1,202:
  
  
'''50. Write a one line code to invert the last four bits of an integer.'''
+
'''10. Write a one line code to invert the last four bits of an integer.'''
  
 
'''Ans''':
 
'''Ans''':
Line 1,115: Line 1,208:
  
  
[[Category:Algorithms & Data Structures]]
+
{{Template:FBD}}
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
[[Category:Programming & Data Structures]]
 
[[Category:Code]]
 
[[Category:Code]]

Latest revision as of 13:35, 27 May 2015


Some Questions

<quiz display="simple">

{Consider the following program: <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int a[10][20][30]={0};
   printf("%ld",&a+1 - &a);
   return 0;

} </syntaxhighlight> What is the output of this program? |type="{}" /} { 1 }


{Consider the following program: <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int a[10][20][30] = {0};
   int *b = a;
   int *c = a+1;
   printf("%ld", c-b);
   return 0;

} </syntaxhighlight> What is the output of this program? (You may ignore compiler warnings) |type="{}" /} { 600 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <stdlib.h>

int* fun();

int main() {

   int *a = fun();
   printf("%d",*a);
   return 0;

} int* fun() {

   int *a =(int*) malloc(sizeof(int));
   *a = 10;
   return a;

} </syntaxhighlight> |type="{}" /} { 10 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int *a = fun();
   printf("%d",a);
   return 0;

} int fun() {

   int a = 10;
   return a;

} </syntaxhighlight> |type="{}" /} { 10 }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

int main() {

   char string[] = "Hello";
   printf("%zu %zu",sizeof(string),strlen(string));
   return 0;

} </syntaxhighlight> |type="{}" /} { 6 5 }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   float a = 0.5;
   if(a == 0.5)
       printf("Yes");
   else
       printf("No");
   return 0;

} </syntaxhighlight> |type="{}" /} { Yes }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

void foo(char *);

int main() {

   char a[100] = {0};
   printf("%zu %zu",sizeof(a),strlen(a));
   return 0;

} </syntaxhighlight> |type="{}" /} { 100 0 }



{What is the output of the following program? (Assume input is 10) <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int a;
   printf("%d",scanf("%d",&a));
   return 0;

} </syntaxhighlight> |type="{}" /} { 1 }


{If the binary equivalent of 5.375 in normalised form is 01000000 10101100 00000000 00000000, what will be the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <math.h>

int main() {

   float a=5.375;
   char *p;
   int i;
   p = (char*)&a;
   for(i=0; i<2; i++)
       printf("%x", (unsigned char)(p[i]^p[3-i]));
   return 0;

} </syntaxhighlight> |type="{}" /} { 40ac }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   char str[] = {'a','b','c','\0'};
   str[0] -= 32;
   printf("%s",str);
   return 0;

} </syntaxhighlight> |type="{}" /} { Abc }

</quiz>

Questions from Datatypes

<quiz display="simple"> {Consider an implementation where int is 4 bytes and long int is 8 bytes. Which of the following initializations are correct? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  long int a = 0x7fffffff * 0x7ffffff;
  long int b = 0x7ffffffff * 0x7ffffff;
  long int c = 0x7fffffff * 0x7fffffff;
  long int d = 0x7fffffff * 0x7fffffffl;
  printf("a = %ld, b = %ld, c = %ld, d = %ld\n", a, b, c, d);
  return 0;

} </syntaxhighlight> |type="()" /} -All are correct -a, c, d +b, d -a, d ||a and c choices cause integer overflow. Even though long int can hold 8 bytes as given in the question, the operands are of 4 bytes only and hence the result is also 4 bytes.
In b choice, 0x7ffffffff is taking more than 4 bytes and hence is considered a long int. So, the next operand is implicitly typecasted to long int and the result is also calculated as long int. Hence there'll be no overflow.
In d choice, by adding l at the end, we force the compiler to use long int operand and hence the operations will be done using 8 byte operands and there will be no overflow.

{Consider an implementation where int is 4 bytes and long int is 8 bytes. What will be the output of the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  int i = 0;
  size_t a = sizeof i, b = sizeof (long);
  printf("a = %zd, b = %zd\n", a, b); //If %zd is given the compiler will automatically give it the correct type whether short, long or normal. This is useful for special data types like size_t whose size is implementation specific
  return 0;

} </syntaxhighlight> |type="()" /} -Compile error -Runtime error -a = 4, b = 4 +a = 4, b = 8 ||sizeof is an operator and hence we don't need a parentheses for giving variables to sizeof. So, sizeof i will return 4 which is the size of int and sizeof(long) will return 8


{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  unsigned int a = 5;
  if(a > -1)
     printf("5 is > -1\n");
  return 0;

} </syntaxhighlight> |type="()" /}

-5 is > -1 +No output -Compile Error -Runtime Error ||When an operation involves different data types the lower ranked one is promoted to higher ranked one. So, when we compare a signed int with an unsigned int, the signed int will be promoted to unsigned (unsigned has higher rank than signed).
a > -1
will turn to
00...101 > 11...111
and will evaluate to false

If a was declared as signed, then the if condition would have behaved as expected.
(Ideally an unsigned integer would never be compared with a negative number in real world and so do in programs)

{What will be printed by the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

int main() {

  char buff[255] = "abc\

pee";

  printf("%s", buff);
  return 0;

} </syntaxhighlight> |type="()" /}

+
abcpee -
abc
pee -
Compile error -
abc
ee

||\ followed by a newline will make the compiler escape both the characters. So, this is used to write long lines of code across multiple lines for enhancing readability. So,
"abc\
pee";
is equivalent to
"abcpee";

{What will be printed by the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include<string.h>

int main() {

   char buff[] = "abc" "hello";
   printf("%zd\n", strlen(buff));
   return 0;

} </syntaxhighlight> |type="()" /}

+8 -9 -10 -Compile Error ||Compiler will append any continuous string literals given inside "" and make a single string literal ignoring the space(s) between them. So, "abc" "hello" will become "abchello" and its length is 8


{How can you print the following sentence exactly as it is by changing the assignment to buff? "Hello\\" "World\\" <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

int main() {

   char buff[255] = "\0";
   printf("%s", buff);
   return 0;

} </syntaxhighlight> |type="()" /}

-char buff[255] = "\"Hello/\/\\" \"World/\/\\""; +char buff[255] = "\"Hello\\\\\" \"World\\\\\""; -char buff[255] = ""Hello\\" "World\\""; -char buff[255] = ""Hello\\\\\" "World\\\\""; ||To put " in a string we have to escape it with \. i.e., use \" instead of ". For '\' also we do the same \\ instead of \

{What will be the output of the following code? (Assume a 64 bit machine/compiler) <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   char *p = "Hello World";
   char q[] = "Hello World";
   printf("%zd %zd", sizeof p, sizeof *p);
   printf("\n");
   printf("%zd %zd", sizeof q, sizeof *q);
   return 0;

} </syntaxhighlight> |type="()" /}

+
8 1
12 1 -
12 1
12 1 -
Compile error -
11 1
12 1 ||p is a char pointer and all pointers on 64 bit platform takes 64bits -> 8 bytes
*p is a char and size of char is 1 byte
q is an array and sizeof array is the no. of elements in the array X sizeof an element
= 12 (including \0 which is added by compiler at the end of all string literals) * 1   = 12 bytes ||*q is a char and sizeof char is 1 byte

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  {
     char a = 5;
     int b = 5;
     if(a == b)          
        printf("char and int compared equal\n");    }
  {
     int a = 5;
     long int b = 5;

if(a == b)

        printf("int and long compared equal\n");
  }
  {
     float a = 5.0;
     double b = 5.0;
     if(a == b)
        printf("float and double compared equal\n");
  }
  {
     float a = 5.2;
     double b = 5.2;
     if(a == b)
        printf("float and double again compared equal\n");
  }
  {
     float a = 5.2;
     if(a == 5.2)
        printf("float compared equal with constant\n");
  }
  {
     double a = 5.2;
     if(a == 5.2)
        printf("double compared equal with constant\n");
  }
  return 0;

} </syntaxhighlight> |type="()" /} +
char and int compared equal
int and long compare equal
float and double compared equal
double compared equal with constant

-
char and int compared equal
int and long compare equal
double compared equal with constant

-
char and int compared equal
int and long compare equal
float and double again compared equal
double compared equal with constant

-
char and int compared equal
int and long compare equal
float and double compared equal

||
When we compare an int and a char, the char will be promoted to int by adding 0s to the left. So, signed integers less than 128 (the maximum value representable by a signed char is 127) will compare equal with char.

||When we compare a long and an int, int is promoted to long by adding 0s to the left. So, any long number than can be represented by an int will compare equal with int.


||Like above , float will be promoted to double. If the float number is exactly representable (without any approximation) within a float, then it'll compare equal with its double counterpart.

||Here, 5.2 cannot be exactly representable using a float. Hence, its double version will have more accuracy and won't compare equal with float.

||By default all real values are taken as double. If we want to take as float we have to use

if(a == 5.2f)
instead of

if(a == 5.2)

</quiz>


Questions from Pointers

<quiz display="simple"> {What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 5;
 int* p = &a;
 printf("%d", ++*p);

} </syntaxhighlight> |type="{}" /} { 6 } ||p is pointing to the address of a. *p will return the content of a which is 5 and ++ will increment it to 6.


{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 char a[] = "Hello World";
 char* p = &a;
 printf("%s", p+2 );

} </syntaxhighlight> |type="()" /} -Compiler Error -World +llo World -Runtime Error

||Since p is a char pointer p+2 will add 2 to p (since sizeof(char) is 1). So, p+2 will be pointing to the string "llo World".

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a;
 int* p = &a;
 printf("%zu", sizeof( *(char*)p  ));

} </syntaxhighlight> |type="()" /}

+1 -2 -4 -Compile Error

||p is typecasted to char pointer and then dereferenced. So, returned type will be char and sizeof(char) is 1.

{Is the following code legal? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 1;
 if((char*)  &a)
 {
   printf("My machine is little endian");
 }
 else
 {
    printf("My machine is big endian\n");
 }

} </syntaxhighlight> |type="()" /} +Yes -No ||On a little endian machine the lower address will contain the least significant byte. Suppose a is stored at address 1000 and contains 1, then character at 1000 will be 1, if the machine is little endian.

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int *a = (int*) 1;
 printf("%d", a);

} </syntaxhighlight> |type="()" /} -Garbage value +1 -0 -Compile error -Segmentation fault ||Assigning int values to pointer variable is possible. Only when we dereference the variable using *, we get a segmentation fault.

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 1, *p, **pp;
 p = &a;
 pp = p;
 printf("%d", **pp);

} </syntaxhighlight> |type="()" /} -Garbage value -1 -Compile error +Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a, but **p will use 1 as an address and tries to access the memory location 1, giving segmentation fault.

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 1, *p, **pp;
 p = &a;
 pp = p;
 printf("%d", *pp);

} </syntaxhighlight> |type="()" /} +1 -Garbage value -Compile error -Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a.


{Assuming a little endian machine, what will be the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

fun(int a) {

 char *arr[] = {"0000", "0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
 unsigned char* p = (unsigned char*) &a ;
 p+=3;
 int i;
 for(i = 0; i < sizeof a; i++)
 {
   int d = (*p)>>4; 
   printf("%s", arr[d]);
   d = (*p) & 0xf;
   printf("%s ", arr[d]);
   p--;
 }

}

int main() {

int a;
scanf("%d", &a);
fun(a);

} </syntaxhighlight> |type="()" /} +Print the binary of the input number -Compile error -Runtime error -Compiler dependent output ||The code is printing the binary equivalent of the input number. Suppose a is stored starting from address 1000. Since, we assume a little endian machine, the LSB of a will be stored at address 1000 and MSB will be stored at address 1003. So, we make a char pointer to 1003 and take out the MSB. Then using shift operator we get the most significant 4 bits from it and then the lest significant 4 bits. We repeat the same for the other three bytes of a. </quiz>


Some codes

1. What is the following function doing? <syntaxhighlight lang="c"> int foo(int n) {

   int sum = 0;
   while(n > 0)
   {
       n = n & n-1;
       sum++;
   }
   return sum;

} </syntaxhighlight> Ans:


2. What is the following function doing? <syntaxhighlight lang="c"> int foo(int a, int b) {

   int c = a, d = b;
   while(a != b)
   {
       if(a < b)
           a = a+c;
       else
           b = b+d;
   }
   return a;

} </syntaxhighlight> Ans:


3. What is the following function doing? <syntaxhighlight lang="c"> int foo( int a, int b) {

   int c = a-b;
   c = c&(0x80000000);
   return (!c)*a +(!!c)*b;

} </syntaxhighlight> Ans:


4. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned a, unsigned b) {

   int i;
   unsigned  j = 0;
   for(i = 0; i < 32; i++)
   {
       j <<= 1;
       j += !!(a & 0x80000000);
       a <<= 1;
       if(j >=b)
       {
           j -= b;
           a++;
       }
   }
   return a;

} </syntaxhighlight> Ans:


5. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned int a) {

   unsigned int i, x = 0, y = 0, z = 0;
   for(i = 0; i < 16; i++)
   {
      	y <<= 2;
      	y += !!(a & 0x80000000) << 1;
      	y += !!(a & 0x40000000);
      	a <<= 2;
      	x = x + (x&1);
      	x <<= 1;
      	z <<= 1;
      	if(x + 1 <= y)
      	{
           x++;
           z++;

y-=x;

       }
   }
   return z;

} </syntaxhighlight> Ans:



6. Write the code to dynamically allocate a 2-D array of size m x n.

Ans:



7. Declare a pointer to a function accepting an integer and returning void.

Ans:


8. Write the condition so that the below code outputs <math>\unicode{x201C}</math>Hello World<math>\unicode{x201D}</math>. <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   if(<condition>)
   {

printf("Hello ");

   }
   else
   {

printf("World\n");

   }
   return 0;

} </syntaxhighlight> Ans:


9. Write a one line code to check if a number is a power of 2.

Ans:


10. Write a one line code to invert the last four bits of an integer.

Ans:





blog comments powered by Disqus

1. What is the output of this program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   char *ptr;
   char string[] = "How are you?";
   ptr = string;
   ptr += 4;
   printf("%s",ptr);
   
   return 0;

} </syntaxhighlight> (a) How are you?

(b) are you?

(c) are

(d) No output


2. Which of the following will print the value 2 for the above code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a[10][20][30] = {0};
   a[5][2][1] = 2;
   return 0;

} </syntaxhighlight> (a) printf("%d",*(((a+5)+2)+1));

(b) printf("%d",***((a+5)+2)+1);

(c) printf("%d",*(*(*(a+5)+2)+1));

(d) None of these


3. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a = 5;
   int b = ++a * a++;
   printf("%d ",b);
   return 0;

} </syntaxhighlight> (a)25

(b) 30

(c) 36

(d) Undefined Behavior


4. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a = 5;
   switch(a)
   {
       default:
       a = 4;
       case 6:
           a--;
       case 5:
           a = a+1;
       case 1:
           a = a-1;
   }
   printf("%d \n",a);
   return 0;

} </syntaxhighlight> (a) 5

(b) 4

(c) 3

(d) None of these


5. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a = 2,b = 5;
   a = a^b;
   b = b^a;
   printf("%d %d",a,b);
   return 0;

} </syntaxhighlight> (a) 5 2

(b) 2 5

(c) 7 7

(d) 7 2


6. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int a[][3] = {1, 2, 3, 4, 5, 6};
   int (*ptr)[3] = a;
   printf("%d %d ", (*ptr)[1], (*ptr)[2]);
   ++ptr;
   printf("%d %d\n", (*ptr)[1], (*ptr)[2]);
   return 0;

} </syntaxhighlight> (a) 2 3 5 6

(b) 2 3 4 5

(c) 4 5 0 0

(d) none of the above


7. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void f(char**);

int main() {

   char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };
   f(argv);
   return 0;

} void f(char **p) {

   char *t;
   t = (p += sizeof(int))[-1];
   printf("%s\n", t);

} </syntaxhighlight> (a) ab

(b) cd

(c) ef

(d) gh


8. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdarg.h>
  2. include <stdio.h>

int ripple(int n, ...) {

   int i, j, k;
   va_list p;
   k = 0;
   j = 1;
   va_start(p, n);
   for (; j < n; ++j)
   {
       i = va_arg(p, int);
       k += i;
   }
   va_end(p);
   
   return k;

} int main() {

   printf("%d\n", ripple(3, 5, 7));
   return 0;

} </syntaxhighlight> (a) 12

(b) 5

(c) 7

(d) 15


9. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int counter(int i) {

   static int count = 0;
   count = count + i;
   
   return count;

}

int main() {

   int i, j;
   for (i = 0; i <= 5; i++)
       j = counter(i);
   printf("%d\n", j);
   return 0;

} </syntaxhighlight> (a) 10

(b) 15

(c) 6

(d) 7


10. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   const int x = 5;
   const int *ptrx;
   ptrx = &x;
   *ptrx = 10;
   printf("%d\n", x);
   return 0;

} </syntaxhighlight> (a) 5

(b) 10

(c) Compile Error

(d) Garbage value


11. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. define x 4+1

int main() {

   int i;
   i = x*x*x;
   printf("%d",i);
   return 0;

} </syntaxhighlight> (a) 125

(b) 13

(c) 17

(d) None of above


12. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   char c=125;
   c=c+10;
   printf("%d",c);
   return 0;

} </syntaxhighlight> (a) 135

(b) +INF

(c) -121

(d) -8


13. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int i=10;
   static int x=i;
   if(x==i)
       printf("Equal");
   else if(x>i)
       printf("Greater");
   else
       printf("Lesser");
   return 0;

} </syntaxhighlight> (a) Equal

(b) Greater

(c) Lesser

(d) Compile Error


14. Consider the following code segment: <syntaxhighlight lang="c">

  1. include <stdlib.h>

int *f1() {

   int x = 10;
   return &x;

} int *f2() {

   int *ptr;
   *ptr = 10;
   return ptr;

} int *f3() {

   int *ptr;
   ptr = (int*) malloc(sizeof (*ptr));
   return ptr;

} </syntaxhighlight> Which of these functions uses pointers incorrectly?

(a) f3 only

(b) f1 and f3

(c) f1 and f2

(d) f1, f2, and f3


15. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int i = 3;
   int j;
   j = sizeof(++i + ++i);
   printf("i=%d j=%d\n", i, j);
   return 0;

} </syntaxhighlight> (a) i=4 j=4

(b) i=3 j=4

(c) i=5 j=4

(d) the behavior is undefined


16. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void f1(int*, int); void f2(int*, int); void (*p[2])(int*, int);

int main() {

   int a = 3;
   int b = 5;
   p[0] = f1;
   p[1] = f2;
   p[0](&a, b);
   printf("%d %d ", a, b);
   p[1](&a, b);
   printf("%d %d\n", a, b);
   return 0;

}

void f1(int *p, int q) {

   int tmp = *p;
   *p = q;
   q = tmp;

} void f2(int *p, int q) {

   int tmp = *p;
   *p = q;
   q = tmp;

} </syntaxhighlight> (a) 5 5 5 5

(b) 3 5 3 5

(c) 5 3 3 5

(d) none of the above


17. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void e(int);

int main() {

   int a = 3; 
   e(a);
   putchar('\n');
   return 0;

} void e(int n) {

   if (n > 0)
   {
       e(--n);
       printf("%d ", n);
       e(--n);
   }

} </syntaxhighlight> (a)0 1 2 0

(b) 0 1 2 1

(c) 1 2 0 1

(d) 0 2 1 1


18. Consider the following code segment: <syntaxhighlight lang="c"> typedef int (*test)(float*, float*); test tmp; </syntaxhighlight> What is the type of tmp?


(a) function taking two pointer-to-float arguments and returning pointer to int

(b) pointer to int

(c) pointer to function taking two pointer-to-float arguments and returning int

(d) none of the above


19. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   char p;
   char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
   p = (buf + 1)[5];
   printf("%d\n", p);
   return 0;

} </syntaxhighlight> (a) 5

(b) 6

(c) 9

(d) none of the above


20. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   struct node
   {
       int a;
       int b;
       int c;
   };
   struct node s = { 3, 5, 6 };
   struct node *pt = &s;
   printf("%d\n", *((int*)pt+1));
   return 0;

} </syntaxhighlight> (a) 3

(b) 5

(c) 6

(d) 7


21. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main(void) {

   char a[5] = { 1, 2, 3, 4, 5 };
   char *ptr = (char*)(&a + 1);
   printf("%d %d\n", *(a + 1), *(ptr - 1));
   return 0;

} </syntaxhighlight> (a) Compile Error

(b) 2 1

(c) 2 5

(d) none of the above


22. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

void foo(int[][3]);

int main(void) {

   int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
   foo(a);
   printf("%d\n", a[2][1]);
   return 0;

}

void foo(int b[][3]) {

   ++b;
   b[1][1] = 9;

} </syntaxhighlight> (a) 8

(b) 9

(c) 7

(d) none of the above


23. Consider the following function: <syntaxhighlight lang="c"> int foo(int x, int n) {

   int val = 1;
   if (n > 0)
   {
       if (n % 2 == 1)
       val *= x;
       val *= foo(x * x, n / 2);
   }
   return val;

} </syntaxhighlight> What function of x and n is computed by foo?

(a) x^n

(b) x×n

(c) nx

(d) none of the above

24. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a = 0;
   switch(a)
   {
       default:
           a = 4;
       case 6:
           a--;
       case 5:
           a = a+1;
       case 1:
           a = a-1;
   }
   printf("%d \n",a);
   return 0;

} </syntaxhighlight> (a) 5

(b) 4

(c) 3

(d) 0


25. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a = 2;
   if(a == (1,2))
       printf("Hello");
   if(a == 1,2)
       printf("World");
   return 0;

} </syntaxhighlight> (a) Hello

(b) World

(c) Hello World

(d) Compile Error


26. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a = 1,2;
   int b = (1,2);
   if(a == b)
       printf("Equal");
   else
       printf("Not Equal");
   return 0;

} </syntaxhighlight> (a) Equal

(b) Not Equal

(c) Compiler Dependent

(d) Compile Error


27. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

void foo(char *);

int main() {

   char *string = "Hello";
   foo(string);
   printf("%s",string);
   return 0;

}

void foo(char *a) {

   while(*a)
   {
       *a += 1;
       a++;
   }

} </syntaxhighlight> (a) Hello

(b) Ifmmp

(c) Compile Error

(d) Segmentation fault


28. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<stdlib.h>

int main() {

   char s[] = "Opendays2012";
   int i = 0;
   while(*(s++))
       i++;
   printf("%d",i);
   return 0;

} </syntaxhighlight> (a) Segmentation Fault

(b) Compile Error

(c) 12

(d) 0


29. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int a = 10; int main() {

   fun();
   fun();
   return 0;

}

int fun() {

   static int a = 1;
   printf("%d ",a);
   a++;
   return 0;

} </syntaxhighlight> (a) 1 2

(b) 1 1

(c) 10 11

(d) 10 10


30. What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. define crypt(s,t,u,m,p,e,d) m##s##u##t
  3. define begin crypt(a,n,i,m,a,t,e)

int begin() {

   printf("Hello\n");
   return 0;

} </syntaxhighlight> (a) Hello

(b) Link error

(c) Segmentation fault

(d) Compiler error


31. Consider the following program: <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a[10][20][30]={0};
   printf("%ld",&a+1 - &a);
   return 0;

} </syntaxhighlight> What is the output of this program? Ans:


32. Consider the following program: <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a[10][20][30] = {0};
   int *b = a;
   int *c = a+1;
   printf("%ld", c-b);
   return 0;

} </syntaxhighlight> What is the output of this program? (You may ignore compiler warnings)

Ans:


33. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<stdlib.h>

int* fun();

int main() {

   int *a = fun();
   printf("%d",*a);
   return 0;

} int* fun() {

   int *a =(int*) malloc(sizeof(int));
   *a = 10;
   return a;

} </syntaxhighlight>

Ans:


34. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int *a = fun();
   printf("%d",*a);
   return 0;

} int fun() {

   int a = 10;
   return a;

} </syntaxhighlight>

Ans:


35. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<string.h>

int main() {

   char string[] = "Hello";
   printf("%lu %lu",sizeof(string),strlen(string));
   return 0;

} </syntaxhighlight>

Ans:


36. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   float a = 0.5;
   if(a == 0.5)
       printf("Yes");
   else
       printf("No");
   return 0;

} </syntaxhighlight>

Ans:


37. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<string.h>

void foo(char *);

int main() {

   char a[100] = {0};
   printf("%lu %lu",sizeof(a),strlen(a));
   return 0;

} </syntaxhighlight>

Ans:



38. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   int a;
   printf("%d",scanf("%d",&a));
   return 0;

} </syntaxhighlight>


Ans:

39. If the binary equivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what will be the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>
  2. include<math.h>

int main() {

   float a=5.375;
   char *p;
   int i;
   p = (char*)&a;
   for(i=0; i<2; i++)
       printf("%02x ", (unsigned char)(p[i]^p[3-i]));
   return 0;

} </syntaxhighlight> Ans:


40. What is the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   char str[] = {'a','b','c','\0'};
   str[0] -= 32;
   printf("%s",str);
   return 0;

} </syntaxhighlight>

Ans:


41. What is the following function doing? <syntaxhighlight lang="c"> int foo(int n) {

   int sum = 0;
   while(n > 0)
   {
       n = n & n-1;
       sum++;
   }
   return sum;

} </syntaxhighlight> Ans:


42. What is the following function doing? <syntaxhighlight lang="c"> int foo(int a, int b) {

   int c = a, d = b;
   while(a != b)
   {
       if(a < b)
           a = a+c;
       else
           b = b+d;
   }
   return a;

} </syntaxhighlight> Ans:


43. What is the following function doing? <syntaxhighlight lang="c"> int foo( int a, int b) {

   int c = a-b;
   c = c&(0x80000000);
   return (!c)*a +(!!c)*b;

} </syntaxhighlight> Ans:


44. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned a, unsigned b) {

   int i;
   unsigned  j = 0;
   for(i = 0; i < 32; i++)
   {
       j <<= 1;
       j += !!(a & 0x80000000);
       a <<= 1;
       if(j >=b)
       {
           j -= b;
           a++;
       }
   }
   return a;

} </syntaxhighlight> Ans:


45. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned int a) {

   unsigned int i, x = 0, y = 0, z = 0;
   for(i = 0; i < 16; i++)
   {
      	y <<= 2;
      	y += !!(a & 0x80000000) << 1;
      	y += !!(a & 0x40000000);
      	a <<= 2;
      	x = x + (x&1);
      	x <<= 1;
      	z <<= 1;
      	if(x + 1 <= y)
      	{
           x++;
           z++;

y-=x;

       }
   }
   return z;

} </syntaxhighlight> Ans:



46. Write the code to dynamically allocate a 2-D array of size m x n.

Ans:



47. Declare a pointer to a function accepting an integer and returning void.

Ans:


48. Write the condition so that the below code outputs $``$Hello World<math>$"$</math>. <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   if(<condition>)
   {

printf("Hello ");

   }
   else
   {

printf("World\n");

   }
   return 0;

} </syntaxhighlight> Ans:


49. Write a one line code to check if a number is a power of 2.

Ans:


50. Write a one line code to invert the last four bits of an integer.

Ans: