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

Why classes are needed in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Class Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use classes instead of separate variables?

Imagine you want to store information about many books: title, author, and pages. Why is using a class better than separate variables?

AClasses make the program run faster by using less memory than variables.
BClasses group related data and behavior, making code organized and reusable.
CClasses automatically print data without writing extra code.
DClasses prevent any errors in the program without debugging.
Attempts:
2 left
💡 Hint

Think about how you keep related things together in real life, like a folder for papers.

Predict Output
intermediate
2:00remaining
What is the output of this C# class example?

Look at this code and choose the output it produces.

C Sharp (C#)
class Book {
    public string Title;
    public string Author;
    public Book(string title, string author) {
        Title = title;
        Author = author;
    }
}

class Program {
    static void Main() {
        Book b = new Book("1984", "Orwell");
        System.Console.WriteLine(b.Title + " by " + b.Author);
    }
}
A1984 by Orwell
BOrwell by 1984
CTitle by Author
DError: Cannot access Title
Attempts:
2 left
💡 Hint

The constructor sets the Title and Author fields. The WriteLine prints them in order.

🔧 Debug
advanced
2:00remaining
Why does this class code cause an error?

Find the reason this code causes a compile error.

C Sharp (C#)
class Car {
    string model;
    public Car(string m) {
        model = m;
    }
}

class Program {
    static void Main() {
        Car c = new Car("Tesla");
        System.Console.WriteLine(c.model);
    }
}
AThe constructor is missing a return type.
BThe class Car is missing a semicolon after its definition.
Cmodel is private by default and cannot be accessed outside the class.
DThe variable c is not initialized before use.
Attempts:
2 left
💡 Hint

Think about the default access level of class fields in C#.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a class with a method?

Choose the code that correctly defines a class with a method that prints a message.

A
class Dog {
    void Bark() {
        System.Console.WriteLine("Woof!")
    }
}
B
class Dog {
    public void Bark() {
        print("Woof!");
    }
}
C
class Dog {
    public Bark() {
        System.Console.WriteLine("Woof!");
    }
}
D
class Dog {
    public void Bark() {
        System.Console.WriteLine("Woof!");
    }
}
Attempts:
2 left
💡 Hint

Remember C# method syntax requires return type and semicolons.

🚀 Application
expert
2:00remaining
How many objects are created and what is output?

Analyze this code. How many objects are created and what is printed?

C Sharp (C#)
class Person {
    public string Name;
    public Person(string name) {
        Name = name;
    }
}

class Program {
    static void Main() {
        Person p1 = new Person("Alice");
        Person p2 = p1;
        p2.Name = "Bob";
        System.Console.WriteLine(p1.Name);
    }
}
AOne object; prints Bob
BTwo objects; prints Alice
CTwo objects; prints Bob
DOne object; prints Alice
Attempts:
2 left
💡 Hint

Think about what happens when you assign one object variable to another.