Menu

Friday, November 15, 2013

C/C++- question & answer

  1. With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed?
    1. unalloc()
    2. dropmem()
    3. dealloc()
    4. free()
  2. void *ptr; myStruct myArray[10]; ptr = myArray;
    Which of the following is the correct way to increment the variable "ptr"?
    1. ptr = ptr + sizeof(myStruct);
    2. ++(int*)ptr; 
    3. ptr = ptr + sizeof(myArray);
    4. ptr = ptr + sizeof(ptr);
  3. main()
    { char *ptr = ” Hi How r U?”;
    *ptr++; printf(“%s\n”,ptr); ptr++; printf(“%s\n”,ptr);
    }
    1. Empty string
    2. Compile error
    3. Hi How r U?
      i How r U?
    4. Hi How r U? Hi How r U?
  4. int testarray[2][2][2] = {11, 22, 33, 44, 55, 66, 77, 88};
    What value does testarray[1][1][0] in the sample code above contain?
    1. 55
    2. 77
    3. 88
    4. 11
  5. int a=10,b; b=a++ + ++a;
    printf("%d,%d,%d,%d",b,a++,a,++a);
    what will be the output when following code is executed
    1. 12,10,11,13
    2. 22,10,11,13
    3. 22,11,11,11
    4. 22,13,13,13
  6. When reallocating memory if any other pointers point into same piece of memory do these pointers-
    1. need to be readjusted 
    2. they get readjusted automatically
    3. C does it on behalf of user
    4. none of above 
  7. Which of these are correct method of declaring pointer?
    1. char *p,
    2. char* p,
    3. char * p,
    4. char*p.
    1. I only
    2. I and II
    3. I, II and III
    4. all of above 
  8. What is the output of this code
    main()
    { int c[ ]={2.8,3.4,4,6.7,5};
    int j,*p=c,*q=c;
    for(j=0;j<5;j++)
    { printf(" %d ",*c);
    ++q; }
    for(j=0;j<5;j++)
    { printf(" %d ",*p);
    ++p;
    }
    }
    1. 2 2 2 2 2 2 3 4 6 5
    2. 2 2 2 2 2 3 4 6 5
    3. 2 2 2 2 3 4 6 5
    4. 2 2 2 2 3 4 6 5
  9. What will be the output of following code
    Int main(void)
    { int *p =NULL;
    int *c =(int*)malloc(sizeof(p));
    printf(" %0x" &c);
    return 0;
    }
    1. Some address
    2. Blank Screen
    3. Some integer value
    4. Garbage value
    5.  
  10. What is the output of the following program?
    void main()
    { int *ptr =55;
    clrscr();
    printf("%d", ++(ptr));
    }
    1. 54
    2. 55
    3. 56
    4. some address
  11. Find the output for the following C programr
    main()
    { char *ptr = "Ramco Systems";
    *ptr++;
    printf("%sn",ptr);
    ptr++;
    printf("%sn",ptr);
    }
    a. Ramco Systemsnamco Systemsn
    b. Ramco systems
    c. amco Systems
    d. Empty string
  12. main()
    {
    int a,b,result;
    printf("nEnter the numbers to be multiplied :");
    scanf("%d%d",&a,&b);
    result=0;
    while(b != 0)
    {
    if (b&01)
    result=result+a;
    a<<=1;
    b>>=1;
    }    printf("nResult:%d",result);
    }

    For a=8 & b=6 what will be the output
    1. 68
    2. 86
    3. 48
    4. 84
  13. Between a long pointer and a char pointer , which one consumes more memory?
    1. long pointer
    2. Char pointer
    3. Both will occupy same memory
    4. None of above
  14. main()
    { int a[]={2,4,6,8,10};
    int i;
    change(a,5);
    for(int i=0;i<=4;i++)
    printf("\n %d", a[i]);
    }
    change(int *b,int n)
    { int i;
    for(i=0;i<n;i++)
    *(b+i) = *(b+i)+5;
    }
    Output will be
    1. 2,4 6,8 10
    2. 7,9,11,13,15
    3. 5,7,9,11,13
    4. 3,5,7,9,11
  15. main()
    { int i;
    float *pf;
    pf = (float *)&i; *pf = 100.00;
    printf("n %d", i);}
    1. Some Integer not 100
    2. 100
    3. Runtime error.
    4. 0
  16. main()
    { int i, j, *p;
    i = 25;
    j = 100;
    p = &i;
    printf("%f", i/(*p) );
    }
    1. Compile erro
    2. 1.00000
    3. Runtime error.
    4. 0.00000
  17. main()
    {
    int i, j;
    scanf("%d %d"+scanf("%d %d", &i, &j));
    printf("%d %d", i, j);
    }
    1. Compile error
    2. 0, 0
    3. Runtime error.
    4. the first two values entered by the user.
  18. struct Foo
    { char *pName;
    };
    main()
    {
    struct Foo *obj = malloc(sizeof(struct Foo));
    clrscr();
    strcpy(obj->pName,"Your Name");
    printf("%s", obj->pName);
    }
    1. Name
    2. compile error
    3. Your Name
    4. Runtime error
  19. In the following code, in which order the functions would be called?
    x = f1(23,14)*f2(12/4)+f3();
    1. f1, f2, f3
    2. f3, f2, f1
    3. The order may vary from compiler to compiler
    4. None of the above
  20. What is the equivalent pointer expression for referring the same element a[i][j][k][l]?
    1. *(*(*(*(a+l)+k)+j))
    2. *(*(*(*(a+i)+j)+k)+l)
    3. *(*(*(*(a+l)+k)+j)i)
    4. None of the above
     


Answers-
1-d 2-b 3-c 4-b 5-d 6-a 7-d 8-a 9-a 10-d
11-a 12-c 13-c 14-b 15-a 16-a 17-d 18-c 19-a 20-b

No comments:

Post a Comment