Tuesday, 5 February 2013

L.D.P Assignment L-7



Q.1 state whether the following statement are true or false.
     (a) The type of all elements in an array must be the same.
         Ans:-true
      (b) When an array is declared, C automatically initializes its elements to zero.
         Ans:-false
      (c) An expression that evaluates to an integral value may be used as a subscript.
         Ans:-true
      (d) Accessing an array outside its range is a compile time error.
        Ans:-true
      (e) A char type variable cannot be used as a subscript in an array.
       Ans:-false
      (f) An unsigned long int type can be used as a subscript in an array.
        Ans:-true
      (g) In C, by default, the first subscript is zero.
         Ans:-true
  (h) When initializing a multidimensional array, not specifying  all its dimensions is an error.
        Ans:-false
(i)When we use  expressions as a subscript, its result should be always greater than zero.
       Ans:-false
(j) In C, we can use a maximum of 4 dimensions  for an array.
      Ans:-false
(k)In declaring an array, the array size can be a constant or variable or an expression.
     Ans:-true
(l)The declaration int x[2]={1,2,3}; is illegal.
      Ans:-true

Q.2 Fill in the blanks in the following statements.
(a)The variable used as a subscript in an array is popularly known as  -----------  variable.
    Ans:-index
(b)An array can be initialized either at compile time or at --------.
   Ans:-run time
(c) An array created using malloc function at run time is referred to as ----------array.
     Ans:-dynamic
(d) An array that uses more than two subscript is referred to as      
        ---------array.
         Ans:-multidimensional
  (e) -------- is the process of arranging the elements of an array in order.
     Ans:-sorting   

Q.3 Identify errors, if any, in each of the following array declaration statements, assuming that ROW and COLUMN are declared symbolic constants:
(a) Int score (100);
   error: declaration syntax error  
   Ans:-[100];      
(b) Float values [10, 15];
    Ans:-float value[10][15];
(c) float average[row],[column];
    Ans:-float average[row][column];
(d) char name[15];
   Ans:-true
(e)  Int sum [];
   Ans:-value is not defined
(f)  Double salary [i + row]
     Ans:-semicolon
         Double salary [i+row];
(g)  Long int number [row]
    Ans:-long int number [row];
(h)  Int array x [column];
    Ans:-int x [column];

Q.4 Identify errors, if any, in each of the following initialization statements.
(a) Int number [] = {0,0,0,0,0};
   Ans:-true
(b) float item[3][2]={0,1,2,3,4,5};
    Ans:-true
(c) Char word [] = {‘A’,’R’,’R’,’A’,’Y’};
     Ans:-true
(d) int m[2,4]={(0,0,0,0)(1,1,1,1)};
      Ans:-int m[2][4]={(0,0,0,0)(1,1,1,1)};
(e) Float result [10] =0;
    Ans:-true


Q.5 Assume that the array A and B are declared as follows:
Int A [5] [4];
Float B [4];
Find the error (if any) in the following program segments.

(a)             for (i=1; i<=5; i++)
for (j=1; j<=4;j++)
A[i][j] = 0;
            Ans:-logic error
(b)             for (i=1; i<=4; i++)
                 Scanf (“%f”,B[i]);
            Ans:-not generate error of compile time but get three value
(c)               for (i=0; i<=4; i++)
             B[i]=B[i]+i;
       Ans:-compile time  no error but logical error
(d)            for (i=4; i<=0; i--)
for (j=0;j<4;j++)
A[i] [j]=B[j]+1.0;
           Ans:- compile time not generate error but logical value


Q.6 We want to declare a two-dimensional integer type array called matrix for 3 rows and 5 columns. Which of the following declarations are correct?
  
(a) Int maxtrix [3] , [5];
  Ans:-correct
(b) Int matrix [5] [3];
 Ans:- wrong  
(c) Int matrix [2+1] [2+3];
  Ans:-correct
(d) Int matrix [3,5];
   Ans:-wrong
(e) Int matrix [3] [5];
  Ans:-correct

Q.7 which of the following initialization statements are correct?
 (a) char str1 [4] = “GOOD”;
    Ans:-correct
 (b) char str2 [ ] = “C”;
    Ans:-wrong
 (c) char str3 [5] =”Moon”;
    Ans:-correct
 (d) char str4 [ ] = {‘S’,’U’,’N’};
     Ans:-correct
 (e)char str5 [10] = “SUN”;

Ans:-correct


Q.8 what is the error in the following program?

main( )
     {
         int x;
float y [ ] ;
…….
}
 *Error:-size of ‘y’ is unknown or zero


Q.9 what is the output of the following program?
  main()
    {
        int m [] = {1,2,3,4,5};
        int x, y=0;
        for(x=0;x<5;x++)
           y = y+m[x];
      printf(“%d”,y);
}
OUTPUT:- 15
           
Q.10 what is the output of the following program?
   main()
     {
        chart string [] =”HELLO WORLD”;
        int m;
        for(m=0;string [m] !=’\0’;m++)
          if((m%2) = = 0)
        printf(“%c”,string[m]);
}                    
OUTPUT:-HLOWRD

No comments:

Post a Comment