Point out the errors, if any, in the following program segments:
/* 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.
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.
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:
An array is a collection of :
different data types scattered throughout memory
the same data type scattered throughout memory
the same data type placed next to each other in memory
different data types placed next to each other in memory
Ans: 3. the same data type placed next to each other in memory
Are the following array declarations correct?
int a (25) ;
Ans: Use [ ] in place of ( ).
Which element of the array does this expression reference? num[4]
Ans: Fifth element.
State whether the following statements are True or False:
The array int num[26] has twenty-six elements.
Ans: True
The expression num[1] designates the first element in the array.
Ans: False
The expression num[27] designates the twenty-eighth element in the array.
Ans: True
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?
Nothing
possible system malfunction
error message from the compiler
other data may be overwritten
Ans: 2. possible system malfunction
In an array int arr[12] the word arr represents the a_________ of the array.
Ans: address
What would happen if you put too few elements in an array when you initialize it?
nothing
possible system malfunction
error message from the compiler
unused elements will be filled with 0’s or garbage
Ans: 4. unused elements will be filled with 0’s or garbage
What would happen if you assign a value to an element of an array whose subscript exceeds the size of the array?
the element will be set to 0
nothing, it’s done all the time
other data may be overwritten
error message from the compiler
Ans: 3. other data may be overwritten
Which of these are reasons for using pointers?
To manipulate parts of an array
To refer to keywords such as for and if
To return more than one value from a function
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
If you don’t initialize a static array, what would be the elements set to?
0
an undetermined value
a floating point number
the character constant '\0'
Ans: 2. an undetermined value
True/False
Address of a floating-point variable is always a whole number.
Ans: True
Which of the following is the correct way of declaring a float pointer:
float ptr ;
float *ptr ;
*float ptr ;
None of the above
Ans: 2. float *ptr ;
GATE PYQ:
#include <stdio.h>
#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
Which of the following is true about arrays in C?
For every type T,there can be an array of T.
For every type T except void and function type,there can be an array of T.
When an array is passed to a function,the C compiler creates a copy of the array.
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.
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.