C++ Interview Questions Part 4










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

C++ interview question: 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++ interview question: 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++ interview question: 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++ interview question: What are the types of STL containers? 
Answer: deque 
hash_map 
hash_multimap 
hash_multiset 
hash_set 
list
map 
multimap 
multiset 
set 
vector 

C++ interview question: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++ interview question: 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++ interview question: 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.

Read more...

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
}

Read more...

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP