Categories: Programming & DS

C Coding Questions

  1. Consider the following program:
    #include <stdio.h>
    int main()
    {
        int a[10][20][30]={0};
        printf("%ld",&a+1 - &a);
     
        return 0;
    }

    What is the output of this program?

  2. Consider the following program:
    #include <stdio.h> 
    int main()
    {
        int a[10][20][30] = {0};
        int *b = a;
        int *c = a+1;
        printf("%ld", c-b);
     
        return 0;
    }
    What is the output of this program? (You may ignore compiler warnings)
    
    
  3. What is the output of the following program?
    #include <stdio.h>
    #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;
    }
  4. What is the output of the following program?
    #include <stdio.h> 
    int main()
    {
        int *a = fun();
        printf("%d",a);
     
        return 0;
    }
    int fun()
    {
        int a = 10;
        return a;
    }
  5. What is the output of the following program?
    #include <stdio.h> 
    #include <string.h>
     
    int main()
    {
        char string[] = "Hello";
        printf("%zu %zu",sizeof(string),strlen(string));
     
        return 0;
    }
  6. What is the output of the following program?
    #include <stdio.h>
    int main()
    {
        float a = 0.5;
        if(a == 0.5)
            printf("Yes");
        else
            printf("No");
     
        return 0;
    }
  7. What is the output of the following program?
    #include <stdio.h> 
    #include <string.h>
    void foo(char *);
     
    int main()
    {
        char a[100] = {0};
        printf("%zu %zu",sizeof(a),strlen(a));
     
        return 0;
    }
  8. What is the output of the following program? (Assume input is 10)
    #include <stdio.h>
    int main()
    {
        int a;
        printf("%d",scanf("%d",&a));
     
        return 0;
    }
  9. If the binary equivalent of 5.375 in normalised form is 01000000 10101100 00000000 00000000, what will be the output of the following program?
    #include <stdio.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;
    }

     

  10. What is the output of the following program?
    #include <stdio.h>
    int main()
    {
        char str[] = {'a','b','c','\0'};
        str[0] -= 32;
        printf("%s",str);
     
        return 0;
    }

Questions from Datatypes

  1. Consider an implementation where int is 4 bytes and long int is 8 bytes. Which of the following initializations are correct?
    #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;
    }
  2. Consider an implementation where int is 4 bytes and long int is 8 bytes. What will be the output of the following code?
    #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;
    }
  3. What will be the output of the following code?
    #include <stdio.h>
    int main()
    {
       unsigned int a = 5;
       if(a > -1)
          printf("5 is > -1\n");
       return 0;
    }
  4. What will be printed by the following code?
    #include <stdio.h>
    
    int main()
    {
       char buff[255] = "abc\
    pee";
       printf("%s", buff);
       return 0;
    }
    
  5. What will be printed by the following code?
    #include <stdio.h>
    
    int main()
    {
        char buff[255] = "\0";
        printf("%s", buff);
        return 0;
    }
  6. What will be the output of the following code? (Assume a 64 bit machine/compiler)
    #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;
    }
  7. What will be the output of the following code?
    #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;
    }
    

Questions from Pointers

  1. What will be the output of the following code?
    #include <stdio.h>
     
    int main()
    {
      int a = 5;
      int* p = &a;
      printf("%d", ++*p);
    }
    
  2. What will be the output of the following code?
    #include <stdio.h>
     
    int main()
    {
      char a[] = "Hello World";
      char* p = &a;
      printf("%s", p+2 );
    }
  3. What will be the output of the following code?
    #include <stdio.h>
     
    int main()
    {
      int a;
      int* p = &a;
      printf("%zu", sizeof( *(char*)p  ));
    }
  4. Is the following code legal?
    #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");
      }
    }
  5. What will be the output of the following code?
    #include <stdio.h>
     
    int main()
    {
      int *a = (int*) 1;
      printf("%d", a);
    }
  6. What will be the output of the following code?
    #include <stdio.h>
     
    int main()
    {
      int a = 1, *p, **pp;
      p = &a;
      pp = p;
      printf("%d", **pp);
    }
  7. What will be the output of the following code?
    #include <stdio.h>
     
    int main()
    {
      int a = 1, *p, **pp;
      p = &a;
      pp = p;
      printf("%d", *pp);
    }
  8. Assuming a little endian machine, what will be the output of the following program?
    #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);
    }

Some codes

  1. What is the following function doing?
    int foo(int n)
    {
        int sum = 0;
        while(n > 0)
        {
            n = n & n-1;
            sum++;
        }
        return sum;
    }
  2. What is the following function doing?
    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;
    }
  3. What is the following function doing?
    int foo( int a, int b)
    {
        int c = a-b;
        c = c&(0x80000000);
        return (!c)*a +(!!c)*b;
    }
  4. What is the following function doing?
    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;
    }
  5. What is the following function doing?
    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;
    }
  6. Write the code to dynamically allocate a 2-D array of size m x n.
  7. Declare a pointer to a function accepting an integer and returning void.
  8. Write the condition so that the below code outputs Hello World.
    #include <stdio.h> 
    int main()
    {
        if()
        {
     printf("Hello ");
        }
        else
        {
     printf("World\n");
        }
     
        return 0;
    }
  9. Write a one line code to check if a number is a power of 2.
  10. Write a one line code to invert the last four bits of an integer.

ibia

Share
Published by
ibia

Recent Posts

GATE CSE 2025 Syllabus

SectionTopicsSubtopics (GO Link Attached)1. Engineering MathematicsDiscrete Mathematics-- Propositional and first-order logic, -- Sets, Relations, Functions,…

10 months ago

GATE CSE 2022 Admissions, Results and Placement Responses

This page shows all details regarding GATE CSE 2022 Admissions including results, admission offers and…

3 years ago

GATE CSE 2021 Admission Responses

Source Add your Response Rank Predictor for GATE CSE 2021 Result Responses: GATE CSE 2021…

4 years ago

GATE CSE Books – More Options

Best Books for GATE CSE with Relevant Chapters to Read  Heads Up! These GATE CSE…

4 years ago

ISI PCB Previous Year Papers with Solution

Indian Statistical Institute(ISI) offers M Tech in Computer Science with the Admission Test Codes MMA/PCA…

4 years ago

ISI JRF Previous Year Papers with Solution

Indian Statistical Institute(ISI) offers Junior Research Fellowships (JRF) in Computer Science, Statistics, Mathematics, Quantitative Economics,…

4 years ago