C Programming Language Array Quizzes


  • 1. Predict the output of below program:

    #include<stdio.h>	
    int main()
    {
        int arr[5];
        // Assume base address of arr is 2000 and size of integer is 32 bit
        printf("%u %u", arr + 1, &arr + 1);
     
        return 0;
    } 





  • 2. What is output?

    #include<stdio.h>
    void print(int arr[])
    {
       int n = sizeof(arr)/sizeof(arr[0]);
       int i;
       for (i = 0; i < n; i++)
          printf("%d ", arr[i]);
    }
     
    int main()
    {
       int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
       print(arr);
       return 0;
    }





  • 3. Output of following program?

    #include<stdio.h>
    int main() 
    { 
      int a[] = {1, 2, 3, 4, 5, 6}; 
      int *ptr = (int*)(&a+1); 
      printf("%d ", *(ptr-1) ); 
      return 0; 
    }
    





  • 4. What is the output of the below program?

    #include<stdio.h>
    #define SIZE(arr) sizeof(arr) / sizeof(*arr);
    void fun(int* arr, int n)
    {
        int i;
        *arr += *(arr + n - 1) += 10;
    }
     
    void printArr(int* arr, int n)
    {
        int i;
        for(i = 0; i < n; ++i)
            printf("%d ", arr[i]);
    }
     
    int main()
    {
        int arr[] = {10, 20, 30};
        int size = SIZE(arr);
        fun(arr, size);
        printArr(arr, size);
        return 0;
    }





  • 5. What is x in the following program?

    #include<stdio.h>
    int main()
    {
        int i;
        int arr[5] = {1};
        for (i = 0; i < 5; i++)
            printf("%d ", arr[i]);
        return 0;
    }
    





  • 6. Does C perform array out of bound checking ? What is the output ?

    #include<stdio.h>
    int main()
    {
        int i;
        int arr[5] = {0};
        for (i = 0; i <= 5; i++)
            printf("%d ", arr[i]);
        return 0;
    }





  • 7. What is the output of this C code?

    #include<stdio.h>
    int main()
    {
        int a[][] = {{1,2},{3,4}};
        int i, j;
        for (i = 0; i < 2; i++)
            for (j = 0; j < 2; j++)
                printf("%d ", a[i][j]);
        return 0;
    }
    





  • 8. Which of the following will print the value 2 for the below program ?

    #include<stdio.h>
    int main()
    {
        int a[10][20][30] = {0};
        a[5][2][1] = 2;
        return 0;
    }
    





  • 9. What is the output of this code?

    #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;
    }
    





  • 10. What is the output of this C code?

    #include<stdio.h>
    int size = 4;
     
    int main()
    {
     int idx;
     
     int arr1[size] = {0};
     int arr2[size];
     
     for(idx=0; idx<size; idx++)
      arr2[idx]=0;
     
     return 0;
    }