1.What will be output if you will compile and execute the
following c code?
void
main(){
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
}
(a) Equal
(b) Greater than
(c) Less than
(d) Compiler error
(e) None of above
Answer: (d)
Explanation:
static variables are load time
entity while auto variables are run time entity. We can not initialize any load
time variable by the run time variable.
In this example i is run time
variable while x is load time variable.
2.What
will be output if you will compile and execute the following c code?
void
main(){
int i;
float a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=3;i++)
printf("%d ",*ptr++);
}
(a)0 0 0 0
(b)Garbage Garbage Garbage Garbage
(c)102 56 -80 32
(d)102 102 -90 64
(e)Compiler error
Answer: (d)
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
ptr pointer will point first fourth byte then third byte then second byte then first byte.
Content of fourth byte:
Binary value=01100110
Decimal value= 64+32+4+2=102
Content of third byte:
Binary value=01100110
Decimal value=64+32+4+2=102
Content of second byte:
Binary value=10100110
Decimal value=-128+32+4+2=-90
Content of first byte:
Binary value=01000000
Decimal value=64
Note:
Character pointer treats MSB bit of each byte i.e. left most bit of above
figure as sign bit.
3.What will be output if you will compile and execute the
following c code?
void
main(){
printf("%s","c" "question"
"bank");
}
(a) c question bank
(b) c
(c) bank
(d) cquestionbank
(e) Compiler error
Answer: (d)
Explanation:
In c string constant “xy” is same as
“x” “y”
4.What will be output if you will compile and execute the
following c code?
void
main(){
printf("%s",__DATE__);
}
(a) Current system date
(b) Current system date with time
(c) null
(d) Compiler error
(e) None of these
Answer: (a)
Explanation:
__DATE__ is global identifier which
returns current system date.
5.What will be output if you will compile and execute the
following c code?
void
main(){
char *str="c-pointer";
printf("%*.*s",10,7,str);
}
(a) c-pointer
(b) c-pointer
(c) c-point
(d) cpointer null null
(e) c-point
Answer: (e)
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:
6.What will be output if you will compile and execute the
following c code?
void
main(){
int a=-12;
a=a>>3;
printf("%d",a);
}
(a) -4
(b) -3
(c) -2
(d) -96
(e) Compiler error
Answer :(
c)
Explanation:
Binary value of 12 is: 00000000
00001100
Binary value of -12 wills 2’s complement of 12 i.e.
So 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 as shown following
figure:
Since it is negative number so output will also a negative number but its 2’s complement.
Hence final out put will be:
And its decimal value is: 2
7.What will be output if you will compile and execute the
following c code?
#include
"string.h"
void
main(){
clrscr();
printf("%d%d",sizeof("string"),strlen("string"));
getch();
}
(a) 6 6
(b) 7 7
(c) 6 7
(d) 7 6
(e) None of these
Answer: (d)
Explanation:
Sizeof operator returns the size of
string including null character while strlen function returns length of a
string excluding null character.
8.What will be output if you will compile and execute the
following c code?
void
main(){
static main;
int x;
x=call(main);
clrscr();
printf("%d ",x);
getch();
}
int
call(int
address){
address++;
return address;
}
(a) 0
(b) 1
(c) Garbage value
(d) Compiler error
(e) None of these
Answer: (b)
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.
9. What will be output if you will compile and
execute the following c code?
void
main(){
int a,b;
a=1,3,15;
b=(2,4,6);
clrscr();
printf("%d ",a+b);
getch();
}
(a) 3
(b) 21
(c) 17
(d) 7
(e) Compiler error
Answer: (d)
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:
Hence 1 will assign to a.
Assigning the priority of each operator in the second
statement:
10. What will be output if you will compile and execute the
following c code?
int
extern
x;
void
main()
printf("%d",x);
x=2;
x=2;
getch();
}
int x=23;
(a) 0
(b) 2
(c) 23
(d) Compiler error
(e) None of these
Answer: (c)
Explanation:
extern variables can search the
declaration of variable any where in the program.
11. What will be output if you will compile and execute the
following c code?
int
extern
x;
void
main()
printf("%d",x);
x=2;
x=2;
getch();
}
int x=23;
(a) 0
(b) 2
(c) 23
(d) Compiler error
(e) None of these
Answer: (c)
Explanation:
extern variables can search the
declaration of variable any where in the program.
12.What will be output if you will compile and execute the
following c code?
void
main(){
int a=25;
clrscr();
printf("%o %x",a,a);
getch();
}
(a) 25 25
(b) 025 0x25
(c) 12 42
(d) 31 19
(e) None of these
Answer: (d)
Explanation:
%o is used to print the number in
octal number format.
%x is used to print the number in
hexadecimal number format.
Note:
In c octal number starts with 0 and hexadecimal number starts with 0x.
13. What
will be output if you will compile and execute the following c code?
#define
message "union is\
power of c"
void
main(){
clrscr();
printf("%s",message);
getch();
}
(a) union is power of c
(b) union ispower of c
(c) union is
Power of c
(d) Compiler error
(e) None of these
Answer:
(b)
Explanation:
If you want to write macro constant
in new line the end with the character \.
14. What
will be output if you will compile and execute the following c code?
#define
call(x) #x
void
main(){
printf("%s",call(c/c++));
}
(a)c
(b)c++
(c)#c/c++
(d)c/c++
(e)Compiler error
Answer: (d)
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: }
test.c 5:
It is clear macro call
is replaced by its argument in the string format.
II.DOES NOT contain any options
(15) What will be output of following code?
#include<stdio.h>
#define max
10
int main(){
int i;
i=++max;
printf("%d",i);
return 0;
}
(1)
output: compiler error.
Explanation:
output: compiler error.
Explanation:
Here max is preprocessor macro symbol which process first before the actual compilation. First preprocessor replace the symbol to its value in entire the program before the compilation. So in this program max will be replaced by 10 before compilation. Thus program will be converted like this:
int main(){
int i;
i=++10;
printf("%d",i);
return 0;
}
Meaning of ++10 is:
10=10+1
or 10=11
Which is error because we cannot assign constant value to another constant value .Hence compiler will give error.
(16) What will be output of following code?
#include<stdio.h>
#define max
10+2
int main(){
int i;
i=max*max;
printf("%d",i);
return 0;
}
Output: 32
Output: 32
Explanation:
Here max is preprocessor macro symbol which process first before the actual compilation start. Preprocessor replace the symbol to its value in entire the program before the compilation. So in this program max will be replaced by 10+2 before compilation. Thus program will be converted as:
Here max is preprocessor macro symbol which process first before the actual compilation start. Preprocessor replace the symbol to its value in entire the program before the compilation. So in this program max will be replaced by 10+2 before compilation. Thus program will be converted as:
int main(){
int i;
i=10+2*10+2;
printf("%d",i);
return 0;
}
now i=10+2*10+2
i=10+20+2
i=32
(17) What will be output of following code?
#include<stdio.h>
#define A
4-2
#define B
3-1
int main(){
int ratio=A/B;
printf("%d ",ratio);
return 0;
}
Output:3 3
Explanation:
A and B are preprocessor macro symbol which process first before the actual compilation start. Preprocessor replace the symbol to its value in entire the program before the compilation. So in this program A and B will be replaced by 4-2 and 3-1 respectively before compilation. Thus program will be converted as:
int main(){
Explanation:
A and B are preprocessor macro symbol which process first before the actual compilation start. Preprocessor replace the symbol to its value in entire the program before the compilation. So in this program A and B will be replaced by 4-2 and 3-1 respectively before compilation. Thus program will be converted as:
int main(){
int ratio=4-2/3-1;
printf("%d
",ratio);
return 0;
}
Here ratio=4-2/3-1
ratio=4-0-1
ratio=3
(18) What will be output of following code?
#include<stdio.h>
#define MAN(x,y)
(x)>(y)?(x):(y)
int main(){
int i=10,j=9,k=0;
k=MAN(i++,++j);
printf("%d %d %d",i,j,k);
return 0;
}
Output: 11 11 11
Explanation:
Explanation:
Preprocessor’s macro which process first before the actual compilation. Thus program will be converted as:
int main(){
int i=10,j=9,k=0;
k=(i++)>(++j)?(i++):(++j);
printf("%d
%d %d",i,j,k);
return 0;
}
now k=(i++)>(++j)?(i++):(++j);
first it will check the condition
(i++)>(++j)
i++ i.e. when postfix is used with variable in expression then expression is evaluated first with original value then variable is incremented
now k=(i++)>(++j)?(i++):(++j);
first it will check the condition
(i++)>(++j)
i++ i.e. when postfix is used with variable in expression then expression is evaluated first with original value then variable is incremented
Or 10>10
This condition is false.
Now i = 10+1 = 11
There is rule, only false part will execute after? i.e. ++j, i++ will be not execute.
So after ++j
j=10+1=11;
This condition is false.
Now i = 10+1 = 11
There is rule, only false part will execute after? i.e. ++j, i++ will be not execute.
So after ++j
j=10+1=11;
And k will assign value of j .so k=11;
(19) What will be output of following code?
#include<stdio.h>
#define START
main() {
#define PRINT
printf("*******");
#define END
}
START
PRINT
END
Output: *******
Explanation:
Explanation:
This program will be converted
as:
main(){
printf("*******");
}
(20) What will be output of following code?
#define CUBE(x)
(x*x*x)
#define M 5
#define N
M+1
#define PRINT
printf("RITESH");
int main(){
int volume
=CUBE(3+2);
printf("%d %d ",volume,N);
PRINT
return 0;
}
Output: 17 6
Explanation: This program will be converted as:
int main(){
Explanation: This program will be converted as:
int main(){
int volume
=(3+2*3+2*3+2);
printf("%d
%d ",volume,5+1);
PRINT
return 0;
}
(21)
#include<stdio.h>
#define ABC 25
#define PQR "Exact Help"
int main(){
int num = 3;
#ifdef ABC
printf("%d",ABC * ABC);
#else
printf("%s",PQR);
#endif
return 0;
}
Output: 625
Explanatiopn:
Since macro constant ABC has defined so #ifdef condition is true.
Pragma is implementation specific directive i.e each pragma
directive has different implementation rule and use . There are many type of
pragma directive and varies from one compiler to another compiler .If compiler
does not recognize particular pragma the it simply ignore that pragma statement
without showing any error or warning message and execute the whole program
assuming this pragma statement is not present. For
example suppose there is any pragma directive is #pragma world .
(22).#include<stdio.h>
#pragma world
int main(){
printf("C is powerful
language ");
return
0;
}
Output : C is powerful language
Explanation:
Since #pragma world is unknown for Turbo c 3.0 compiler so it will
ignore this directive without showing any error or warning message and execute
the whole program assuming #pragma world statement is not present.
List of pragma directives in turbo c 3.0:
1. #pragma
startup
2. #pragma
exit
3. #pragma
warn
4. #pragma
option
5. #pragma
inline
6. #pragma
argsused
7. #pragma
hdrfile
8. #pragma
hdrstop
9. #pragma saveregs
What is dangling
pointer in c?
Explanation:
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 pointing such memory location.
Such pointer is known as dangling pointer and this problem is known as dangling
pointer problem.
Initially:
Later:
(23).What will be output of
following c program?
#include<stdio.h>
int *call();
int main(){
int *ptr;
ptr=call();
fflush(stdin);
printf("%d",*ptr);
return 0;
}
int * call(){
int x=25;
++x;
return &x;
}
Output: Garbage value
Note: In some compiler you may
get warning message returning address of local variable or temporary
Explanation: variable x is local
variable. Its scope and lifetime is within the function call hence after
returning address of x variable x became dead and pointer is still pointing ptr
is still pointing to that location.
Solution of this problem:
Make the variable x is as static
variable. In other word we can say a pointer whose pointing object has
been deleted is called dangling pointer.
#include<stdio.h>
int *call();
int main(){
int *ptr;
ptr=call();
fflush(stdin);
printf("%d",*ptr);
return 0;
}
int * call(){
static int x=25;
++x;
return &x;
}
Output: 26
What is pointer to a
function?
Explanation:
(24)
What will be output if you will execute following code?
int * function();
int main(){
auto int *x;
int *(*ptr)();
ptr=&function;
x=(*ptr)();
printf("%d",*x);
}
int *function(){
static int a=10;
return &a;
}
Output: 10
Explanation: Here
function is function whose parameter is void data type and return type is
pointer to int data type.
x=(*ptr)()
=> x=(*&functyion)() //ptr=&function
=> x=function() //From rule *&p=p
=> x=&a
So, *x = *&a = a =10
(25)
What will be output if you will execute following code?
int find(char);
int(*function())(char);
int main(){
int x;
int(*ptr)(char);
ptr=function();
x=(*ptr)('A');
printf("%d",x);
return 0;
}
int find(char c){
return c;
}
int(*function())(char){
return find;
}
Output: 65
Explanation: Here
function whose name is function which passing void data type and returning
another function whose parameter is char data type and return type is int data
type.
x=(*ptr)(‘A’)
=> x= (*function ()) (‘A’) //ptr=function ()
//&find=function ()
i.e. return type of function ()
=> x= (* &find) (‘A’)
=> x= find (‘A’) //From rule*&p=p
=> x= 65
(26)
What will be output if you will execute following code?
char * call(int *,float *);
int main(){
char *string;
int a=2;
float b=2.0l;
char *(*ptr)(int*,float *);
ptr=&call;
string=(*ptr)(&a,&b);
printf("%s",string);
return 0;
}
char *call(int *i,float *j){
static char *str="c-pointers”;
str=str+*i+(int)(*j);
return str;
}
Output: inters
Explanation: Here
call is function whose return type is pointer to character and one parameter is pointer to int data type and second
parameter is pointer to float data type and ptr is pointer to such function.
str= str+*i+ (int) (*j)
=”c-pointers” + *&a+ (int) (*&b)
//i=&a, j=&b
=”c-pointers” + a+ (int) (b)
=”c-pointers” +2 + (int) (2.0)
=”c-pointers” +4
=”inters”
(27)
What will be output if you will execute following code?
char far * display(char far*);
int main(){
char far*
string="cquestionbank 1 ";
char far
*(*ptr)(char far *);
ptr=&display;
string=(*ptr)(string);
printf("%s",string);
}
char far *display(char far * str){
char far
* temp=str;
temp=temp+13;
*temp='\0';
return str;
}
Output: cquestionbank
Explanation: Here
display is function whose parameter is pointer to character and return type is
also pointer to character and ptr is its pointer.
temp is char pointer
temp=temp+13
temp=’\0’
Above two lines replaces first dot character by null character of
string of variable string i.e.
"cquestionbank\0 1 "
As we know %s print the character of stream up to null
character.
28. Which of the following statements should be used
to obtain the reminder after dividing 56.4/6.3?
a. rem = 56.4%6.3;
b. rem = fmod(56.4,6.3);
c. rem = modf(56.4,6.3);
d. Remainder cannot be obtained for floating
point division
(b) rem = fmod( 56.4,6.3)
29. How will you round off the value 7.66 to 8.0 ?
a. roundto(7.66);
b. ceil(7.66);
c. floor(7.66);
d. roundup(7.66);
(b) ceil(7.66);
30. Which of the following function sets first n
characters of a string to a given character?
a. strinit()
b. strset()
c. strnset()
d. strcset()
(c) strnset()