Code: C-05 / T-05                                                                         Subject: PROGRAMMING  & PROBLEM SOLVING THROUGH’ C

                                                                                                                           Flowchart: Alternate Process: December 2005      

Time: 3 Hours                                                                                                      Max. Marks: 100

 

NOTE: There are 9 Questions in all.

·      Question 1 is compulsory and carries 20 marks. Answer to Q. 1. must be written in the space provided for it in the answer book supplied and nowhere else.

·      Out of the remaining EIGHT Questions answer any FIVE Questions. Each question carries 16 marks.

·      Any required data not explicitly given, may be suitably assumed and stated.

Q.1       Choose the correct or best alternative in the following:                       (2x10)

                

             a.  Consider the following code segment:

                  int a[10], *p1, *p2;

                  p1 = &a[4];

                  p2 = &a[6];

                  Which of the following statements is incorrect w.r.t. pointers? 

 

                  (A)  p1 + 2                                           (B)  p2 – 2

                  (C)  p2 + p1                                         (D)  p2 – p1                                         

                                                                                                                                                                 

             b.   The second expression (j – k) in the following expression will be evaluated

                   (i + 5) && (j – k)

(A)    if expression (i + 5) is true.           

(B)    if expression (i + 5) is false.

(C)  irrespective of whether (i + 5) is true or false. 

(D)  will not be evaluated in any case.

 

             c.   In the for statement:     for (exp1; exp2; exp3) { … }

                   where exp1, exp2 and exp3 are expressions. What is optional?

(A)    None of the expressions is optional.

(B)    Only exp1 is optional.

(C)    Only exp1 and exp3 are optional.

(D)   All the expressions are optional.

                                                                                                                     

             d.   The output of the following code segment will be

                   char x = ‘B’;

      switch (x) {

      case ‘A’: printf(“a”);

      case ‘B’: printf(“b”);

      case ‘C’: printf(“c”);

                    }

                   (A)  B                                                 (B)  b

                   (C)  BC                                               (D)  bc

 

             e.   What will be the output of the following code segment?

  main( ) {

      char      s[10];

      strcpy(s, “abc”);

      prinf(“%d %d”, strlen(s), sizeof(s));

                   }                                                                          

(A)     3 10                                             (B)  3 3

(C)  10 3                                             (D)  10 10

             f.    Which of the following is the odd one out?

 

(A)     j = j + 1;                                      (B)  j =+ 1;

(C)  j++;                                             (D)  j += 1;

 

             g.   Which of the following is true for the statement:

                   NurseryLand.Nursery.Students = 10;

 

(A)     The structure Students is nested within the structure Nursery. 

(B)     The structure NurseryLand is nested within the structure Nursery.

(C)     The structure Nursery is nested within the structure NurseryLand.

(D)    The structure Nursery is nested within the structure Students. 

 

             h.   What will be the output of the following code segment, if any?

      myfunc ( struct test t) {

      strcpy(t.s, “world”);

      }

 main( ) {

      struct test { char s[10]; } t;

     

      strcpy(t.s, “Hello”);

      printf(“%s”, t.s);

      myfunc(t);

      printf(“%s”, t.s);

                   }

 

(A)    Hello Hello                                   (B) world world

(C) Hello world                                   (D) the program will not compile

 

             i.    If a function is declared as void fn(int *p), then which of the following statements is valid to call function fn?

 

(A)   fn(x) where x is defined as int x; 

(B)   fn(x) where x is defined as int *x;

(C) fn(&x) where x is defined as int *x;   

(D) fn(*x) where x is defined as int *x;

 

             j.    What is the following function computing? Assume a and b are positive integers.

  int fn( int a, int ) {

      if (b == 0)

                  return b;

      else

                  return (a * fn(a, b - 1));

                   }

 

(A)  Output will be 0 always                (B)  Output will always be b

(C)  Computing ab                               (D)  Computing a + b


 

 

Answer any FIVE Questions out of EIGHT Questions.

