Mask-group

Exercise



Point out the errors, if any, in the following program segments:

  1. /* mixed has some char and some int values */
    int char mixed[100] ; 

Ans: Error.An array cannot be declared to be of the type in char.

  1. main( ) {

int a[10], i ; 

for ( i = 1 ; i <= 10 ; i++ ) {

scanf ( "%d", a[i]) ; 

printf ( "%d", a[i] ) ; 

 } 

}

Ans: Error.Arrays bounds are exceeded.

  1. main( ) {

int size ; 

scanf ( "%d", &size ) ;

int arr[size] ;

for ( i = 1 ; i <= size ; i++ ) {

scanf ( "%d", arr[i] ) ;

printf ( "%d", arr[i] ) ;

}

 }

Ans: Error.Dimension of the array should be constant and all declarations should be at the beginning.

Answer the following:

  1. An array is a collection of :

    1. different data types scattered throughout memory 

    2. the same data type scattered throughout memory 

    3. the same data type placed next to each other in memory 

    4. different data types placed next to each other in memory 

Ans: 3. the same data type placed next to each other in memory 

  1. Are the following array declarations correct? 

int a (25) ;

Ans: Use [ ] in place of ( ).

  1. Which element of the array does this expression reference? num[4] 

Ans: Fifth element.

  1. State whether the following statements are True or False: 

    1. The array int num[26] has twenty-six elements.

Ans: True

  1. The expression num[1] designates the first element in the array.

Ans: False

  1. The expression num[27] designates the twenty-eighth element in the array.

Ans: True

  1. What would happen if you try to put so many values into an array when you initialize it that the size of the array is exceeded?

    1. Nothing

    2. possible system malfunction

    3. error message from the compiler

    4. other data may be overwritten 

Ans: 2. possible system malfunction

  1. In an array int arr[12] the word arr represents the a_________ of the array.

Ans: address

  1. What would happen if you put too few elements in an array when you initialize it? 

    1. nothing 

    2. possible system malfunction

    3. error message from the compiler 

    4. unused elements will be filled with 0’s or garbage 

Ans: 4. unused elements will be filled with 0’s or garbage 

  1. What would happen if you assign a value to an element of an array whose subscript exceeds the size of the array? 

    1. the element will be set to 0

    2. nothing, it’s done all the time 

    3. other data may be overwritten 

    4. error message from the compiler

Ans: 3. other data may be overwritten

  1. Which of these are reasons for using pointers? 

    1. To manipulate parts of an array 

    2. To refer to keywords such as for and if 

    3. To return more than one value from a function 

    4. To refer to particular programs more conveniently.

Ans: 1. To manipulate parts of an array 

3. To return more than one value from a function

  1. If you don’t initialize a static array, what would be the elements set to? 

    1. 0

    2. an undetermined value

    3. a floating point number

    4. the character constant '\0'

Ans: 2. an undetermined value 

  1. True/False

Address of a floating-point variable is always a whole number.

Ans: True

  1. Which of the following is the correct way of declaring a float pointer: 

    1. float ptr ; 

    2. float *ptr ;

    3. *float ptr ; 

    4. None of the above

Ans: 2. float *ptr ;

GATE PYQ:

#include <stdio.h>

  1. #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;

}

Ans: 2004 2020

  1. Which of the following is true about arrays in C?

    1. For every type T,there can be an array of T.

    2. For every type T except void and function type,there can be an array of T.

    3. When an array is passed to a function,the C compiler creates a copy of the array.

    4. 2D arrays are stored in column major form.

Ans: B.For every type T except void and function type,there can be an array of T.

In C language,we cannot have an array of void types and function types.We can have an array of void pointers or function pointers.

  1. int main()

{

    int i;

    int arr[5] = {1};

    for (i = 0; i < 5; i++)

        printf("%d ", arr[i]);

    return 0;

}

Ans: 1 0 0 0 0

In C language,if we initialize fewer elements of an array,then all the remaining elements are automatically initialized to 0.