C Programming Questions and Answers
What will be the output of the program?
#includeint main()
{
int x = 10, y = 20;
if(!(!x) && x)
printf("x = %d\n", x);
elseprintf("y = %d\n", y);
return0;
}
Answer :
Option C
Explanation :
The logical not operator takes expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it reverses the value of the expression.
Step 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.
What will be the output of the program?
#includeint main()
{
char j=1;
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
return0;
}
Option D
Point out the error, if any in the for loop.
#includeint main()
{
int i=1;
for(;;)
{
printf("%d\n", i++);
if(i>10)
break;
}
return0;
}
Answer :
Option D
Explanation :
Step 1: for(;;) this statement will genereate infinite loop.
Step 2: printf("%d\n", i++); this statement will print the value of variable i and increement i by 1(one).
Step 3: if(i>10) here, if the variable i value is greater than 10, then the for loop breaks.
Hence the output of the program is
1
2
3
4
5
6
7
8
9
10
Point out the error, if any in the program.
#includeint main()
{
int a = 10;
switch(a)
{
}
printf("This is c program.");
return0;
}
Answer :
Option C
Explanation :
There can exists a switch statement, which has no case.
Point out the error, if any in the program.
#includeint main()
{
int i = 1;
switch(i)
{
printf("This is c program.");
case1:
printf("Case1");
break;
case2:
printf("Case2");
break;
}
return0;
}
Answer :
Option C
Explanation :
switch(i) becomes switch(1), then the case 1: block is get executed. Hence it prints "Case1".
printf("This is c program."); is ignored by the compiler.
Hence there is no error and prints "Case1".
Point out the error, if any in the while loop.
#includeint main()
{
int i=1;
while()
{
printf("%d\n", i++);
if(i>10)
break;
}
return0;
}
Answer :
Option A
Explanation :
The while() loop must have conditional expression or it shows "Expression syntax" error.
Example: while(i > 10){ ... }
In C, if you pass an array as an argument to a function, what actually gets passed?
Answer : Option C
Explanation :
The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array will be passed.
What will be the output of the program ?
#include < stdio.h >int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return0;
}
Option C
Explanation :Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15
What will be the output of the program ?
#include < stdio.h >int main()
{
staticint a[2][2] = {1, 2, 3, 4};
int i, j;
staticint *p[] = {(int*)a, (int*)a+1, (int*)a+2};
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i),
*(*(i+p)+j), *(*(p+j)+i));
}
}
return0;
}
Option C
What will be the output of the program ?
#include < stdio.h >int main()
{
void fun(int, int[]);
int arr[] = {1, 2, 3, 4};
int i;
fun(4, arr);
for(i=0; i<4; i++)
printf("%d,", arr[i]);
return0;
}
void fun(int n, int arr[])
{
int *p=0;
int i=0;
while(i++ < n)
p = &arr[i];
*p=0;
}
Option B
Explanation :Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything.
Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to
a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4
Step 3: int i; The variable i is declared as an integer type.
Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function.
Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs untill the variable i is less than '4' and it prints the each value of array a.
Hence the output of the program is 1,2,3,4