C Programming
C Interview Question with Solution
Share your C knowledge…
”There is no delight in owning anything unshared”

<b>4th-idiot</b> <b>Q.1</b><p>main()
{
int a,b;
printf(“%d”,scanf(“%d%d”,&a,&b)); // values 10,20 are given as inputs here
}
<b>4th-idiot</b> Answers:
a.2
b.1
c.20,10
d.10,20
<b>4th-idiot</b> Explanation Ans: a.1
Scanf returns number of items successfully read. Here 10 and 20 are given as inputs which should have been scanned successfully. So number of items read is 2</p>
<b>4th-idiot</b> <b>Q.2</b><p>main()
{
char s[ ]=”man”;
int i;
for(i=0;s[ i ];i++)
printf(“n%c%c%c%c”,s[ i ],*(s+i),*(i+s),i[s]);
}
<b>4th-idiot</b> Answers:
1) Compiler Error
2) mmmm
aaaa
nnnn
3) mmmmaaaannnn
4) manmanmanman
<b>4th-idiot</b> Answer: 2
<b>4th-idiot</b> Explanation:
<p>s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i]. </p></p>
<b>4th-idiot</b>&nbsp;&nbsp;&nbsp;&nbsp;<strong>what will be the output of this program?</strong>
<p>void main()
{
int const * p=5;
printf(“%d”,++(*p));
}
1) 6
2) 7
3) Compiler Error: Cannot Modify a Constant Value
4) Linker Error</p>
<b>4th-idiot</b><b> Answer:</b><p> 3) Compiler error: Cannot modify a constant value.
<b>4th-idiot</b> Explanation: p is a pointer to a “constant integer”. But we tried to change the value of the “constant integer”. </p>
<b>4th-idiot</b>&nbsp;&nbsp;&nbsp;&nbsp; <strong>What will be the output of the below program?</strong>
<p>void main()
{
extern int i;
i=20;
printf(“%d”,i);
}
1) 20
2) Compile time Error
3) i
4) Linker Error – Undefined Symbol ‘_i'</p>
<b>4th-idiot</b> <strong>Answer: </strong>
<p>Linker Error : Undefined symbol ‘_i’ </p>
<b>4th-idiot.c</b><strong>Explanation: </strong>
<p>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 . </p>
<b>4th-idiot</b>&nbsp;&nbsp;&nbsp;&nbsp; <strong>What should be the output of the following?</strong>
<p>void pascal f(int i,int j,int k)
{
printf(“%d %d %d”,i, j, k);
}
void cdecl f(int i,int j,int k)
{
printf(“%d %d %d”,i, j, k);
}
main()
{
int i=10;
f(i++,i++,i++);
printf(” %d\n”,i);
i=10;
f(i++,i++,i++);
printf(” %d”,i);
}</p>
<b>4th-idiot</b> <b>Answer:</b>
<p>
a)10 11 12 13
12 11 10 13

b)10 11 12 13
11 12 10 13

c)10 12 11 13
12 11 10 13

d)10 11 13 12
12 10 11 13
<b>4th-idiot</b> <b>Explanation :</b>

<b>4th-idiot</b> <b>Answer:</b>a)
10 11 12 13
12 11 10 13</p>

<b>4th-idiot.c</b><b>Explanation:</b>
<p>Pascal argument passing mechanism forces the arguments to be called from left to right. cdecl is the normal C argument passing mechanism where the arguments are passed from right to left.</p>
<b>4th-idiot</b><strong>What is the output for the program given below </strong>
<p>typedef enum errorType{warning, error, exception,}error;
main()
{
error g1;
g1=1;
printf(“%d”,g1);
}</p>
<b>4th-idiot</b> <p>a)Compiler error: Multiple declaration for error
b)Garbage error
c)No error
d)0</p>
<b>4th-idiot</b> <b>Explanation :</b>

<b>4th-idiot</b> <b>Answer:</b><p> a)Compiler error: Multiple declaration for error

<b>4th-idiot</b> <b>Explanation</b>
<p>The name error is used in the two meanings. One means that it is a enumerator constant with value 1. The another use is that it is a type name (due to typedef) for enum errorType. Given a situation the compiler cannot distinguish the meaning of error to know in what sense the error is used: </p>
<p> error g1;
g1=error;
// which error it refers in each case?</p>
<p>When the compiler can distinguish between usages then it will not issue error (in pure technical terms, names can only be overloaded in different namespaces).</p>
<b>Note: the extra comma in the declaration,</b>
<p>enum errorType{warning, error, exception,}
is not an error. An extra comma is valid and is provided just for programmer’s convenience.</p>
<b>4th-idiot</b> <b>What is the output of the following?</b>
<p>main()
{
int i;
i = 64/square(4);
printf(“%d”,i);
}
a) 16
b) 4
c) 64
d) error</p>
<b>4th-idiot</b> <b>Explanation :</b>

<b>4th-idiot.c</b><b>Answer:</b>
<p>64</p>

<b>4th-idiot</b><b>Explanation:</b>
<p>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</p>

 

<h2>C Programming</h2>
<h3>C Interview Question with Solution</h3>
<h3>Share your C knowledge…</h3>
<h4>”There is no delight in owning anything unshared”</h4>
<p><strong>4thidiot.in</strong>  is a place to share your C knowledge with the world in the form of multiple choice questions and answers with explanations. There is no limit to the number of questions you can submit. However, the submissions will be posted after it has been reviewed by the moderator.</p>
<a href=”CInterviewQuestions.html” style=”background-color:#FF0000; color:#FFFFFF”>Previous</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=”CInterviewQuestions2.html” style=”background-color:#FF0000; color:#FFFFFF”>Next>>></a>

<b>4th-idiot.c</b><b>What is the output of the following?</b>
<p>main()
{
struct xx
{
int x=3;
char name[]=”hello”;
};
struct xx *s;
printf(“%d”,s->x);
printf(“%s”,s->name);
}</p>

<p>a) Compiler Error
b) hello
c) 3
d) 0</p>
<b>4th-idiot.c</b><b>Explanation :</b>

<b>4th-idiot.c</b><b>Answer:</b>
<p>Compiler Error</p>

<b>Explanation:</b>
<p>You should not initialize variables in declaration.</p>
<b>4th-idiot.c</b><b>What is the output of the following? </b>
<p>main()
{
char s[]={‘a’,’b’,’c’,’\n’,’c’,’\0′};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf(“%d”,++*p + ++*str1-32);
}
a) 11
b) 22
c) 77
d) 66
<b>4th-idiot.c</b>Explanation :

<b>4th-idiot.c</b>Answer:
77

<b>4th-idiot.c</b>Explanation:
<p>p is pointing to character ‘\n’. str1 is pointing to character ‘a’ ++*p. “p is pointing to ‘\n’ and that is incremented by one.” the ASCII value of ‘\n’ is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to ‘a’ that is incremented by 1 and it becomes ‘b’. ASCII value of ‘b’ is 98.
Now performing (11 + 98 – 32), we get 77(“M”);
So we get the output 77 :: “M” (Ascii is 77).</p>
<b>4th-idiot.c</b><b>What is the range of “real constants” in ‘C’? </b>

<p>a) -3.8 x 10 38 to 3.8 x 10 38
b) -34E37 to 34E37
c) -6.4 x 10 34 to 6.4 x 10 34
d) -68E33 to 68E33
<b>4th-idiot.c</b>ANSWER : B
<b>4th-idiot.c</b>Explanation :
Range of real constants is -3.4×1038    to    +3.4×1038 .
In exponential form it is -3.4E38    to    3.4E38
or -34E37    to    34E37
Notes :
Exponential form is also used in calculators etc.
For example:    3.94E2
Here “3.94” is known as “Mantissa” and “2” is known as “Exponent”.</p>
<b>4th-idiot.c</b><b>How many keywords are there in ‘C’ ? </b>
<p>a) 24
b) 32
c) 44
d) 52
<b>4th-idiot.c</b>ANSWER : B
<b>4th-idiot.c</b>Explanation :
int, char, float, long, double, short, signed, unsigned, near, far, huge, if, else, for, while, do, break, continue, switch, case, default, goto, return, void, enum, struct, union, auto, register, static, extern, typedef.</p>
<b>4th-idiot.c</b><b>What will be the output of the following statement ? </b>

