C question bank with detial solutions



(1)What will be output of following c program?

#include<stdio.h>
void main(){
char far *p=(char far *)0x55550005;
char far *q=(char far *)0x53332225;
*p=25;
(*p)++;
printf("%d",*q);
}

Output: 26
Explanation:

Far address of p and q are representing same physical address. Physical address of 0x55550005 = 0x5555*ox10 + ox0005 = 0x55555 Physical address of 0x53332225 = 0x5333 * 0x10 + ox2225 = 0x55555

*p =25, means content at memory location 0x55555 is assigning value 25

(*p)++ means increase the content by one at memory location 0x5555 so now content at memory location 0x55555 is 26
*q also means content at memory location 0x55555 which is 26

(2) What will be output of following c program?

#include<stdio.h>
void main(){
char i=13;
while(i){
i++;
}
printf("%d",i);
}

Output: 0
Explanation:
while(i) will true if i non negative. while(i) will false only when i=0. Since i is char type of data which is cyclic in nature.

i will get maximum value to 127 the minimum value -128 then it will be zero .When i=0 ,while condition will false and i will come outside the loop and at that time value of I is zero.

(3) Write a c program without using == operator compare two numbers?
Answer:

#include<stdio.h>
void main(){
int a=5,b=5;
if(b^a)
printf("EQUAL”);
}

Output: EQUAL

(4) What will be output of following c program?

#include<stdio.h>
void main(){
char a='\x82';
printf("%d",a);
}

Output:-126

Explanation:
\x symbol indicate the hexadecimal number system in character constant. So 82 is hexadecimal number its equivalent binary value is 130. 130 is beyond the range of signed char. Since signed char is cyclic in nature so its value will be
130-256=-126
(5) What will be output of following c program?

#include<stdio.h>
void main(){
enum {a,b=32766,c,d,e=0};
printf("%d",a);
}

Output: error
Explanation:
Value of enum constant must be within range of signed int .Maximum value of signed int is 32767 while value of d=32768

(6) What will be output of following c program?

#include<stdio.h>
#define char double
void main(){
char a=9;
printf("%d",sizeof(a));
}

Output: 8

Explanation:
#define is preprocessor directive which process before the actual compilation. #define paste value in the in place of macro in the program before actual compilation. New source code will be:

#include<stdio.h>
void main(){
double a=9;
printf("%d",sizeof(a));
}

Now a is double type of data which size is 8 byte.

(7)How you will copy the content of one array to another in another array by one line statement?
Answer:

#include<stdio.h>
void main(){
int i;
struct arr1{
int a[8];
} ;
struct arr1 p={1,2,3,4,5,6,7,8},q;
q=p; //copy in one line
for(i=0;i<8;i++){
printf("%d",q.a[i]);
}
}

Output: 12345678

(8) What will be output of following c program?

#include<stdio.h>
void main() {
char arr[8]={'S','A','V','R','I','Y','A'};
char *p;
p=(char *)(arr+2)[2];
printf("%c",p);  
}

Output: I

(9) What will be output of following c program?

#include<stdio.h>
void main() {
float a=5e4;
printf("%f",a);
}

Output: 50000.000000
Explanation:
5e4 is in exponential form is (5*10^4)

(10) What will be output of following c program?

#include<stdio.h>
void main(){
float a=5e40;
printf("%f",a);
}

Output: +INF
Explanation:
5e40 is beyond the range of float so output is plus infinity.

(11) What will be output of following c program?

#include<stdio.h>
int * operation(int,int);
void main() {
int p=6,q=9,*r;
r=operation (p,q);
printf("%d",*r);
}
int * operation(int p,int q) {
int a=1,b; b=!(p)+ a * q++;
return &b;
}

Output: garbage
Explanation:

Variable b is auto type. Its scope is within the operation function. When control will transfer to the operation function b will die. We are returning address of variable b to r. *r means content at location control will transfer to the operation function b will die. We are returning address of variable b to r. When control transfer to main function assigning the address of b till now variable b has dead and r will store some garbage value.*r means content at location &r so it will show wrong output. To find write output declare b as static data type. Scope of static data type is entire the program.

#include<stdio.h>
int * operation(int,int);
void main(){
int p=6,q=9,*r;
r=operation(p,q);
printf("%d",*r);
}
int * operation(int p,int q){
int a=1;
static int=b;
b=!(p)+ a * q++;
return &b;
}

Output: 9

(12) What will be output of following c program?

#include<stdio.h>
void main() {
extern int p;
printf("%d",p);
}
int p=34;

Output: 34
Explanation:
extern storage class search the value of p in outside the function.

(13) What will be output of following c program?

#include<stdio.h>
void main() {
extern int p=23;
printf("%d",p);
}

Output: Compilation error
Explanation:
Extern variable cannot be initialized due to for extern variable has not any storage space.

#include<stdio.h>
void main() {
extern int p;
printf("%d",p);
}
int p;

Output: 0
Explanation:
Default value of extern data type is zero.

(14) What will be output of following c program?

#include<stdio.h>
void main(){
int p;
p= 4>7 ? return 5:return 6;
printf("%d",p);
}

Output: Compilation error

Explanation:
We cannot use return keyword in conditional operator.

(15) What will be output of following c program?

#include<stdio.h>
void main(){
int p=0;
p= 4>7 ? p=11:p=22;
printf("%d",p);
}

Output: error
Explanation:
Assignment operator = has less precedence than conditional operator. Condition 4>7 is false .So false i.e. p=12 will execute but since = has less precedence so first value of p will assign 0 because initial value of p is. Now statement is
0=22
We cannot assign a value to constant. So error will Lvalue required. To solve such problem use () operator it has higher precedence than conditional operator.

(16) What will be output of following c program?

#include<stdio.h>
void main(){
int p=0;
p= 4>7 ? (p=11):(p=22);
printf("%d",p);
}

Output: 22

(17) What will be output of following c program?

#include<stdio.h>
void main(){
int p=0;
p= 11>7 ? (p=11):p=22;
printf("%d",p);
}

Output: 11
Explanation:
Condition operator evaluates only true part 11>7 is true. So it will assign 11 to p .It doesn’t check p=22 is write or wrong. It only checks its syntax is correct or not.

13 comments:

Anonymous said...

This is very good.

Anonymous said...

it is quite nice..

Anonymous said...

thank you

Pramod said...

Question 3:
if condition should be like this....
if(!(a^b))
printf("EQUAL")

Alok Kumar said...

it is very nice question...

Cool Alok..

mahesh said...

Nice.....Good explination..

Rocky said...

There are some wrong things. I think that...
As number 15, I don't need () and output is still 22, not error
And I don't understand definitely about number 11. The difference between int and static int

Anonymous said...

ty!! helped me a lot!!

sidharth gupta said...

15 and 17 are wrong. 15 gives output same as 16 and 17 will produce error.

Anonymous said...

hmmmmmm nt that gud

techvark said...

i like it...... nice work....

Anonymous said...

While(!(1||0))
{
printf(''\n¥");
}

Anonymous said...

not right