Programming and data structure miscellaneous


Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the function func shown below :
    int func(int num) {
    int count = 0;
        while (num) {
       count++; num>>= 1;
      }
    return (count);
    }
    The value returned by func (435) is __________.









  1. View Hint View Answer Discuss in Forum

    435 ⇒ 110110011 num > > 1
    Thus a num is shifted one bit right every time while loop is executed.
    While loop is executed 9 times successfully, and 10th time num is zero.
    &there; Count is incremented 9 times.

    Correct Option: A

    435 ⇒ 110110011 num > > 1
    Thus a num is shifted one bit right every time while loop is executed.
    While loop is executed 9 times successfully, and 10th time num is zero.
    &there; Count is incremented 9 times.


  1. Consider the C function given below.
    int f(int j)
    {
       static int i = 50;
       int k;
       if (i == j)
         {
         printf(“something”);
         k = f(i);
         return 0;
       }
      else return 0;
    }
    Which one of the following is TRUE ?









  1. View Hint View Answer Discuss in Forum

    When j = 50, j (50), the function goes to an infinite loop by calling f(i) recursively for i = j = 50

    Correct Option: D

    When j = 50, j (50), the function goes to an infinite loop by calling f(i) recursively for i = j = 50



  1. Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3 . If n is large, which one of the following statements is most likely to set p correctly?









  1. View Hint View Answer Discuss in Forum

    P = nC3

    =
    n(n - 1)(n - 2)
    6

    If we multiply, n, (n – 1) and (n – 2) together, it may go beyond the range of unsigned integer. (\ option (a) and (d) are wrong) For all values of n, n(n – 1)/2 will always be an integer value, But for n(n – 1)/3, it is not certain.
    Take options (b)
    P =
    n(n - 1) / 2
    ×
    (n - 2) / 3
    P1P2

    P1 will be having no error, thus P will be more accurate Option (c)
    P =
    n(n - 1) / 3
    ×
    (n - 2) / 2
    P1P2

    There is possibility of truncation in P1, the accuracy of P is less

    Correct Option: B

    P = nC3

    =
    n(n - 1)(n - 2)
    6

    If we multiply, n, (n – 1) and (n – 2) together, it may go beyond the range of unsigned integer. (\ option (a) and (d) are wrong) For all values of n, n(n – 1)/2 will always be an integer value, But for n(n – 1)/3, it is not certain.
    Take options (b)
    P =
    n(n - 1) / 2
    ×
    (n - 2) / 3
    P1P2

    P1 will be having no error, thus P will be more accurate Option (c)
    P =
    n(n - 1) / 3
    ×
    (n - 2) / 2
    P1P2

    There is possibility of truncation in P1, the accuracy of P is less


  1. Consider the following pseudo code. What is the total number of multiplications to be performed ?
    D = 2
    for i = 1 to n do
       for j = i to n do
         for k = j + 1 to n do
         D = D*3









  1. View Hint View Answer Discuss in Forum

    One-sixth of the product of the 3 consecutive integers.

    Correct Option: C

    One-sixth of the product of the 3 consecutive integers.



  1. Consider the following program in C language :
    #include
    main ()
    {
    int i;
    int *pi = &i;
    scanf ("%d", pi);
    printf ("%d\n", i + 5);
    }
    Which one of the following statements is TRUE ?









  1. View Hint View Answer Discuss in Forum

    We concentrate on following code segment :
        → int *Pi = & i;
    Nothing wrong as ‘Pi’ is declared as integer pointer and is assigned the address of ‘i’ which is an “int”.
        → scanf (“%d”, Pi);
    We know that scanf () has two arguments :
    First is control string (“%d” in this case), telling it what to read from the keyboard.
    Second is the address where to store the read item. So, ‘Pi’ refers to the address of “i”, so the value scanned by scanf () will be stored at the address of ‘i’.
    Above statement is equivalent to :
        scanf (“%d”, & i);
    Finally the best statement :
        printf (“%d\n”, i + 5); It prints the value 5 more than the value stored in i. So, the program executes successfully and prints the value 5 more than the integer value entered by the user.

    Correct Option: D

    We concentrate on following code segment :
        → int *Pi = & i;
    Nothing wrong as ‘Pi’ is declared as integer pointer and is assigned the address of ‘i’ which is an “int”.
        → scanf (“%d”, Pi);
    We know that scanf () has two arguments :
    First is control string (“%d” in this case), telling it what to read from the keyboard.
    Second is the address where to store the read item. So, ‘Pi’ refers to the address of “i”, so the value scanned by scanf () will be stored at the address of ‘i’.
    Above statement is equivalent to :
        scanf (“%d”, & i);
    Finally the best statement :
        printf (“%d\n”, i + 5); It prints the value 5 more than the value stored in i. So, the program executes successfully and prints the value 5 more than the integer value entered by the user.