Programming and data structure miscellaneous


Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following snippet of a C program. Assume that swap (&x, &y) exchanges the contents of x and y.
    int main() {
    int array [] = {3, 5, 1, 4, 6, 2};
    int done = 0;
    int i;
    while {done == 0) {
      done = 1;
      for (i=0; i <=4; i + +) {
       if (array[i] < array[i+1]) {
        swap (&array [i], &array [i+1]);
          done = 0;
       }
      }
      for (i = 5; i>=l; i – –) {
       if (array [i] > array[i – 1]) {
          swap (&array [i], &array [i – 1]);
         done = 0;
         }
       }
     }
      printf (“%d”, array[3]);
    }
    The output of the program is _______.









  1. View Hint View Answer Discuss in Forum

    The initial contents of the Array is :-

    For first loop : i very from (0 to 4), then the contents of the Array are :

    For second loop : i very from (5 to 1) then the contents of the Array are :

    Now, since done = 0, then the for loops will execute again.
    For first loop : again i is very from 0 to 4, then the contents of the Array are :

    For second loop : Again i is very from (5 to 1) then the contents of the array are :

    The value of done is still "zero". Hence the for loop will execute again.
    First for loop :
    This time there will be no change by the for loop.
    Then the value of done is '1', hence the loop terminate here, then the final contents of the Array are :

    Hence, the output of the program array [3] is '3'. Hence answer is 3.

    Correct Option: D

    The initial contents of the Array is :-

    For first loop : i very from (0 to 4), then the contents of the Array are :

    For second loop : i very from (5 to 1) then the contents of the Array are :

    Now, since done = 0, then the for loops will execute again.
    For first loop : again i is very from 0 to 4, then the contents of the Array are :

    For second loop : Again i is very from (5 to 1) then the contents of the array are :

    The value of done is still "zero". Hence the for loop will execute again.
    First for loop :
    This time there will be no change by the for loop.
    Then the value of done is '1', hence the loop terminate here, then the final contents of the Array are :

    Hence, the output of the program array [3] is '3'. Hence answer is 3.


  1. Let A be an array of 31 numbers consisting of a sequence of 0’s followed by a sequence of l’s. The problem is to find the smallest index i such that A[i] is 1 by probing the minimum number of locations in A. The worst case number of probes performed by an optimal algorithm is _____.









  1. View Hint View Answer Discuss in Forum

    To find out the smallest index i such that A[i] is 1 by probing the minimum number of location in A. Assume i be the index of first element and j be the index of last element.
    By using the binary search algorithm with some changes.
    Now,

    Find middle element of Array = A
    low + high
    2

    Assuming ‘A’ is an array of 31 elements with ‘1’ and if it is ‘1’, we check the left part recursively and if it is ‘0’, we check the right part of the array recursively. Which take log2 (31) comparisons in the worst case, so the total worst case probes is
    ⇒ Ceil (log2 31) = 5
    (∵ Ceil means compute fast log base 2 ceiling) 5 probes performed by an optimal algorithm. To find out the smallest index i such that A[i] is 1 by probing the minimum number of location in A. Assume i be the index of first element and j be the index of last element.
    By using the binary search algorithm with some changes.
    Now,
    Find middle element of Array = A
    low + high
    2

    Assuming ‘A’ is an array of 31 elements with ‘1’ and if it is ‘1’, we check the left part recursively and if it is ‘0’, we check the right part of the array recursively. Which take log2 (31) comparisons in the worst case, so the total worst case probes is
    ⇒ Ceil (log2 31) = 5
    (∵ Ceil means compute fast log base 2 ceiling) 5 probes performed by an optimal algorithm.

    Correct Option: B

    To find out the smallest index i such that A[i] is 1 by probing the minimum number of location in A. Assume i be the index of first element and j be the index of last element.
    By using the binary search algorithm with some changes.
    Now,

    Find middle element of Array = A
    low + high
    2

    Assuming ‘A’ is an array of 31 elements with ‘1’ and if it is ‘1’, we check the left part recursively and if it is ‘0’, we check the right part of the array recursively. Which take log2 (31) comparisons in the worst case, so the total worst case probes is
    ⇒ Ceil (log2 31) = 5
    (∵ Ceil means compute fast log base 2 ceiling) 5 probes performed by an optimal algorithm.



  1. The value of j at the end of the execution of the following C program int incr (int i) { static int count = 0; count = count + i; return (count); } main () {
    int i j; for (i = 0; i < = 4; i + +) j = incr (i); } is









  1. View Hint View Answer Discuss in Forum

    Count is static variable in incr(). Statement static int count = 0 will assign count to 0 only in first call. Other calls to this function will take the old values of count. Count will become 0 after the call incr(0). Count will become 1 after the call incr(1) Count will become 3 after the call incr(2) Count will become 6 after the call incr(3) Count will become 10 after the call incr(4)

    Correct Option: A

    Count is static variable in incr(). Statement static int count = 0 will assign count to 0 only in first call. Other calls to this function will take the old values of count. Count will become 0 after the call incr(0). Count will become 1 after the call incr(1) Count will become 3 after the call incr(2) Count will become 6 after the call incr(3) Count will become 10 after the call incr(4)


  1. The number of tokens in the following C statements print
    f(“i =% d, & i =% x”, i & i) is









  1. View Hint View Answer Discuss in Forum

    A token is strictly a string of characters that the compiler replaces with another string of characters. Token are defined as a sort of search and replace. Token can be used for common phrases, page elements, handy shortcuts, etc. Token are defined with either the define or replace tags and can use single or paired tag syntax.
    Examples

       

       <:replace [mailto]     mailto: [email] > [email] :>

    Correct Option: C

    A token is strictly a string of characters that the compiler replaces with another string of characters. Token are defined as a sort of search and replace. Token can be used for common phrases, page elements, handy shortcuts, etc. Token are defined with either the define or replace tags and can use single or paired tag syntax.
    Examples

       

       <:replace [mailto]     mailto: [email] > [email] :>



  1. Consider the following C declaration
    struct {
        short s [5]
       union {
       float y;
       long z;
       } u;
    } t;
    Assume that objects of the type short, float and long occupy 2 byte, 4 byte and 8 byte, respectively. The memory requirement for variable t, ignoring alignment considerations, is









  1. View Hint View Answer Discuss in Forum

    The amount of memory required to store a structure variable is the sum of the sizes of all its members. But in the case of union, the amount of memory required is the amount required by its largest member. Therefore u, which is a union member of the struct, occupies only 8 bytes of memory, because the largest memory is 8 bytes consumed by long z;. Another member in the struct is short s [5], this will occupy 10 bytes of memory (2 bytes x 5).

    Correct Option: C

    The amount of memory required to store a structure variable is the sum of the sizes of all its members. But in the case of union, the amount of memory required is the amount required by its largest member. Therefore u, which is a union member of the struct, occupies only 8 bytes of memory, because the largest memory is 8 bytes consumed by long z;. Another member in the struct is short s [5], this will occupy 10 bytes of memory (2 bytes x 5).