<p>/* /* printf(“hello”); */ */

a) hello
b) no output
c) error
d) “hello”
<b>4th-idiot.c</b>ANSWER : C

<b>4th-idiot.c</b>Explanation :
Nested comments are not allowed in ‘C’.
Notes :
Comments should be written in /* ————– */ without any nesting in between.</p>
<b>4th-idiot.c</b><b>What will be the output of the following arithmetic expression ?</b>
<p>5+3*2%10-8*6
a) -37
b) -42
c) -32
d) -28
<b>4th-idiot.c</b>ANSWER : A
<b>4th-idiot.c</b>Explanation :
Hierarchy of operators
*    /    %
+    –
=
Notes :
5 + (3 * 2) % 10 – 8 * 6
5 + (6 % 10) – 8 * 6
5 + 6 – (8 * 6)
(5 + 6) – 48
(11 – 48)
-37.</p>
<b>4th-idiot.c</b><b>What will be the output of the following statement ? </b>

<p>printf(“%i”,35,2+8*5%10-2);

a) error
b) 0
c) 35
d) 350
<b>4th-idiot.c</b>ANSWER : C
<b>4th-idiot.c</b>Explanation :
printf( “%i “, 35, 2+8*5%10-2 );
As no format specifier is mentioned for the expression ” 2+8*5%10-2 ” , hence only “35” will get printed on the screen.
Notes :
Format specifiers
int       –    %i , %d
float    –    %f
char     –    %c</p>
<b>4th-idiot.c</b><b>What will be the output of the following statement ? </b>
<p>int a=10; printf(“%d &i”,a,10);
a) error
b) 10
c) 10 10
d) none of these
ANSWER : D
Explanation :
int a = 10;
printf(” %d &i “,a,10);
Format specifier for “a” is mentioned , hence value of “a” will get printed on screen . No format specifier is mentioned for “10” hence it will not get printed on screen . So final output will be “10 &i”. </p>
<b>4th-idiot.c</b><b>What will be the output of the following statement ? </b>
<p>printf(“%X%x%ci%x”,11,10,’s’,12);
a) error
b) basc
c) Bas94c
d) none of these
<b>4th-idiot.c</b>ANSWER : D
<b>4th-idiot.c</b>Explanation :
printf(“%X%x%ci%x”,11,10,’s’,12);
11,10,12 will get converted into equivalent hexadecimal entities .
11          B
10            a
12            c
hence final output will be “Basic”.</p>
<b>4th-idiot.c</b><b>What will be the output of the following statements ? </b>
<p>int a = printf(“00”); printf(“%d”,a);
<p>a) 0
b) 00
c) 002
d) garbage value
<b>4th-idiot.c</b>ANSWER : C
<b>4th-idiot.c</b>Explanation :
int a = printf(“00”);    printf(“%d”,a);
printf() function returns the number of characters being printed on screen. Hence “2” gets stored in variable “a”. Therefore the final output is “002”.</p>
<b>4th-idiot.c</b><b>What will be the output of the following statements ? </b>
int a = 3,b = 8; printf(“%d”, a<=b);
a) error
b) garbage value
c) 3
d) 1
<b>4th-idiot.c</b>ANSWER : D
<b>4th-idiot.c</b>Explanation :
int a = 3,b = 8;    printf(“%d”, a<=b);
The condition “a<=b” is true , hence “1” gets printed out.
If the condition is false “0” gets printed out.
Notes :
General Form :
printf(“format string”,list);
List can contain variables , constants or expressions . However list is optional.
In the above given problem “a<=b” is an expression which is a perfectly valid syntax.</p>
<b>4th-idiot.c</b><b>What will be the output of the following statements ? </b>
<p>int a = 4, b = 7,c; c = a = = b; printf(“%i”,c);
a) 0
b) error
c) 1
d) garbage value

