Self Studies

Programming and Data Structures Test 1

Result Self Studies

Programming and Data Structures Test 1
  • 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

    What is the output of the below given code?

    #include <stdio.h>
    int main()
    {
    int j;
    j = printf("Testbook TestSeries\n");
    j = printf("%d\n",j);
    printf("%d\n",j);
    }

    Solution

    Code:

    #include <stdio.h>
    int main()
    {
    int j;
    j = printf("Testbook TestSeries\n");   \\print Testbook TestSeries and return 20 to j
    j = printf("%d\n",j); \\print 20 and return 3 to j
    printf("%d\n",j);
    }

    Explanation:
    printf return length of the string 
    Since = is right to left associative
    Testbook TestSeries  (will be printed) number of character 20 is returned
    value of j is printed that is 20 and 3 is return 
    at the end value of j is printed that is 3.
    Important Point:

    int j = printf("%d\n",j)   \\ since value of j = 20

    int j = printf("20\n",j)   \\assume  length = 3  (2, 0, \n)

    3 is returned to j

    printf("%d\n",j); \\ hence 3 is printed

  • Question 2
    1 / -0

    Match the following list:

    List – I

    (Storage class)

    List – II

    (Storage, initial – value, scope)

    (a) auto

    (i) register, garbage, local

    (b) register

    (ii) memory, zero, global

    (c) static

    (iii)  memory, garbage, local

    (d) extern

    (iv) memory, zero, local

    Solution

    Concept:

    Storage classes are used to fully define a variable. Storage class tells us that where the variable would be stored, what will be initial value of variable (if initial value is not specifically assigned then default value), what would be the scope of variable and life of variable.

    Explanation:

    Four types of storage classes: auto, register, static and extern.

    auto:

    Features of auto storage class are :

    • Storage is memory
    • Default value is garbage
    • Scope is local to the block in which variable is defined
    • Life till the control remains within the block in which variable is defined.


    register:

    Features of register storage class are:

    • Storage is in CPU registers
    • Default initial value is garbage value
    • Scope is local to the block in which variable is defined
    • Life: till the control remains within the block in which variable is defined


    static:

    Features of static storage class are:

    • Storage is memory
    • Default initial value is zero
    • Scope is local to the block in which variable is defined
    • Life: value of variable persists between different function calls


    extern:

    Features of extern storage class are:

    • Storage is memory.
    • Default initial value is zero
    • Scope is global
    • Life: as long as the program’s execution does not come to an end
  • Question 3
    1 / -0

    What is the output of the below given code?

    #include <stdio.h>
    int main()
    {
    int a = 2;
    int year = 2020;
    char flag = 'a' + a;
    switch(flag)
    {
    case 'a': printf("GATE CS %d ",year);           
    case 'b':
    case 'c': printf("GATE CS %d ",year + 1);
    case 'd': break;
    default: printf("GATE CS %d",year + 2);
    }
    return 0;

    }

    Solution

    int year = 2020;

    char flag = 'a' + a = 'a' + 2 = 'c'

    switch('c')

    control will come to case 'c'

    In case 'c': 

    Statement printf("GATE CS %d ",year + 1); is executed

    Output: GATE CS 2021

    since case 'c': doesn't have break statement it will go to case 'd'

    In case 'd' break will execute and the switch will terminate.

    Therefore, the output is GATE CS 2021.

  • Question 4
    1 / -0

    Consider the below given code in which integer is of 2 bytes?

    #include <stdio.h>
    void function(int *co, int *vid)
    {
    co = vid;
    *co = 19;
    }
    int main()
    {
     int p = 20, q = 19;
    function(&q,&p);
    printf("Co-vid %d%d", p, q);
    return 0;
    }

    The output is Co-vid ____? (Fill in the blanks)

    Solution

    Before function execution: 

    Variable

    p

    q

    value stored

    20

    19

    Assume address

    1000

    1002

    At the time of  function call: 

    Pointer Variable

    co

    vid

    address stored

    1002

    1000

     

    After function execution:

    Pointer Variable

    co

    vid

    address stored

    1000

    1000

     

    *co = 19

    ∴ p = 19

    Variable q remained unchanged

    Variable

    p

    q

    Value

    19

    19

     

    The output is Co-vid 1919.

  • Question 5
    1 / -0

    Which one of the following choices given below would be printed when the following program is executed?

    #include<stdio.h>

    void display(int *tb[]);

    int main()

    {

    int array1[] = {2,5,8,12,6,9};

    int array2[] = {-12, 4, 6, -13,7};

    int array3[] = {-8, -6, -7, 9};

    int array4[] = {21, 23, 27,15,0};

    int *t[] = { array1, array2, array3, array4};

    display(t);

    }

    void display(int *tb[])

    {

    printf("%d and ", *(tb[2]+1));

    printf("%d", tb[3][1]);

    }
    Solution

    Size of a character is 1 byte:

    Assume the starting addresses of array

    array1

    2

    5

    8

    12

    6

    9

    \0

    Address

    101

    102

    103

    104

    105

    106

    107

    array2

    -12

    4

    6

    -13

    7

    \0

     

    Address

    201

    202

    203

    204

    205

    206

     

    array3

    -8

    -6

    -7

    9

    \0

     

     

    Address

    301

    302

    303

    304

    304

     

     

    array4

    21

    23

    27

    15

    0

    \0

     

    Address

    401

    402

    403

    404

    405

    406

     

     

    Assume the addresses of t:

    Address

    value

    701

    101

    702

    201

    703

    301

    704

    401

     

    Assume the addresses of t:

    Address

    value

    900

    701

     

    *(tb[2]+1) = *(*(tb + 2) + 1) = *(*(701 + 2) + 1) = *(301 + 1) = *(302) = -6

    tb[3][1 = *(*(tb +3) + 1) = *(*(701 + 3) + 1) = *(401 + 1) = *(402) = 23

    Important Points:

    Since size of a character is 1 byte it is not included in address calculation
  • Question 6
    1 / -0

    If ‘a’ is declared as one-dimensional array in ‘C’ then

    a) *(a + i) is same as *(&a[i])

    b) *(a + i) is same as *a + i

    c) &a[i] is same as a + i - 1

    d) *(a + i) is same as a[i]

    Which of the above statements are incorrect?
    Solution

    Assume

    Decimal address

    100

    102

    104

    106

    Integer

    array

    a[0]

    a[1]

    a[2]

    a[3]

    corresponding value

    2

    4

    6

    8

    Take, i = 1 and size of interger be 2 bytes

    *(a + i) = *(100 + i × size of integer)

    = *(100 + 1 × 2) = *(102) = 4

     *(&a[2]) = *(102) = 4

    Therefore, *(a + i) is same as *(&a[i])

    *a + i = *(100) + 1 = 2 + 1 = 3

    Therefore, *(a + i) is not same as *a + i

    &a[2] = 102

    a + i - 1 = 100 + 1 - 1 = 100

    Therefore, &a[i] is not same as a + i - 1

    a[i] = a[1] = 4

    Therefore, *(a + i) is same as a[i]

    Hence statement b and c are incorrect

    Code to verify the output: 

    #include<stdio.h>

    int main( )        
    {
    int i = 1;
    int a[] = {2, 4, 6, 8};
    printf("%d = %d\n",*(a + i),*(&a[i])); // 4 = 4
    printf("%d = %d\n",*(a + i), *a + i);  // 4 = 3 (incorrect)
    printf("%d = %d\n",&a[i],a + i - 1);   //address of 2nd element = address of 1st element (incorrect) 
    printf("%d = %d\n",*(a + i),a[i]);   // 4 = 4
    return 0 ;
    }

  • Question 7
    1 / -0

    What is the output of the below given C program?

    #include <stdio.h>
    int main()
    {
    int loops = 0;
    int _shift = 7916; 
    while(_shift)
    {
    loops++;
    _shift >>= 2;
    }
    printf("%d",loops);
    }

    Solution

    The binary representation of 7916 is 1111011101100

    Iteration number

    while

    loops

    Binary equivalent

    1st iteration

     true

    1

    0011110111011

    2nd iteration

    true

    2

    0000111101110

    3rd iteration

    true

    3

    0000001111011

    4th iteration

    true

    4

    0000000011110

    5th iteration

    true

     

    5

    0000000000111

    6th iteration

    true

    6

    0000000000001

    7th iteration

    true

    7

    0000000000000
    8th iterationfalse (while loop terminated)  

     

    loops variable is 7. Therefore the output is 7.

  • Question 8
    1 / -0

    Consider the C program given belwo

    #include <stdio.h>
    struct charNode
    {
    char a, b, c, d; 
    };
    int main()
    {
    struct charNode ch = {'o'+3,'e'+10,'j'+15,'t'+1};
    struct charNode *ptr = &ch;
    printf("%c, %c",*(char *)ptr + 1,*((char *)ptr + 2));
    }

    What is the output?

    Solution

    charNode:

    Data

    'o'+3

    'e'+10

    'j'+15

    't'+1

    Assume address

    200

    201

    202

    203

     

    ch points to 200

    ptr = ch = 200

    (char *)ptr = 200

    'o' + 3 = r

    *(char *)ptr  + 1 = r +1 = 's' 

    *((char *)ptr + 2)) = *(202)

    'j' + 15 = y

    Output = s, y

  • Question 9
    1 / -0

    Consider the following definition of array in C:

    int a [ ] [2] [3]=

          {

             {

                {1, 2, 3},

                {4, 5, 6}

             },

             {

               {10, 20, 30},

               {40, 50, 60}

            },

            {

               {100, 200, 300},

               {400, 500, 600}

       }

    };

    Which of the following expressions would give value 1 for the array defined above?

    (a) a[0] [0] [0]

    (b) * * * a

    (c) * * * a[0]

    (d) * * a[0] [0]

    The correct answer is :
    Solution

    Array representation is somewhat like this:

     1

    2

    3

    10

    20

    30

    100

    200

    300

    4

    5

    6

    40

    50

    60

    400

    500

    600


    (a) a[0] [0] [0]

    When when index value is 0 , it means we are accessing the 1st element . Here it is one. So, a[0][0][0] will give the element 1.

    (b) * * * a

    In case of 1 D array, single * will lead to element at a[0] position. In 2 –D array if we use “**” then it will lead us to element at a[0][0]. In similar way, ***a will give value 1 which is a[0][0][0].

    (c) * * * a[0]

    This is incorrect form. It will not give value 1. When there is *** a already giving value 0. No need to consider a[0] with it. It will lead to one element inside.

    (d) * * a[0] [0]

    It is similar to option c) which is incorrect form for output as 1. It will not result in value 1.
  • Question 10
    1 / -0

    Consider the below given code.

    #include<stdio.h>

    int main()

    {

    static int x[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int i;

    for(int i = 2; i < 7; i++)

    x[x[i]] = x[i];

    int sum = 0;

    for(int j = 0; j < 10;j++)

    sum = sum + x[j];

    printf("%d",sum);

    }

    The value printed of sum is ____.

    Solution

    Array:

    Index

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

     

    Value

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

     

     

    value of i (i<7)

    operation

    value modified

    2

    x[x[2]] = x[2];

    x[3] = 3;

    a[3] = 3

    3

    x[x[3]] = x[3]

    x[3] = x[3]

    x[3] = 3

    a[3] = 3

    4

    x[x[4]] = x[4]

    x[5] =5

    a[5] = 5

    5

    x[x[5]] = x[5]

    x[5] =5

    a[5] = 5

    6

    x[x[6]] = x[6]

    x[7]  = 7

    x[7]  = 7

     

    Modified Array:

    Index

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    Value

    1

    2

    3

    3

    5

    5

    7

    7

    9

    10

     

    sum = 1 + 2 + 3 + 3 + 5 + 5 + 7 + 7 + 9 + 10 = 52.

    Output: 52. 

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