Depth in c

Posted by Unknown at 19:24
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
Answer: (a)
Explanation:
__DATE__ is global identifier which returns current system date.


Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived object, calling of that virtual method will result in which method being called? 
a. Base method
b. Derived method..
Ans. b

For the following C program
#define AREA(x)(3.14*x*x)

main()

{float r1=6.25,r2=2.5,a;

a=AREA(r1);

printf("\n Area of the circle is %f", a);

a=AREA(r2);
printf("\n Area of the circle is %f", a);


What is the output?
Ans. Area of the circle is 122.656250
        Area of the circle is  19.625000

void main()
{
int d=5;

printf("%f",d);

}
Ans: Undefined

void main()
{
int i;

for(i=1;i<4,i++)

switch(i)

case 1: printf("%d",i);break;

{
case 2:printf("%d",i);break;

case 3:printf("%d",i);break;

}
switch(i) case 4:printf("%d",i);
}
Ans: 1,2,3,4

void main()
{
char *s="\12345s\n";

printf("%d",sizeof(s));

}
Ans: 6

void main()
{
unsigned i=1; /* unsigned char k= -1 => k=255; */

signed j=-1; /* char k= -1 => k=65535 */

/* unsigned or signed int k= -1 =>k=65535 */

if(i)

printf("less");
else

if(i>j)

printf("greater");

else

if(i==j)

printf("equal");

}
Ans: less

void main()
{
float j;

j=1000*1000;

printf("%f",j);

}
1. 1000000

2. Overflow

3. Error

4. None 

Ans:

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
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.

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
Answer :( c)
Explanation:
Binary value of 12 is: 00000000 00001100 Binary value of -12 wills 2’s complement of 12

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
Answer: (d)
Explanation:
Sizeof operator returns the size of string including null character while strlen function returns length of a string excluding null character.

What will be output if you will compile and execute the following c code?
#include<stdio.h>
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
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.
 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
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:

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
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.

 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
Answer: (b)
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
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: 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
Answer: (c)
Explanation:
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
Answer: (d)
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>
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);
}
(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
Answer: (b)
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

click the Below link download this file


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

© 2011. All Rights Reserved | Interview Questions | Template by Blogger Widgets

Home | About | Top