Saturday, November 8, 2008

Programming and Problem solving in C (MC151) : July 2005

Section A : Basic Concepts (30 Marks) 
 
This section consists of questions with serial number 1 - 30.
Answer all questions.
Each question carries one mark.
Maximum time for answering Section A is 30 Minutes.
 
1.Arrays are passed as the arguments to a function by
(a)Value (b) Reference (c) Constant value (d) Can’t be passed (e)Both a & b.
2.Which of the following statements are true after execution of the program.
     int  a[10], i, *p;
         a[0] = 1;
          a[1] = 2;
          p = a;
         (*p)++;
 
(a)a[1] = 3 (b)a[0] = 2 (c)a[1] = 2 (d)compilation error (e)a[0] = 3.
3.It is necessary to declare the type of the function in the calling program if
(a)The function returns an integer
(b)Function returning only the float values
(c)The function is not defined in the same file
(d)The function returns a non- integer value
(e)The size of the function is vary big.
4.The recursive functions are executed in a
(a)Parallel order (b)First In First Out order (c)Last FirstOut order (d)  Iterative order (e)  Random order.
5.A static variable is one
(a)Which can not be initialized
(b)Which is initialized once at the commencement of execution and can’t be changed during the run time
(c)Can’t be declared as global, but as a local only
(d)Which is same as the auto variable but is placed at the head of the program
(e)Retains its value throughout the life of the program.
6.Consider the following program fragment, and choose the correct one
    
main( )
     {                
        int  a, b = 2, c;
             a = 2 * (b++);
             c = 2 * (++b);
     }
              
(a)a = 4,  c = 8 (b)  a = 3,  c = 8 (c)  b = 3,  c = 6 (d)  a = 4,  c = 6
(e)b= 4,  c = 6.
7.What will be the value of sum after the following program is executed?
        main( )
            {
                int sum = 1, index = 9;
                      do {
                            index = index – 1;
                            sum *= 2;
                            }while( index > 9 );
             }
(a)1 (b)2  (c)9  (d)0.5 (e)0.25.
8.What is the right choice, if the following loop is implemented
          
     {
          int  num = 0;
               do {
                     --num;
                     printf( “ %d ”, num);
                   }while( ++num >= 0 );
     }
 
(a)A run time error will be generated
(b)The program will not enter into the loop
(c)There will be a compilation error reported
(d)The loop will run infinitely many times
(e)Prints the value of 0 one time only.
9.What is the final value of the digit?
     main( )
         {
           int digit = 0;
             for ( ; digit <= 9;  )
             printf( “%d\n”, digit++);
             digit  *= 2;
             --digit;
         }
(a)Error (b)-1 (c)17 (d)16 (e)19.
10.If the following code is executed then what is the value of the variable HI?
      main( )
         {
            int HI, L, B;
                L = B = 2;
                HI = ( L == B )  ? 1 : 0;
         }
(a)0 (b)2  (c)1 (d)Run time Error (e)Compile time error.
11.Consider the following code in a   C program.
       struct   custom {
                      int acct_no;
                        float balance;
         } customer,*pc=&customer;
 customer’s acct_no  can be accessed by
 
(a)customer.acct_no (b)pc->acct_no (c)(*pc).acct_no (d)Both (a) and (b)
(e)(a),(b) and  (c).
12.What is the return value of the following statement if it is placed in C program?
   strcmp (“ABC”,”ABC”);
(a)33 (b)-1 (c)1 (d)Compilation error (e)0.
13.Which of the following is a not a keyword in C language.
(a)void (b)volatile (c)sizeof (d)getchar (e)short.
14.What is the following code’s output if choice = ‘R’?
switch(choice)
{
      case   ‘R’  : printf( “RED ”);
      case   ‘W’ : printf( “WHITE ”);
      case   ‘B’  : printf( “BLUE ”);
      default      : printf( “ERROR ”);
      break;
   }
