C - Question Paper
Note : There are 20 questions and no negative marking.
Time allotted is 30 minutes
1. What is the output of the following code
main()
{
printf("Hello %d",printf("QUARK test? "));
}
a. Compile time error.
b. Hello QUARK test?
c. Run time error.
d. None of the above.
e. Quark Test ?Hello.
Ans. d.
This is because the evaluation of the parameters in a function call is done from right to left, becz the parameters were passed via a stack hence the first parameter (the leftmost one) is at the bottom of the stack and the rightmost parameter (if it is an expresseion, it will be evaluated before putting it on the stack) is on the top of the stack, hence while popping the parameters from the stack, the function printf (or any other function) gets them in the reverse order, i.e., from right to left, hence the statement printf("Quark test? ") gets evaluated first while pushing it as a parameter on to the stack, and then the statement printf("Hello %d") is executed. The '12' is the result of the return of printf("Quark test? ") which returns the number of characters printed.
2.) Out put of the following code is
main()
{
int i,j,A;
for (A = -1;A<=1; A++)
printf("%d\t",!!A);
}
a. 1 0 1
b. 65534 0 65534
c. -1 0 1
d. -65534 0 65534
e. None of the above
Ans. 1 0 1
3) What is the out put of the following code?
main()
{
int i=255;
printf("%d\t",++(i++));
}
a. Compilation error
b. Runtime error
c. 256
d. 0
e. None of the above
Ans. Compile Time Error 'Lvalue Required
4) What shall be the output of the following code?
main()
{
char i = 'a';
printf("%c \t %c \t", i ,(++i));
}
a. a b
b. Compile time error
c. b b
d. a a
e. 65 66
Ans. b b
5) What shall be the output of the following code?
main() {
int i,j;
printf("QUARK %s\n",main());
}
a. Compilation error.
b. Run-time error
c. Continuous scrolling Quark on the screen.
d. None of the above.
Ans. There is nothing on the screen and prog waits till the memory lasts and then out of memory run time error, so ans is b.
6) What shall be the output of the following code ?
#define f(x) x*x*x
main(){
printf("\n%d",f(2+2));
}
a. 8
b. 64
c. 10
d. 12
Ans. f(2+2) will expand to 2+2*2+2*2+2
= 2+4+4+2
= 12
7) What shall be the output of the following code ?
main()
{
void fun1(void *);
char a[] = "quark";
void *temp;
temp = a;
fun1(temp);}
void fun1(void *temp1 )
{
int t1 = 0;
while(*((char*)temp1+ t1++ )!='\0') {
printf("%c",*((char*)temp1 + t1));
}
}
a. Compilation error
b. ark
c. quark
d. uark
Ans, uark
8. What will be the out put of the following code?
void main()
{ int x=3;
printf("%d\t %d",x>>1, x<<3);
}
a. 1 and 4
b. 1 and 24
c. 1 and 27
d. None of the above
Ans. 1 and 24
This is because 3 in binary is 00000000 00000011 in two bytes (integer). Again, the right to left evaluation rule of parameters is applicable and so x<<3 gets executed first, it means left shift 3 times, but this operator does not change the value of x itself, it simply returns a value, so x retains its value after this operation has been carried out, so we get 00000000 00011000 which is 24, so 24 is pushed onto the stack, and then x>>1, right shift 1, 00000000 00000011, which is 1 in decimal, so 1 is pushed onto the stack, then printf("%d\t %d") gets executed displaying 1 24.
9. What will be the result of the following code?
int *x;
x =(int *) 15;
a. Compilation error
b. Compiles but gives a runtime error
c. Absolute location 15 in the memory space shall be assigned to pointer x;
d. Location 15 in the program space is assigned to pointer x;
e. Location 15 contains the address to an integer.
Ans d
10. Which of the following functions cannot be called from another file?
a. const void func(){ ……..}
b. extern void func(){………}
c. void func(){………}
d. static void func(){……….}
Ans. static
11. What will be the out come of the following code?
#include
int * func(){
static int x=0;
x++; return &x;
}
int main()
{
int * y = func();
printf("%d",(*y)++);
func();
printf("%d\n",*y);
return 0;
}
a. Compilation error.
b. Prints 1 and 3
c. Prints 1 and 3 but it is not good practice.
d. Prints 1 and 1
e. The code will not execute properly because y points to a variable whose life span is limited to execution of the function func();
Ans. Prints 1 and 3 but it is not a good practice
12. Referring to the above code , which of the following would be the correct
implementation for myFunc ?
char *format = "%d";
int main()
{
int x;
myFunc(scanf,&x);
printf("%d\n",x);
return(0);
}
a. void myFunc(int(*y)(const char*,…),int *x) {(*y)(format,&x);}
b. void myFunc(int(*y)(const char*,…),int *x) {(*y)(format,*x);}
c. void myFunc(int*y(const char*,…),int *x) {(*y)(format,&x);}
d. void myFunc(*(int y(const char*,…)),int *x) {(*y)(format,x);}
e. void myFunc(int(*y)(const char*,…),int *x) {(*y)(format,x);}
No comments:
Post a Comment