Loading

SET 6


  Question 1

What does the following C-statement declare?
int ( * f) (int * ) ;


A : A function that takes an integer pointer as argument and returns an integer.
B : A function that takes an integer as argument and returns an integer pointer.
C : A pointer to a function that takes an integer pointer as argument and returns an integer.
D : A function that takes an integer pointer as argument and returns a function pointer


  •  
    .

     Correct answer is :C


  •   Question 2

    An Abstract Data Type (ADT) is:

    A : Same as an abstract class
    B : A data type that cannot be instantiated
    C : A data type type for which only the operations defined on it can be used, but none else
    D : All of the above


  •  
    .

     Correct answer is :C

     Solution :
      An abstract data type (ADT) is a mathematical model for data types where a data type is defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations. This contrasts with data structures, which are concrete representations of data, and are the point of view of an implementer, not a user.

  •   Question 3

    A common property of logic programming languages and functional languages is:

    A : both are procedural languages
    B : both are based on λ -calculus
    C : both are declarative
    D : both use Horn-clauses


  •  
    .

     Correct answer is :C


  •   Question 4

    Which one of the following are essential features of an object-oriented programming language?
    (i) Abstraction and encapsulatoin
    (ii) Strictly-typedness
    (iii) Type-safe property coupled with sub-type rule
    (iv) Polymorphism in the presence of inheritance


    A : (i) and (ii) only
    B : (i) and (iv) only
    C : (i),(ii) and (iv) only
    D : (i),(iii) and (iv) only


  •  
    .

     Correct answer is :B

     Solution :
      Abstraction, Encapsulation, Polymorphism and Inheritance are the essential features of a OOP Language

  •   Question 5

    Consider the following C-program:
    void foo(int n, int sum)
    {
    int k = 0, j = 0;
    if (n == 0) return;
    k = n % 10;
    j = n / 10;
    sum = sum + k;
    foo (j, sum);
    printf ("%d,", k);
    }
    int main ()
    {
    int a = 2048, sum = 0;
    foo (a, sum);
    printf ("%d\n", sum);
    getchar();
    }
    What does the above program print?


    A : 8, 4, 0, 2, 14
    B : 8, 4, 0, 2, 0
    C : 2, 0, 4, 8, 14
    D : 2, 0, 4, 8, 0


  •  
    .

     Correct answer is :D

     Solution :
      sum has no use in foo(), it is there just to confuse. Function foo() just prints all digits of a number. In main, there is one more printf statement after foo(), so one more 0 is printed after all digits of n.

  •   Question 6

    Consider the following C-program:
    double foo (double); /* Line 1 */
    int main()
    {
    double da, db;
    // input da
    db = foo(da);
    }
    double foo(double a)
    {
    return a;
    }
    The above code compiled without any error or warning. If Line 1 is deleted, the above code will show:


    A : no compile warning or error
    B : some compiler-warnings not leading to unintended results
    C : some compiler-warnings due to type-mismatch eventually leading to unintended results
    D : compiler errors


  •  
    .

     Correct answer is :D


  •   Question 7

    Consider the following C-function:
    double foo (int n)
    {
        int i;
        double sum;
        if (n = = 0) return 1.0;
        else
        {
            sum = 0.0;
            for (i = 0; i < n; i++)
                sum += foo (i);
            return sum;
        }
    }

    The space complexity of the above function is:


    A : O(1)
    B : O(n)
    C : O(n!)
    D : O(nn)


  •  
    .

     Correct answer is :B

     Solution :
      Note that the function foo() is recursive. Space complexity is O(n) as there can be at most O(n) active functions (function call frames) at a time.

  •   Question 8

    Consider the following C-function:
    double foo (int n)
    {
        int i;
        double sum;
        if (n = = 0) return 1.0;
        else
        {
            sum = 0.0;
            for (i = 0; i < n; i++)
                sum += foo (i);
            return sum;
        }
    }
    

    Suppose we modify the above function foo() and store the values of foo (i), 0 < = i < n, as and when they are computed. With this modification, the time complexity for function foo() is significantly reduced. The space complexity of the modified function would be


    A : O(1)
    B : O(n)
    C : O(n!)
    D : O(nn)


  •  
    .

     Correct answer is :B

     Solution :
      Space complexity now is also O(n). We would need an array of size O(n). The space required for recursive calls would be O(1) as the values would be taken from stored array rather than making function calls again and again.

  •   Question 9

    Consider the following C-program fragment in which i, j and n are integer variables.
    for (i = n, j = 0; i >0; i /= 2, j += i);
    et val(j) denote the value stored in the variable j after termination of the for loop. Which one of the following is true?


    A : val(j) = θ(logn)
    B : val(j) = θ(√n)
    C : val(j) = θ(n)
    D : val(j) = θ(n logn)


  •  
    .

     Correct answer is :C

     Solution :
      Note the semicolon after the for loop, so there is nothing in the body. The variable j is initially 0 and value of j is sum of values of i. i is initialized as n and is reduced to half in each iteration.
    j = n/2 + n/4 + n/8 + .. + 1 = θ(n)

  •   Question 10

    Consider the following C-function in which a[n] and b[m] are two sorted integer arrays and c[n + m] be another integer array.
    void xyz(int a[], int b [], int c[])
    {
      int i, j, k;
      i = j = k = O;
      while ((i<n) && (j<m))
         if (a[i] < b[j]) c[k++] = a[i++];
         else c[k++] = b[j++];
    }
    

    Which of the following condition(s) hold(s) after the termination of the while loop?
    (i) j < m, k = n+j-1, and a[n-1] < b[j] if i = n
    (ii) i < n, k = m+i-1, and b[m-1] <= a[i] if j = m


    A : only (i)
    B : only (ii)
    C : Either (i) or (ii) but not both
    D : neither (i) nor (ii)


  •  
    .

     Correct answer is :C

     Solution :
      The function xyz() is similar to merge() of mergeSort(). The condition (i) is true if the last inserted element in c[] is from a[] and condition (ii) is true if the last inserted element is from b[].

  •   Question 11

    Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code.
    subroutine swap(ix,iy)
         it = ix
    L1 : ix = iy
    L2 : iy = it
    end
      ia = 3
      ib = 8
      call swap (ia, 1b+5)
      print *, ia, ib
    end 

    S1: The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell swap
    S2: On execution the code will generate a runtime error on line L1
    S3: On execution the code will generate a runtime error on line L2
    S4: The program will print 13 and 8 S5: The program will print 13 and -2 Exactly the following set of statement(s) is correct:


    A : S1 and S2
    B : S1 and S4
    C : S3
    D : S1 and S5


  •  
    .

     Correct answer is :A


  •   Question 12

    Consider this C code to swap two integers and these five statements after it:
    void swap(int *px, int *py) 
    { 
       *px = *px - *py; 
       *py = *px + *py; 
       *px = *py - *px; 
    }
    

    S1: will generate a compilation error
    S2: may generate a segmentation fault at runtime depending on the arguments passed
    S3: correctly implements the swap procedure for all input pointers referring to integers stored in memory locations accessible to the process
    S4: implements the swap procedure correctly for some but not all valid input pointers S5: may add or subtract integers and pointers.


    A : S1
    B : S2 and S3
    C : S2 and S4
    D : S2 and S5


  •  
    .

     Correct answer is :C

     Solution :
      S2: May generate segmentation fault if value at pointers px or py is constant or px or py points to a memory location that is invalid
    S4: May not work for all inputs as arithmetic overflow can occur

  •   Question 13

    Consider the following C code segment.
    for (i = 0, i<n; i++)
    {
        for (j=0; j<n; j++)
        {
            if (i%2)
            {
                x += (4*j + 5*i);
                y += (7 + 4*j);
            }
        }
    }

    Which one of the following is false?


    A : The code contains loop invariant computation
    B : There is scope of common sub-expression elimination in this code
    C : There is scope of strength reduction in this code
    D : There is scope of dead code elimination in this code


  •  
    .

     Correct answer is :B


  • MY REPORT
    TOTAL = 13
    ANSWERED =
    CORRECT / TOTAL = /13
    POSITIVE SCORE =
    NEGATIVE SCORE =
    FINAL SCORE =