(25 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
<metadesc>C coding questions for understanding the concepts and features of C language</metadesc>
 
<metadesc>C coding questions for understanding the concepts and features of C language</metadesc>
 +
__FORCETOC__
 +
<!--
 +
==C Objective Questions==
 +
 
<quiz display="simple">
 
<quiz display="simple">
 
{'''What is the output of this program?'''
 
{'''What is the output of this program?'''
 
<syntaxhighlight lang="c" >
 
<syntaxhighlight lang="c" >
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 23: Line 27:
  
 
-'''(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)
  
  
 
{''' 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 46: Line 50:
  
 
-'''(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?'''
 
{''' 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 68: Line 72:
  
 
+'''(d)''' Undefined Behavior
 
+'''(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
  
 
{'''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 101: Line 105:
  
 
-'''(d)''' None of these
 
-'''(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.
  
 
{'''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 125: Line 129:
  
 
+'''(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?'''
 
{'''What is the output of the following program?'''
Line 255: Line 259:
 
{''' 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 280: Line 284:
 
{'''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 304: Line 308:
 
{'''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 328: Line 332:
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
  
#include<stdio.h>
+
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 388: Line 392:
  
  
{''' 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 509: Line 513:
  
  
{'' 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 645: Line 649:
 
{'''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 679: Line 683:
 
{'''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 704: Line 708:
 
{'''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 730: Line 734:
 
{'''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 764: Line 768:
 
{'''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()
 
{
 
{
Line 790: Line 794:
 
{'''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 841: Line 845:
  
 
-'''(d)''' Compiler error
 
-'''(d)''' Compiler error
 
+
</quiz>
 +
-->
 +
==Some Questions==
 +
<quiz display="simple">
  
 
{'''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 862: Line 869:
 
{'''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 883: Line 890:
 
{''' 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 909: Line 916:
 
{''' 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 931: Line 938:
 
{'''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()
Line 950: Line 957:
 
{'''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 970: Line 977:
 
{'''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 991: Line 998:
 
{'''What is the output of the following program?''' (Assume input is 10)
 
{'''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 1,005: Line 1,012:
  
  
{'''If the binary equivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000
+
{'''If the binary equivalent of 5.375 in normalised form is 01000000 10101100 00000000 00000000, what will be the output of the following program?'''
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 1,030: Line 1,036:
 
{''' 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 1,045: Line 1,051:
  
 
</quiz>
 
</quiz>
 +
==Questions from Datatypes==
 +
{{:C_Questions_on_Datatypes}}
 +
 +
 +
==Questions from Pointers==
 +
{{:C_Questions_on_Pointers}}
  
==Data types==
 
<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">
 
#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="()"
 
/}
 
  
</quiz>
 
 
==Some codes==
 
==Some codes==
  
Line 1,221: Line 1,215:
  
  
[[Category:Algorithms & Data Structures]]
+
[[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

<quiz display="simple"> {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> |type="()" /} -(a) How are you?

+(b) are you?

-(c) are

-(d) No output


{ 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> |type="()" /} -(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


{ 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> |type="()" /} -(a)25

-(b) 30

-(c) 36

+(d) Undefined Behavior


{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> |type="()" /} +(a) 5

-(b) 4

-(c) 3

-(d) None of these


{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> |type="()" /} -(a) 5 2

-(b) 2 5

-(c) 7 7

+(d) 7 2


{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> |type="()" /} +(a) 2 3 5 6

-(b) 2 3 4 5

-(c) 4 5 0 0

-(d) none of the above


{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> |type="()" /} -(a) ab

-(b) cd

-(c) ef

+(d) gh


{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 = 0;
   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(2, 5, 7));
   return 0;

} </syntaxhighlight> |type="()" /} +(a) 12

-(b) 5

-(c) 7

-(d) 15


{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> |type="()" /} -(a) 10

+(b) 15

-(c) 6

-(d) 7


{ 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> |type="()" /} -(a) 5

-(b) 10

+(c) Compile Error

-(d) Garbage value


{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> |type="()" /} -(a) 125

+(b) 13

-(c) 17

-(d) None of above


{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> |type="()" /} -(a) 135

-(b) +INF

+(c) -121

-(d) -8


{ 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> |type="()" /} -(a) Equal

-(b) Greater

-(c) Lesser

+(d) Compile Error


{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? |type="()" /} +(a) f3 only

-(b) f1 and f3

-(c) f1 and f2

-(d) f1, f2, and f3


{ 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> |type="()" /} -(a) i=4 j=4

+(b) i=3 j=4

-(c) i=5 j=4

-(d) the behavior is undefined


{ 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> |type="()" /} +(a) 5 5 5 5

-(b) 3 5 3 5

-(c) 5 3 3 5

-(d) none of the above


{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> |type="()" /} +(a)0 1 2 0

-(b) 0 1 2 1

-(c) 1 2 0 1

-(d) 0 2 1 1


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

/} -(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


{ 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> |type="()" /} -(a) 5

-(b) 6

+(c) 9

-(d) none of the above


{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> |type="()" /} -(a) 3

+(b) 5

-(c) 6

-(d) 7


{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> |type="()" /} -(a) Compile Error

-(b) 2 1

+(c) 2 5

-(d) none of the above


{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> |type="()" /} -(a) 8

+(b) 9

-(c) 7

-(d) none of the above


{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? |type="()" /} +(a) $x^n$

-(b) $x×n$

-(c) $n^x$

-(d) none of the above

{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> |type="()" /} -(a) 5

-(b) 4

+(c) 3

-(d) 0


{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> |type="()" /} -(a) Hello

-(b) World

+(c) HelloWorld

-(d) Compile Error


{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> |type="()" /} -(a) Equal

-(b) Not Equal

-(c) Compiler Dependent

+(d) Compile Error


{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> |type="()" /} -(a) Hello

-(b) Ifmmp

-(c) Compile Error

+(d) Segmentation fault


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

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

int main() {

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

} </syntaxhighlight> |type="()" /} -(a) Segmentation Fault

+(b) Compile Error

-(c) 12

-(d) 0


{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> |type="()" /} +(a) 1 2

-(b) 1 1

-(c) 10 11

-(d) 10 10


{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> |type="()" /} +(a) Hello

-(b) Link error

-(c) Segmentation fault

-(d) Compiler error


{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 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("%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>

Data types[edit]

<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="()" /}

</quiz>

Some codes[edit]

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