Pages

Friday, November 13, 2009

C++ questions: Part 3




C++ Q1: What is the output of printf ("%d")
Answer :Usually the output value cannot be predicted. It will not give any error. It will print a garbage value. But if the situation is
main()
{
int a=1,b=2,c=3;
printf("%d");
}
The output will be the value of the last variable, ie. 3

C++ Q2: # what is an algorithm (in terms of the STL/C++ standard library)?
Answer: Algorithm in STL consist different searching and sorting algos implementation, which takes start and end iterators of STL container on which algo is going to work.

C++ Q3: How can you force instantiation of a template?
Answer: you can instantiate a template in two ways. 1. Implicit instantiation and 2. Explicit Instantiation implicit instatanitioan can be done by the following ways:

template
class A
{
public:
A(){}
~A(){}
void x();
void z();
};
void main()
{
A ai;
A af;
}

External Instantion can be done the following way:
int main()
{
template class A;
template class A;
}

C++ Q4: What is the difference between operator new and the new operator?
Answer: This is what happens when you create a new object: 1. the memory for the object is allocated using "operator new". 2. the constructor of the class is invoked to properly initialize this memory. As you can see, the new operator does both 1 and 2. The operator new merely allocates memory, it does not initialize it. Where as the new operator also initializes it properly by calling the constructor.

C++ Q5: What is the Basic nature of "cin" and "cout" and what concept or principle we are using on those two?
Answer: Basically "cin and cout" are INSTANCES of istream and ostream classes respectively. And the concept which is used on cin and cout is operator overloading. Extraction and Insertion operators are overloaded for input and ouput operations.

C++ Q6: What are virtual functions?
Answer: C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes.
C++ virtual function is,
* A member function of a class
* Declared with virtual keyword
* usually has a different functionality in the derived class
* A function call is resolved at run-time

C++ Q7: We can overload assignment operator as a normal function.But we can not overload assignment operator as friend function why?
Answer :If the operation modifies the state of the class object, it operates on, it must be a member function, not a friend fucntionThus all operator such as =, *=, +=, etc are naturally defined as member functions not friend functions Conversely, if the operator does not modify any of its operands, but needs only a representation of the object, it does not have to be a member function and often less confusing. This is the reason why binary operators are often implemented as friend functions such as + , *, -, etc..


C++ Q8: What is the difference between class and structure?
Answer: 1:By default, the members of structures are public while that for class is private
2: structures doesn't provide something like data hiding which is provided by the classes
3: structures contains only data while class bind both data and member functions

C++ Q9: What is virtual class and friend class?
Answer: Friend classes are used when two or more classes are designed to work together and virtual base class aids in multiple inheritance.

C++ Q10: Is there any way to write a class such that no class can be inherited from it. Please include code
Answer: Simple, make all constructors of the class private.

C++ Q11: Why can’t we overload the sizeof, :?, :: ., .* operators in c++
Answer: The restriction is for safety. For example if we overload. Operator then we can’t access member in normal way for that we have to use ->.

C++ Q12: What is importance of const. pointer in copy constructor?
Answer: Because otherwise you will pass the object to copy as an argument of copy constructor as pass by value which by definition creates a copy and so on... an infinite call chain

C++ Q13: Define namespace.
Answer: It is a feature in c++ to minimize name collisions in the global name space. This namespace keyword assigns a distinct name to a library that allows other libraries to use the same identifier names without creating any name collisions. Furthermore, the compiler uses the namespace signature for differentiating the definitions.


No comments: