C Language Questions and Answers
C Language Questions and Answers
C Language Questions and Answers
C PROGRAMMING QUESTIONS
C AND ANSWER
C2 - Confidential
1.
#include<stdio.h>
int main(){
int x;
x=10,20,30;
printf("%d",x);
return 0;
}
(A) 10
(B) 20
(C) 30
(D) 0
(E) Compilation error
Answer :(A)
Explanation:
Precedence table:
2.
What will be output of following program?
#include<stdio.h>
int main(){
int a = 320;
char *ptr;
ptr =( char *)&a;
printf("%d ",*ptr);
return 0;
}
(A) 2
(B) 320
(C) 64
(D) Compilation error
(E) None of above
Answer: (C)
Explanation:
As we know int is two byte data byte while char is one
byte data byte. Character pointer can keep the address
one byte at time.
Binary value of 320 is 00000001 01000000 (In 16 bit)
Memory representation of int a = 320 is:
3.
What will be output when you will execute following c
code?
#include<stdio.h>
int main(){
char arr[11]="The African Queen";
printf("%s",arr);
return 0;
}
Answer: (D)
Explanation:
Size of any character array cannot be less than the
number of characters in any string which it has
assigned. Size of an array can be equal (excluding null
character) or greater than but never less than.
4.
What will be output of the following c program?
#include<stdio.h>
int main(){
int _=5;
int =10;
int ;
=_+ ;
printf("%i", );
return 0;
}
(A) 5
(B) 10
(C) 15
(D) Compilation error
(E) None of these
Answer: (C)
Explanation:
Variable name can have only underscore.
5.
What will be output of the following program?
#include<stdio.h>
int main(){
float a=0.7;
if(a<0.7){
printf("C");
}
else{
printf("C++");
}
return 0;
}
(A) C
(B) C++
(C) NULL
(D) Compilation error
(E) None of these
Answer: (A)
Explanation:
0.7 is double constant (Default). Its binary value is
written in 64 bit.
Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011
0011 0011 0011 0011 0011)
Now here variable a is a floating point variable while
0.7 is double constant. So variable a will contain only
32 bit value i.e.
a = 0.1011 0011 0011 0011 0011 0011 0011 0011 while
0.7 = 0.1011 0011 0011 0011 0011 0011 0011 0011 0011
0011 0011....
It is obvious a < 0.7
6.
What will be output of the following program?
#include<stdio.h>
int main(){
int a=2,b=7,c=10;
c=a==b;
printf("%d",c);
return 0;
}
(A) 0
(B) 1
(C) 7
(D) 2
(E) Compilation error
Answer :(A)
Explanation:
== is relational operator which returns only two
values.
0: If a == b is false
1: If a == b is true
Since
a=2
b=7
So, a == b is false hence b=0
7.
What will be output of the following c program?
#include<stdio.h>
int main(){
int max-val=100;
int min-val=10;
int avg-val;
avg-val = max-val + min-val / 2;
printf("%d",avg-val);
return 0;
}
(A) 55
(B) 105
(C) 60
(D) Compilation error
(E) None of these
Answer: (D)
Explanation:
We cannot use special character – in the variable name.
8.
What will be output when you will execute following c
code?
Answer: (E)
Explanation:
PRINT is macro constant. Macro PRINT will be replaced
by its defined statement just before the actual
compilation starts. Above code is converted as:
int main(){
int x=1;
if(x--)
printf("Star Wars");
printf(" Psycho");
else
printf("The Shawshank Redemption");
}
Explanation:
As we know in c zero represents false and any non-zero
number represents true. So in the above code:
9.
What is meaning of following declaration?
int(*ptr[5])();
Answer: (B)
Explanation:
Here ptr is array not pointer.
10.
What will be output when you will execute following c
code?
#include<stdio.h>
int main(){
int const X=0;
switch(5/4/3){
case X: printf("Clinton");
break;
case X+1:printf("Gandhi");
break;
case X+2:printf("Gates");
break;
default: printf("Brown");
}
return 0;
}
(A) Clinton
(B) Gandhi
(C) Gates
(D) Brown
(E) Compilation error
Answer: (E)
Explanation:
Case expression cannot be constant variable.
11.
What will be output when you will execute following c
code?
#include<stdio.h>
enum actor{
SeanPenn=5,
AlPacino=-2,
GaryOldman,
EdNorton
};
int main(){
enum actor a=0;
switch(a){
case SeanPenn: printf("Kevin Spacey");
break;
case AlPacino: printf("Paul Giamatti");
break;
case GaryOldman:printf("Donald Shuterland");
break;
case EdNorton: printf("Johnny Depp");
}
return 0;
}
Answer: (D)
Explanation:
Default value of enum constant
GaryOldman = -2 +1 = -1
And default value of enum constant
EdNorton = -1 + 1 = 0
Note: Case expression can be enum constant.
#include<stdio.h>
extern int x;
int main(){
do{
do{
printf("%o",x);
}
while(!-2);
}
while(0);
return 0;
}
int x=8;
(A) 8
(B) 10
(C) 0
(D) 9
(E) Compilation error
Answer: (B)
Explanation:
13.
What will be output of following c code?
#include<stdio.h>
int main(){
static int i;
for(++i;++i;++i) {
printf("%d ",i);
if(i==4) break;
}
return 0;
}
(A) 4
(B) 24
(C) 25
(D) Infinite loop
(E) Compilation error
Answer: (b)
Explanation:
14.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
return 0;
}
(A) c questions
(B) c (null)
(C) (null) (null)
(D) Compilation error
(E) None of above
Answer: (C)
Explanation:
We cannot assign any string constant in null pointer by
strcpy function.
Page 18
C2 - Confidential
15.
What will be output of following c code?
#include<stdio.h>
int main(){
int *p1,**p2;
double *q1,**q2;
printf("%d %d ",sizeof(p1),sizeof(p2));
printf("%d %d",sizeof(q1),sizeof(q2));
getch();
return 0;
}
(A) 1 2 4 8
(B) 2 4 4 8
(C) 2 4 2 4
(D) 2 2 2 2
(E) 2 2 4 4
Answer: (D)
Explanation:
Size of any type of pointer is 2 byte (In case of near
pointer)
Page 19
C2 - Confidential
16.
What will be output if you will compile and execute the
following c code?
#include<stdio.h>
int main(){
char huge *p=(char *)0XC0563331;
char huge *q=(char *)0XC2551341;
if(p==q)
printf("Equal");
else if(p>q)
printf("Greater than");
else
printf("Less than");
return 0;
}
(A) Equal
(B) Greater than
(C) Less than
(D) Compiler error
(E) None of above
Answer: (A)
Explanation:
As we know huge pointers compare its physical address.
Physical address of huge pointer p
Huge address: 0XC0563331
Offset address: 0x3331
Segment address: 0XC056
Physical address= Segment address * 0X10 + Offset
address
=0XC056 * 0X10 +0X3331
=0XC0560 + 0X3331
=0XC3891
Physical address of huge pointer q
Huge address: 0XC2551341
Offset address: 0x1341
Segment address: 0XC255
Physical address= Segment address * 0X10 + Offset
Page 20
C2 - Confidential
address
=0XC255 * 0X10 +0X1341
=0XC2550 + 0X1341
=0XC3891
Since both huge pointers p and q are pointing same
physical address so if condition will true.
Page 21
C2 - Confidential
17.
What will be output if you will execute following c
code?
#include<stdio.h>
int main(){
char arr[7]="Network";
printf("%s",arr);
return 0;
}
(A) Network
(B) N
(C) network
(D) Garbage value
(E) Compilation error
Answer: (D)
Page 22
C2 - Confidential
18.
What will be output if you will execute following c
code?
#include<stdio.h>
int main(){
char arr[20]="MysticRiver";
printf("%d",sizeof(arr));
return 0;
}
(A) 20
(B) 11
(C) 12
(D) 22
(E) 24
Answer: (A)
Page 23
C2 - Confidential
19.
What will be output if you will execute following c
code?
#include<stdio.h>
enum power{
Dalai,
Vladimir=3,
Barack,
Hillary
};
int main(){
float leader[Dalai+Hillary]={1.f,2.f,3.f,4.f,5.f};
enum power p=Barack;
printf("%0.f",leader[p>>1+1]);
return 0;
}
(A) 1
(B) 2
(C) 3
(D) 5
(E) Compilation error
Answer: (B)
Page 24
C2 - Confidential
20.
What will be output when you will execute following c
code?
#include<stdio.h>
enum power{
Dalai,
Vladimir=3,
Barack,
Hillary
};
int main(){
float leader[Dalai+Hillary]={1.f,2.f,3.f,4.f,5.f};
enum power p=Barack;
printf("%0.f",leader[p>>1+1]);
return 0;
}
Answer: (B)
Explanation:
Size of an array can be enum constantan.
Value of enum constant Barack will equal to Vladimir +
1 = 3 +1 = 4
So, value of enum variable p = 4
leader[p >> 1 +1]
= leader[4 >> 1+1]
=leader[4 >> 2] //+ operator enjoy higher precedence
than >> operator.
=leader[1] //4>>2 = (4 / (2^2) = 4/4 = 1
=2
Page 25