Friday, April 3, 2009

C++/Perl/Bash/Python questions

As usual some interview questions and answers that I encountered in one of the telephonic ones.

1. What is the difference between Bash/Shell, perl, python scripting languages?

--> Perl and Python can be used for web based scripting while bash cannot be used.
--> Easy portability for perl and python since both are platform independent while bash is not.
--> Various flavors of shell scripting, bash, korn, ksh, etc.

2. Why is a virtual constructor not possible?

A constructor cannot be virtual because at the time when the constructor is invoked, the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.

3. Can we have an array of references?

No we cannot have an array of references because references cannot be null as pointers can be. Once a reference is created, it cannot be later made to reference another object. We can say it cannot be reseated which is often done with pointers.

References cannot also be uninitialized.

4. Describe the callback function syntax.


5. What is the difference between exec() and system() in perl/bash?

exec() executes in the same shell while system() executes in another shell.

6. Define a constructor and how it might be 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.

There are two ways of calling a constructor

1. Implicitly - automatically by compiler when an object is created.
2. Explicitly - Calling the constructors explicitly is possible but it makes code unverifiable.

class Point
{
int x,y;
public:
Point():x(0),y(0) {} // Default constructor with no arguments
};

void main()
{
Point MyPoint; // Implicit constructor call. In order to allocate memory on stack, the default constructor is implicitly called.
Point *pPoint = new Point(); //Explicit constructor call. In order to allocate memory on heap we call the default constructor.


What is a modifier?

A modifier, also called a modifying function is a member function that changes the value of at least one data member. In other words, an operation that modifies the state of an object. Modifiers are also known as ‘mutators’. Example: The function mod is a modifier in the following code snippet:

class test
{
int x,y;
public:
test()
{
x=0; y=0;
}
void mod()
{
x=10;
y=15;
}
};

What is an accessor?

An accessor is a class operation that does not modify the state of an object. The accessor functions need to be declared as const operations
Differentiate between a template class and class template.
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

Differentiate between a template class and class template.

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.

What is a const method?

Marking a method as const signs a contract with the client code guaranteeing that you will not try to change the internal values of the object within the method.


If you try to declare a method const that actually modifies a data member, the compiler will complain.
You cannot declare a static method const because it is redundant.
Static methods do not have an instance of the class so it would be impossible for them to change internal values.
const works by making it appear inside the method that you have a const reference to each data member. Thus, if you try to change the data member the compiler will flag an error.

A non-const object can call const and no-const methods. However, a const object can only call const methods.

Syntax of a const method:

double SpreadSheetCell::getValue() const
{
return(mValue);
}

string SpreadSheetCell::getString() const
{
return (mString);
}

One should not get into the habit of declaring const all methods that don't modify the object so that you can use references to const objects in your program.

Note that const objects can still be destroyed, and their destructor can be called. You shouldn't try to mark the destructor const.

No comments: