0
0
C Sharp (C#)programming~20 mins

Built-in attributes (Obsolete, Serializable) in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Built-in Attributes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
ANew method called only
BCompilation error due to Obsolete attribute
COld method called\nNew method called
DRuntime exception thrown
Attempts:
2 left
💡 Hint
Obsolete attribute with 'false' as second parameter issues a warning, not an error.
Predict Output
intermediate
2: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();
    }
}
AProgram runs and prints 'Forbidden'
BCompilation error: 'ForbiddenMethod' is obsolete
CRuntime exception thrown
DCompilation warning only
Attempts:
2 left
💡 Hint
Obsolete attribute with 'true' as second parameter causes a compilation error.
Predict Output
advanced
2: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}");
    }
}
AName: Alice, Age: 30
BRuntime exception: SerializationException
CCompilation error: BinaryFormatter not found
DName: , Age: 0
Attempts:
2 left
💡 Hint
The Person class is marked Serializable, so it can be serialized and deserialized correctly.
Predict Output
advanced
2: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);
    }
}
ARuntime exception: SerializationException
BProgram runs and serializes successfully
CProgram prints serialized data
DCompilation error: Person not serializable
Attempts:
2 left
💡 Hint
Classes must be marked [Serializable] to be serialized by BinaryFormatter.
🧠 Conceptual
expert
2: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?
ANeither method causes any compiler warnings or errors.
BBoth methods cause compiler errors when called.
CBoth methods cause only compiler warnings when called.
DCalling OldMethod causes a compiler warning; calling ForbiddenMethod causes a compiler error.
Attempts:
2 left
💡 Hint
The second parameter of Obsolete attribute controls if usage is error or warning.