(a)RED (b)RED WHITE BLUE ERROR (c)RED ERROR (d)RED WHITE BLUE (e)ERROR.
15.The for statement which can precede a loop to be executed 50 times or till  a boolean variable “found” becomes false is given by ( let true, false are boolean constants )
(a)for(  i=0;  i<= 50  && found==true;  i++ )
(b)for(  i=0;  i<= 50  found==true;  i++ )
(c)for(  i=0;  i<= 50  found==false;  i++ )
(d)for(  i=0;  i<= 50  && found==false;  i++ )
(e)for(  i=0;  ++i<= 50  found==false; 
16.The function fprintf is used in the program
(a)When too many printf calls have been used in the program
(b)In place of printf, as it uses more memory
(c)When the output is to be printed on to a file
(d)When the type of the variables to be printed are not known
(e)If mixed data type variables are required to be printed on to the memory.
17.If ASCII value of ‘x’ is 120, then what is the value of the H, if
   H = (‘x’ – ‘w’ ) / 3;
(a)1  (b)2 (c)4  (d)0 (e)3.
18.Consider the following program fragment
   for(  c = 1, sum=0;   c <= 10;  c++ )
         {
             scanf(“%d”, &x );
             if( x < 0 ) continue;
             sum += x;
          }
What would be the value of sum for the input  1, -1, 2, -2, 3, -3, 4, -4, 5, -5
(a)0 (b)20 (c)10 (d)1 (e)15.
19.What is the value of u1 and u2 for the following piece of code
     int   u1, u2, v = 3, *pv;
           u1 = 2 *(v + 5);
           pv = &v;
           u2 = 2 *(*pv + 5 );
 
(a)u1 = 16, u2 = 16 (b)u1 = 8, u2 = 16 (c)u1 = 16, u2 = 3 (d)u1 = 8, u2 = 3
(e)u1 = 16, u2 = 8.
20.What will be the output of the following program if ‘e’ is the input to the program
          main( )
             {
                int   lower, upper;
                      upper = getchar( );
                      upper = toupper( lower );
                      putchar( upper );
              }
(a)53 (b)Nothing (c)e (d)E (e)Error.
21.if  a = 0x6db7 and  b = 0xa726, what is the value of a^b;
(a)0x58d9 (b)0x1111 (c)0xceb7 (d)0x2526 (e)0xca91.
22.Consider the following declerations
         union id {
                     char color;
                     int size;
                  };
         struct  {
                      char country;
                      int date;
                      union id;
         }flag;
To assign a color to a flag, the correct statement would be
 
(a)flag.color = ‘W’; (b)flag.id.color = ‘W’; (c)flag.color = ‘WHITE’;
(d)flag.id.color = ‘WHITE’; (e)  color.flag = ‘W’;
23.Consider the following declerations enum colors{black, blue, green };
This represent
(a)black = 0,   blue = 1,    green = 2
(b)color[1]=’black’, color[2]=’blue’, color[3]=’green’
(c)color = ‘black’ or  color = ‘blue’ or color = ‘green’
(d)black = -1, blue=0, green=1;
(e)Syntax error.
24.The function sprintf( )  works like printf( ), but operates on
(aDatafile (b) stderr (c)string(d)stdin (e)no such function in ‘C’.
25.The most widely used method for interpreting the bit setting as non-negative integer is
(a)EBCD system (b)BCD system (c)ASCII system (d)Binary number system (e)EBCDIC system.
26.Elements of the array are accessed by
(a)Index (b)  Mathematic function (c)Accessing the function in built-in data structure (d)int I (e)  a[0].
27.String concatenation means
(a)Combining two strings
(b)Extracting a substring out of a string
(c)Partitioning the string into two strings
(d)Merging two strings
(e)Comparing the two strings to define the larger one.
28.malloc( ) function returns pointer to
(a)integer (b)void (c)character (d)structure (e)stack memory.
29.# define preprocessors command can be used for defining
(a)Macros  (b)For loop (c)Symbolic constants (d)Both (a) and (c) (e)(a), (b) and (c).
30.‘C’ is a middle level language because
(a)It is evolved in Middle age of computer development
(b)Not having so many features, but still is robust
(c)Combines the best features of high, low level languages
(d)Easier to learn and use
(e)Highly Portable.

Section B : Problems (50 Marks)This section consists of questions with serial number 1 – 5.
Answer all questions.
Marks are indicated against each question.
Detailed workings should form part of your answer.
Do not spend more than 110 - 120 minutes on Section B.
1.a.Draw a flowchart to find the factorial of a given number
b.Write a recursive algorithm for finding the maximum and minimum of an array
(4 + 6 = 10 marks)
2.a.Differentiate structure with union using a good set of example
b.Write about unconditional control Structures provided by ‘C’ language, with suitable examples (5 + 5 = 10 marks)
3. a.Write a ‘C’ program to search whether an element is there in the array or not using Binary Search Technique
b.Write a ‘C’ program to merge two arrays into one sorted array(5 + 5 = 10 marks)
4.Write short notes on the following and give one example for each
a.Pointer and array of pointer
b.Function prototypes
c.Advantages of having a pointer to structure variables (3 + 3 + 4 =10 marks)
5.a.Write about the dynamic memory management functions provided by ‘C’ language.
b.Differentiate functions with macros (6 + 4 = 10 marks)

Section C : Applied Theory (20 Marks)
This section consists of questions with serial number 7 - 9.
Answer all questions.
Marks are indicated against each question.
Do not spend more than 25 -30 minutes on section C.
6.Write a modular ‘C’ program to sort ‘n’ names using pointers (Don’t use character arrays and array representation for names, and use dynamic memory management functions).(10 marks)
7.Write a menu driven ‘C’ program that will generate a data file containing the list of customers and their corresponding telephone numbers. Use a structure variable to store the name and telephone number of each customer. Create a data file using a sample data and handle the following
a.Determine the telephone number of a specified customer.
b.Determine the customer whose telephone number is specified. (5 + 5 = 10 marks)



Suggested Answers

1.Answer :(b)
Reason :Because according to the C language syntax array can only be passed by reference, not by value
2.Answer :(a)
Reason :As p is a pointer to the array a, any operation performed on p effects array a also, and hence (*p++
3.Answer :(d)
Reason :According to the definition of functions.
4.Answer :(c)
Reason :Because for each function call an entry is created in stack frame( known as Active Record Instance), and are executed in LIFO manner.
5.Answer :(e)
Reason :According to the definition of static variable
6.Answer :(a)
Reason :Because of post increment and pre increment of operators
7.Answer :(b)
Reason :As the loop is terminated after 1st iteration ( index = index – 1 causes it to be 8 )
8.Answer :(d)
Reason :As the value of num is decremented(--num) and again incremented(++num) and hence no change in num and remains 0 only causing infinite loop.
9.Answer :(e)
Reason :For 10 iterations digit becomes 20, but the last statement ( --digit ) causes this to be 19 only
10.Answer :(c)
Reason :Because of the ternary operator
11.Answer :(e)
Reason :As all are valid
12.Answer :(e)
Reason :strcmp returns 0 if both the strings are equal.
13.Answer :(d)
Reason :Because getchar [ getchar( )] is a function in C
14.Answer :(b)
Reason :As the first option is matching, the cases are evaluated till the break statement is encountered or end of switch statement is encountered.
15.Answer :(a)
Reason :Because b, c, e are invalid and d is not correct one
16.Answer :(c)
Reason :According to the definition of fprintf( )
17.Answer :(d)
Reason :Because ‘x’-‘w’ = 120-119. And hence 1/3 results in 0( integer division)
18.Answer :(e)
Reason :It is summation of 1+2+3+4+5 as continue statement is going to be executed for every input of -1, -2, -3, -4, -5.
19.Answer :(a)
Reason :Because of the pointer assignment ( pv = &v causes *pv = 3 )
20.Answer :(b)
Reason :Its equivalent to puchar(0)
21.Answer :(e)
Reason :Because of the XOR ( ^ ) operator
22.Answer :(b)
Reason :According to the concept of nested structures
23.Answer :(a)
Reason :All the other are invalid
24.Answer :(c)
Reason :According to the syntax and definition of sprintf( )
25.Answer :(d)
Reason :According to the ISO standards Binary number system is used for this purpose26.Answer :(a)
Reason :According to the definition of the arrays
27.Answer :(a)
Reason :Concatenation means combining two strings together ( appending the second at the end of first one )
28.Answer :(b)
Reason :According to the syntax of malloc function
29.Answer :(d)
Reason :As # define is the preprocessor directive, is used for defining the symbolic constants, and defining the macros
30.Answer :(c)
Reason :According to the features of ‘C’ language.

No comments: