Embercode
✕
Pointers and Memory AddressesQuick quiz

Dangling and null

6 questions. Covers Dangling and null bugs.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``cpp #include <iostream> int value_or_zero(const int* p) { return p != nullptr ? *p : 0; } int main() { int a = 4; int b = 9; int* p = &a; int* q = p; p = &b; std::cout << value_or_zero(p) << ' ' << value_or_zero(q) << ' ' << value_or_zero(nullptr) << '\n'; return 0; } ``
  2. 2. Which statement describes the bug in this program? ``cpp #include <iostream> int main() { int* p = new int(7); int* q = p; delete p; p = nullptr; std::cout << *q << "\n"; return 0; } ``
  3. 3. What is the bug in this function? ``cpp int* make_number() { int local = 5; return &local; } ``
  4. 4. Why does a p != nullptr check not protect against a dangling pointer?
  5. 5. A Java program has String name = null; and then calls name.length(). What happens?

5 question(s) left on this page.