Thursday, December 25, 2008

C Programming Quiz Question With Answers

1. Point out error, if any, in the following program
main()
{
int i=1;
switch(i)
{
case 1:
printf("\nRadioactive cats have 18 half-lives");
break;
case 1*2+4:
printf("\nBottle for rent -inquire within");
break;
}
}
Ans. No error. Constant expression like 1*2+4 are acceptable in cases of a switch.
2. Point out the error, if any, in the following program
main()
{
int a=10,b;
a>= 5 ? b=100 : b=200;
printf("\n%d",b);
}
Ans. lvalue required in function main(). The second assignment should be written in parenthesis as follows:
a>= 5 ? b=100 : (b=200);

9.
int f()
void main()
{
f(1);
f(1,2);
f(1,2,3);
}
f(int i,int j,int k)
{
printf("%d %d %d",i,j,k);
}

What are the number of syntax errors in the above?

Ans: None.

9. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind() <>
Ans. True

10.
void main()
{
int i=7;
printf("%d",i++*i++);
}

Ans: 56

11.
#define one 0
#ifdef one
printf("one is defined ");
#ifndef one
printf("one is not defined ");

Ans: "one is defined"


12.
void main()
{
intcount=10,*temp,sum=0;
temp=&count;
*temp=20;
temp=∑
*temp=count;
printf("%d %d %d ",count,*temp,sum);
}

Ans: 20 20 20


13. There was question in c working only on unix machine with pattern matching.


14. what is alloca()
Ans : It allocates and frees memory after use/after getting out of scope

15.
main()
{
static i=3;
printf("%d",i--);
return i>0 ? main():0;
}

Ans: 321

16.
char *foo()
{
char result[100]);
strcpy(result,"anything is good");
return(result);
}
void main()
{
char *j;
j=foo()
printf("%s",j);
}

Ans: anything is good.

17.
void main()
{
char *s[]={ "dharma","hewlett-packard","siemens","ibm"};
char **p;
p=s;
printf("%s",++*p);
printf("%s",*p++);
printf("%s",++*p);
}
Ans: "harma" (p->add(dharma) && (*p)->harma)
"harma" (after printing, p->add(hewlett-packard) &&(*p)->harma)
"ewlett-packard"

Ans. (a)
13. In the following code, is p2 an integer or an integer pointer?
typedef int* ptr
ptr p1,p2;
Ans. Integer pointer

14. Point out the error in the following program
main()
{
const int x;
x=128;
printf("%d",x);
}
Ans. x should have been initialized where it is declared.
16. What is the difference between the following declarations?
const char *s;
char const *s;
Ans. No difference

17. What is the difference between the following declarations?
const char *const s; char const *const s;
Ans. No difference
22. Point out the error in the following program
main()
{
int a=10;
void f();
a=f();
printf("\n%d",a);
}
void f()
{
printf("\nHi");
}
Ans. The program is trying to collect the value of a "void" function into an integer variable.

23. In the following program how would you print 50 using p?
main()
{
int a[]={10, 20, 30, 40, 50};
char *p;
p= (char*) a;
}
Ans. printf("\n%d",*((int*)p+4));
27. Point out the error in the following program
main()
{
const char *fun();
*fun()='A';
}
const char *fun()
{
return "Hello";
}
Ans. fun() returns to a "const char" pointer which cannot be modified

25. For the following C program
int x(char *a)
{
a=(char *) malloc(10*sizeof(char));
*a="hello";
}
main()
{
char *a="new";
x(a);
printf("%s",a);
}
The output is
a) Hello
b) New
c) Hello new
d) Run time error

Ans. (b)

No comments:

Blog List