You are on page 1of 22

(1) What will be output if you will compile and execute the following c code?

void main(){
int i=320;
char *ptr=(char *)&i;
printf("%d",*ptr);
}
(a)320
(b)1
(c)64
(d)Compiler error
(e)None of above
Output: (c)
(2) What will be output if you will compile and execute the following c code?
#define x 5+2
void main(){
int i;
i=x*x*x;
printf("%d",i);
}
(a)343
(b)27
(c)133
(d)Compiler error
(e)None of above
Output: (b)
Explanation:
As we know #define is token pasting preprocessor it only paste the value of
micro constant in the program before the actual compilation start. If you will
see intermediate file you will find:
test.c 1:
test.c 2: void main(){
test.c 3: int i;
test.c 4: i=5+2*5+2*5+2;
test.c 5: printf("%d",i);
test.c 6: }
test.c 7:
You can absorb #define only pastes the 5+2 in place of x in program. So,
i=5+2*5+2*5+2
=5+10+10+2
=27
(3) What will be output if you will compile and execute the following c code?
void main(){
char c=125;
c=c+10;
printf("%d",c);
}
(a)135
(b)+INF
(c)-121
(d)-8
(e)Compiler error
Output: (c)
Explanation:
As we know char data type shows cyclic properties i.e. if you will increase or
decrease the char variables beyond its maximum or minimum value
respectively it will repeat same value according to following cyclic order:
So,
125+1= 126
125+2= 127
125+3=-128
125+4=-127
125+5=-126
125+6=-125
125+7=-124
125+8=-123
125+9=-122
125+10=-121
(4) What will be output if you will compile and execute the following c code?
void main(){
float a=5.2;
if(a==5.2)
printf("Equal");
else if(a<5.2)
printf("Less than");
else
printf("Greater than");
}
(a)Equal
(b)Less than
(c)Greater than
(d)Compiler error
(e)None of above
Output: (b)
Explanation:
5.2 is double constant in c. In c size of double data is 8 byte while a is float
variable. Size of float variable is 4 byte.
So double constant 5.2 is stored in memory as:
101.00 11001100 11001100 11001100 11001100 11001100 11001101
Content of variable a will store in the memory as:
101.00110 01100110 01100110
It is clear variable a is less than double constant 5.2
Since 5.2 is recurring float number so it different for float and double.
Number likes 4.5, 3.25, 5.0 will store same values in float and double data
type.
Note: In memory float and double data is stored in completely different way.
If you want to see actual memory representation goes to question number (60)
and (61).
(5) What will be output if you will compile and execute the following c code?
void main(){
int i=4,x;
x=++i + ++i + ++i;
printf("%d",x);
}
(a)21
(b)18
(c)12
(d)Compiler error
(e)None of above
Output: (a)
Examination :
In ++a, ++ is pre increment operator. In any mathematical expression pre
increment operator first increment the variable up to break point then starts
assigning the final value to all variable.
Step 1: Increment the variable I up to break point.

Step 2: Start assigning final value 7 to all variable i in the expression.

So, i=7+7+7=21
(7) What will be output if you will compile and execute the following c code?
void main(){
int a=10;
printf("%d %d %d",a,a++,++a);
}
(a)12 11 11
(b)12 10 10
(c)11 11 12
(d)10 10 12
(e)Compiler error
Output: (a)
Explanation:
In c printf function follows cdecl parameter passing scheme. In this scheme
parameter is passed from right to left direction.

So first ++a will pass and value of variable will be a=10 then a++ will pass now
value variable will be a=10 and at the end a will pass and value of a will be
a=12.
(8) What will be output if you will compile and execute the following c code?
void main(){
char *str="Hello world";
printf("%d",printf("%s",str));
}
(a) 11Hello world
(b) 10Hello world
(c) Hello world10
(d) Hello world11
(e) Compiler error
Output: (d)
Explanation:
Return type of printf function is integer and value of this integer is exactly
equal to number of character including white space printf function prints. So,
printf(“Hello world”) will return 13.
(10) What will be output if you will compile and execute the following c code?
#include "stdio.h"
#include "string.h"
void main(){
int i=0;
for(;i<=2;)
printf(" %d",++i);
}
(a)0 1 2
(b)0 1 2 3
(c)1 2 3
(d)Compiler error
(e)Infinite loop
Output: (c)

