Amazon OOPS Interview Questions

Hi Friends,

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.


  1. What are the major differences between C and C++?

  2. What are the differences between new and malloc?

  3. What is the difference between delete and delete[?

  4. What are the differences between a struct in C and in C++?

  5. What are the advantages/disadvantages of using #define?

  6. What are the advantages/disadvantages of using inline and const?

  7. What is the difference between a pointer and a reference?

  8. When would you use a pointer? A reference?

  9. What does it mean to take the address of a reference?

  10. What does it mean to declare a function or variable as static?

  11. What is the order of initialization for data?

  12. What is name mangling/name decoration?

  13. What kind of problems does name mangling cause?

  14. How do you work around them?

  15. What is a class?

  16. What are the differences between a struct and a class in C++?

  17. What is the difference between public, private, protected, and friend access?

  18. For class CFoo { }; what default methods will the compiler generate for you>?

  19. How can you force the compiler to not generate them?

  20. What is the purpose of a constructor? Destructor?

  21. What is a constructor initializer list?

  22. When must you use a constructor initializer list?

  23. What is a:
    * Constructor?
    * Destructor?
    * Default constructor?
    * Copy constructor?
    * Conversion constructor?

  24. What does it mean to declare a...

    * member function as virtual?
    * member function as static?
    * member variable as static?
    * destructor as static?

  25. Can you explain the term "resource acquisition is initialization?"

  26. What is a "pure virtual" member function?

  27. What is the difference between public, private, and protected inheritance?

  28. What is virtual inheritance?

  29. What is placement new?

  30. What is the difference between operator new and the new operator?

  31. What is exception handling?

  32. Explain what happens when an exception is thrown in C++.

  33. What happens if an exception is not caught?

  34. What happens if an exception is throws from an object's constructor?

  35. What happens if an exception is throws from an object's destructor?

  36. What are the costs and benefits of using exceptions?

  37. When would you choose to return an error code rather than throw an exception?

  38. What is a template?

  39. What is partial specialization or template specialization?

  40. How can you force instantiation of a template?

  41. What is an iterator?

  42. What is an algorithm (in terms of the STL/C++ standard library)?

  43. What is std::auto_ptr?

  44. What is wrong with this statement?
    std::auto_ptr ptr(new char[10]);

  45. 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.


template void 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;
}
]]

1 comment:

  1. I found some mistakes in this post..i am trying to edit them to my knowledge..if you find one just comment it..

    ReplyDelete