Challenge - 5 Problems
Namespace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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());
}
}Attempts:
2 left
💡 Hint
Look at how fully qualified names are used to call Print methods.
✗ Incorrect
The program calls Print() from Alpha.Printer and Beta.Printer explicitly, so it prints both lines in order.
❓ Predict Output
intermediate2: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());
}
}Attempts:
2 left
💡 Hint
Two namespaces contain Printer class, both imported with using.
✗ Incorrect
The compiler cannot decide which Printer to use because both Alpha and Beta namespaces are imported and both have Printer class. This causes a compilation error.
❓ Predict Output
advanced2: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());
}
}Attempts:
2 left
💡 Hint
Alias directives create shortcuts to fully qualified names.
✗ Incorrect
Alias directives A and B refer to Alpha.Printer and Beta.Printer respectively, so calling A.Print() and B.Print() prints both lines correctly.
❓ Predict Output
advanced2: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());
}
}Attempts:
2 left
💡 Hint
Nested namespaces are accessed by chaining names.
✗ Incorrect
Outer.Printer.Print() calls the class in Outer namespace, Outer.Inner.Printer.Print() calls the nested Inner namespace class. Both print their respective strings.
❓ Predict Output
expert2: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); } }
Attempts:
2 left
💡 Hint
using static allows calling static members without class name.
✗ Incorrect
Sqrt is called directly because of using static System.Math; Sqrt(3*3) = 3, so output is 3.