Tuesday, June 09, 2009

Some for technical interview questions

1.

char *f(char *a)
{
char s[100];
strcpy(s, a);
return s;
}

char *f(char *a)
{
static char s[100];
strcpy(s, a);
return s;
}

char *f(char *a)
{
char *s = (char *)malloc(strlen(a));
if (s)
strcpy(s, a);
return s;
}


Which is correct implementation?
All functions have issue of not validate the source (a) before cop.
- first function does not work due to s declared as local variable (stack)
- 2nd function works. static variables are stored in data segment of memory and initialized only once
- 3rd function works. malloc from heap. Length should be strlen(a)+1 to reserve for null terminator.

2.

class A {
...
private:
char * m_myString;
};

implement assignment operator for A.

A& operator=(const A& a);

A& A::operator=(const A& a)
{
// check a is not this
// check m_myString is allocated
// copy a.m_myString to m_myString
return *this;
}

read this super-duper operator overloading tutorial -> link.

3. What event does selection on combobox trigger?

4. Explain Document View architecture of MFC.

5. What are the differences between stl:vector and stl:map

6. What is std::auto_ptr?

7. Multi-thread protection

8. How do you open a dialogbox?

No comments: