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

Namespaces and using directives in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Namespace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of accessing classes with namespaces
What is the output of this C# program?
C Sharp (C#)
namespace Alpha {
    public class Printer {
        public static string Print() => "Alpha Printer";
    }
}

namespace Beta {
    public class Printer {
        public static string Print() => "Beta Printer";
    }
}

class Program {
    static void Main() {
        System.Console.WriteLine(Alpha.Printer.Print());
        System.Console.WriteLine(Beta.Printer.Print());
    }
}
ACompilation error due to ambiguous Printer class
BBeta Printer\nAlpha Printer
CAlpha Printer
DAlpha Printer\nBeta Printer
Attempts:
2 left
💡 Hint
Look at how fully qualified names are used to call Print methods.
❓ Predict Output
intermediate
2:00remaining
Effect of using directive on ambiguous class names
Given these namespaces and using directives, what will be the output?
C Sharp (C#)
using Alpha;
using Beta;

namespace Alpha {
    public class Printer {
        public static string Print() => "Alpha Printer";
    }
}

namespace Beta {
    public class Printer {
        public static string Print() => "Beta Printer";
    }
}

class Program {
    static void Main() {
        System.Console.WriteLine(Printer.Print());
    }
}
ACompilation error due to ambiguous Printer
BBeta Printer
CAlpha Printer
DRuntime error
Attempts:
2 left
💡 Hint
Two namespaces contain Printer class, both imported with using.
❓ Predict Output
advanced
2:00remaining
Using alias directive to resolve ambiguity
What is the output of this program using alias directives?
C Sharp (C#)
using A = Alpha.Printer;
using B = Beta.Printer;

namespace Alpha {
    public class Printer {
        public static string Print() => "Alpha Printer";
    }
}

namespace Beta {
    public class Printer {
        public static string Print() => "Beta Printer";
    }
}

class Program {
    static void Main() {
        System.Console.WriteLine(A.Print());
        System.Console.WriteLine(B.Print());
    }
}
ARuntime error
BBeta Printer\nAlpha Printer
CAlpha Printer\nBeta Printer
DCompilation error due to alias misuse
Attempts:
2 left
💡 Hint
Alias directives create shortcuts to fully qualified names.
❓ Predict Output
advanced
2:00remaining
Namespace nesting and class access
What will this program print?
C Sharp (C#)
namespace Outer {
    public class Printer {
        public static string Print() => "Outer Printer";
    }
    namespace Inner {
        public class Printer {
            public static string Print() => "Inner Printer";
        }
    }
}

class Program {
    static void Main() {
        System.Console.WriteLine(Outer.Printer.Print());
        System.Console.WriteLine(Outer.Inner.Printer.Print());
    }
}
AInner Printer\nOuter Printer
BOuter Printer\nInner Printer
CCompilation error due to nested namespace
DRuntime error
Attempts:
2 left
💡 Hint
Nested namespaces are accessed by chaining names.
❓ Predict Output
expert
2:00remaining
Using directives and static class members
What is the output of this program?
C Sharp (C#)
using static System.Math;

class Program {
    static void Main() {
        double x = 3.0;
        double y = Sqrt(x * x);
        System.Console.WriteLine(y);
    }
}
A3
B9
CCompilation error due to using static
DRuntime error
Attempts:
2 left
💡 Hint
using static allows calling static members without class name.

Practice

(1/5)
1. What is the main purpose of a namespace in C#?
easy
A. To group related classes and avoid name conflicts
B. To execute code faster
C. To store variables globally
D. To create user interfaces

Solution

  1. Step 1: Understand what a namespace does

    A namespace groups related classes, structs, and other types to organize code logically.
  2. Step 2: Identify the main benefit

    Namespaces help avoid name conflicts by separating code into distinct areas.
  3. Final Answer:

    To group related classes and avoid name conflicts -> Option A
  4. Quick Check:

    Namespace purpose = grouping and avoiding conflicts [OK]
Hint: Namespaces group code to prevent name clashes [OK]
Common Mistakes:
  • Thinking namespaces speed up code execution
  • Confusing namespaces with global variables
  • Assuming namespaces create UI elements
2. Which of the following is the correct syntax to use a namespace in C#?
easy
A. namespace System.Text;
B. use System.Text;
C. import System.Text;
D. using System.Text;

Solution

  1. Step 1: Recall the keyword for importing namespaces

    In C#, the keyword to include namespaces is using.
  2. Step 2: Check the syntax format

    The correct syntax is using NamespaceName;, so using System.Text; is correct.
  3. Final Answer:

    using System.Text; -> Option D
  4. Quick Check:

    Keyword for namespaces = using [OK]
Hint: Remember: C# uses 'using' to include namespaces [OK]
Common Mistakes:
  • Using 'use' or 'import' instead of 'using'
  • Confusing namespace declaration with usage
  • Adding semicolon after 'namespace' keyword
3. What will be the output of this code?
using System;
namespace DemoApp {
  class Program {
    static void Main() {
      Console.WriteLine("Hello from DemoApp");
    }
  }
}
medium
A. Hello from DemoApp
B. Error: Missing using directive
C. No output
D. Hello from System

Solution

  1. Step 1: Analyze the code structure

    The code uses the System namespace and defines a DemoApp namespace with a Program class and Main method.
  2. Step 2: Understand the output command

    The Console.WriteLine prints the string "Hello from DemoApp" to the console.
  3. Final Answer:

    Hello from DemoApp -> Option A
  4. Quick Check:

    Console.WriteLine prints the given string [OK]
Hint: Console.WriteLine prints exactly the string inside quotes [OK]
Common Mistakes:
  • Thinking namespace affects output text
  • Assuming missing using causes error here
  • Expecting output from namespace name
4. Identify the error in this code snippet:
namespace MyApp {
  class Program {
    static void Main() {
      Console.WriteLine("Welcome");
    }
  }
}
medium
A. Incorrect namespace syntax
B. Missing class declaration
C. Missing using directive for System namespace
D. Main method must be public

Solution

  1. Step 1: Check usage of Console.WriteLine

    The code uses Console.WriteLine which is inside the System namespace.
  2. Step 2: Verify if System namespace is included

    There is no using System; directive, so the compiler won't recognize Console.
  3. Final Answer:

    Missing using directive for System namespace -> Option C
  4. Quick Check:

    Console requires 'using System;' [OK]
Hint: Always add 'using System;' to use Console class [OK]
Common Mistakes:
  • Assuming Main must be public (it can be private or default)
  • Thinking namespace syntax is wrong
  • Missing class declaration (class is present)
5. You have two classes named Logger in different namespaces: Utils.Logging and App.Services. How can you use both classes in the same file without name conflicts?
hard
A. Put both classes in the global namespace
B. Use full names with namespaces or alias one namespace with 'using Alias = ...;'
C. Use only one namespace at a time
D. Rename one class to avoid conflict

Solution

  1. Step 1: Understand the problem of same class names

    Two classes named Logger in different namespaces cause conflicts if used without qualification.
  2. Step 2: Use full names or aliasing

    You can use full names like Utils.Logging.Logger and App.Services.Logger or create an alias with using LogUtils = Utils.Logging; to differentiate.
  3. Final Answer:

    Use full names with namespaces or alias one namespace with 'using Alias = ...;' -> Option B
  4. Quick Check:

    Alias or full names avoid class name conflicts [OK]
Hint: Use aliases or full names to avoid class name clashes [OK]
Common Mistakes:
  • Thinking you must rename classes physically
  • Assuming you cannot use both namespaces together
  • Ignoring namespace qualification
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