CPlusPlus Interview Questions and Answers
Question - 81 : - Will the following program execute?
void main()
{
void *vptr = (void *) malloc(sizeof(void));
vptr++;
}
Answer - 81 : - 1.
It will throw an error, as arithmetic operations cannot be performed on void pointers.
2.
It will not build as sizeof cannot be applied to void* ( error “Unknown size” )
3.
How can it execute if it won’t even compile? It needs to be int main, not void main. Also, cannot increment a void *.
4.
According to gcc compiler it won’t show any error, simply it executes. but in general we can’t do arthematic operation on void, and gives size of void as 1
5.
The program compiles in GNU C while giving a warning for “void main”. The program runs without a crash. sizeof(void) is “1? hence when vptr++, the address is incremented by 1.
6.
Regarding arguments about GCC, be aware that this is a C++ question, not C. So gcc will compile and execute, g++ cannot. g++ complains that the return type cannot be void and the argument of sizeof() cannot be void. It also reports that ISO C++ forbids incrementing a pointer of type ‘void*’.
7.
in C++
voidp.c: In function `int main()’:
voidp.c:4: error: invalid application of `sizeof’ to a void type
voidp.c:4: error: `malloc’ undeclared (first use this function)
voidp.c:4: error: (Each undeclared identifier is reported only once for each function it appears in.)
voidp.c:6: error: ISO C++ forbids incrementing a pointer of type `void*’
But in c, it work without problems
Question - 82 : - void main()
{
char *cptr = 0?2000;
long *lptr = 0?2000;
cptr++;
lptr++;
printf(” %x %x”, cptr, lptr);
}
Will it execute or not?
Answer - 82 : - 3.
For Q2: As above, won’t compile because main must return int. Also, 0×2000 cannot be implicitly converted to a pointer (I assume you meant 0×2000 and not 0?2000.)
2.
Not Excute.
Compile with VC7 results following errors:
error C2440: ‘initializing’ : cannot convert from ‘int’ to ‘char *’
error C2440: ‘initializing’ : cannot convert from ‘int’ to ‘long *’
Not Excute if it is C++, but Excute in C.
The printout:
2001 2004
3.
In C++
[$]> g++ point.c
point.c: In function `int main()’:
point.c:4: error: invalid conversion from `int’ to `char*’
point.c:5: error: invalid conversion from `int’ to `long int*’
in C
———————————–
[$] etc > gcc point.c
point.c: In function `main’:
point.c:4: warning: initialization makes pointer from integer without a cast
point.c:5: warning: initialization makes pointer from integer without a cast
[$] etc > ./a.exe
2001 2004
Question - 83 : - What is the difference between Mutex and Binary semaphore?
Answer - 83 : - semaphore is used to synchronize processes. where as mutex is used to provide synchronization between threads running in the same process.
Question - 84 : - In C++, what is the difference between method overloading and method overriding?
Answer - 84 : - Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
Question - 85 : - What methods can be overridden in Java?
Answer - 85 : - In C++ terminology, all public methods in Java are virtual. Therefore, all Java methods can be overwritten in subclasses except those that are declared final, static, and private.
Question - 86 : - What are the defining traits of an object-oriented language?
Answer - 86 : - The defining traits of an object-oriented langauge are:
* encapsulation
* inheritance
* polymorphism
Question - 87 : - Assignment Operator - What is the diffrence between a "assignment operator" and a "copy constructor"?
Answer - 87 : - 1.
In assignment operator, you are assigning a value to an existing object. But in copy constructor, you are creating a new object and then assigning a value to that object. For example:
complex c1,c2;
c1=c2; //this is assignment
complex c3=c2; //copy constructor
2.
A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler:
There is no need to test to see if it is being initialized from itself.
There is no need to clean up (eg, delete) an existing value (there is none).
A reference to itself is not returned.
Question - 88 : - RTTI - What is RTTI?
Answer - 88 : - 1.
RTTI stands for "Run Time Type Identification". In an inheritance hierarchy, we can find out the exact type of the objet of which it is member. It can be done by using:
1) dynamic id operator
2) typecast operator
2.
RTTI is defined as follows: Run Time Type Information, a facility that allows an object to be queried at runtime to determine its type. One of the fundamental principles of object technology is polymorphism, which is the ability of an object to dynamically change at runtime.
Question - 89 : - STL Containers - What are the types of STL containers?
Answer - 89 : - There are 3 types of STL containers:
1. Adaptive containers like queue, stack
2. Associative containers like set, map
3. Sequence containers like vector, deque
Question - 90 : - What is the need for a Virtual Destructor ?
Answer - 90 : - Destructors are declared as virtual because if do not declare it as virtual the base class destructor will be called before the derived class destructor and that will lead to memory leak because derived class’s objects will not get freed.Destructors are declared virtual so as to bind objects to the methods at runtime so that appropriate destructor is called.