<b>4th-idiot.c</b>ANSWER : A
<b>4th-idiot.c</b>Explanation :
int a = 4, b = 7,c;    c = a = = b;    printf(“%i”,c);
The condition (a = = b) is false . Hence “0” gets stored in variable “c” which eventually gets printed out. </p>
<b>4th-idiot.c</b><b>What will be the output of the following statements ?</b>
int a = 1, b = 2 , c; c = = a = = b; printf(“%d”,c);

a) 0
b) 1
c) error
d) garbage value
<b>4th-idiot.c</b>ANSWER : D
<b>4th-idiot.c</b>Explanation :
int a = 1, b = 2 , c;    c = = a = = b;    printf(“%d”,c);
(c = = a = = b) is a condition, hence nothing is being stored in variable “c”. Therefore garbage value is being printed out. </p>
<b>4th-idiot.c</b><b>What will be the output of the following program ?</b>
<p>#include<stdio.h>
#include<math.h>
void main()
{ float a = 3.26; printf(“%f”,ceil(a)); }

a) 4.0
b) 3.0
c) 3.2
d) none of these
ANSWER : A
Explanation :
float a = 3.26;    printf(“%f”,ceil(a));
“ceil()” function present in the header file “math.h” rounds up the value of “a” from “3.26” to “4.0”.
Notes :
“floor()” function does the opposite of “ceil()” function .i.e “floor()” function rounds down the value.
For example : 3.26 to 3.0</p>
<b>4th-idiot.c</b><b>What will be the output of the following statements ?</b>

<p>int a = 5, b = 2, c = 10, i = a>b<c; printf(“%d”,i);

a) error
b) 1
c) garbage value
d) 5
<b>4th-idiot.c</b>ANSWER : B
<b>4th-idiot.c</b>Explanation :
int a = 5, b = 2, c = 10, i = a>b<c;    printf(“%d”,i);
Since (a>b) is true it is replaced by “1”. Now “1” is compared with “c”.
Since (1<10) is true it is replaced by “1”. </p>
<b>4th-idiot.c</b><b>What will be the output of the following program ?</b>

#include<stdio.h>
int a = 10;
void main()
{ int a = 50; printf(“%d”,a); }

a) 50
b) error
c) 10
d) garbage value

<b>4th-idiot.c</b>ANSWER : A
<b>4th-idiot.c</b>Explanation :
#include<stdio.h>
int a = 10;    // “a” is a global variable here
void main()
{    int a = 50;    printf(“%d”,a);    }    // “a” is a local variable here
Whenever there is a conflict , local variable enjoys a higher priority than global variable.</p>

<b>4th-idiot.c. What will be the output of the following program ?</b>
<p>#include<stdio.h>
void main()
{ extern int x; printf(“%d”,x); }int x = 10;

a) error
b) 0
c) garbage value
d) 10
ANSWER : D
Explanation :
#include<stdio.h>
void main()
{    extern int x;    // declaration not definition
printf(“%d”,x);
}int x = 10;        // definition
As declaration of external variable is already done in main() , hence definition of external variable can be done outside main(). </p>

<b>4th-idiot.c. What will be the output of the following program ?</b>
<p>
#include<stdio.h>
void main()
{ struct p
{
int a,c ; float b;
}d = {1};
printf(“%d%d%f”,d.a,d.c,d.b);
}

a) garbage value
b) 100.0
c) error
d) 000.0
ANSWER : B
Explanation :
void main()
{    struct p
{
int a,c ; float b;
}d = {1};    // partially initialised
printf(“%d%d%f”,d.a,d.c,d.b);
}
Here the structure (automatic storage class) is partially initialised.
Hence the remaining elements of structure are initialised to “0”</p>

<b>4th-idiot.c. What will be the output of the following statements ?</b>
<p>
float *s; printf(“%d”,sizeof(s));

