Challenge - 5 Problems
Explicit Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of explicit interface implementation call
What is the output of this C# program when calling
obj.Print() and ((IPrinter)obj).Print()?C Sharp (C#)
using System; interface IPrinter { void Print(); } class Document : IPrinter { void IPrinter.Print() { Console.WriteLine("Printing from interface method"); } public void Print() { Console.WriteLine("Printing from class method"); } } class Program { static void Main() { Document obj = new Document(); obj.Print(); ((IPrinter)obj).Print(); } }
Attempts:
2 left
💡 Hint
Remember explicit interface methods can only be called through the interface reference.
✗ Incorrect
The class Document has two Print methods: one public and one explicit interface implementation. Calling obj.Print() calls the public method. Casting to IPrinter calls the explicit interface method.
❓ Predict Output
intermediate2:00remaining
Which method is called with explicit interface implementation?
Given the code below, what will be printed when
obj.Print() is called?C Sharp (C#)
using System; interface IPrinter { void Print(); } class Document : IPrinter { void IPrinter.Print() { Console.WriteLine("Interface Print"); } } class Program { static void Main() { Document obj = new Document(); obj.Print(); } }
Attempts:
2 left
💡 Hint
Explicit interface methods are not accessible through the class instance directly.
✗ Incorrect
The explicit interface method Print is not visible on the class instance. Calling obj.Print() causes a compile error because Document does not have a public Print method.
🔧 Debug
advanced2:00remaining
Why does this explicit interface implementation cause a runtime error?
Examine the code below. Why does calling
printer.Print() cause a runtime error?C Sharp (C#)
using System; interface IPrinter { void Print(); } class Document : IPrinter { void IPrinter.Print() { Console.WriteLine("Printing document"); } } class Program { static void Main() { Document doc = new Document(); IPrinter printer = null; printer.Print(); } }
Attempts:
2 left
💡 Hint
Check the value of the variable used to call the method.
✗ Incorrect
The variable printer is null. Calling any method on a null reference causes a NullReferenceException at runtime.
📝 Syntax
advanced2:00remaining
Identify the syntax error in explicit interface implementation
Which option contains a syntax error in explicit interface implementation?
Attempts:
2 left
💡 Hint
Explicit interface implementations cannot have access modifiers.
✗ Incorrect
Explicit interface implementations must not have access modifiers like public or private. Option A incorrectly uses 'public'.
🚀 Application
expert2:00remaining
How many methods are accessible from Document instance?
Given the code below, how many Print methods can be called directly from a Document instance without casting?
C Sharp (C#)
interface IPrinter { void Print(); } interface IScanner { void Print(); } class Document : IPrinter, IScanner { void IPrinter.Print() { } void IScanner.Print() { } public void Print() { } }
Attempts:
2 left
💡 Hint
Explicit interface methods are not accessible without casting.
✗ Incorrect
Only the public Print method is accessible directly from Document instance. The two explicit interface implementations require casting.