PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Software Engineering Fundamentals/J.P. Morgan

Explain C++ virtual dispatch and object lifetime

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's understanding of C++ virtual function dispatch, destructor and object lifetime semantics, stack versus heap allocation, dangling-pointer and double-delete pitfalls, and pointer versus reference distinctions within the Software Engineering Fundamentals domain and the C++ language.

  • medium
  • J.P. Morgan
  • Software Engineering Fundamentals
  • Data Scientist

Explain C++ virtual dispatch and object lifetime

Company: J.P. Morgan

Role: Data Scientist

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

Answer the following C++ fundamentals questions. ## 1) Virtual function dispatch: predict output Consider this C++ program: ```cpp #include <iostream> using namespace std; struct Base { Base() { cout << "Base()\n"; } virtual void vf() { cout << "Base::vf\n"; } void nvf() { cout << "Base::nvf\n"; } virtual ~Base() { cout << "~Base()\n"; } }; struct Derived : Base { Derived() { cout << "Derived()\n"; } void vf() override { cout << "Derived::vf\n"; } void nvf() { cout << "Derived::nvf\n"; } ~Derived() override { cout << "~Derived()\n"; } }; int main() { Base* p = new Derived(); p->vf(); p->nvf(); delete p; } ``` - What is the exact output (line by line)? - Explain why each line prints (virtual dispatch vs non-virtual binding, and destructor behavior). ## 2) Destructors and dangling pointers For each snippet below: - identify whether it is correct, leaks memory, causes undefined behavior, or creates a dangling pointer; - explain why; - show a safer/fixed version. ### (a) ```cpp int* make() { int x = 42; return &x; } ``` ### (b) ```cpp int* p = new int(7); delete p; cout << *p << "\n"; ``` ### (c) ```cpp int* p = new int(7); int* q = p; delete p; delete q; ``` ## 3) Pointer vs reference Explain the differences between pointers and references in C++ (at least: nullability, reseating, ownership semantics, parameter passing, const behavior), and give examples of when you would choose each.

Quick Answer: This question evaluates a candidate's understanding of C++ virtual function dispatch, destructor and object lifetime semantics, stack versus heap allocation, dangling-pointer and double-delete pitfalls, and pointer versus reference distinctions within the Software Engineering Fundamentals domain and the C++ language.

Related Interview Questions

  • Review a PR for thread-safe request handling - J.P. Morgan (medium)
  • Review concurrent code quality - J.P. Morgan (medium)
  • Compute expected value of simple gambling games - J.P. Morgan (easy)
J.P. Morgan logo
J.P. Morgan
Jan 22, 2026, 12:00 AM
Data Scientist
Onsite
Software Engineering Fundamentals
2
0
Loading...

Answer the following C++ fundamentals questions.

1) Virtual function dispatch: predict output

Consider this C++ program:

#include <iostream>
using namespace std;

struct Base {
    Base() { cout << "Base()\n"; }
    virtual void vf() { cout << "Base::vf\n"; }
    void nvf() { cout << "Base::nvf\n"; }
    virtual ~Base() { cout << "~Base()\n"; }
};

struct Derived : Base {
    Derived() { cout << "Derived()\n"; }
    void vf() override { cout << "Derived::vf\n"; }
    void nvf() { cout << "Derived::nvf\n"; }
    ~Derived() override { cout << "~Derived()\n"; }
};

int main() {
    Base* p = new Derived();
    p->vf();
    p->nvf();
    delete p;
}
  • What is the exact output (line by line)?
  • Explain why each line prints (virtual dispatch vs non-virtual binding, and destructor behavior).

2) Destructors and dangling pointers

For each snippet below:

  • identify whether it is correct, leaks memory, causes undefined behavior, or creates a dangling pointer;
  • explain why;
  • show a safer/fixed version.

(a)

int* make() {
    int x = 42;
    return &x;
}

(b)

int* p = new int(7);
delete p;
cout << *p << "\n";

(c)

int* p = new int(7);
int* q = p;
delete p;
delete q;

3) Pointer vs reference

Explain the differences between pointers and references in C++ (at least: nullability, reseating, ownership semantics, parameter passing, const behavior), and give examples of when you would choose each.

Solution

Show

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Software Engineering Fundamentals•More J.P. Morgan•More Data Scientist•J.P. Morgan Data Scientist•J.P. Morgan Software Engineering Fundamentals•Data Scientist Software Engineering Fundamentals
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.