Self Studies

Programming and Data Structures Test 2

Result Self Studies

Programming and Data Structures Test 2
  • Score

    -

    out of -
  • Rank

    -

    out of -
TIME Taken - -
Self Studies

SHARING IS CARING

If our Website helped you a little, then kindly spread our voice using Social Networks. Spread our word to your readers, friends, teachers, students & all those close ones who deserve to know what you know now.

Self Studies Self Studies
Weekly Quiz Competition
  • Question 1
    1 / -0

    Consider the following recursive function.

    Int function (int x, int y) {

                    If (y <= 0) return x;

                    return function (y, x%y);

                    }

    The above recursive function computes ______.

    Solution

    Consider x = 10 and y = 25

    CASE1:

    If (y <= 0) return x;                           // 25<=0 (false)

                    return function (y, x%y);            return function(25, 10)

    CASE 2:

    x = 25, y= 10

    If (y <= 0) return x;                          // 10<=0 (False)

                    return function (y, x%y);        // return function (10, 5)

    CASE 3:

    x =10, y =5

    If (y <= 0) return x;                         // 5<=0 (false)

                    return function (y, x%y);             //return function(5, 0)

    CASE 4:

    x = 5, y =0

    If (y <= 0) return x;                         // condition true, return 5

                    return function (y, x%y);

    Output = 5

    Which is greatest common divisor of 10 and 25.

    So, given program computes GCD of x and y

  • Question 2
    1 / -0

    Consider a 1-D array in which the index starts from 2 and ends at 153. The address of index 2 is 1100 and each data in the array takes 4 words. What is the address of A[125] where 125 is the index?

    Solution

    Data:

    base address = 1100

    starting index = 2

    index = 125

    size of element = 4 word

    Formula:

    Array index starts with 0:

    Address of array location : base address + index × size of data

    Array index starts with 2:

    Address of array location : base address + (index - 2) ×size of data

    Calculation

    Address of A[125] = 1100 + (125 - 2) × 4

    = 1100 + 123 × 4

    = 1592
  • Question 3
    1 / -0

    Consider the below given code:

    #include <stdio.h>
    int main()
    {
    char c[] = "INDIA_CAPITAL_DELHI";
    char *p = c;
    printf("%s", p - p[2] - p[6] + p[7] + p[1]);
    return 0;
    }

    The number of characters in the output is _____.

    Solution
    Index012345678910111213141516171819
    Character

    I

    N

    D

    I

    A

    _

    C

    A

    P

    I

    T

    A

    L

    _

    D

    E

    L

    H

    I

    \0

    Address100101102103104105106107108109        118119
    pointer

    p

     

     

     

    Let us assume ASCII value of A be x

    p =  100

    p[2] = D= x + 3

    p[6] = C = x + 2

    p[7] = A = x

    p[1] = N = x + 13

    p - p[2] - p[6] + p[7] + p[1] = 

    = 100 - (x + 3) - (x + 2)  + x + (x + 13)

    = 108

    print the character staring from address 108 until null (\0) is encountered

    Output: PITAL_DELHI

    ∴ number of character in the output character array is 11

     

  • Question 4
    1 / -0

    Consider the following belw mentioned C code:

    #include <stdio.h>
    int call( ) {
    static int x = 13;
    return x--;
    }
    int main( ) {
    int count = 1;
    for(call( ); call( ); call( ))
    count = count + call();
    printf("%d", count);
    return 0;
    }

    What is the output on the execution of the program?
    Solution

    Initially count = 0

    Iteration

    Function call

    Value returned by function

    Explanation

    count

    1st iteration:

    for (call( ); call( ); call( ) )

    count  = count  + call( )

    for (13; 12; fun ( ) )

    count = 1 + 11

    → Initialization statement: Optional

    → Conditional statement = 12 and x = 11

    12

    2nd iteration:

    for (call( )call( )call( ))

    count  = count  + call( )

    for (13; 9; 10)

    sum = 12 + 8

    → Incremental statement = 10 and x = 9

    → Conditional statement = 9 and x = 8

    20

    3rd iteration

    for (call( )call( )call( ))

    count  = count  + call( )

    for (13; 6; 7)

    count = 20 + 5

    → Incremental statement = 7 and x = 6

    → Conditional statement = 6 and x = 5

    25

    4th  iteration

    for (call( ); call( )call( ))

    count  = count  + call( )

    for (13; 3; 4)

    count = 25 + 2

    → Incremental statement = 4 and x = 3

    → Conditional statement = 3 and x = 2

    27

    5th iteration

    for (call( ); call( )call( ))

    scount  = count  + call( )

    for (13; 0; 1)

    count statement will not execute

    → Incremental statement = 1 and x = 1

    → Conditional statement = 0 and x = -1

    → Loops conditions is false

    27

     

    Output of the code is 27.

    Important Points:

    Initialization statement is executed only once.

    Incremental statement is executed at the end of every iteration
  • Question 5
    1 / -0
    A is a 2D-array with the range [-5…..5, 3…..13] of elements. The starting location is 100. Each element occupies 2 memory cells. Calculate the location of A[0] [8] using column major order.
    Solution

    Concepts:

    Range of given array: [-5....5,3......13]

    Old location (Given): A[0][8]

    A[-5][3] = 100

    Number of rows: 5 – (–5) + 1= 11

    Number of columns: 13 – 3 + 1 = 11

    Array size: A11 x 11

    New range: [0 ...10, 0 ... 10]

    New location: A[0 + 5][8 -3] = A[5][5]

    Size of cell = 2

    A[0][0] = 100

    Formula:

    A[n][m] = A[0][0] + (11m + n)× Size of cell

    Calculation:

    A[5][5] = 100 + (11× 5 + 5)× 2

    ∴ A[5][5] = 100 + 60 × 2

    ∴ A[5][5] = 220.

    Old location of A[0] [8] = New location of A[5] [5]  = 220.

  • Question 6
    1 / -0

    Consider the following C functions.

    int f1 (int m) {

       static int j = 0;

       if (m > 0) {

          ++j;

          f1(m-1);

       }

       return (j) ;

    }

    int f2 (int m) {

       static int j = 0;

       if (m > 0) {

          j = j + f1(m) ;

          f2(m-1) ;

       }

       return (j) ;

    }

     

    The return value of f2(4) is ______
    Solution
    • Variable ijis static in both the functions thus its value will persist throughout the program.
    • When f2 (4) gets called, it will first call f1 with the value of n as 5 and then f2 will get called with n = 4.
    • Similarly, the same steps are taken for all the upcoming values of n.

     

    f2(4) called

    0 + fun1 (4)

    • f1 (4) called
    • f1 (3)
    • f1 (2)
    • f1 (1)
    • f(0) -> fun1 (0) returns 0 + 4 = 4.
    • f1 (1) -> 4
    • f1 (2) -> 4
    • f1 (3) -> 4
    • f1 (4) -> 4

     

    Fun2 (3) called

    4 + fun1 (3)

    • f1 (3) called
    • f1 (2)
    • f1 (1)
    • f1 (0) -> f1 (0) returns 4 + 3 = 7.
    • f1 (1) -> 7
    • f1 (2) -> 7
    • f1 (3) -> 7

     

    f2(2) called

    4 + 7 + fun1 (3)

    • f1 (2) called
    • f1 (1)
    • f1 (0) -> f1 (0) returns 7 + 2 = 9.
    • f1 (1) -> 9
    • f1 (2) -> 9

     

    f2(1) called

    4 + 7 + 9 + fun1 (3)

    • f1 (1) called
    • f1 (1)
    • f1 (0) -> f1 (0) returns 9 + 1 = 10.
    • fun1 (1) -> 10

     

    f2 (0) called

    • f2 (0) returns 4 + 7 + 9 + 10 = 30
    • f2 (1) -> 30
    • f2 (2) -> 30
    • f2 (3) -> 30
    • f2 (4) -> 30
    • f2 (5) -> 30
Self Studies
User
Question Analysis
  • Correct -

  • Wrong -

  • Skipped -

My Perfomance
  • Score

    -

    out of -
  • Rank

    -

    out of -
Re-Attempt Weekly Quiz Competition
Selfstudy
Selfstudy
Self Studies Get latest Exam Updates
& Study Material Alerts!
No, Thanks
Self Studies
Click on Allow to receive notifications
Allow Notification
Self Studies
Self Studies Self Studies
To enable notifications follow this 2 steps:
  • First Click on Secure Icon Self Studies
  • Second click on the toggle icon
Allow Notification
Get latest Exam Updates & FREE Study Material Alerts!
Self Studies ×
Open Now