Challenge - 5 Problems
Built-in Attributes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Obsolete attribute warning
What will be the output when compiling and running this C# code?
C Sharp (C#)
using System; class Program { [Obsolete("Use NewMethod instead", false)] static void OldMethod() => Console.WriteLine("Old method called"); static void NewMethod() => Console.WriteLine("New method called"); static void Main() { OldMethod(); NewMethod(); } }
Attempts:
2 left
💡 Hint
Obsolete attribute with 'false' as second parameter issues a warning, not an error.
✗ Incorrect
The Obsolete attribute with 'false' means the compiler shows a warning but still compiles. The program runs and calls both methods, printing both lines.
❓ Predict Output
intermediate2:00remaining
Effect of Obsolete attribute with error=true
What happens when compiling this code?
C Sharp (C#)
using System; class Program { [Obsolete("Do not use this method", true)] static void ForbiddenMethod() => Console.WriteLine("Forbidden"); static void Main() { ForbiddenMethod(); } }
Attempts:
2 left
💡 Hint
Obsolete attribute with 'true' as second parameter causes a compilation error.
✗ Incorrect
The Obsolete attribute with 'true' causes the compiler to treat usage as an error, so the code does not compile.
❓ Predict Output
advanced2:00remaining
Serialization with Serializable attribute
What will be the output of this program?
C Sharp (C#)
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] class Person { public string Name; public int Age; } class Program { static void Main() { var p = new Person { Name = "Alice", Age = 30 }; var stream = new MemoryStream(); var formatter = new BinaryFormatter(); formatter.Serialize(stream, p); stream.Position = 0; var p2 = (Person)formatter.Deserialize(stream); Console.WriteLine($"Name: {p2.Name}, Age: {p2.Age}"); } }
Attempts:
2 left
💡 Hint
The Person class is marked Serializable, so it can be serialized and deserialized correctly.
✗ Incorrect
Because Person is marked with [Serializable], the BinaryFormatter can serialize and deserialize it, preserving the data.
❓ Predict Output
advanced2:00remaining
Serialization failure without Serializable attribute
What happens when running this code?
C Sharp (C#)
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; class Person { public string Name; public int Age; } class Program { static void Main() { var p = new Person { Name = "Bob", Age = 25 }; var stream = new MemoryStream(); var formatter = new BinaryFormatter(); formatter.Serialize(stream, p); } }
Attempts:
2 left
💡 Hint
Classes must be marked [Serializable] to be serialized by BinaryFormatter.
✗ Incorrect
Since Person is not marked with [Serializable], serialization throws a SerializationException at runtime.
🧠 Conceptual
expert2:00remaining
Understanding Obsolete attribute behavior
Consider this code snippet:
[Obsolete("Use NewMethod", false)]
void OldMethod() {}
[Obsolete("Use NewMethod", true)]
void ForbiddenMethod() {}
Which statement is true about these methods?
Attempts:
2 left
💡 Hint
The second parameter of Obsolete attribute controls if usage is error or warning.
✗ Incorrect
Obsolete with false causes warning; with true causes error.