Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Why understanding memory matters in C# - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a variable that stores an integer value.

C Sharp (C#)
int number = [1];
Drag options to blanks, or click blank then click option'
A"10"
B10
Ctrue
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the number, which makes it a string.
Assigning a boolean value instead of an integer.
2fill in blank
medium

Complete the code to allocate memory for a new object of class Person.

C Sharp (C#)
Person p = [1] Person();
Drag options to blanks, or click blank then click option'
Anew
Bcreate
Cmake
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keywords like 'create' or 'make' which are not valid in C#.
Forgetting the 'new' keyword when creating objects.
3fill in blank
hard

Fix the error in the code to correctly release unmanaged resources by implementing the Dispose pattern.

C Sharp (C#)
public void Dispose() [1]
Drag options to blanks, or click blank then click option'
A{ Console.WriteLine("Dispose called"); }
B{ return; }
C{ Dispose(true); GC.SuppressFinalize(this); }
D{ GC.SuppressFinalize(this); }
Attempts:
3 left
💡 Hint
Common Mistakes
Not calling the Dispose overload with the boolean parameter.
Only suppressing finalization without disposing resources.
4fill in blank
hard

Fill both blanks to create a dictionary that maps strings to their lengths, but only include words longer than 3 characters.

C Sharp (C#)
var lengths = new Dictionary<string, int> { { [1], [2] } };
Drag options to blanks, or click blank then click option'
A"example"
B"word"
C7
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string shorter than or equal to 3 characters.
Mismatching the string and its length.
5fill in blank
hard

Fill both blanks to create a method that returns the size of an array safely, returning 0 if the array is null.

C Sharp (C#)
public int GetSize(int[] arr) { return arr [1] [2] 0; }
Drag options to blanks, or click blank then click option'
A?
B?.Length
C??
D.Length
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking for null before accessing Length.
Using incorrect operators that cause errors.

Practice

(1/5)
1. Why is it important to understand how memory works in C#?
easy
A. To write faster and more efficient programs
B. To learn how to use loops
C. To create user interfaces
D. To change the color of text

Solution

  1. Step 1: Understand memory's role in performance

    Memory management affects how fast a program runs and how much memory it uses.
  2. Step 2: Connect memory knowledge to program efficiency

    Knowing stack and heap helps avoid slowdowns and memory leaks.
  3. Final Answer:

    To write faster and more efficient programs -> Option A
  4. Quick Check:

    Memory knowledge = faster programs [OK]
Hint: Memory knowledge improves speed and avoids errors [OK]
Common Mistakes:
  • Thinking memory only affects program size
  • Confusing memory with user interface design
  • Believing loops control memory
2. Which of the following correctly declares an integer variable stored on the stack in C#?
easy
A. string number = 10;
B. int[] number = new int[10];
C. var number = new object();
D. int number = 10;

Solution

  1. Step 1: Identify stack storage for simple types

    Simple types like int are stored on the stack when declared directly.
  2. Step 2: Check each option's type and declaration

    int number = 10; declares an int variable directly, stored on stack; others involve references or arrays on heap.
  3. Final Answer:

    int number = 10; -> Option D
  4. Quick Check:

    Simple int declaration = stack storage [OK]
Hint: Simple types declared directly go on stack [OK]
Common Mistakes:
  • Confusing arrays as stack variables
  • Using var with new object() creates heap object
  • Assigning string to int variable
3. What will be the output of this C# code?
using System;
class Program {
  static void Main() {
    int x = 5;
    int y = x;
    y = 10;
    Console.WriteLine(x);
  }
}
medium
A. 10
B. 0
C. 5
D. Compilation error

Solution

  1. Step 1: Understand value type assignment on stack

    Assigning x to y copies the value, so y is independent of x.
  2. Step 2: Check effect of changing y

    Changing y to 10 does not affect x, which remains 5.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    Value copy means x stays 5 [OK]
Hint: Value types copy values, original stays unchanged [OK]
Common Mistakes:
  • Thinking y changes x because of assignment
  • Expecting reference behavior for int
  • Confusing output with y's value
4. Identify the error in this C# code related to memory usage:
using System;
class Program {
  static void Main() {
    string s1 = "hello";
    string s2 = s1;
    s2 = "world";
    Console.WriteLine(s1);
  }
}
medium
A. No error; s1 remains "hello" because strings are immutable
B. s1 and s2 point to the same mutable object
C. s2 assignment causes a runtime error
D. Strings are mutable, so s1 changes to "world"

Solution

  1. Step 1: Understand string immutability in C#

    Strings cannot be changed after creation; assigning s2 = "world" creates a new string.
  2. Step 2: Check effect on s1

    s1 still points to "hello"; s2 points to new string "world".
  3. Final Answer:

    No error; s1 remains "hello" because strings are immutable -> Option A
  4. Quick Check:

    Immutable strings mean s1 unchanged [OK]
Hint: Strings are immutable; assignments create new objects [OK]
Common Mistakes:
  • Assuming strings can be changed in place
  • Expecting s1 to change after s2 assignment
  • Thinking runtime error occurs
5. You want to optimize a C# program that creates many small objects rapidly. Which memory understanding helps you improve performance?
hard
A. Knowing that value types are stored on the heap
B. Understanding that objects are stored on the heap and managing references carefully
C. Using only strings to reduce memory usage
D. Avoiding all use of the stack

Solution

  1. Step 1: Recall where objects are stored in C#

    Objects are stored on the heap, which is managed by the garbage collector.
  2. Step 2: Understand performance impact of heap allocations

    Creating many objects causes heap pressure; managing references and object reuse helps performance.
  3. Final Answer:

    Understanding that objects are stored on the heap and managing references carefully -> Option B
  4. Quick Check:

    Heap object management improves performance [OK]
Hint: Manage heap objects carefully to boost speed [OK]
Common Mistakes:
  • Thinking value types are on heap
  • Believing strings always reduce memory
  • Avoiding stack use unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard