• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Arrays

Question 11

C
#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;
}
  • 1 2 3 4
  • Compiler Error in line " int a[][] = {{1,2},{3,4}};"
  • 4 garbage values
  • 4 3 2 1

Question 12

C
#include<stdio.h>
int main()
{
    int a[10][20][30] = {0};
    a[5][2][1] = 2;
    return 0;
}
Which of the following will print the value 2 for the above code?
  • printf("%d",*(((a+5)+2)+1));
  • printf("%d",***((a+5)+2)+1);
  • printf("%d",*(*(*(a+5)+2)+1));
  • None of these

Question 13

C
#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;
}
  • 5
  • 6
  • 9
  • None of the above

Question 14

For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits.
  t0 = i ∗ 1024
  t1 = j ∗ 32
  t2 = k ∗ 4
  t3 = t1 + t0
  t4 = t3 + t2
  t5 = X[t4] 
Which one of the following statements about the source code for the C program is CORRECT?
  • X is declared as “int X[32][32][8]”.
  • X is declared as “int X[4][1024][32]”.
  • X is declared as “char X[4][32][8]”.
  • X is declared as “char X[32][16][2]”.

Question 15

What’s the meaning of following declaration in C language? C
int (*p)[5];
  • It will result in compile error because there shouldn\'t be any parenthesis i.e. “int *p[5]” is valid.
  • p is a pointer to 5 integers.
  • p is a pointer to integer array.
  • p is an array of 5 pointers to integers.
  • p is a pointer to an array of 5 integers

Question 16

In a C file (say sourcefile1.c), an array is defined as follows. Here, we don’t need to mention array arr size explicitly in [] because the size would be determined by the number of elements used in the initialization. C
int arr[] = {1,2,3,4,5};
In another C file (say sourcefile2.c), the same array is declared for usage as follows: C
extern int arr[];
In sourcefile2.c, we can use sizeof() on arr to find out the actual size of arr.
  • TRUE
  • FALSE

Question 17

Find out the correct statement for the following program. C
#include \"stdio.h\"

int * arrPtr[5];

int main()
{
 if(*(arrPtr+2) == *(arrPtr+4))
 {
   printf(\"Equal!\");
 }
 else
 {
  printf(\"Not Equal\");
 }
 return 0;
}
  • Compile Error
  • It’ll always print Equal.
  • It’ll always print Not Equal.
  • Since elements of arrPtr aren’t initialized in the program, it’ll print either Equal or Not Equal.

Question 18

Pick the best statement for the below: C
int arr[50] = {0,1,2,[47]=47,48,49};
  • This isn’t allowed in C and it’ll give compile error
  • This is allowed in C as per standard. Basically, it’ll initialize arr[0], arr[1], arr[2], arr[47], arr[48] and arr[49] to 0,1,2,47,48 and 49 respectively. The remaining elements of the array would be initialized to 0.

Question 19

Pick the best statement for the below program: C
#include \"stdio.h\"
 
void fun(int n)
{
   int idx;
   int arr1[n] = {0};
   int arr2[n];
 
   for (idx=0; idx<n; idx++)
       arr2[idx] = 0;    
}
 
int main()
{
   fun(4);
   return 0;
}
  • Definition of both arr1 and arr2 is incorrect because variable is used to specify the size of array. That’s why compile error.
  • Apart from definition of arr1 arr2, initialization of arr1 is also incorrect. arr1 can’t be initialized due to its size being specified as variable. That’s why compile error.
  • Initialization of arr1 is incorrect. arr1 can’t be initialized due to its size being specified as variable. That’s why compile error.
  • No compile error. The program would define and initializes both arrays to ZERO.

Question 20

Pick the best statement for the below program: C
#include \"stdio.h\"

int size = 4;
int arr[size];

int main()
{
 if(arr[0])
  printf(\"Initialized to ZERO\");
 else
  printf(\"Not initialized to ZERO\");

 return 0;
}
  • No compile error and it’ll print “Initialized to ZERO”.
  • No compile error and it’ll print “Not initialized to ZERO”.
  • Compile error because size of arr has been defined using variable outside any function.
  • No compile error and it’ll print either “Initialized to ZERO” or “Not initialized to ZERO” depending on what value is present at arr[0] at a particular run of the program.

There are 37 questions to complete.

Last Updated :
Take a part in the ongoing discussion