c Practice Questions and Answers
c Practice Questions and Answers
-----------------------------------------------------------------------------------------------------
1.
main()
{
static int var = 5;
printf(“%d ”,var--);
if(var)
main();
}
Answer:
54321
Explanation:
When static storage class is given, it is initialized once. The change in the value of a
static variable is retained even between the function calls. Main is also treated like any
other ordinary function, which can be called recursively.
--------------------------------------------------------------------------------------------------------
2.
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.
-----------------------------------------------------------------------------------------------------
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.
-----------------------------------------------------------------------------------------------------
4.
main()
{
int i=3;
switch(i)
{
default:printf(“zero”);
case 1: printf(“one”);
break;
case 2:printf(“two”);
break;
case 3: printf("three");
break;
}
}
Answer:
three
Explanation:
The default case can be placed anywhere inside the loop. It is executed only when all
other cases doesn’t match.
-----------------------------------------------------------------------------------------------------
6.
#define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf(“%d”,i);
}
Answer:
64
Explanation:
The macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4.
Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e., 16*4 =
64
-----------------------------------------------------------------------------------------------------
7.
#include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
{
case 1: printf(“GOOD”);
break;
case j: printf(“BAD”);
break;
}
}
-----------------------------------------------------------------------------------------------------
8.
main()
{
printf(“%d”, out);
}
int out=100;
Answer:
Compiler error: undefined symbol out in function main.
Explanation:
The rule is that a variable is available for use from the point of declaration. Even
though a is a global variable, it is not available for main. Hence an error
-----------------------------------------------------------------------------------------------------
9.
main()
{
extern out;
printf(“%d”, out);
}
int out=100;
Answer:
100
Explanation:
This is the correct way of writing the previous program.
-----------------------------------------------------------------------------------------------------
main()
{
int i=A_abc(10);
printf(“%d\n”,--i);
}
Answer:
9
Explanation:
return(i++) it will first return i and then increments.
i.e.,10 will be returned
-----------------------------------------------------------------------------------------------------
11.
#include<stdio.h>
main()
{
int a= 0;int b = 1;int c =0;int d =1;
if(a,b,c,d)
printf("hello");
if(d,c,b,a)
printf("hello");
}
Answer:
hello
Explanation:
The comma operator has associativity from left to right. Only the rightmost value is
returned and the other values are evaluated and ignored. Thus the value of last variable
y is returned to check in if. Since it is a non zero value if becomes true so, “hello” will
be printed.
#include<stdio.h>
int z=5;
void print();
main()
{
static int var = 3;
printf("%d\n",--var);
if(var)
main();
print();
z++;
}
void print()
{
auto int w=0;
printf("%d\n",w++);
printf("%d\n",z);
}
Answer:
210050607
Explanation:
Function, Recursion, Stack, Auto, Static and Global concepts are applied.
-----------------------------------------------------------------------------------------------------
13.
main()
{
unsigned int i=10;
while(i-->=0)
printf(“%u ”,i);
}
Answer:
10 9 8 7 6 5 4 3 2 1 0 65535 65534.....
Explanation:
Since i is an unsigned integer it can never become negative. So the expression i-- >=0
will always be true, leading to an infinite loop.
16.
If any error in the following code, correct those errors otherwise write its output.
#include<stdio.h>
void main() {
int a[3]=30,i;
for(i=0;i<3;i++)
printf(“%d”,a[i]); }
Answer: Error: int a[3]=30(This statement not permitted), rewrite this as int a[3]={30};
-----------------------------------------------------------------------------------------------------
17.
If any error in the following code, correct those errors otherwise write its output.
#include<stdio.h>
int i=0;
void main()
{ void f1(); f1(); f1(); f1();}
void f1()
{
static int i; int j=0;
printf(“%d %d”,i,j); i++; j++;
}
Answer: Output: 0 0 1 0 2 0 (ie. i values are 0 1 2 (bcz of static); j values are 0 0 0)
19.
main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf(“%d %d”,i,j);
}
Answer:
10 10
Explanation:
The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question
can be written as:
if(i,j)
{
if(i,j)
j = i;
else
j = j;
}
else
j = j;
20.
main()
{
float i=1.5;
switch(i)
{
case 1: printf(“1”);
case 2: printf(“2”);
default : printf(“0”);
}
}
Answer:
Compiler Error: switch expression not integral
Explanation:
Switch statements can be applied only to integral types.
-----------------------------------------------------------------------------------------------------
21.
#include <stdio.h>
void main()
{
int i = 0;
for (i = 0;i < 5; i++)
if (i < 4)
{
printf("Hello");
break;
}
}
Answer: Hello
Explanation: For i=0 itself, if condition satisfied. Once the Hello is printed break will
terminate the loop.
******