C++ FAQ
C++ FAQ
com
C,C++ Questions
1. 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
#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);
}
• int(*p)[10]
• int*f()
• int(*pf)()
• int*p[10]
Refer to:
-- Kernighan & Ritchie page no. 122
-- Schaum series page no. 323
4.
void main()
{
int d=5;
printf("%f",d);
}
Ans: Undefined
5.
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
6.
void main()
{
char *s="\12345s\n";
printf("%d",sizeof(s));
}
Ans: 6
7.
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<j)
printf("less");
else
if(i>j)
printf("greater");
else
if(i==j)
printf("equal");
}
Ans: less
8.
void main()
{
float j;
j=1000*1000;
printf("%f",j);
}
1. 1000000
2. Overflow
3. Error
4. None
Ans: 4
1. char *(*(*a[N])())();
3. Use the cdecl program, which turns English into C and vice
versa:
10. A structure pointer is defined of the type time . With 3 fields min,sec hours
having pointers to intergers.
Write the way to initialize the 2nd element to 10.
12.
int f()
void main()
{
f(1);
f(1,2);
f(1,2,3);
}
f(int i,int j,int k)
{
printf("%d %d %d",i,j,k);
}
Ans: None.
13.
void main()
{
int i=7;
printf("%d",i++*i++);
}
Ans: 56
14.
#define one 0
#ifdef one
printf("one is defined ");
#ifndef one
printf("one is not defined ");
15.
void main()
{
int count=10,*temp,sum=0;
temp=&count;
*temp=20;
temp=∑
*temp=count;
printf("%d %d %d ",count,*temp,sum);
}
Ans: 20 20 20
16. There was question in c working only on unix machine with pattern matching.
Ans : It allocates and frees memory after use/after getting out of scope
17.
main()
{
static i=3;
printf("%d",i--);
return i>0 ? main():0;
}
Ans: 321
18.
char *foo()
{
char result[100]);
strcpy(result,"anything is good");
return(result);
}
void main()
{
char *j;
j=foo()
printf("%s",j);
}
19.
void main()
{
char *s[]={ "dharma","hewlett-packard","siemens","ibm"};
char **p;
p=s;
printf("%s",++*p);
printf("%s",*p++);
printf("%s",++*p);
}
main()
{int i=0;
for(i=0;i<20;i++)
{switch(i)
case 0:i+=5;
case 1:i+=2;
case 5:i+=5;
default i+=4;
break;}
printf("%d,",i);
}
}
a) 0,5,9,13,17
b) 5,9,13,17
c) 12,17,22
d) 16,21
e) Syntax error
Ans. (d)
main()
{char c=-64;
int i=-32
unsigned int u =-16;
if(c>i)
{printf("pass1,");
if(c<u)
printf("pass2");
else
printf("Fail2");
}
else
printf("Fail1);
if(i<u)
printf("pass2");
else
printf("Fail2")
}
a) Pass1,Pass2
b) Pass1,Fail2
c) Fail1,Pass2
d) Fail1,Fail2
e) None of these
Ans. (c)
void main()
{
int i;
char a[]="String";
char *p="New Sring";
char *Temp;
Temp=a;
a=malloc(strlen(p) + 1);
strcpy(a,p); //Line number:9//
p = malloc(strlen(Temp) + 1);
strcpy(p,Temp);
printf("(%s, %s)",a,p);
free(p);
free(a);
} //Line number 15//
Ans. (b)
23. In the following code segment what will be the result of the function,
value of x , value of y
{unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same");
else
printf("not same");
}
a) same, MAXINT, -1
b) not same, MAXINT, -MAXINT
c) same , MAXUNIT, -1
d) same, MAXUNIT, MAXUNIT
e) not same, MAXINT, MAXUNIT
Ans. (a)
char *gxxx()
{static char xxx[1024];
return xxx;
}
main()
{char *g="string";
strcpy(gxxx(),g);
g = gxxx();
strcpy(g,"oldstring");
printf("The string is : %s",gxxx());
}
Ans. (b)
main()
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);
}
main()
{
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d\n",x,y);
}
Ans. 57 94
main()
{
int x=5;
printf("%d %d %d\n",x,x<<2,x>>2);
}
Ans. 5 20 1
Ans. 10 5
main()
{
char *ptr = "Ramco Systems";
(*ptr)++;
printf("%s\n",ptr);
ptr++;
printf("%s\n",ptr);
}
#include<stdio.h>
main()
{
char s1[]="Ramco";
char s2[]="Systems";
s1=s2;
printf("%s",s1);
}
#include<stdio.h>
main()
{
char *p1;
char *p2;
p1=(char *) malloc(25);
p2=(char *) malloc(25);
strcpy(p1,"Ramco");
strcpy(p2,"Systems");
strcat(p1,p2);
printf("%s",p1);
}
Ans. RamcoSystems
32. Find the output for the following C program given that
[1]. The following variable is available in file1.c
static int average_float;
Ans. All the functions in the file1.c can access the variable
# define TRUE 0
some code
while(TRUE)
{
some code
}
Ans. Wrong
int i;
i=1;
i=i+2*i++;
printf(%d,i);
Ans. 4
fp1=fopen("one","w")
fp2=fopen("one","w")
fputc('A',fp1)
fputc('B',fp2)
fclose(fp1)
fclose(fp2)
}
38. #include<malloc.h>
char *f()
{char *s=malloc(8);
strcpy(s,"goodbye");
}
main()
{
char *f();
printf("%c",*f()='A'); }
Ans. 10 5 0
40.
void main()
{
int i=7;
printf("%d",i++*i++);
}
Ans: 56
More …..
What are the main differences between procedure oriented languages and object
oriented languages? \par
What is R T T I? \par
What are generic functions and generic classes? \par
What is namespace? \par
What is the difference between pass by reference and pass by value? \par
Why do we use virtual functions? \par
What do you mean by pure virtual functions? \par
What are virtual classes? \par
Does c++ support multilevel and multiple inheritance ? \par
What are the advantages of inheritance? \par
When is a memory allocated to a class? \par
What is the difference between declaration and definition? \par
What is virtual constructors/destructors? \par
In c++ there is only virtual destructors, no constructors. Why? \par
What is late bound function call and early bound function call? Differentiate. \par
How is exception handling carried out in c++? \par
When will a constructor executed? \par
What is Dynamic Polymorphism? \par
Write a macro for swapping integers. \par
\par