Instantiation of Objects

Unit 2 Β· Topic 2.9⏱ 8 min read❓ 5 MCQs

Introduction

Instantiation creates an object from a class using default or parameterized constructors.

Definition

Instantiation: creating an object instance from a class.

C++ Code Example

class Point { int x,y; public: Point(int a,int b):x(a),y(b){} };
int main(){
    Point p1(1,2); // instantiation
    Point p2 = p1;
}

Important Questions

Short Questions

  1. What is instantiation?
  2. Ways to instantiate?
  3. Stack vs heap instantiation?
  4. What is object creation?

Long Questions

  1. Explain instantiation with example.
  2. Discuss stack vs heap objects.

MCQ Practice

1. Instantiation means:

  • Creating object
  • Deleting object
  • Compiling class
  • Linking
Answer: Creating object.

2. Stack object is created by:

  • ClassName obj;
  • new ClassName
  • malloc
  • None
Answer: ClassName obj;.

3. Heap object is created by:

  • new
  • ClassName obj;
  • malloc only
  • None
Answer: new.

4. Copy constructor is used in:

  • Object copying
  • Deletion
  • Compilation
  • None
Answer: Object copying.

5. Instantiation calls:

  • Constructor
  • Destructor
  • main
  • None
Answer: Constructor.

Quick Revision

  • Instantiate via constructor.
  • Stack/heap both possible.