a) 1
b) 2
c) 4
d) s
ANSWER : B
Explanation :
Size of all the “near pointers” is “2” under DOS.
Notes :
Three types of pointers are near , far and huge. By default every pointer is a “near pointer” unless and until “far” and “huge” keywords are defined with it.
Size of all “far” and “huge” pointers is “4” under DOS.</p>

<b>4th-idiot.c. What will be the output of the following statements ?</b>
<p>
int *a; float far *b; char huge*c;

printf(“%d,%d,%d”,sizeof(a),sizeof(b),sizeof(c));

a) 222
b) 444
c) 244
d) 224
ANSWER : C
Explanation :
Three types of pointers are near , far and huge. By default every pointer is a “near pointer” unless and until “far” and “huge” keywords are defined with it.
Size of all the “near pointers” is “2” under DOS.
Size of all “far” and “huge” pointers is “4” under DOS.</p>

<b>4th-idiot.c. What will be the output of the following statements ?</b>
<p>
float k = 3.84; printf(“%d”,(int)k);

a) 3.8
b) 3.0
c) 3
d) 4
ANSWER : C
Explanation :
float k = 3.84;    printf(“%d”,(int)k);
Typecasting is being done i.e float is being converted into int.
3.84 gets truncated to 3.</p>

<b>4th-idiot.c. How many times the following program will print “hello” ?</b>

<p>#include<stdio.h>
void main()
{ printf(“hello”); main(); }

a) 1
b) 2
c) infinite number of times
d) none of these
ANSWER : D
Explanation :
#include<stdio.h>
void main()
{    printf(“hello”);    main();    }
The program will printf “hello” till the stack doesn’t overflow.</p>

<b>4th-idiot.c. What will be the output of the following statements ?</b>

int x[4] = {1,2,3}; printf(“%d %d %D”,x[3],x[2],x[1]);

a) 03%D
b) 000
c) 032
d) 321
ANSWER : A
Explanation :
Array x will contain ->     x[0] = 1
x[1] = 2
x[3] = 3
x[4] = 0
When an array (automatic storage class) is partially initialized, the rest of the array elements are initialized to “0”.”%D” gets printed out as it is.
Notes :
“%D” is not a valid format specifier.
%d , %i  – int
%f          – float
%c          – char</p>
<b>4th-idiot.c. What will be the output of the following statements ?</b>
<p>
float c = 1.3; printf(“%d%d”,sizeof(c),sizeof(1.3));
a) 44
b) 48
c) 42
d) 24
ANSWER : B
Explanation :
Type                    Size                Format Specifier
float                     4   bytes                 %f
double                 8   bytes                 %lf
long double        10 bytes                  %Lf
“c” is of type “float” hence “4” gets printed on screen. By default a real number is treated as a “double” hence “8” gets printed after it.</p>

<b>4th-idiot.c. Which of the following definition is incorrect ?</b>
<p>
a) long int;
b) float double;
c) float real;
d) long long;
ANSWER : C
Explanation :
Keywords in ‘C’ :
int, char, float, long, double, short, signed, unsigned, near, far, huge, if, else, for, while, do, break, continue, switch, case, default, goto, return, void, enum, struct, union, auto, register, static, extern, typedef.
A keyword cannot be used as a variable name. “real” is not a keyword, hence it can be used as a variable name.</p>
<b>4th-idiot.c. Which of them can’t be checked in a “switch-case” statement?</b>
<p>
a) enum
b) int
c) char
d) float
ANSWER : D
Explanation :
“float” cannot be checked in a “switch – case” statement.</p>

<b>4th-idiot.c. What will be the output of the following statement ?</b>
<p>
printf( 3 + “goodbye”);

a) goodbye
b) odbye
c) bye
d) dbye
ANSWER : D
Explanation :
printf(“goodbye”);
Here address of ‘g’ is being passed to the printf() function.
printf(  3 + “goodbye”);
Here 3 + address of ‘g’ is being passed to printf() function.
i.e address of ‘d’ is being passed, hence “dbye” gets printed on the screen.   </p>

