Technical Questions asked in Walmart

  1. Consider the following three C functions:
    [PI] int * g (void)
    {
    int x= 10;
    return (&x);
    }
    [P2] int * g (void)
    {
    int * px;
    *px= 10;
    returnpx;
    }
    [P3] int *g (void)
    {
    int *px;
    px = (int *) malloc (sizeof(int));
    *px= 10;
    returnpx;
    }
    Which of the above three functions are likely to cause problems with pointers?
    1. Only P3
    2. Only P1 and P3
    3. Only P1 and P2
    4. P1, P2 and P3

    Answer: Option C.
  2. Assume the following C variable declaration
    int *A[10], B[10][10];
    Of the following expressions
    1. A[2]
    2. A[2][3]
    3. B[1]
    4. B[2][3]

    which will not give compile-time errors if used as left hand sides of assignment statements in a C program?

    Answer: Option A.
  3. Consider the C program shown below
    #include
    #define print(x) printf("%d", x)
    int x;
    void Q(int z)
    {
    z+=x;
    print(z);
    }
    void P(int *y)
    {
    int x = *y + 2;
    Q(x);
    *y = x - 1;
    print(x);
    }
    main(void) {
    x = 5;
    P(&x);
    print(x);
    }
    The output of this program is
    1. 12 7 6
    2. 22 12 11
    3. 14 6 6
    4. 7 6 6

    Answer: Option A.
    x=5;
    P(&x);
    x=*y+2=5+2=7;
    Q(x);
    Z=7;
    Z=z+x=12 <=> print (z) =12
    *y =x-1 =6 => print (x) =7
    Print (x) =6
  4. What is the output of the following program?
    #include
    intfuncf (int x);
    intfuncg (int y);
    main()
    {
    int x = 5, y = 10, count;
    for (count = 1; count < = 2; ++count)
    {
    y + = funcf (x) + funcg(x); printf(«%d",y);
    } }funcf(int x)
    {
    inty;
    y = funcg(g);
    return (y);
    } funcg(int x)
    {
    staticint y = 10;
    y + = l;
    return (y + x);
    1. 43 80
    2. 42 74
    3. 33 37
    4. 32 32

    Answer: Option A.
    funcf(x) + funcg(x)
    funcf or funcg can be executed first. Let's assume funcf is executed first. It calls funcg.
    So even if the order of call is revrsed, result will be same.
    In first call of funcg, y becomes 11 and it returns 5+11 = 16.
    In second call of funcg, y becomes 12 and it returns 5+12 - 17.
    So, in main y is incremented by 16 + 17 = 33 to become 10+33 = 43 (option (a))In the second iteration y will be incremented by 18 + 19 = 37 to give 43 + 37 = 80
  5. Consider the following C program segment:
    char p[20];
    char *s = "string";
    int length = strlen(s);
    int i;
    for (i = 0; i < length; i++)
    p[i] = s[length — i];
    printf("%s", p);
    The output of the program is?
    1. gnirts
    2. Gnirt
    3. String
    4. no output is printed

    Answer: Option D
    Because of first character is '\0' nothing will be printed.
  6. 6. What will be output of following program?
    #include
    int main(){
    int a = 10;
    void *p = &a;
    int *ptr = p;
    printf("%u",*ptr);
    return 0;
    }
    1. 10
    2. Address
    3. 2
    4. Compilation error

    Answer: Option A.
Test Site