Pages

Friday, November 13, 2009

C++ questions: Part 2





C++ Q1: 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++ Q2: Why cant one make an object of abstract class?Give compiler view of statement
Answer: we cant make object of abstract class because, 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++ Q3: 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++ Q4: 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
}


C++ Q5: What is "strstream”?
Answer: Class that reads and writes to an array in memory


C++ Q6: Can we generate a C++ source code from the binary file?
Answer: Technically this is possible, but in my knowledge their no such software available yet. Why this is possible? In program flow we do like this to generate binary file. High level language programming code -low level programming code- hex code- binary code. How we can do reverse can be illustrated with this example. When I type 0 on screen the ASCII equivalent is 65 and so the binary code will be by converting 65 (01010 0101) so I can recognize this and decode this. Same technique can be used. Some secret mission defense org. I heard have this code splitter from binary to assembly language (low level language)/ Converter type devices available, they use them for secret national purpose.


C++ Q7: Explain "passing by value", "passing by pointer" and "passing by reference"
Answer: There is major difference between these three are when we want to avoid making the copy of variable and we want to change value of actual argument on calling function. There are we use passing by pointer, passing the reference. We can not perform arithmetic operation on reference.


C++ Q8: Difference between "vector" and "array"?
Answer: Vector and Array List are very similar. Both of them represent a 'grow able array', where you access to the elements in it through an index.Array List it's part of the Java Collection Framework, and has been added with version 1.2, while Vector it's an object that is present since the first version of the JDK. Vector, anyway, has been retrofitted to implement the List interface.The main difference is that Vector it's a synchronized object, while Array List it's not.While the iterator that are returned by both classes are fail-fast (they cleanly throw Concurrent Modification Exception when the original object has been modified), the Enumeration returned by Vector are not.Unless you have strong reason to use a Vector, the suggestion is to use the Array List.


C++ Q9: What are the types of STL containers?
Answer:
deque

hash_map
hash_multimap
hash_multiset
hash_set
list
map
multimap
multiset
set
vector




C++ Q10: Difference between a "assignment operator" and a "copy constructor"

Answer: Copy constructor is called every time a copy of an object is made. When you pass an object by value, either into a function or as a function's return value, a temporary copy of that object is made. Assignment operator is called whenever you assign to an object. Assignment operator must check to see if the right-hand side of the assignment operator is the object itself. It executes only the two sides are not equal


C++ Q11: Can we have "Virtual Constructors"?
Answer: No, we cannot have virtual constructors. But if the need arises, we can simulate the implementation of virtual constructor by calling a Init method from the constructor which, should be a virtual function.


C++ Q12: Explain the need for "Virtual Destructor".
Answer: In case of inheritance, objects should be destructed exactly the opposite way of their construction. If virtual keyword is not added before base class destructor declaration, then derived class destructor will not at all be called. Hence there will be memory leakage if allocated for derived class members while constructing the object.



C++ Q13: What will happen if I say delete this
Answer: if you say "delete this", you are effectively calling the destructor twice, which could well be a disaster if your class uses heap. The destructor will be called when you say “delete this” and again when that object goes out of scope. Since this is the language behavior, there is no way to prevent the destructor from being called twice. Please refrain from forcibly calling a destructor or using clause like this.




No comments: