C++ Interview Questions Part 3

C++ interview question: what is memory leaking in c++ ?
Answer: When a class uses dynamically allocated memory internally, all sorts of problems arise. If not properly used or handled, they can lead to memory leaks & corrupts Data Structures.

A memory leak is the situation that occurs when dynamically allocated memory is lost to the program.
Char * p;
p= new char[10000];
...
p= new char[5000]; 
          Initially, 10000 bytes are dynamically allocated & the the address of those bytes is stored in p. later 5000 bytes are dynamically allocated & the address is stored in p. However, the original 10000 bytes’ve not been returned to the system using delete [] operator. 

Memory leak actually depends on the nature of the program. 
Question: 
class A()
{
};
int main()
{
A a;
}
Whether there will be a default contructor provided by the compiler in above case ?
Answer :yes, if the designer of the class donot define any constructor in the class. then the compiler provides the default constructor in the class. 

C++ interview question: what is the use of virtual destructor?
Answer :virtual destructor is very useful....everyone should use that......if there is no any strong reason for not using virtual destructor....like...One class having two char variable...........so it's size is two byte........if u use virtual destructor it's size will be 6 bytes....4 byte for virtual ptr....Now if this class have 1 millions objects...so 4 magabyte memory will be lost...where all ptr do the same thing..... 

C++ interview question: Why cant one make an object of abstract class?Give compiler view of statement 
Answer :we cant make object of abstract class becoz, in the vtable the vtable entry for the abstract class functions will be NULL, which ever are defined as pure virtual functions.Even if there is a single pure virtual function in the class the class becomes as abstract class.If there is a virtual function in your class the compiler automatically creates a table called virtual function table .. to store the virtual function addresses.... if the function is a pure virtual function the vtable entry for that function will be NULL.Even if there is a single NULL entry in the function table the compiler does not allow to create the object. 

C++ interview question: In c++ have a default constructor?
Answer: Yes C++ does have a default constructor provided by the compiler. In this case all the members of the class are initialized to null values. These values act as the default values. For eg: My Class me; In the above case since the object is not initialized to any value so the default constructor will be called which will initialize the class with the default values. 

C++ interview question: Have you heard of "mutable" keyword? 
Answer :The mutable keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.

SEE FOLLOWING CODE :- 

********************************************
class Mutable
{
private :
int m_iNonMutVar;
mutable int m_iMutVar;
public:
Mutable();
void TryChange() const;
};
Mutable::Mutable():m_iNonMutVar(10),m_iMutVar(20) {};
void Mutable::TryChange() const
{
m_iNonMutVar = 100; // THis will give ERROR
m_iMutVar = 200; // This will WORK coz it is mutable
}

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP