THOUGHTS

**The youth needs to develop an attitude : I can do it! We can do it !! India will do it!!! ""மரங்களை நடுவோம் பசுமை இல்ல விளைவை தடுப்போம்"" !!! PLEASE DO IT**

Thursday, August 11, 2011

Interview questions C AND C++ Part 1


What is reference ?

Reference is a name that acts as an alias, or alternative name, for a previously defined variable or an object. prepending variable with “&” symbol makes it as reference.
for example:
int a;
int &b = a;

What are C++ storage classes?

C++ Storage Classes:
auto: the default. Variables are automatically created and initialized when they are defined
and are destroyed at the end of the block containing their definition. They are not visible
outside that block
register: a type of auto variable. a suggestion to the compiler to use a CPU register for
performance
static: a variable that is known only in the function that contains its definition but is
never destroyed and retains its value between calls to that function. It exists from the
time the program begins execution
extern: a static variable whose definition and placement is determined when all object and
library modules are combined (linked) to form the executable code file. It can be visible
outside the file where it is defined.

What is conversion operator?

Class can have a public method for specific data type conversions.
for example:
class Boo
{
double value;
public:
Boo(int i )
operator double()
{
return value;
}
};
Boo BooObject;
double i = BooObject; // assigning object to variable i of type double. now conversion
 

How can a ‘::’ operator be used as unary operator?

The scope operator can be operator gets called to assign the value. used to refer to members of the global namespace. Because the global namespace doesn’t have a name, the notation :: member-name refers to a member of the global namespace. This can be useful for referring to members of global namespace whose names have been hidden by names declared in nested local scope. Unless we specify to the compiler in which namespace to search for a declaration, the compiler simple searches the current scope, and any scopes in which the current scope is nested, to find the declaration for the name.

Describe the main characteristics of static functions.

The main characteristics of static functions include,
It is without the a this pointer,
It can’t directly access the non-static members of its class
It can’t be declared const, volatile or virtual.
It doesn’t need to be invoked through an object of its class, although for convenience, it may.

What is name mangling?

Name mangling is the process through which your c++ compilers give each function in your program a unique name. In C++, all programs have at-least a few functions with the same name. Name mangling is a concession to the fact that linker always insists on all function names being unique.
Example:
In general, member names are made unique by concatenating the name of the member with that of the class e.g. given the declaration:
class Bar
{
public:
int ival;

};
ival becomes something like:
// a possible member name mangling
ival__3Bar
Consider this derivation:
class Foo : public Bar
{
public:
int ival;

}
The internal representation of a Foo object is the concatenation of its base and derived class members.
// Pseudo C++ code
// Internal representation of Foo
class Foo
{
public:
int ival__3Bar;
int ival__3Foo;

};
Unambiguous access of either ival members is achieved through name mangling. Member functions, because they can be overloaded, require an extensive mangling to provide each with a unique name. Here the compiler generates the same name for the two overloaded instances(Their argument lists make their instances unique).

What is slicing?

Slicing means that the data added by a subclass are discarded when an object of the subclass is passed or returned by value or from a function expecting a base class object.
Explanation:
Consider the following class declaration:
class base
{

base& operator =(const base&);
base (const base&);
}
void fun( )
{
base e=m;
e=m;
}
As base copy functions don’t know anything about the derived only the base part of the derived is copied. This is commonly referred to as slicing. One reason to pass objects of classes in a hierarchy is to avoid slicing. Other reasons are to preserve polymorphic behavior and to gain efficiency.

Why doesn’t C have nested functions?

C Compilers do not allow class types so they do not support internal function reference table like a C++ compiler does. Instead, you can use a structure having members function pointers. Older C++ compilers use to transform the C++ sources to C using this isomorphic transformation.

Difference between overloading and overriding?

1. Overload - two functions that appear in the same scope are overloaded if they have the same name but have different parameter list
2. main() cannot be overloaded
3. notational convenience - compiler invokes the functions that is the best match on the args – found by finding the best match between the type of arg expr and parameter
4. if declare a function locally, that function hides rather than overload the same function declared in an outer scope
5. Overriding - the ability of the inherited class rewriting the virtual method of a base class - a method which completely replaces base class FUNCTIONALITY in subclass
6. the overriding method in the subclass must have exactly the same signature as the function of the base class it is replacing - replacement of a method in a child class
7. writing a different body in a derived class for a function defined in a base class, ONLY if the function in the base class is virtual and ONLY if the function in the derived class has the same signature
8. all functions in the derived class hide the base class functions with the same name except in the case of a virtual functions which override the base class functions with the same signature

What is Constructor ? How it is called ?

Constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized.Ways of calling constructor:
1) Implicitly: automatically by complier when an object is created.
2) Calling the constructors explicitly is possible, but it makes the code unverifiable.

No comments:

Post a Comment