What is the basic syntax to declare a class in C#?
Use the keyword <code>class</code> followed by the class name and curly braces. Example:<br><pre>class MyClass { }</pre>
Click to reveal answer
beginner
What keyword is used to declare a class in C#?
The keyword class is used to declare a class.
Click to reveal answer
beginner
Can a class in C# contain methods and variables?
Yes, a class can contain variables (fields) and methods (functions) inside its curly braces.
Click to reveal answer
beginner
How do you declare a public class named <code>Car</code> in C#?
Use the <code>public</code> access modifier before the class keyword:<br><pre>public class Car { }</pre>
Click to reveal answer
beginner
What is the purpose of curly braces <code>{ }</code> in a class declaration?
Curly braces define the body of the class where you put variables, methods, and other members.
Click to reveal answer
Which keyword is used to declare a class in C#?
Aclass
Bstruct
Cinterface
Dnamespace
✗ Incorrect
The class keyword declares a class in C#.
What symbol is used to enclose the body of a class?
A[]
B()
C{}
D<>
✗ Incorrect
Curly braces { } enclose the class body.
How do you declare a class named Person that is accessible from other classes?
Apublic class Person { }
Bclass Person { }
Cprivate class Person { }
Dprotected class Person { }
✗ Incorrect
Use public to make the class accessible from other classes.
Which of these can be inside a class body?
AOnly variables
BVariables and methods
COnly methods
DNothing
✗ Incorrect
Classes can contain both variables (fields) and methods.
What is the correct way to start a class declaration named Animal?
Aclass Animal[] {
BAnimal class {
Cclass Animal() {
Dclass Animal {
✗ Incorrect
The correct syntax is class Animal { followed by the class body.
Explain how to declare a simple class in C# including access modifier and body.
Think about the keywords and symbols you use to start and end a class.
You got /4 concepts.
Describe what can be included inside a class body in C#.
What parts make up the behavior and data of a class?
You got /4 concepts.
Practice
(1/5)
1. What is the correct way to declare a class named Car in C#?
easy
A. Car class { }
B. class Car { }
C. class = Car { }
D. class: Car { }
Solution
Step 1: Understand class declaration syntax
In C#, a class is declared using the keyword class followed by the class name and curly braces.
Step 2: Match the correct syntax
class Car { } uses class Car { }, which is the correct syntax for declaring a class named Car.
Final Answer:
class Car { } -> Option B
Quick Check:
Class declaration = class ClassName { } [OK]
Hint: Remember: class keyword + name + braces [OK]
Common Mistakes:
Putting class name before keyword
Using = or : instead of space
Missing curly braces
2. Which of the following is a syntax error when declaring a class in C#?
easy
A. public class Person { }
B. class Animal { }
C. class 123Car { }
D. internal class House { }
Solution
Step 1: Check class name rules
Class names must start with a letter or underscore, not a number.
Step 2: Identify invalid class name
class 123Car { } uses class 123Car { }, which starts with digits, causing a syntax error.
Final Answer:
class 123Car { } -> Option C
Quick Check:
Class names cannot start with numbers [OK]
Hint: Class names must start with letter or underscore [OK]
Common Mistakes:
Starting class name with a digit
Using spaces in class name
Using reserved keywords as class names
3. What will be the output of the following code?
class Dog {
public string Name = "Buddy";
}
class Program {
static void Main() {
Dog d = new Dog();
System.Console.WriteLine(d.Name);
}
}
medium
A. Buddy
B. Name
C. Dog
D. Compilation error
Solution
Step 1: Understand class field initialization
The class Dog has a public field Name initialized to "Buddy".
Step 2: Trace the program output
In Main, a Dog object is created and d.Name is printed, so output is "Buddy".
Final Answer:
Buddy -> Option A
Quick Check:
Field value printed = Buddy [OK]
Hint: Prints field value assigned in class [OK]
Common Mistakes:
Confusing class name with field value
Expecting method output instead of field
Assuming compilation error without reason
4. Identify the error in this class declaration:
class Book
{
string title;
void SetTitle(string t)
{
title = t;
}
}
medium
A. Missing access modifiers for field and method
B. Class name should be lowercase
C. Method SetTitle must return a value
D. Field title must be static
Solution
Step 1: Check access modifiers in class members
By default, class members are private, but it's good practice to specify access modifiers explicitly.
Step 2: Identify missing access modifiers
Field title and method SetTitle lack access modifiers like private or public, which can cause confusion or errors in some contexts.
Final Answer:
Missing access modifiers for field and method -> Option A
Quick Check:
Always specify access modifiers [OK]
Hint: Always add public/private to fields and methods [OK]
Common Mistakes:
Assuming lowercase class names are required
Thinking void methods must return a value
Believing fields must be static
5. You want to create a class Student with a field name and a method GetName that returns the student's name. Which is the correct complete class declaration?
hard
A. class Student { public string name; public void GetName() { return name; } }
B. class Student { string name; string GetName() { name; } }
C. class Student { public string name; string GetName() { return name; } }
D. class Student { public string name; public string GetName() { return name; } }
Solution
Step 1: Check field and method access modifiers
The field name and method GetName should be public to be accessible outside the class.
Step 2: Verify method return type and body
GetName returns a string, so its return type must be string and it must return name.
Step 3: Identify correct option
class Student { public string name; public string GetName() { return name; } } correctly declares name as public string and GetName as public string method returning name.
Final Answer:
class Student { public string name; public string GetName() { return name; } } -> Option D
Quick Check:
Public field + public string method returning field [OK]
Hint: Method return type must match returned value type [OK]
Common Mistakes:
Missing return statement in method
Wrong method return type (void instead of string)
Missing public keyword for accessibility
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.