<b>4th-idiot.c. What will be the output of the following statement ?</b>
<p>
printf(“hello””””world”);

a) error
b) hello””””world
c) hello
d) helloworld
ANSWER : D
Explanation :
“helloworld” gets printed on the screen.</p>

<b>4th-idiot.c. What will be the output of the following statements ?</b>
<p>
long int a = scanf(“%ld%ld”,&a,&a); printf(“%ld”,a);

a) error
b) garbage value
c) 0
d) 2
ANSWER : D

Explanation :

long int a = scanf(“%ld%ld”,&a,&a);    printf(“%ld”,a);

scanf() function returns the number of entities supplied  by keyboard.
Hence ‘2’ is being printed out.</p>

<b>4th-idiot.c. What will be the output of the following program ?</b>
<p>
#include<stdio.h>
void main()
{ int i = 30;
{ int i = 10;
printf(“%d”,i);
} printf(“%d”,i);
}

a) error
b) 1010
c) 3030
d) 1030
ANSWER : D

Explanation :

In case of a conflict between local variables the one that is more more local gets the higher priority.</p>

<b>4th-idiot.c. What will be the output of the following program ?</b>
<p>
#include<stdio.h>
void main()
{ int a = 2;
switch(a)
{ case 1:
printf(“goodbye”); break;
case 2:
continue;
case 3:
printf(“bye”);
}
}

a) error
b) goodbye
c) bye
d) byegoodbye
ANSWER : A

Explanation :
“continue” can work only with loops and not with switch.</p>

<b>4th-idiot.c. What will be the output of the following statements ?</b>
<p>
int i = 1,j; j=i— -2; printf(“%d”,j);

a) error
b) 2
c) 3
d) -3
ANSWER : C

Expalnation :
=>  i— -2      (i = 1)
=>  (1)- (-2)
=>  1 + 2
=>   3.  </p>

<b>4th-idiot.c. What will be the output of following program ?</b>
<p>
#include<stdio.h>
main()
{
int x,y = 10;
x = y * NULL;
printf(“%d”,x);
}

a) error
b) 0
c) 10
d) garbage value
ANSWER : B

Explanation :
#include<stdio.h>
main()
{
int x,y = 10;
x = y * NULL;
printf(“%d”,x);
}

During preprocessing NULL will be replaced by ‘0’. Hence ‘0’ will get printed out.

Notes :

NULL has been defined in “stdio.h” as #include NULL 0.</p>

<b>4th-idiot.c. What will be the output of following statements ?</b>
<p>
char x[ ] = “hello hi”; printf(“%d%d”,sizeof(*x),sizeof(x));

a) 88
b) 18
c) 29
d) 19
ANSWER : D

Explanation :

char x[ ] = “hello hi”;        printf(“%d%d”,sizeof(*x),sizeof(x));

“sizeof” displays the number of bytes occupied by an entity in the memory.

sizeof(*x) = 1 byte ( size of ‘h’ is one byte)

sizeof(x) = 9 bytes ( ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘ ‘ ‘h’ ‘i’ ‘’ )</p>

<b>4th-idiot.c. What will be the output of the following statements ?</b>

int a[2][2] = { 1,2,3,4 }; int *r,**s; r = &a[0][0]; s = &r;
printf(“%d”,**s);

a) error
b) 1
c) 2
d) garbage value
ANSWER : B

Explanation :

In ‘r’ address of ‘1’ is being stored. In ‘s’ (pointer to an int pointer) address of ‘r’ is being stored. Hence dereferencing this pointer will give ‘1’. </p>
<b>4th-idiot.c. What will be the output of the following statements
?</b><p>
int a[2][2] = { 3,2,5,4 };
printf(“%d”,*(*(*(a))));

a) error
b) 3
c) garbage value
d) 2
ANSWER : A

Explanation :

It will report an error : “Invalid indirection”.</p>

