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

Class declaration syntax in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Class Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple class instance property
What is the output of this C# code?
C Sharp (C#)
class Person {
    public string Name = "Alice";
}

var p = new Person();
Console.WriteLine(p.Name);
Anull
BName
CAlice
DPerson
Attempts:
2 left
💡 Hint
Look at the value assigned to the Name field inside the Person class.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in class declaration
Which option contains a syntax error in the class declaration?
Aclass Plane { public void Fly() { Console.WriteLine("Flying"); } }
Bclass Bike { private string model; }
Cpublic class Car { public int Speed; }
Dpublic sealed class Boat { }
Attempts:
2 left
💡 Hint
Check if all braces are properly closed.
🔧 Debug
advanced
2:00remaining
Why does this class instantiation cause an error?
What error will this code produce and why?
C Sharp (C#)
class Animal {
    private Animal() { }
}

var a = new Animal();
ACompile-time error: Missing semicolon
BCompile-time error: 'Animal.Animal()' is inaccessible due to its protection level
CNo error, outputs 'Animal created'
DRuntime error: NullReferenceException
Attempts:
2 left
💡 Hint
Check the accessibility of the constructor.
🧠 Conceptual
advanced
2:00remaining
Effect of 'sealed' keyword on class inheritance
What happens if you try to inherit from a sealed class in C#?
C Sharp (C#)
sealed class Vehicle { }
class Car : Vehicle { }
ACar inherits Vehicle successfully
BRuntime error: InvalidCastException
CWarning but compiles and runs
DCompile-time error: cannot derive from sealed type 'Vehicle'
Attempts:
2 left
💡 Hint
The sealed keyword prevents inheritance.
🚀 Application
expert
2:00remaining
Number of members in a class with properties and methods
How many members does this class have?
C Sharp (C#)
public class Calculator {
    public int Add(int a, int b) => a + b;
    public int Subtract(int a, int b) => a - b;
    public int Result { get; private set; }
    private void Reset() { Result = 0; }
}
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Count all methods and properties declared inside the class.