INTRODUCTION TO PROGRAMMING


INTRODUCTION TO PROGRAMMING MODEL QUESTIONS AND SOLVED ANSWERS


  1. If you are given to delete a particular number from an  Integer array, What is the best way to do it? Choose one answer from following.
                  a. Find the location of the number, set the value of the location to zero.
                  b. Find the location of the number, move all the numbers right to the location one position                              forward.
                  c. Find the location of the number, move all numbers right to the location one  position                                   forward, reduce the size of the array by one.
                  d. Find the location of the number, move all numbers left to the location one position                                       backward.   

Answer - (a)    

Imagine the given array is int num[5], the given number set is {5,6,3,2,4} and you are given to delete number 3. As we know the location of number 3 is num[2]. At the first point we have to find this/location. What will be the output when the next instructions are executed is shown in following photo. It will be more easy to understand.



2. 
    int numArray[3][3] = {{10,20,30}, {25,35,45}, {1,2,3}};
    int i,j;
    for(i = 0;  i<3 ; i++)
    {
                    for(j = 0 ; j<3 ; j++)
                    {
                                    if(i==j)
                                          printf("%d ", numArray[i][j]);
                    }
    }

    What will be the output of above code segment?

        a. The numbers stored in the last row
        b. The numbers stored in the first row
        c. The numbers stored in the diagonal
        d. The numbers stored in the first column

Answer - (c)

I ran the above code segment in GDB  compiler and the output was    10 35 3 . That is a diagonal.


Following diagram will show you how the numbers/values have been saved in the above 2D array.



Following chart will show what is diagonal.(According to the above question)



3. Which of the following C code is equivalent  to the strlen function used for char array called arr and         maximum size 'size'

                                a. for ( i = 0 ; i < size || arr[i] != '\0' ; ++i);
                                b. for ( i = 0 ; i < size && arr[i] != '\0' ; ++i);
                                c. for ( i = 0 ; arr[i] != '\0' ; ++i);
                                d. for ( i = 0 ; i < size ; ++i);


4. Equality operator can't be used to compare two variables of _______

    Select one.
                a.  int
                b. float
                c. char
                d.string

5. Which of the following statement declares a float array called values with 5 rows of 6 columns?

    Select one.
                a. float Values[5][4];
                b. float Values[4][5];
                c. float Values[5][6];
                d. float Values[6][5];

6. State whether the following statements about switch statement are correct.

    Ⅰ. Switch statement often provides a better alternative than a large series of if-else-if statements.
    Ⅱ. The break statement is used inside the switch to terminate a statement sequence.

    Select one.
                a. False , True
                b. False , False
                c. True , True
                d. True , False

7. Select the correct order of characters and C statements to fill the given program segment to display the output as ' iOS Windows' ?

    char ch = _____ ;
    switch(ch)
{
    case 'A' : printf ("Android\n");
    case 'B' : printf ("iOS");
                    ___________
    case 'C': printf("Windows\n");
}    
    
    Select one.
                a.  ' ' , empty
                b. 'B' , break ; 
                c. ' ' ,  break;
                d. 'B' , empty

8. A two-dimensional array called image of m rows and  columns, holds the brightness values for the pixels in an image. The brightness can range from 0 to 255. What does the following C code computes?

    iMax = 0;
    for (r =  0 ;  r < m ; r++)
    {
        for (c = 0; c < n ; c++)
           {
                i = image[r][c];
                if( i > iMax )
                        {
                                iMax = i;
                                cMax = c;
                         }
            }
    }

Select one; 
    a. The value and the row index of maximum brightness value for all pixels in image.
    b. The value and  the column index of maximum brightness value for all pixels in image.
    c. The maximum brightness value for all pixels in image.
    d. The location of the maximum brightness value for all pixels in image.
    
9. Select the incorrect assert() statement that can be used to validate the function shown below; 

    float calTime( float speed , int distance)
    {
        if( speed >  0.0 )
            return (distance / speed);
        else 
            printf("Speed is not greater than 0!");
            return 0.0;
    }

Select one;
    a. assert(calTime(0.0 , 100) == 0.0);
    b. assert(calTime( -60.0 / 60.0) == 1.0);
    c. assert(calTime(60.0 , 120) == 2.0);
    d. assert(calTime(-40.0 , 130) == 0.0);

10. 






















-Source of Questions - SLIIT-

Comments