<b>4th-idiot.c. Which of the following are binary operators in C ?</b>
<p>
a) !
b) sizeof
c) ~
d) =
ANSWER : D

Explanation :

!, sizeof and ~ are unary operators.</p>

<p>4th-idiot.c. What will be the output of the following statements ?</p>
<p>
int k = 12; k=k–; printf(“%d”,k);

a) error
b) 11
c) 12
d) garbage value
ANSWER : B

Explanation :

Firstly value of ‘k’ gets decremented and then it gets stored in ‘k’ itself.</p>

<b>4th-idiot.c. What will be the output of the following statements
?</b>
int a=5,b=6,c=9,d; d=(a<b?(a>c?1:2):(c>b?6:8)); printf(“%d”,d);

a) 1
b) 2
c) 6
d) 8
ANSWER : B

Explanation :
(a<b) is false so is (a>c), hence ‘2’ gets printed out.

Notes :

The conditional ? and : are called ternary operators as they take three arguments.

In General:

expression A ? expression B : expression C
If expression A is true, then the value returned will be expression B, otherwise expression C will get returned.</b>

<b>4th-idiot.c. What will be the output of the following program ?</b>

#include<stdio.h>
#include<math.h>
void main()
{ int a = fmod(5,3); printf(“%d”,a); }

a) 0
b) 1
c) 2
d) 3

ANSWER :  C

Explanation :

fmod() function ( present in math.h ) calculates the remainder of (5/3), hence ‘2’ gets stored in ‘a’.</b>

<b>4th-idiot.c. Which of the functions is most apt for reading a multi-word ?</b>
<p>
a) puts()
b) gets()
c) scanf()
d) vsscanf()
ANSWER : B</p>

<b>4th-idiot.c. What will be the output of the following program ?</b>
<p>
#include<stdio.h>
#include<math.h>
void main()
{
int n; char *str = “324.89”;
n = atoi(str); printf(“%d”,n);
}

a) 300
b) 324
c) 32489
d) 89
ANSWER : B

Explanation :

atoi() function ( present in math.h ) converts a string like “324.89” to “324”.</p>

<b>4th-idiot.c. What will be the output of following program ? </b>
<p>
#include<stdio.h>
void main()
{ printf(“%d”); }

a) error
b) no output
c) %d
d) 0
ANSWER : D</p>

<b>4th-idiot.c. What will be the output of the following statements ?</b>
<p>
int i = 3;
printf(“%d%d”,i,i++);

a) 34
b) 43
c) 44
d) 33
ANSWER : C

Explanation :
Whenever we pass arguments in C, the arguments gets passed in “right to left” fashion.
(Calling Convention).</p>
<b>4th-idiot.c. What will be the output of the following program ?</b>
<p>
#include<stdio.h>
void main()
{
int a = 36, b = 9;
printf(“%d”,a>>a/b-2);
}

a) 9
b) 7
c) 5
d) none of these
ANSWER : A

Explanation :
In C the operator procedure is “/”, “-“and then” >>”.
Hence,
36>>36/9-2
36>>4-2
36>>2
9.</p>

<b>4th-idiot.c. What will be the output of the following program ?</b>
<p>
#include<stdio.h>
int main()
{
printf(“%d”,main);
return 0;
}

a) exits with stack overflow
b) compile error “variable undeclared”
c) base address of the function main
d) 0
ANSWER : C

Explanation :
Printing the function name gives the base address of the function.</p>

<b>4th-idiot.c. What will be the output of the following program ? </b>
<p>
#include<stdio.h>
a()
{
printf(“Hello World”);
}
main()
{
a();
a(1,3);
a(1,2,3,4,5,6);
}

a) Compiler Error (“undefined reference to a”)
b) Segmentation Error
c) Hello WorldHello WorldHello World
d) Hello World<garbage value><garbage value>
ANSWER : C

Explanation :
The compiler ignores the extra arguments in the function call, if it is not present in the function definition.</p>
<b>4th-idiot.c. What will be the output of the following program ? </b>
<p>
main()
{
float a=5,b=2;
int c;
c=a%b;
printf(“%d”,c);
}

