[go: up one dir, main page]

0% found this document useful (0 votes)
19 views56 pages

C-Interview Questions - update 2

Uploaded by

Anand Duraiswamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views56 pages

C-Interview Questions - update 2

Uploaded by

Anand Duraiswamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

C-Interview Questions

Questions on Data Type


• What will be output if you will compile and
execute the following c code?
void main()
{
int i=320; //variable intialization 2 bytes
char *ptr=(char *)&i; //type casting ptr = 4
bytes
printf("%d",*ptr);
}
Answer:
320
1
64
Compiler error
None of above
• As we know size of int data type is two byte
while char pointer can point one byte at time.
• Memory representation of int i=320..
101000000
Second Byte 0 0 0 0 0 0 0 1

First byte
0 1 0 0 0 0 0 0

101000000
Ptr
• What will be output if you will compile and
execute the following c code?
void main()
{
char c=125; // 0 to 255
c=c+10;
printf("%d",c);
}
Char- Range
0
-1 1

-2 2

Char
: :

-127 126

-128 127
• What will be output if you will compile and execute the
following c code?

void main()
{
long double a = 5.45 //10 bytes sizeof()
float a=5.2; // 4 byes 8 bytes
if(a==5.2)
printf("Equal");
else if(a<5.2)
printf("Less than");
else
printf("Greater than");
}
Explanation
• 5.2 is double constant in c. In c size of double data is 8
byte while a
• is float variable. Size of float variable is 4 byte.
• So double constant 5.2 is stored in memory as:
• 101.00 11001100 11001100 11001100 11001100
11001100 11001101
• Content of variable a will store in the memory as:
• 101.00110 01100110 01100110
• It is clear variable a is less than double constant 5.2
• Since 5.2 is recurring float number so it different for
float and double.
What will be output if you will compile and execute the
following c code?

void main()
{
printf("%d",sizeof(5.2));
}
a.2,
b.4,
c.8,
d.10,
e.Error
What will be output if you will compile and
execute the following c code?
#include "stdio.h"
#include "string.h"
void main()
{
char c='\08';
printf("%d",c);
}
Explanation

In c any character is starting with character ‘\’


represents octal number in character. As we
know octal digits are: 0, 1, 2, 3, 4, 5, 6, and 7.
So 8 is not an octal digit. Hence ‘\08’ is invalid
octal character constant.
What will be output if you will compile and
execute the following c code?
void main()
{
int x;
for(x=1;x<=5;x++); //x++ ++x
printf("%d",x);
}
4,5,6, Error, None
What will be output if you will compile and
execute the following c code?
#define call(x,y) x##y //xy
void main()
{
int x=5,y=10,xy=20;
printf("%d",xy+call(x,y));
}
Explanation
## is concatenation c preprocessor operator. It only concatenates the
operands i.e. a##b=ab
If you will see intermediate file then you will find code has converted
into following intermediate code before the start of actual
compilation.
Intermediate file:
test.c 1:
test.c 2: void main(){
test.c 3: int x=5,y=10,xy=20;
test.c 4: printf("%d",xy+xy);
test.c 5: }
test.c 6:
It is clear call(x, y) has replaced by xy.
• What will be output if you will compile and execute the following c
code?

#include <stdio.h>
struct marks{
int p:3; //2 bytes
int c:3;
int m:2;
};
int main()
{ p c m
struct marks s={2,-6,2}; //00000010 11111010 00000101
printf("%d %d %d",s.p,s.c,s.m);
return 0;
}
(a) 2 -6 5 (b) 2 -6 1 (c) 2 2 1 (d) Compiler error (e) None of these
Explanation
• Binary value of 2: 00000010 (Select three two
bit)
• Binary value of 6: 00000110
• Binary value of -6: 11111001+1=11111010
• (Select last three bit)
• Binary value of 5: 00000101 (Select last two bit)
Complete memory representation:
• What will be output if you will compile and
execute the following c code?
#include <stdio.h>
int main()
{
int huge*p=(int huge*)0XC0563331; // 0xc3891
int huge*q=(int huge*)0xC2551341; // 0xc3891
*p=200;
printf("%d",*q);
return 0;
}
(a)0 (b)Garbage value (c)null (d) 200 (e)Compiler error
Explanation
• 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 address
=0XC255 * 0X10 +0X1341 =0XC2550 + 0X1341 =0XC3891
• Since both huge pointers p and q are pointing same physical
address so content of q will also same as content of q.
What will be output if you will compile and execute the
following c code?
#include <stdio.h>
int main()
{ int i;
float a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=3;i++)
printf("%d ",*ptr++);
return 0;
} 01000000 10100110 01100110 01100110
64 102 102
Explanation
• In c float data type is four byte data type while char pointer
ptr can point one byte of memory at a time. Memory
representation of float a=5.2
• Fourth byte : 01000000
• Third byte:10100110
• Second byte: 01100110
• First byte: 01100110
• ptr pointer will point first fourth byte then third and so on..
• Ans: 102 102 -90 64
• 01000000 10100110 01100110 01100110
What will be output if you will compile and execute the following c
code?
#include <stdio.h>
int main()
{
int i;
double a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=7;i++)
printf("%d ",*ptr++);
return 0;
}
01000000 00010100 11001100 11001100
11001100 11001100 11001100 11001101
What is error in following declaration?
struct outer
{
int a;
struct inner
{
char c;
};
};
(A)Nesting of structure is not allowed in c.
(B) It is necessary to initialize the member variable.
(C) Inner structure must have name.
(D) Outer structure must have name.
(E) There is not any error.
• (A)Nesting of structure is not allowed in c.
• (B) It is necessary to initialize the member
variable.
• (C) Inner structure must have name.
• (D) Outer structure must have name.
• (E) There is not any error.
Explanation
It is necessary to assign name of inner structure at the
time of declaration other wise we cannot access the
member of inner structure.
So correct declaration is:
struct outer{
int a;
struct inner{
char c;
}name;
};
• What will be output if you will compile and execute the following
c code?
• #include <stdio.h>

int main()
{
printf("%s","c" "question" "bank");
return 0;
}
• (a) c question bank
• (b) c
• (c) bank
• (d) cquestionbank
• (e) Compiler error
Explanation
• In c string constant “xy” is same as “x” “y”
• What will be output if you will compile and
execute the following c code?
• #include <stdio.h>
• int main()
• {
• char *str="c-pointer";
• printf("%*.*s",10,7,str); return 0;
• }

• (a) c-pointer (b) c-pointer (c) c-point (d)


cpointer null null (e) c-point
Explanation
• Meaning of %*.*s in the printf function:
• First * indicates the width i.e. how many
spaces will take to print the string and second
* indicates how many characters will print of
any string.
• Following figure illustrates output of above
code:
c - p o i n t
• What will be output if you will compile and execute
the following c code?
• #include <stdio.h>

• int main()
• {
• int a=-12; a=a>>3;
• printf("%d",a); return 0;
• }
• (a) -4 (b) -3 (c) -2 (d) -96 (e) Compiler error

• Can discuss for left shift also


Explanation
• binary value of -12 is: 11111111 11110100
• Right shifting rule:
• Rule 1: If number is positive the fill vacant
spaces in the left side by 0.
• Rule 2: If number is negative the fill vacant
spaces in the left side by 1.
• In this case number is negative.
• So right shift all the binary digits by three space
and fill vacant space by 1
What will be output if you will compile and
execute the following c code?
#include <stdio.h>
#include <string.h>
int main()
{
printf("%d %d",sizeof("string"),strlen("string"));
return 0;
}
(a) 6 6 (b) 7 7 (c) 6 7 (d) 7 6 (e) None of these
Explanation
• Sizeof operator returns the size of string
including null character while strlen function
returns length of a string excluding null
character.
• int main()
• {
• static main;
• int x;
• x=call(main);
• printf("%d ",x);
• return 0; }
• int call(int address)
• { address++;
• return address;
• }
• (a) 0 (b) 1 (c) Garbage value (d) Compiler error (e) None of
these
Explanation
• As we know main is not keyword of c but is
special type of function. Word main can be
name variable in the main and other
functions.
• What will be output if you will compile and
execute the following c code?
• #include <stdio.h>
• int main()
• { int a,b;
• a=1,3,15;
• b=(2,4,6);
• printf("%d ",a+b);
• return 0; }
• (a) 3 (b) 21 (c) 17 (d) 7 (e) Compiler error
Explanation
• In c comma behaves as separator as well as
operator. a=1, 3, 15; b= (2, 4, 6); In the above
two statements comma is working as operator.
Comma enjoys least precedence and
associative is left to right. Assigning the
priority of each operator in the first
statement:
• http://www.c4learn.com/c-programming/c-co
mma-operator
/`
• What will be output if you will compile and
execute the following c code?
• #include <stdio.h>
• int extern x;
• int main()
• {
• printf("%d",x);
• x=2; return 0;
• }
• int x=23;
• (a) 0 (b) 2 (c) 23 (d) Compiler error (e) None of
these
Explanation
• extern variables can search the declaration of
variable anywhere in the program.
• What will be output if you will compile and
execute the following c code?
• #include <stdio.h>
• int main()
• {
• int i=0;
• if(i==0)
• { i=((5,(i=3)),i=1);
• printf("%d",i); }
• else printf("equal"); }
• (a) 5 (b) 3 (c) 1 (d) equal (e) None of above
• What will be output if you will compile and
execute the following c code?
• int main()
• { int a=25;
• printf("%o %x",a,a);
• return 0;
• }
• (a) 25 25 (b) 025 0x25 (c) 12 42 (d) 31 19 (e)
None of these
What will be output if you will compile and execute the
following c code?
#include <stdio.h>
#define message "union is\
power of c"
int main()
{
printf("%s",message);
return 0;
}
(a) union is power of c (b) union ispower of c (c) union
is Power of c (d) Compiler error (e) None of these
Explanation
• If you want to write macro constant in new
line the end with the character \.
• What will be output if you will compile and
execute the following c code?
• #include <stdio.h>
• #define call(x) #x
• int main()
• {
• printf("%s",call(c/c++));
• return 0;
• }
• (a)c (b)c++ (c)#c/c++ (d)c/c++ (e)Compiler error
Explanation
• # is string operator. It converts the macro function call
argument in the string.
• First see the intermediate file:
• test.c 1:
• test.c 2: void main(){
• test.c 3: printf("%s","c/c++");
• test.c 4: return 0;
• test.c 4: }
• test.c 5:
• It is clear macro call is replaced by its argument in the string
format.
• What will be output if you will compile and
execute the following c code?
• #include <stdio.h>
• int main()
• {
• if(printf("cquestionbank"))
• printf("I know c");
• else printf("I know c++");
• return 0; }
• (a) I know c (b) I know c++ (c) cquestionbankI know
c (d) cquestionbankI know c++ (e) Compiler error
Explanation
• Ans c
• Return type of printf function is integer which
returns number of character it prints including
blank spaces. So printf function inside if
condition will return 13. In if condition any
non- zero number means true so else part will
not execute.
• What will be output if you will compile and execute the
following c code?
• #include <stdio.h>
• int main(){
• int i=10;
• static int x=i;
• if(x==i) printf("Equal");
• else if(x>i)
• printf("Greater than");
• else printf("Less than");
• return 0; }
• (a) Equal (b) Greater than (c) Less than (d) Compiler error
(e) None of above
Explanation
• Static variables are load time entity while auto
variables are run time entity.
• We cannot initialize any load time variable by
the run time variable.
• In this example i is run time variable while x is
load time variable.
• What will be output if you will compile and
execute the following c code?
• #include <stdio.h>
• int main()
• {
• printf("%s",__DATE__);
• return 0; }
• (a) Current system date (b) Current system
date with time (c) null (d) Compiler error (e)
None of these
Explanation
• __DATE__ is global identifier which returns
current system date.
• What will be output if you will compile and execute the following c code?
• #include <stdio.h>
• void start();
• void end();
• #pragma startup start
• #pragma exit end
• int static i;
• int main()
• {
• printf("\nmain function: %d",++i);
• return 0; }
• void start()
• {
• printf("\nstart function: %d",++i);
• }
• void end()
• {
• printf("\nend function: %d",++i);
• }
Options
• (a) main function: 2 start function: 1 end
function:3
• (b) start function: 1 main function: 2 end
function:3
• (c) main function: 2 end function:3 start
function: 1
• (d) Compiler error
• (e) None of these
Explanation
• Every c program start with main function and
terminate with null statement. But #pragma
startup can call function just before main
function and #pragma exit
Explanation
Explanation

You might also like