Each question carries 16 marks.

 

  Q.2     a.   Write a function to display the binary number corresponding to the integer passed to it as an argument.                                                                   (6)

       

             b.   Write a function, my_atoi, similar to the library function atoi that returns the nu7meric value corresponding to the string passed to it as an argument.   (6)

 

             c.   Briefly explain what do you understand by stepwise refinement of the program?                  (4)

 

  Q.3     a.   Write a function to remove duplicates from an ordered array. For example, if input is: a,a,c,d,q,q,r,s,u,w,w,w,w; then the output should be a,c,d,q,r,s,u,w.                                   (8)

 

             b.   Write a function to sort the characters of the string passed to it as argument.                       (8)       

 

  Q.4     a.   Give the outputs of the following code segments, if any and justify your answers.

                   (i)  #define CUBE(x)               (x * x * x)

                   main( ) {

                   printf(“%d”, CUBE(4+5));

                   }

                   (ii) int j = 5;

                   printf(“%d”, j = j == 6);

                   printf(“%d”, j = ++j == 6);

                   (iii)   for (j = 0; j = 3; j++)

                   printf(“%d”, j);

                   (iv)   main( ) {

                   static char a[ ] = “Test String”;

                   static char *b = “Test String”;

                   printf(“%d %d”, sizeof(a), sizeof(b));

                   }

                   (v) main( ) {

                   enum test {RED, BLUE, GREEN};

                   enum test t = BLUE;

                   printf(“%d”, t);

                   }     

                   (vi)   main( ) {

                   union U { int j; char c; float f; } u;

                   u.j = 10; u.c = ‘A’; u.f = 99.99;

                   printf(“u.j = %d u.c = %c u.f = %f”, u.j, u.c, u.f);

                   }                                                                                                                 (2 x 6)

 

             b.   What are preprocessor directives? List three types of them. What is the difference between the following directives: #include <filename> and #include “filename”?                                                     (4)

  Q.5     a.   Write a function to compute the frequency of ‘the’ in a file.                                  (8)

       

             b.   Write a recursive function to print the reverse of a string passed to it as an argument.                       (8)

 

  Q.6     a.   Write a program which accepts two file names from the command line and prints whether the contents of the two files are same or not. If not, then print the first line number and then the contents of the lines from which they differ. Assume that the lines in both the files have at most 80 characters.                      (12)

 

            b.   Differentiate between the following:

                   (i)    call by value and call by reference

                   (ii)   do..while and while loops                                                                            (4)

 

  Q.7     a.   Write a program to generate a triangle as shown below for n = 4. The program should take the input from the user.

                                                                        A

                                                                  A   B    A

                                                            A   B    C    B   A

                                                      A   B    C   D    C   B    A                                                (8)

 

             b.   Write a function that accepts two strings str1 and str2 as arguments and finds which of the two is alphabetically greater (without using the library functions). The function should return 1 if str1 is greater than str2, 0 if str1 is equal to str2, and -1 is str1 is smaller than str2.                           (8)

 

  Q.8     a.   Consider a linked list to store a polynomial, that is, every node of the linked list has coefficient, exponent and pointer to the next node in the list.

                   (i) Define a structure for node of such a list.

                   (ii)Write a function to subtract two such polynomials. The function should accept pointers to the two polynomials as arguments and return the pointer to the resultant polynomial. Assume that the polynomials passed to the function are in decreasing order on the exponents.                   (2 + 8)

 

             b.   Differentiate between the following:

                   (i)    compiler and interpreter

                   (ii)   unit testing and integration testing

                   (iii)  syntax errors and logical errors                                                                    (6)

 

  Q.9     a.   Write a function to reverse the links in a linked list such that the last node becomes the first and the first becomes the last by traversing the linked list only once.                                                       (8)

 

             b.   Define a structure for an employee of an organization having employee code, name, address, phone number and number of dependents. Assume that “allEmployees” is an array of employees in ascending order on the employee code. Write a function to display the details of an employee given its employee code.                                                                 (8)