Review question’s answer
Chapter
no-07(Array)
===========================================
7.1: State whether the
following statements are true or
false.
(a)
The type of all elements in an array must be the same.
Answer:
True.
(b)
When an array is declared, C automatically initializes its elements to zero. Answer:
True.
(c)
An expression that evaluates to an integral value may be used as a subscript. Answer:
True.
(d)
Accessing an array outside its range is a compile time error.
Answer:
True.
(e)
A char type variable can not be
used as a subscript in an array.
Answer:
True.
(f)
An unsigned long int type can be used as a subscript in an array.
Answer:
True.
(g)
In C, by default, the first subscript is zero. Answer:
True.
(h)
When initializing a multidimensional array, not specifying all its dimensions
is an error. Answer:
True.
(i)
When we use expression as a subscript, its result should be always greater than
zero. Answer:
True.
(j)
In C, we can use a maximum of 4 dimensions of an array.
Answer:
False.
(k)
In declaring an array, the array size can be constant or variable or an
expression. Answer: False.
(l)
The declaration int x[2]={1,2,3};is illegal. Answer:
True.
Question-7.2:
Fill in the blanks in the following statements.
(a)
The variable used as a subscript in an array is popularly known as index variable.
(b)
An array can be initialized either at compile time or at run time.
(c)
An array created using malloc function at run time is referred to as pointer
variable array.
(d)
An array that uses more than two subscripts is referred to as multidimensional array.
(e)
sorting is the process of
arranging the elements of an array in order.
Question-7.3: Identify errors, if
any, in each of the following array declaration statements, assuming that ROW
and COLUMN are declared as symbolic constants.
Questions
|
Answer
|
(a) int score (100)
|
Incorrect
|
(b) float values [10,15]
|
Incorrect
|
(c) float average [ROW],[COLUMN]
|
Incorrect
|
(d) char name [15]
|
Correct
|
(e) int sum []
|
Correct
|
(f) double salary [i+ROW]
|
Incorrect
|
(g) long int number [ROW]
|
Incorrect
|
(h) int array x[COLUMN]
|
Incorrect
|
Question-7.4:
Identify errors, if any, in each of the following initialization statements.
Statement
|
Answer
|
(a) int
number []={0,0,0,0,0}
|
Correct
|
(b)
float item [3] [2] ={0,1,2,3,4,5}
|
Correct
|
(c)
float word []={‘A’,’R’,’R’,’A’,’Y’}
|
Incorrect
|
(d) int
m[2,4] ={(0,0,0,0)(1,1,1,1)}
|
Incorrect
|
(e)
float result [10] =0
|
Correct
|
Qusetion-7.5:
Assume that the arrays A and B are declared as follows:
int A [5] [4];
float B [4]
Find the errors (if any) in the following
program segments.
Statement
|
Answer
|
Correction
|
(a(a) for (i=0;i<=5;i++)
for(j=1;j<=4;j++)
A [i] [j] =0;
|
No error
|
----
|
(b) for (i=1;i<4;i++)
scanf(“%f”,B[i]);
|
Error
|
Correction: for (i=1; i<=4; i++)
scanf (“%f”, &B[i]);
|
(c) (i=0;i<=4;i++)
B[i] =B [i] + i;
|
Error
|
for (i=1; i<=4;
i++)
B[i] = B[i] + i;
|
(d) for (i=4;i>=4;i--)
for (j=0;j<4;j++)
A [i] [j] =B [j] +1.0
|
No error
|
---
|
Question-7.6:
write a for loop statement that initializes all the dioganal elements of
an array to one and other to zero as shown below. assume 5 rows and 5 columns.
1
|
0
|
0
|
0
|
0
|
..............
|
0
|
0
|
1
|
0
|
0
|
0
|
..............
|
0
|
0
|
0
|
1
|
0
|
0
|
..............
|
0
|
-
-
-
-
-
-
|
-
-
-
-
-
-
|
-
-
-
-
-
-
|
-
-
-
-
-
-
|
-
-
-
-
-
-
|
-
-
-
-
-
-
|
|
0
|
0
|
0
|
0
|
-
|
..............
|
1
|
Answer: for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
if(i==j)
printf(“1”);
else
printf(“0”);
}
Question-7.7:
we want to declare a two-dimentional integer type array called matrix for
3 rows and 5 columns. which for the following declarations are correct?
Statement
|
Answer
|
a) int matrix [3],[5]
|
Incorrect
|
b) int matrix [5] [3]
|
Incorrect
|
c) int matrix [1+2] [2+3]
|
Correct
|
d) int matrix [3,5]
|
Incorrect
|
e) int matrix [3] [5]
|
Correct
|
Question-7.8:
which of the following initialization statements are correct?
Statement
|
Answer
|
a) char str1[4]=”GOOD”
|
Incorrect
|
b) char str2[ ]=”C”
|
Correct
|
c) char str3[5]=”MOON”
|
Correct
|
d) char str4[ ]={‘S’,’U’,’N’}
|
Incorrect
|
e) char str5[10]=”Sun”
|
Correct
|
Question-7.9:
What is a data structure? Whey is an array called a data structure?
Answer:
C
support a rich set of derived and user-defined data types in C language. Array
and structures are also a structure data types because they are used to
represent data values that have a structure of some sort. In programming
parlance, such data types are known as data types.
Question-7.10:
What is a dynamic array? How is it created? Give a typical example of a dynamic
array?
Answer:
The
process of dynamic memory allocation and the arrays created at run time are
called dynamic array.
Example:
Malloc , calloc and realloc are dynamic array.
Question-7.11:
What is the error in the following program?
Answer:
main
()
{
int x;
float y [10];
..........
}
Question-7.12:
What happens when an array with specified size is assigned
a) with values fewer than the specified size; and
b) with values more than the specified size.
Answer:
Question-7.13:
Discuss how initial values can be assigned to a multidimensional array.
Answer:
C
support arrays of three or more dimensional. the general form of a
multidimensional array is ….
Type
array name [a1] [a2] [a3]…….[am]; Where a1 is the size of the dimensional.
Question-7.14:
What is the output of the following program?
main
()
{
int m [] = {1,2,3,4,5}
int m;
for (x=0; x<5; x++ )
y=y+m [x]
printf(“%d”, y);
}
Answer:
Output: 15
Question-7.15:
What is the output of the following program?
main
()
{
char string [ ]= “HELLO WORLD”;
int m;
for (m=0; string [m] !=’\0’;m++)
if ((m%2)==0 )
printf (“%c”, string [m] );
}
Answer:
Output: HLOWRD
-------------------------------------------------------------------------------
- You can download this file as pdf by clicking below download icon-
- Download
Go ahead by honesty--->Rm
No comments:
Post a Comment