a) 2
b) 2.5
c) 2.0
d) Error
ANSWER : D

Explanation :
Answer is “error” because % operator works only with integer, so it will not work with
float. </p>

<b>4th-idiot.c. Will this program run successfully?</b>

//# NO HEADER FILES INCLUDED
void main void()
{
int i = 0;
}

a) Yes
b) No
ANSWER : A

Explanation :
Includes are not required unless and until some specific external function or macro is referenced.</p>

<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>void main()
{
int  const * p=5;
printf(“%d”,++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation: p is a pointer to a “constant integer”. But we tried to change the value of the “constant integer”.</p>
<b>4th-idiot.c  Predict the output or error(s) for the following:</b>
<p>main()
{
char s[ ]=”man”;
int i;
for(i=0;s[ i ];i++)
printf(“%c%c%c%c”,s[ i ],*(s+i),*(i+s),i[s]);
}
Answer:
mmmm
aaaa
nnnn
Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally  array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base
address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the  case of  C  it is same as s[i].</p>
<b>4th-idiot.c  Predict the output or error(s) for the following:</b>
<p>main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf(“I love U”);
else
printf(“I hate U”);
}
Answer:
I hate U
Explanation:
For floating point numbers (float, double, long double) the values cannot
be predicted exactly. Depending on the number of bytes, the precession with of the value  represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.
Rule of Thumb:
Never compare or at-least be cautious when using floating point numbers
with relational operators (== , >, <, <=, >=,!= ) .</p>
<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>  main()
{
static int var = 5;
printf(“%d “,var–);
if(var)
main();
}
Answer:
5 4 3 2 1
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.</p>
<b>4th-idiot.c Predict the output or error(s) for the following:</b>

<p>main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) {
printf(” %d “,*c);
++q;      }
for(j=0;j<5;j++){
printf(” %d “,*p);
++p;      }
}

Answer:
2 2 2 2 2 2 3 4 6 5
Explanation:
Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.</p>
<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>  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.</p>
<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf(“%d %d %d %d %d”,i,j,k,l,m);
}
Answer:
0 0 1 3 1
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.</p>
<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>main()
{
char *p;
printf(“%d %d “,sizeof(*p),sizeof(p));
}

Answer:
1 2
Explanation:
The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.</p>
<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>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.</p>

<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>  main()
{
printf(“%x”,-1<<4);
}
Answer:
fff0
Explanation :
-1 is internally represented as all 1’s. When left shifted four times the least significant 4 bits are filled with 0’s.The %x format specifier specifies that the integer value be printed as a hexadecimal value.</p>
<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>main()
{
char string[]=”Hello World”;
display(string);
}
void display(char *string)
{
printf(“%s”,string);
}
Answer:
Compiler Error : Type mismatch in redeclaration of function display,
Explanation :
In third line, when the function display is encountered, the compiler doesn’t know anything about the function display. It assumes the  arguments and return types to be integers, (which is the default type). When it see the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.</p>
<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>main()
{
int c=- -2;
printf(“c=%d”,c);
}
Answer:
c=2;
Explanation:
Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus.
Note:
However you cannot give like –2. Because — operator can  only be applied to variables as a decrement operator (eg., i–). 2 is a constant and  not a variable.</p>
<b>4th-idiot.c Predict the output or error(s) for the following:</b>
<p>#define int char
main()
{
int i=65;
printf(“sizeof(i)=%d”,sizeof(i));
}
Answer:
sizeof(i)=1
Explanation:
Since the #define replaces the string  int by the macro char</p>
<b>4th-idiot.c the output or error(s) for the following:</b>
<p>  main()
{
int i=10;
i=!i>14;
printf(“i=%d”,i);
}
Answer:
i=0

Explanation:
In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol.  ! is a unary logical operator. !i (!10) is 0 (not of true is false).  0>14 is false (zero).</p>

Leave a Reply

Leave a Reply

Your email address will not be published.