(11) What will be output if you will compile and execute the following c code?
void main(){
int x;
for(x=1;x<=5;x++);
printf("%d",x);
}
(a)4
(b)5
(c)6
(d)Compiler error
(e)None of above
Output: (c)
Explanation:
Body of for loop is optional. In this question for loop will execute until value of
variable x became six and condition became false.
(12) What will be output if you will compile and execute the following c code?
void main(){
printf("%d",sizeof(5.2));
}
(a)2
(b)4
(c)8
(d)10
(e)Compiler error
Output: (c)
Explanation:
Default type of floating point constant is double. So 5.2 is double constant and
its size is 8 byte.
8) What will be output if you will execute following c code?
#include "string.h"
void main(){
int count;
char const *str="Pietersen";
char const *ptr=str;
ptr+=5;
count=strlen(ptr);
printf("%d ",count);
}
Output:
4
Explanation: After incrimination character pointer ptr is pointing to 6th
character of string str i.e. r. So strlen function return number of characters in
the string “rsen”.
(44) What will be output if you will compile and execute the following c code?
void main(){
const int i=5;
i++;
printf("%d",i);
}
(a)5
(b)6
(c)0
(d)Compiler error
(e)None of above
Output: (d)
Explanation:
We cannot modify the const variable by using increment operator.
(41) What will be output if you will compile and execute the following c code?
void main(){
clrscr();
goto abc;
printf("main");
getch();
}
void dispaly(){
abc:
printf("display");
}
(a)main
(b)display
(c)maindisplay
(d)displaymain
(e)Compiler error
Output: (e)
Explanation:
Label of goto cannot be in other function because control cannot move from
one function to another function directly otherwise it will show compiler
error: unreachable label
void main()
{
int const * p=5;
printf("%d",++(*p));
}

Answer:
Compiler error: Cannot modify a constant value.

Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the
"constant integer".
main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}

Answer:
mmmm
aaaa
nnnn

Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea.
Generally array name is the base address for that array. Here s is the base
address. i is the index number/displacement from the base address. So,
indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C
it is same as s[i].
main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}

Answer:
I hate U

Explanation:
For floating point numbers (float, double, long double) the values cannot be
predicted exactly. Depending on the number of bytes, the precession with of
the value represented varies. Float takes 4 bytes and long double takes 10
bytes. So float stores 0.9 with less precision than long double.

Rule of Thumb:
Never compare or at-least be cautious when using floating point numbers with
relational operators (== , >, <, <=, >=,!= ).
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; }
}

Answer:
2222223465

Explanation:
Initially pointer c is assigned to both p and q. In the first loop, since only q is
incremented and not c , the value 2 will be printed 5 times. In second loop p
itself is incremented. So the values 2 3 4 6 5 will be printed.
main()
{
extern int i;
i=20;
printf("%d",i);
}

Answer:
Linker Error : Undefined symbol '_i'

Explanation:
extern storage class in the following declaration,
extern int i;
specifies to the compiler that the memory for i is allocated in some other
program and that address will be given to the current program at the time of
linking. But linker finds that no other variable of name i is available in any
other program with memory space allocated for it. Hence a linker error has
occurred .
main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}

Answer:
00131

Explanation :
Logical operations always give a result of 1 or 0 . And also the logical AND
(&&) operator has higher priority over the logical OR (||) operator. So the
expression 'i++ && j++ && k++' is executed first. The result of this
expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which
evaluates to 1 (because OR operator always gives 1 except for '0 || 0'
combination- for which it gives 0). So the value of m is 1. The values of other
variables are also incremented by 1.
#define int char
main()
{
int i=65;
printf("sizeof(i)=%d",sizeof(i));
}
Answer:
sizeof(i)=1
Explanation:
Since the #define replaces the string int by the macro char
#include‹stdio.h›
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}

Answer:
77

Explanation:
p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is
pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10,
which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is
pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b'
is 98.
Now performing (11 + 98 - 32), we get 77("M");
So we get the output 77 :: "M" (Ascii is 77).
#include‹stdio.h›
main()
{
inta[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}

Answer:
SomeGarbageValue---1
Explanation:
p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the
third 2D(which you are not declared) it will print garbage values. *q=***a
starting address of a is assigned integer pointer. Now q is pointing to starting
address of a. If you print *q, it will print first element of 3D array.
-------------------------- LG--------------------------
1.
main()
{
int i;
printf("%d", &i)+1;
scanf("%d", i)-1;
}
a. Runtime error. b. Runtime error. Access violation. c. Compile error.
Illegal syntax d. None of the above
2. main(int argc, char *argv[])
{
(main && argc) ? main(argc-1, NULL) : return 0;
}
a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite
loop d. None of the above
3. main()
{
int i;
float *pf;
pf = (float *)&i;
*pf = 100.00;
printf("%d", i);
}
a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above
4. main()
{
int i = 0xff;
printf("%d", i<<2);
}
a. 4 b. 512 c. 1020 d. 1024
5. #define SQR(x) x * x
main()
{
printf("%d", 225/SQR(15));
}
a. 1 b. 225 c. 15 d. none of the above
6. union u
{
struct st
{
int i : 4;
int j : 4;
int k : 4;
int l;
}st;
int i;
}u;
main()
{ u.i = 100;
printf("%d, %d, %d",u.i, u.st.i, u.st.l);
}
a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0
7. union u
{ union u
{ int i; int j;
}a[10];
int b[10];
}u;
main()
{
printf("%d", sizeof(u));
printf("%d", sizeof(u.a));
printf("%d", sizeof(u.a[0].i));
}
a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0
8. main()
{
int (*functable[2])(char *format, ...) ={ printf, scanf};
int i = 100; (*functable[0])("%d", i);
(*functable[1])("%d", i);
(*functable[1])("%d", i);
(*functable[0])("%d", &i); }
}
a. 100, Runtime error. b. 100, Random number, Random number, Random
number. c. Compile error d. 100, Random number
9. main()
{ int i, j, *p; i = 25;
j = 100; p = &i; /* Address of i is assigned to pointer p */
printf("%f", i/(*p)); /* i is divided by pointer p */
}
a. Runtime error. b. 1.00000 c. Compile error d. 0.00000
10. main()
{ int i, j;
scanf("%d %d"+scanf("%d %d", &i, &j));
printf("%d %d", i, j);
}
a. Runtime error. b. 0, 0 c. Compile error d. the first two values entered
by the user
11. main()
{
char *p = "hello world";
p[0] = 'H';
printf("%s", p);
}
a. Runtime error. b. "Hello world" c. Compile error d. "hello world"
12. main()
{ char * strA;
char * strB = "I am OK";
memcpy( strA, strB, 6);
}
a. Runtime error. b. "I am OK" c. Compile error d. "I am O"
13. How will you print % character?
a. printf("%") b. printf("%") c. printf("%%") d. printf("%%")
14. const int perplexed = 2;
#define perplexed 3
main()
{
#ifdef perplexed
#undef perplexed
#define perplexed 4
#endif
printf("%d",perplexed);
}
a. 0 b. 2 c. 4 d. none of the above

15. struct Foo


{
char *pName;
};
main()
{ struct Foo *obj = malloc(sizeof(struct Foo));
strcpy(obj->pName,"Your Name");
printf("%s", obj->pName); }
a. "Your Name" b. compile error c. "Name" d. Runtime error
16. struct Foo
{ char *pName;
char *pAddress; };
main() { struct Foo *obj = malloc(sizeof(struct Foo));
obj->pName = malloc(100);
obj->pAddress = malloc(100);
strcpy(obj->pName,"Your Name");
strcpy(obj->pAddress, "Your Address");
free(obj); printf("%s", obj->pName);
printf("%s", obj->pAddress); }
a. "Your Name", "Your Address" b. "Your Address", "Your Address" c.
"Your
Name" "Your Name" d. None of the above

17. main()
{ char *a = "Hello "; char *b = "World";
printf("%s", stract(a,b));
}
a. "Hello" b. "Hello World" c. "HelloWorld" d. None of the above
18. main()
{ char *a = "Hello ";
char *b = "World";
printf("%s", strcpy(a,b));
}
a. "Hello" b. "Hello World" c. "HelloWorld" d. None of the above
19. void func1
(int (*a)[10])
{ printf("Ok it works");
}
void func2(int a[][10])
{ printf("Will this work?");
}
main()
{ int a[10][10];
func1(a);
func2(a); }
a. "Ok it works" b. "Will this work?" c. "Ok it works Will this work?" d.
None of the above .
20. main()
{
printf("%d, %d", sizeof('c'), sizeof(100));
}
a. 2, 2 b. 2, 100 c. 4, 100 d. 4, 4

20. main()
{ int i = 100;
printf("%d", sizeof(sizeof(i)));
}
a. 2 b. 100 c. 4 d. none of the above
21. main()
{ int c = 5;
printf("%d", mainc);
}
a. 1 b. 5 c. 0 d. none of the above
22. main()
{ char c; int i = 456; c = i;
printf("%d", c);
}
a. 456 b. -456 c. random number d. none of the above
23. void main ()
{ int x = 10;
printf ("x = %d, y = %d", x,--x++);
}
a. 10, 10 b. 10, 9 c. 10, 11 d. none of the above
24 main()
{ int i =10, j = 20;
printf("%d, %d ", j-- , --i);
printf("%d, %d ", j++ , ++i); }
a. 20, 10, 20, 10 b. 20, 9, 20, 10 c. 20, 9, 19, 10 d. 19, 9, 20, 10
25. main()
{ int x=5;
for(;x==0;x--)
{ printf("x=%d", x--);
}
}
a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above
26. main()
{ int x=5; for(;x!=0;x--)
{ printf("x=%d ", x--);
}}
a. 5, 4, 3, 2,1 b. 4, 3, 2, 1, 0 c. 5, 3, 1 d. none of the above
27. main()
{ int x=5;
{ printf("x=%d", x--);
}
}
a. 5, 3, 1 b. 5, 2, 1, c. 5, 3, 1, -1, 3 d. -3, -1, 1, 3, 5
28. main()
{ unsigned int bit=256;
printf("%d", bit); }
{ unsigned int bit=512;
printf(%d", bit); } }
a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error
29 main()
{ int i; for(i=0;i<5;i++)
{ printf("%d ", 1L << i);
}}
a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16
30. main()
{ signed int bit=512, i=5;
for(;i;i--)
{ printf("%d ", bit = (bit >> (i - (i -1)))); } }
a.512, 256, 128, 64, 32 b. 256, 128, 64, 32, 16 c. 128, 64, 32, 16, 8 d.
64, 32, 16, 8, 4
31. main()
{ signed int bit=512, i=5;
for(;i;i--)
{ printf("%d ", bit >> (i - (i -1))); }
}
a. 512, 256, 0, 0, 0 b. 256, 256, 0, 0, 0 c. 512, 512, 512, 512, 512 d.
256, 256, 256, 256, 256
32. main() {
if (!(1&&0)) { printf("OK I am done.");
}
else { printf("OK I am gone.");
}}
a. OK I am done b. OK I am gone c. compile error d. none of the above
33. main()
{
if ((10) && (01))
{ printf("OK I am done.");
}
else
{ printf("OK I am gone.");
} } a. OK I am done b. OK I am gone c. compile error d. none of the above
34. main()
{ signed int bit=512, mBit;
{ mBit = ~bit; bit = bit & ~bit ;
printf("%d %d", bit, mBit);
}}
a. 0, 0 b. 0, 513 c. 512, 0 d. 0, -513

You might also like