0
0
C Sharp (C#)programming~20 mins

Stack vs heap mental model in C Sharp (C#) - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack vs Heap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this C# code involving stack and heap?

Consider the following C# code snippet. What will be printed to the console?

C Sharp (C#)
using System;
class Program {
    static void Main() {
        int x = 10;
        int y = x;
        y = 20;
        Console.WriteLine(x);
    }
}
A0
B20
C10
DCompilation error
Attempts:
2 left
💡 Hint

Think about where value types like int are stored and how assignment works.

Predict Output
intermediate
2:00remaining
What is the output when modifying a reference type?

Look at this C# code. What will it print?

C Sharp (C#)
using System;
class Program {
    class Box { public int Value; }
    static void Main() {
        Box a = new Box();
        a.Value = 5;
        Box b = a;
        b.Value = 10;
        Console.WriteLine(a.Value);
    }
}
A10
BCompilation error
C0
D5
Attempts:
2 left
💡 Hint

Remember that classes are reference types stored on the heap.

🧠 Conceptual
advanced
2:00remaining
Which statement best describes stack and heap in C#?

Choose the statement that correctly explains the difference between stack and heap memory in C#.

AStack and heap are the same memory area but used differently.
BStack stores value types and method call info; heap stores reference type objects.
CHeap stores local variables and method parameters; stack stores objects created with <code>new</code>.
DStack stores reference types and heap stores value types.
Attempts:
2 left
💡 Hint

Think about where value types and reference types are stored during execution.

🔧 Debug
advanced
2:00remaining
Why does this code cause a NullReferenceException?

Examine the code below. Why does it throw a NullReferenceException?

C Sharp (C#)
using System;
class Program {
    class Container { public string Text; }
    static void Main() {
        Container c = null;
        c.Text = "Hello";
        Console.WriteLine(c.Text);
    }
}
ABecause <code>c</code> is null and accessing <code>Text</code> causes an error.
BBecause <code>Text</code> is not initialized to a string.
CBecause <code>Console.WriteLine</code> cannot print strings.
DBecause <code>Container</code> is a value type and cannot be null.
Attempts:
2 left
💡 Hint

Think about what happens when you try to access a member of a null reference.

🚀 Application
expert
2:00remaining
How many objects are created on the heap after this code runs?

Consider this C# code snippet. How many objects are created on the heap after Main finishes?

C Sharp (C#)
using System;
class Program {
    class Node {
        public int Value;
        public Node Next;
    }
    static void Main() {
        Node first = new Node();
        first.Value = 1;
        Node second = new Node();
        second.Value = 2;
        first.Next = second;
        Node third = second;
    }
}
A0
B1
C3
D2
Attempts:
2 left
💡 Hint

Count how many times new is used to create objects.