Social Icons

Friday, January 11, 2013

C++ INTERVIEW QUESTIONS FOR FRESHERS WITH ANSWERS PAPER 2



What do you mean by polymorphism?
Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object.
Example: function overloading, function overriding, virtual functions. Another example can be a plus ‘+’ sign, used for adding two integers or for using it to concatenate two strings.

it is the important concept of OOP, from the greek concept
'poly' means many 'morphism' form ,from this the definition 
is "the ability to take more than one form"
 
EXAMPLE:
method overloading(static polymorphism...early binding)
method overriding(dynamic polymorphism...late binding)
 
There are two types of polymorphism one is compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is functions and operators overloading. Runtime time polymorphism is done using inheritance and virtual functions.

Give an example of polymorphism with code
Example:
Public Class Calc
{
public void fnMultiply(int x, int y)
{ return x * y; }
public void fnMultiply(int x, int y, int z)
{ return x * y * z; }
}
...
...
Calc obj;
int Result;
Result = obj.fnMultiply(2,3,4); // The second fnMultiply would be called
Result = obj.fnMultiply(3,4); // The first fnMultiply would be called
//Here, the call depends on the number of parameters passed, polymorphism is achieved using overloading


Oops:
Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.
  1. Public: The data members and methods having public as access specifier can be accessed by the class objects created outside the class.
  2. Protected: The data members and methods declared as protected will be accessible to the class methods and the derived class methods only.
  3. Private: These data members and methods will be accessible from the class methods only not from derived classes and not from objects created outside the class.
  4. Internal: Some languages define internal as an access specifier which means the data member or method is available to all the classes inside that particular assembly.
  5. Friend: A friend class or method can access all data of a class including private and protected data.
       what is the difference between overloading & overriding

Overriding - same method names with same arguments and same return types associated in a class and its subclass.

Overloading - same method name with different arguments, may or may not be same return type written in the same class itself.

Example for overriding
Clas A
{
Virtual void hi(int a)
{
}
}

Class B:A
{
public overrid void hi(int a)
{

}
}

Example for Over loading

Class A
{
class a()

{

}
class a(int a)
{
}
}


20. What do you mean by inline function?
Ans: An inline function is a function that is expanded inline when invoked.ie. the compiler replaces the function call with the corresponding function code. An inline function is a function that is expanded in line when it is invoked. That is the compiler replaces the function call with the corresponding function code (similar to macro).

21 What is the difference between a NULL pointer and a void pointer?
Ans: A NULL pointer is a pointer of any type whose value is zero. A void pointer is a pointer to an object of an unknown type, and is guaranteed to have enough bits to hold a pointer to any object. A void pointer is not guaranteed to have enough bits to point to a function (though in general practice it does).


22. What is difference between C++ and Java?
Ans: C++ has pointers Java does not.
Java is platform independent C++ is not.
Java has garbage collection C++ does not.


23. What do you mean by multiple inheritance in C++ ?
Ans: Multiple inheritance is a feature in C++ by which one class can be of different types. Say class teaching Assistant is inherited from two classes say teacher and Student.


24. What do you mean by virtual methods?
Ans: virtual methods are used to use the polymorphism feature in C++. Say class A is inherited from class B. If we declare say function f() as virtual in class B and override the same function in class A then at runtime appropriate method of the class will be called depending upon the type of the object.


25. What do you mean by static methods?
Ans: By using the static method there is no need creating an object of that class to use that method. We can directly call that method on that class. For example, say class A has static function f(), then we can call f() function as A.f(). There is no need of creating an object of class A.


26. How many ways are there to initialize an int with a constant?
Ans: Two.
There are two formats for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation.
int foo = 123;
int bar (123);


27. What is a constructor?
Ans: Constructor is a special member function of a class, which is invoked automatically whenever an instance of the class is created. It has the same name as its class.


28. What is destructor?
Ans: Destructor is a special member function of a class, which is invoked automatically whenever an object goes out of the scope. It has the same name as its class with a tilde character prefixed.


29. What is an explicit constructor?
Ans: A conversion constructor declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. It’s purpose is reserved explicitly for construction.


30 What is the Standard Template Library?
Ans: A library of container templates approved by the ANSI committee for inclusion in the standard C++ specification. A programmer who then launches into a discussion of the generic programming model, iterators, allocators, algorithms, and such, has a higher than average understanding


 31. What problem does the namespace feature solve?
Ans: Multiple providers of libraries might use common global identifiers causing a name collision when an application tries to link with two or more such libraries. Thenamespace feature surrounds a library’s external declarations with a unique namespace that eliminates the potential for those collisions. This solution assumes that two library vendors don’t use the same namespace identifier, of course.


32. What is the use of ‘using’ declaration?
Ans: A using declaration makes it possible to use a name from a namespace


33. What is a template?
Ans: Templates allow us to create generic functions that admit any data type as parameters and return a value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones:
template function_declaration;
template function_declaration;


34. Differentiate between a template class and class template?
Ans:
Template class:
A generic definition or a parameterized class not instantiated until the client provides the needed information. It’s jargon for plain templates.
Class template:
A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed. It’s jargon for plain classes.


35. What is the difference between a copy constructor and an overloaded assignment operator?
Ans: A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.


36. What is a virtual destructor?
Ans: The simple answer is that a virtual destructor is one that is declared with the virtual attribute.


37. What is an incomplete type?
Ans: Incomplete type refers to pointers in which there is non availability of the implementation of the referenced location or it points to some location whose value is not available for modification.

Example:
int *i=0×400 // i points to address 400
*i=0; //set the value of memory location pointed by i.
Incomplete types are otherwise called uninitialized pointers.


38. What do you mean by Stack unwinding?
Ans: It is a process during exception handling when the destructor is called for all local objects between the place where the exception was thrown and where it is caught.


39. What is a container class? What are the types of container classes?
Ans: A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container

of the new technology that STL brings to C++ programming.

40. Name some pure object oriented languages?
Ans: Smalltalk, Java, Eiffel, Sather.