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

Runtime cost of dynamic type resolution in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dynamic Type Resolution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of dynamic vs static method call

What is the output of this C# code snippet?

C Sharp (C#)
using System;
class Program {
    static void Main() {
        dynamic d = "hello";
        string s = "hello";
        Console.WriteLine(d.ToUpper());
        Console.WriteLine(s.ToUpper());
    }
}
AHELLO\nhello
Bhello\nhello
CHELLO\nHELLO
Dhello\nHELLO
Attempts:
2 left
💡 Hint

Both dynamic and static strings call the same method, but dynamic resolution happens at runtime.

❓ Predict Output
intermediate
2:00remaining
Performance difference in repeated dynamic calls

Consider this code measuring elapsed time for 1 million calls. What is the expected output pattern?

C Sharp (C#)
using System;
using System.Diagnostics;
class Program {
    static void Main() {
        dynamic d = "test";
        string s = "test";
        Stopwatch sw = new Stopwatch();

        sw.Start();
        for (int i = 0; i < 1000000; i++) {
            var x = d.ToUpper();
        }
        sw.Stop();
        Console.WriteLine("Dynamic: " + sw.ElapsedMilliseconds);

        sw.Restart();
        for (int i = 0; i < 1000000; i++) {
            var y = s.ToUpper();
        }
        sw.Stop();
        Console.WriteLine("Static: " + sw.ElapsedMilliseconds);
    }
}
ACode throws runtime exception
BDynamic time < Static time
CDynamic time == Static time
DDynamic time > Static time
Attempts:
2 left
💡 Hint

Dynamic calls resolve method at runtime, adding overhead.

🔧 Debug
advanced
2:00remaining
Identify the runtime error in dynamic invocation

What error does this code produce when run?

C Sharp (C#)
using System;
class Program {
    static void Main() {
        dynamic d = 10;
        Console.WriteLine(d.ToUpper());
    }
}
ARuntimeBinderException: 'int' does not contain a definition for 'ToUpper'
BNullReferenceException
CNo error, prints 10
DSyntaxError
Attempts:
2 left
💡 Hint

Check if the type supports the called method.

🧠 Conceptual
advanced
2:00remaining
Why does dynamic type resolution add runtime cost?

Which explanation best describes why dynamic type resolution in C# adds runtime cost?

ABecause the compiler generates extra IL code to check types at runtime and invoke methods dynamically
BBecause dynamic variables use more memory than static variables
CBecause dynamic variables require manual type casting by the programmer
DBecause dynamic variables are compiled to slower machine code
Attempts:
2 left
💡 Hint

Think about what happens behind the scenes when you use dynamic.

❓ Predict Output
expert
2:00remaining
Output of mixed dynamic and static method calls with inheritance

What is the output of this C# program?

C Sharp (C#)
using System;
class Base {
    public virtual string GetName() => "Base";
}
class Derived : Base {
    public override string GetName() => "Derived";
}
class Program {
    static void Main() {
        Base b = new Derived();
        dynamic d = new Derived();
        Console.WriteLine(b.GetName());
        Console.WriteLine(d.GetName());
    }
}
ABase\nDerived
BDerived\nDerived
CDerived\nBase
DBase\nBase
Attempts:
2 left
💡 Hint

Remember how virtual methods behave with static and dynamic calls.

Practice

(1/5)
1. What does dynamic keyword in C# do at runtime?
easy
A. It performs type checking at compile time
B. It prevents any type errors completely
C. It delays type checking until the program runs
D. It converts all types to strings automatically

Solution

  1. Step 1: Understand the role of dynamic

    The dynamic keyword tells C# to skip compile-time type checking for that variable.
  2. Step 2: Identify when type checking happens

    Type checking for dynamic variables happens only when the program runs, not before.
  3. Final Answer:

    It delays type checking until the program runs -> Option C
  4. Quick Check:

    Dynamic = runtime type check [OK]
Hint: Dynamic means check types only when running [OK]
Common Mistakes:
  • Thinking dynamic checks types at compile time
  • Believing dynamic prevents all errors
  • Assuming dynamic converts types automatically
2. Which of the following is the correct syntax to declare a dynamic variable in C#?
easy
A. dynamic = 5 x;
B. var x = dynamic 5;
C. int dynamic x = 5;
D. dynamic x = 5;

Solution

  1. Step 1: Recall dynamic variable declaration syntax

    In C#, you declare a dynamic variable by writing dynamic followed by the variable name and value.
  2. Step 2: Check each option's syntax

    dynamic x = 5; matches the correct syntax: dynamic x = 5;. Others have incorrect order or keywords.
  3. Final Answer:

    dynamic x = 5; -> Option D
  4. Quick Check:

    Correct syntax = dynamic x = value [OK]
Hint: Use 'dynamic' before variable name [OK]
Common Mistakes:
  • Putting 'dynamic' after 'var'
  • Mixing 'int' and 'dynamic' keywords
  • Incorrect order of assignment
3. What will be the output of this C# code?
dynamic x = 10;
dynamic y = "20";
Console.WriteLine(x + y);
medium
A. 1020
B. Runtime exception
C. 30
D. Compile-time error

Solution

  1. Step 1: Analyze the types of variables

    Variable x is an integer 10, and y is a string "20".
  2. Step 2: Understand dynamic addition behavior

    With dynamic types, the + operator at runtime performs string concatenation when one operand is a string, resulting in "1020".
  3. Final Answer:

    1020 -> Option A
  4. Quick Check:

    dynamic int + string = concatenation "1020" [OK]
Hint: Dynamic number + string = string concatenation [OK]
Common Mistakes:
  • Expecting a runtime exception
  • Thinking it adds numerically to 30
  • Assuming compile-time error
4. Identify the error in this code snippet using dynamic types:
dynamic a = 5;
dynamic b = "hello";
var result = a - b;
Console.WriteLine(result);
medium
A. Runtime exception due to invalid operation
B. No error, outputs 0
C. Compile-time type mismatch error
D. Syntax error in variable declaration

Solution

  1. Step 1: Check operation between dynamic variables

    Subtracting an integer and a string is invalid at runtime.
  2. Step 2: Determine error type

    Since types are dynamic, error appears only when running, causing a runtime exception.
  3. Final Answer:

    Runtime exception due to invalid operation -> Option A
  4. Quick Check:

    Invalid dynamic operation = runtime exception [OK]
Hint: Invalid dynamic operations fail at runtime, not compile time [OK]
Common Mistakes:
  • Expecting compile-time error
  • Assuming subtraction works with string
  • Ignoring runtime exceptions
5. You want to improve performance in a C# program that uses many dynamic variables. Which approach reduces runtime cost of dynamic type resolution?
hard
A. Avoid testing dynamic code to save time
B. Replace dynamic variables with explicit static types where possible
C. Add more dynamic variables to delay errors
D. Use dynamic variables everywhere for flexibility

Solution

  1. Step 1: Understand runtime cost of dynamic

    Dynamic variables cause extra work at runtime to check types, slowing performance.
  2. Step 2: Choose optimization strategy

    Replacing dynamic with static types lets compiler check types early, improving speed.
  3. Final Answer:

    Replace dynamic variables with explicit static types where possible -> Option B
  4. Quick Check:

    Static types reduce dynamic runtime cost [OK]
Hint: Use static types to speed up dynamic-heavy code [OK]
Common Mistakes:
  • Thinking more dynamic variables improve speed
  • Ignoring testing importance
  • Believing dynamic has no runtime cost
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