Classes, Objects & Constructors

A class is a blueprint; an object is an instance in memory. Constructors initialize objects; destructors clean them up.

Cheat sheet

  • ▸Constructor: same name as class, no return type, runs on creation.
  • ▸Types: default, parameterized, copy constructor (C++).
  • ▸this refers to the current object.
  • ▸static members belong to the class, shared across objects.
  • ▸Shallow copy copies references; deep copy duplicates referenced data.
  • ▸C++ destructors run deterministically; Java uses garbage collection.

Shallow vs Deep copy

AspectShallowDeep
CopiesField values / pointersPointed-to data too
Shared stateYesNo
SpeedFastSlower
RiskDouble free, side effectsMemory use

Top 10 campus interview questions

Quick quiz

1. A constructor has return type:

2. Static variables are stored:

3. Private constructor is typical in:

4. Copying only the pointer is a:

5. Java String is: