Professional Documents
Culture Documents
C Language Questions Answers Explanation
C Language Questions Answers Explanation
com
#include<stdio.h>
int main(){
m
int goto=5;
co
printf("%d",goto);
return 0;
n.
}
io
A) 5
at
B) 0
C) 1
uc
D) Compilation Error
E) 4
ed
Answer: D
hi
#include<stdio.h>
w
int xyz=10;
w
int main(){
w
int xyz=20;
printf("%d",xyz);
return 0;
www.sakshieducation.com
www.sakshieducation.com
A) 10
B) 20
C) 0
D) Compilation Error
Answer: B
m
Local Scope is superior to Global scope.
co
3. What will be output of the following C program?
n.
#include<stdio.h>
io
int main(){
at
int main = 80; uc
printf("%d",main);
return 0;
ed
}
hi
A) Compilation Error
B) 80
ks
C) 0
D) Garbage Value
a
Answer: B
.s
not a keyword.
w
#include<stdio.h>
int main(){
int a=5,b = 6;
www.sakshieducation.com
www.sakshieducation.com
swap( a, b);
printf("%d %d",a,b);
return 0;
m
int temp = a;
co
a= b;
n.
b = temp;
io
}
at
A) 6 5
B) 5 6
uc
C) Garbage Value 5
D) 5 Garbage value
E) No output
ed
hi
Answer: B
ks
to variables.
.s
www.sakshieducation.com
www.sakshieducation.com
#include<stdio.h>
int main(){
int i=1;
i=2+2*i++;
m
printf("%d",i);
co
return 0;
n.
}
io
A) 5
B) 6
at
C) 8
D) 4
uc
Answer: A
ed
i=2+2*1
a
i=4
.s
#include<stdio.h>
w
int main(){
int i=5,j;
j=++i+++i+++i;
www.sakshieducation.com
www.sakshieducation.com
printf("%d %d",i,j);
return 0;
A) 5 20
m
B) 5 18
co
C) Compilation Error
n.
D) 8 24
io
E) 6 24
at
uc
Answer: D
Output: 8 24
ed
Explanation:
hi
www.sakshieducation.com
www.sakshieducation.com
the follow
wing figure:
m
co
So, j=8+88+8
j=24 and
n.
i=8
io
7. What willl be outpu
ut when yo
at
ou executee the follow
wing C cod
de?
uc
#include<stdio.h>
ed
n(){
void main
int a=55,b=2,c=2;
hi
if(a&&b
b<c++){
ks
printf("TRUE"");
a
}
.s
else{
w
printf("FA
ALSE");
w
w
www.sakshieducation.com
www.sakshieducation.com
A) TRUE
B) FALSE
C) Compilation Error
D) Run time error
Answer: B
m
#include<stdio.h>
co
void main(){
n.
int a=5,b=10,c=1;
io
if(a&&b>c){
printf("SAKSHI BITBANK");
}
at
uc
else{
ed
break;
hi
}
ks
}
a
.s
A) SAKSHI BITBANK
B) Print Nothing
w
C) Compilation Error
w
Answer: C
Explanation: Keyword break is not syntactical part of if-else statement.
w
Hence when you will compile, above code compiler will show an error
message: Misplaced break.
www.sakshieducation.com
www.sakshieducation.com
#include<stdio.h>
void main(){
int a=5,b=10;
m
if(++a||++b)
co
printf("%d %d",a,b);
n.
else
io
printf("HTST Quiz");
at
} uc
A) 5 10
B) 6 10
C) 6 11
ed
D) 5 11
E) HTST Quiz
hi
ks
Answer: B
++a || ++b
Property 1:
w
(Expression1) || (Expression2)
www.sakshieducation.com
www.sakshieducation.com
Property 2:
m
returning a non-zero so ++b will not execute and if condition will be true
and if clause will be executed.
co
10. What is output of the following program?
n.
#include<stdio.h>
io
void main(){
at
int a=5,b=10; uc
if(++a && ++b)
printf("%d %d",a,b);
ed
else
hi
printf("Sakshi");
ks
A) 5 10
a
B) 6 10
.s
C) 6 11
D) 5 11
w
E) Sakshi
w
Answer: C
w
www.sakshieducation.com
www.sakshieducation.com
Property 1:
Property 2:
m
Here Both expressions should evaluate not like previous, based on
co
evaluation the result will display. So, both will evaluate.
11. What will be the output when the following C code is executed?
n.
#include<stdio.h>
io
void main(){
int x=-1,y=-1;
at
uc
if(++x=++y)
ed
printf("DennisRitche");
else
hi
printf("JamesGosling");
ks
}
a
A) DennisRitche
.s
B) JamesGosling
C) Warning: Condition is always true
w
D) Compilation error
w
w
Answer: D
++x=++y
www.sakshieducation.com
www.sakshieducation.com
0=0
m
will show an error message: L value required.
co
If we want to make it true…we have to use == (Equal to operator) = is
assignment operator.
n.
12. What will be output when you will execute following C code?
io
#include<stdio.h>
at
void main(){
uc
if(0xA)
if(052)
ed
if('\xeb')
hi
if('\012')
ks
printf("SAKSHI EDUCATION");
a
else;
.s
else;
w
else;
w
else;
w
A) SAKSHI EDUCATION
B) Print Nothing
C) Compilation error: Misplaced else
D) Compilation error: If without any body
www.sakshieducation.com
www.sakshieducation.com
Answer: A
Explanation:
m
052: It octal integer constant.
co
‘\xeb’: It is hexadecimal character constant.
n.
As we know in C language, zero represents false and any non-zero
io
number represents true. All of the above constants return a non-zero
at
value. So all if conditions in the above program are true.
uc
In C, it is possible to write else clause without any body.
ed
13. What will be output when you will execute following C code?
hi
#include<stdio.h>
ks
void main(){
int x=1;
a
.s
if(x--)
w
printf("X = %d"+x);
w
--x;
w
else
printf("%d",x);
A) 0
B) 1
www.sakshieducation.com
www.sakshieducation.com
C) Compilation Error
D) -1
Answer: C
Explanation: If you are not using {and} in if clause then you can write
only one statement. Otherwise it will cause of compilation error: Misplace
m
else
co
14. What will be output when you execute following C code?
#include<stdio.h>
n.
void main(){
io
int a=2;
at
if(a--,--a,a)
uc
printf("I am Good");
ed
else
}
ks
A) I am Good
B) You are Good
a
if statement
D) Run Time Error
w
Answer: B
w
a-- , --a , a
www.sakshieducation.com
www.sakshieducation.com
a=0
m
number represents true. Hence else part will execute.
co
15. What will be output when you execute following C code?
#include<stdio.h>
n.
void main(){
io
int x=1,y=-1,z =0,;
if(x==y)
at
uc
printf("Equal");
ed
else
printf("Not Equal");
hi
if(z)
ks
printf("True");
a
else
.s
printf("False");
w
w
}
w
A) Equal True
B) Not Equal True
C) Not Equal False
D) Equal False
www.sakshieducation.com
www.sakshieducation.com
Answer: C
16. What will be output when you will execute following C code?
#include<stdio.h>
void main(){
int check=2;
m
switch(check){
co
case 1: printf("Sachin");
n.
case 2: printf("Dravid");
io
case 3: printf("Lakshman");
at
default: printf("Dhoni");
uc
}
}
ed
A) Dravid
B) Dhoni
hi
Answer: C
w
A) release
B) free
C) malloc
D) None of the above
Ans: B
www.sakshieducation.com
www.sakshieducation.com
18. What will be output when you will execute following C code?
#include<stdio.h>
void main(){
int x = 0;
m
for (x=1; x<4; x++);
co
printf("x=%d\n", x);
n.
}
io
A) X= 0,X= 1,X= 2,X= 3, X=4
B) X= 1,X= 2,X= 3, X=4, X=5
at
C) X=0
D) X=4
uc
E) X =5
ed
Answer: E
hi
Explanation: for loop have; at it’s end. So printf function is not under
ks
.s
A) 127
B) 128
C) 32768
D) 32767
E) 65536
www.sakshieducation.com
www.sakshieducation.com
Answer: D
Explanation:
m
-2^15 to 2^15 -1 (-1 because including 0)
co
20. What will be output when you will execute following C code?
n.
#include<stdio.h>
io
void main()
at
{ uc
int s=0;
while(++s<10)
ed
{
hi
continue;
printf("\n%d\t",s);
a
.s
}
w
}
w
A) 1 2 3 4 5 6 7 8 9
B) 1 2 3 10
w
C) 4 5 6 7 8 9 10
D) 4 5 6 7 8 9
E) 5 6 7 8 9
Answer: D
www.sakshieducation.com
www.sakshieducation.com
21. What will be output when you will execute following C code?
#include<stdio.h>
void main()
m
int s=0;
co
while(s++<10)
n.
{
io
if(s<4 && s<9)
at
continue; uc
printf("\n%d\t",s);
}
ed
}
hi
A) 1 2 3 4 5 6 7 8 9
B) 1 2 3 10
ks
C) 4 5 6 7 8 9 10
D) 4 5 6 7 8 9
a
E) 5 6 7 8 9
.s
Answer: C
w
w
w
Explanation:
www.sakshieducation.com
www.sakshieducation.com
#include<stdio.h>
void main()
m
int s=0;
co
while(++s<10)
n.
{
io
if(s<4 && s<9)
at
continue; uc
printf("\n%d\t",s);
}
ed
}
hi
ks
A) 1 2 3 4 5 6 7 8
B) 1 2 3 10
a
C) 4 5 6 7 8 9 10
D) 4 5 6 7 8 9
.s
E) 5 6 7 8 9
w
w
Answer: D
w
#include<stdio.h>
void main()
www.sakshieducation.com
www.sakshieducation.com
int s=0;
while(s<10)
{ (s++<4)
continue;
m
printf("\n%d\t",s);
co
}
n.
}
io
A) 1 2 3 4 5 6 7 8 9
B) 1 2 3 10
at
C) 4 5 6 7 8 9 10
D) 5 6 7 8 9 10
uc
E) 5 6 7 8 9
ed
Answer: D
hi
24. What will be output when you will execute following C code?
ks
void main()
{
a
int a=10,b=20;
.s
char x=1,y=0;
if(a,b,x,y)
w
{
printf("EXAM");
w
}
w
}
A) EXAM
B) 0
C) Nothing will be printed.
D) Compilation Error
Answer: C
www.sakshieducation.com
www.sakshieducation.com
25. What will be output if you will compile and execute the following C
code?
#define x 5+2
void main(){
int i;
m
i=x*x*x;
co
printf("%d",i);
n.
A) 125
io
B) 27
at
C) 8
D) 343
uc
Answer: B
ed
hi
compilation start.
a
.s
You can absorb #define only pastes the 5+2 in place of x in program. So,
w
i=5+2*5+2*5+2
w
=5+10+10+2
w
=27
www.sakshieducation.com
www.sakshieducation.com
26. What will be output if you compile and execute the following C
code?
void main(){
int i=10;
m
if(x==i)
co
printf("Equal");
n.
else if(x>i)
printf("Greater than");
io
else
printf("Less than");
at
uc
}
ed
A) Equal
B) Greater than
hi
C) Less than
D) Compiler Error
ks
Answer: D
Explanation: static variables are load time entity while auto variables are
a
run time entity. We cannot initialize any load time variable by the run
.s
time variable.
w
#include<stdio.h>
int main(){
int i;
www.sakshieducation.com
www.sakshieducation.com
for(i=0;i<5;i++){
int i=10;
printf(" %d",i);
i++;
m
return 0;
co
}
n.
A) Compilation Error
B) 10 11 12 13 14
io
C) 10 10 10 10 10
D) 0 1 2 3 4 5
at
Answer: C
uc
Explanation: Local variable is more precious than global.
#include<stdio.h>
hi
void main(){
ks
int i = 5 , j;
a
int *p , *q;
.s
p = &i;
w
q = &j;
w
j = 5;
w
getch();
www.sakshieducation.com
www.sakshieducation.com
A) 5 5
B) Address Address
C) 5 Address
D) Compilation Error
E) Run Time Error
m
Answer: A
co
Explanation: P is pointer variable stores the address location of an
integer variable. Like that q as well.
n.
29. What is the output of the following program?
io
at
#include<stdio.h> uc
void main(){
int *p1;
ed
printf("%d %d",sizeof(p1),sizeof(p2));
ks
}
a
.s
A) 2 4
w
B) 2 8
w
C) 2 2
w
D)4 4
Answer: C
www.sakshieducation.com
www.sakshieducation.com
Since both pointers int and long double are pointing to only first byte of int
data and long double data respectively.
Hence both int pointer and long double pointer stores only address in 16
bits. Thus both of them will occupy exactly equal memory space.
m
30. What will be output of following program?
co
#include<stdio.h>
n.
void main(){
io
int a = 10;
at
void *p = &a; uc
int *ptr = p;
printf("%u",*ptr);
ed
getch();
hi
}
ks
A) Compilation Error
B) 10
a
C) Address
D) Run Time Error
.s
E) 2
w
w
Answer: B
w
Explanation: Void pointer can hold address of any data type without
type casting. Any pointer can hold void pointer without type casting.
#include<stdio.h>
www.sakshieducation.com
www.sakshieducation.com
void main
n(){
int i = 3;
3
int *j;
int **k;
j=&i;
m
k=&j;
co
%u %u %dd ”,k,*k,**kk);
printf(“%
n.
}
io
at
A) 2 2 2 uc
B) Addresss Address 3
C) Addresss 3 3
ed
D) 3 3 3
hi
ks
Answer: B
Explanattion: Memo
ory represeentation
a
.s
w
w
w
www.sakshieducation.com
www.sakshieducation.com
m
Rule: * and & always cancel to each other
co
i.e. *&a = a
n.
So *k = *(&j) since k = &j
io
*&j = j = 6024
at
And uc
**k = **(&j) = *(*&j) = *j = *(&i) = *&i = i = 3
#include<stdio.h>
hi
#include<string.h>
ks
void main(){
register a = 25;
a
.s
int *p;
w
p=&a;
w
printf("%d ",*p);
w
getch();
A) 25
B) 4
C) Address
www.sakshieducation.com
www.sakshieducation.com
D) Compilation Error
Answer: D
m
#include<stdio.h>
co
void main(){
n.
int i = 5;
io
int *p;
at
p = &i;
}
hi
A) 5 Address
ks
(C) Address 5
.s
Answer: B
Explanation:
www.sakshieducation.com
www.sakshieducation.com
i.e. *&a = a
= &(*&i)
= &i
m
So second output is also address of i.
co
34. What will be output of following program?
n.
#include<stdio.h>
io
void main(){
at
int i = 100; uc
printf("value of i : %d addresss of i : %u",i,&i);
i++;
ed
getch();
ks
}
a
(A)
.s
(B)
value of i : 100 addresss of i : Address
w
www.sakshieducation.com
www.sakshieducation.com
Answer: A
Explanation:
Within the scope of any variable, value of variable may change but its
m
address will never change in any modification of variable.
co
35. What will be output of following program?
n.
#include<stdio.h>
io
void main(){
at
int i = 3;
int *j;
uc
int **k;
ed
j = &i;
hi
k = &j;
ks
printf(“%u %u %u”,i,j,k);
}
a
.s
(A) 3 Address 3
w
(C) 3 3 3
w
www.sakshieducation.com
www.sakshieducation.com
Answer: B
Explanattion:
m
co
n.
ere 6024, 8085,
8 9091 is any arbiitrary addreess, it may be
b differen
nt.
io
36. What willl be outpu
ut of follow
wing progrram?
at
uc
n(){
void main
ptr=ptr+1;
hi
prin
ntf(" %u"
",ptr);
ks
}
a
.s
A) 10000
w
B) 10001
w
C) 10002
w
D) Co
ompilatatio
on Error
Answer: C
Explanattion:
Address + Number=
= Address
www.sakshieducation.com
www.sakshieducation.com
Address++ = Address
Address-- = Address
++Address = Address
--Address = Address
m
If we will add or subtract a number from an address result will also be an
co
address.
n.
io
at
uc
ed
hi
a ks
void main(){
w
p=p+3;
printf(" %u",p);
A) 1000
www.sakshieducation.com
www.sakshieducation.com
B) 1002
C) 1024
D) 1003
E) Compilation Error
Answer: C
m
38. What will be output of following C program?
co
int *call();
void main(){
n.
int *ptr;
io
at
ptr=call(); uc
clrscr();
printf("%d",*ptr);
ed
}
hi
int * call(){
ks
int x=25;
a
++x;
.s
w
return &x;
w
}
w
www.sakshieducation.com
www.sakshieducation.com
In other word we can say a pointer whose pointing object has been
deleted is called dangling pointer.
39. What is Dangling pointer?
If any pointer is pointing the memory address of any variable but after
some variable has deleted from that memory location while pointer is still
m
pointing such memory location. Such pointer is known as dangling
co
pointer and this problem is known as dangling pointer problem.
Initially:
n.
io
at
uc
ed
Later:
hi
a ks
.s
w
w
w
www.sakshieducation.com