Loading

SET 3


  Question 1

What does the following fragment of C-program print?
char c[]= "GATE2011";
char *p =c;
printf ("%s", p+p[3]- p[1]);


A : GATE2011
B : E2011
C : 2011
D : 011


  •  
    .

     Correct answer is :C

     Solution :
      p[3] - p[1] = ASCII value of E - ASCII value of A = 4 So the expression p + p[3] - p[1] becomes p + 4 which is base address of string 2011.

  •   Question 2

    What will be the output of the following C program segment?
    Char inChar = ‘A’ ;
    switch (inChar ) {
    case ‘A’ : printf (“Choice A\ n”);
    case ‘B’ :
    case ‘C’ : print f(“Choice B”);
    case ‘D’ :
    case ‘E’ :
    default : printf (“No Choice”) ; }


    A : No choice
    B : Choice A
    C : Choice A
    Choice B No choice

    D : Program gives no output as it is erroneous


  •  
    .

     Correct answer is :C

     Solution :
      Since there is no ‘break’ statement , the program executes all the subsequent case statements after printing “choice A”

  •   Question 3

    Consider the program given below, in a block-structured pseudo-language with lexical scoping and nesting of procedures permitted.
    Program main;
    Var . . .
    Procedure A1;
    Var ….
    Call A2;
    End A1
    Procedure A2;
    Var . . .
    Procedure A21;
    Var . . .
    Call A1;
    End A21
    Call A21;
    End A2
    Call A1;
    End main.
    Consider the calling chain: Main -> A1 -> A2 -> A21 -> A1
    The correct set of activation records along with their access links is given by


    A : B :
    C : D :


  •  
    .

     Correct answer is :D

     Solution :
      Access link is defined as link to activation record of closest lexically enclosing block in program text, so the closest enclosing blocks respectively for A1 ,A2 and A21 are main , main and A2

  •   Question 4

    Consider the following C code segment:
    int a, b, c = 0;
    void prtFun(void);
    main( )
    {
    static int a = 1; /* Line 1 */
    prtFun( );
    a + = 1;
    prtFun( )
    printf(“\n %d %d “, a, b);
    }
    void prtFun(void)
    { static int a=2; /* Line 2 */
    int b=1;
    a+=++b;
    printf(“\n %d %d “, a, b);
    }
    What output will be generated by the given code segment if:
    Line 1 is replaced by auto int a = 1;
    Line 2 is replaced by register int a = 2;


    A : 3 1
    4 1
    4 2

    B : 4 2
    6 1
    6 1

    C : 4 2
    6 2
    2 0

    D : 4 2
    4 2
    2 0



  •  
    .

     Correct answer is :D

     Solution :
      Static local variables: Scope is limited to function/block but life time is entire program.
    Automatic local variables: Storage allocated on function entry and automatically deleted or freed when the function is exited.
    Register variables: Same as automatic variables except that the register variables will not have addresses Hence may not take the address of a register variable.

  •   Question 5

    Consider the following C code segment:
    int a, b, c = 0;
    void prtFun(void);
    main( )
    {
    static int a = 1; /* Line 1 */
    prtFun( );
    a + = 1;
    prtFun( )
    printf(“\n %d %d “, a, b);
    }
    void prtFun(void)
    { static int a=2; /* Line 2 */
    int b=1;
    a+=++b;
    printf(“\n %d %d “, a, b);
    }
    What output will be generated by the given code segment?


    A : 3 1
    4 1
    4 2

    B : 4 2
    6 1
    6 1

    C : 4 2
    6 2
    2 0

    D : 3 1
    5 2
    5 2



  •  
    .

     Correct answer is :C


  •   Question 6

    What is the return value of f (p,p) if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
    int f (int &x, int c) {
    c =c- 1;
    if (c== 0)
    return 1;
    x= x+ 1;
    return f (x,c) * x;
    }


    A : 3024
    B : 6561
    C : 55440
    D : 161051


  •  
    .

     Correct answer is :B


  •   Question 7

    Consider the following function
    int unknown (int n)
    {
    int i, j, k =0;
    for (i= n / 2; i<= n; i++)
    for( j= 2; j<= n; j= j* 2)
    k= k+ n / 2;
    return (k) ;
    }
    The return value of the function is


    A : θ (n2)
    B : θ (n2 log n)
    C : θ (n3)
    D : θ (n3 log n)


  •  
    .

     Correct answer is :B


  •   Question 8

    The procedure given below is required to find and replace certain characters inside an input character string supplied in array A. The characters to be replaced are supplied in array oldc, while their respective replacement characters are supplied in array newc. Array A has a fixed length of five characters, while arrays oldc and newc contain three characters each. However, the procedure is flawed void find _ and _ replace (char *A, char *oldc, char * newc) {
    for (int i= 0; i< 5; i++)
    for (int j= 0; j <3; j++)
    if (A [i]== oldc [j]) A [i]= newc [j] ;
    }
    The procedure is tested with the following four test cases
    (1) oldc = "abc", newc = "dab"
    (2) oldc = "cde", newc = "bcd"
    (3) oldc = "bca", newc = "cda"
    (4) oldc = "abc", newc = "bac"
    The tester now tests the program on all input strings of length five consisting of characters ‘a’, ‘b’, ‘c’, ‘d’ and ‘e’ with duplicates allowed. If the tester carries out this testing with the four test cases given above,many test cases will be able to capture the flaw?


    A : Only one
    B : Only two
    C : Only three
    D : All four


  •  
    .

     Correct answer is :B


  •   Question 9

    In the above question If array A is made to hold the string “abcde”, which of the above four test cases will be successful in exposing the flaw in this procedure?

    A : None
    B : 2 only
    C : 3 and 4 only
    D : 4 only


  •  
    .

     Correct answer is :C

     Solution :
      Now for string “abcde” in array A, both test case (3) and (4) will be successful in finding the flaw, as explained in above question.

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