One of my friend who is working for amazon gave me a good collection of questions which helped him to get into Amazon. I am sharing these questions to help you guys.
- What are the major differences between C and C++?
- What are the differences between new and malloc?
- What is the difference between delete and delete[?
- What are the differences between a struct in C and in C++?
- What are the advantages/disadvantages of using #define?
- What are the advantages/disadvantages of using inline and const?
- What is the difference between a pointer and a reference?
- When would you use a pointer? A reference?
- What does it mean to take the address of a reference?
- What does it mean to declare a function or variable as static?
- What is the order of initialization for data?
- What is name mangling/name decoration?
- What kind of problems does name mangling cause?
- How do you work around them?
- What is a class?
- What are the differences between a struct and a class in C++?
- What is the difference between public, private, protected, and friend access?
- For class CFoo { }; what default methods will the compiler generate for you>?
- How can you force the compiler to not generate them?
- What is the purpose of a constructor? Destructor?
- What is a constructor initializer list?
- When must you use a constructor initializer list?
- What is a:
* Constructor?
* Destructor?
* Default constructor?
* Copy constructor?
* Conversion constructor? - What does it mean to declare a...
* member function as virtual?
* member function as static?
* member variable as static?
* destructor as static? - Can you explain the term "resource acquisition is initialization?"
- What is a "pure virtual" member function?
- What is the difference between public, private, and protected inheritance?
- What is virtual inheritance?
- What is placement new?
- What is the difference between operator new and the new operator?
- What is exception handling?
- Explain what happens when an exception is thrown in C++.
- What happens if an exception is not caught?
- What happens if an exception is throws from an object's constructor?
- What happens if an exception is throws from an object's destructor?
- What are the costs and benefits of using exceptions?
- When would you choose to return an error code rather than throw an exception?
- What is a template?
- What is partial specialization or template specialization?
- How can you force instantiation of a template?
- What is an iterator?
- What is an algorithm (in terms of the STL/C++ standard library)?
- What is std::auto_ptr?
- What is wrong with this statement?
std::auto_ptr ptr(new char[10]); - It is possible to build a C++ compiler on top of a C compiler. How would you do this?
Basic Questions
* What is a friend, and why do you need it?
* If this doesn't compile, why didn't it (or will it compile?) Don't show comments, which explain the problem.
templatevoid HashTable <>::dummy()
{
K* k = NULL;
Hashable* h = k; // If this fails to compile, it's because
// K is not derived from Hashable.
}
* This will loop forever. Why? Will it really loop forever? (Answer: Base:func() does not call Base::func(). Base: is just a label, so the line always will call Derived::func() until it runs out of stack space)
class Base {
public:
Base() {}
virtual void func() { /* do something */ }
};
class Derived : public Base {
public:
Derived() {}
virtual void func()
{
Base:func();
/* do something else */
}
};
main()
{
Derived d;
d.func(); // Never returns!
}
More advanced questions:
* What is a vtbl ?
* What is RTTI and why do you need it?
* How do I specialize a template? Give an example.
To separate sheep from goats (for those claiming C++ Guru status):
* What is a partial template? Why would you use one?
* How to I create a binary functor in the STL?
Given the following code:
class A;
class B;
class C {
A* a_;
B* b_;
public:
};
Implement a copy constructor and assignment operator for C. A sample solution is something like:
class C {
A* a_;
B* b_;
void swap(C& rhs) { rhs.a_ = a_; rhs.b_ = b_; }
public:
C(const C& rhs) {
auto_ptr<> a(new A(rhs.a_));
auto_ptr<> b(new B(rhs.b_)):
delete a_;
delete b_;
a_ = a.release();
b_ = b.release();
}
C& operator=(const C& rhs) {
C temp(rhs);
temp.swap(*this);
return *this;
}
};
What is wrong with this class, assuming that this is its complete interface?
class C {
char *p;
public:
C() { p = new char[64]; strcpy(p, "Hello world"); }
~C() { delete p; }
void foo() { cout << "My ptr is: '" << p << "'" << endl; }
};
Since this has an overtly programmed destructor, the member wise semantics for destruction are not good enough; therefore, they are not good enough for copy and assignment either. But, the copy ctor and op= are not programmed, so we will have some serious trouble.
Gradual hinting: what happens when we make a copy? [correct answer: pointer is copied]. Now, the original goes out of scope, what happens to the copy? [pointer dangles]. How would you fix it?
[also, that delete p should be delete[ p since p was allocated with the array new]
Assuming that swap() and copy construction are part of your interface for class C, what's the cookie-cutter pattern for operator= that uses them?
answer:
C& C::operator=(const C &rhs) {
if (this != &rhs) {
C tmp(rhs);
this->swap(tmp);
}
return *this;
}
]]
I found some mistakes in this post..i am trying to edit them to my knowledge..if you find one just comment it..
ReplyDelete