Explore fundamental concepts of classes and objects in C# with this quiz, covering constructors, methods, properties, object instantiation, and class members. Perfect for beginners looking to reinforce their understanding of object-oriented programming principles within the C# language.
Which of the following defines a basic class named Car in C#?
Explanation: The correct syntax to define a class named Car in C# is 'class Car { }'. The other options are incorrect: 'Car class { }' reverses the order, 'def class Car { }' incorrectly uses the Python keyword 'def', and 'public void Car( )' is the syntax for a method, not a class.
How do you create a new instance of the Student class?
Explanation: In C#, 'Student student = new Student();' is the correct way to instantiate an object of the Student class. 'Student student = Student();' omits 'new', 'student = create Student();' uses an invalid keyword, and 'Student student = new Student{};' lacks parentheses required for object instantiation.
Which access modifier allows fields or methods to be accessible only within the same class?
Explanation: 'private' restricts access so the field or method is only accessible within its own class. 'public' makes members accessible from anywhere, 'protected' allows access within the class and derived classes, while 'internal' permits access within the same assembly but not necessarily the same class.
Given 'public int Age { get; set; }', which of the following best describes 'Age'?
Explanation: 'Age' is an auto-implemented property that provides get and set accessors automatically. It is not a field, which would lack accessors. 'Method' refers to a function, and 'Static variable' is unrelated because no static keyword is present.
What is the primary purpose of a constructor in a C# class?
Explanation: A constructor is used to set up initial values or perform setup tasks when an object is instantiated. It does not delete objects (that’s the job of the garbage collector), does not return values, and is unrelated to exposing static members.
Which statement is true about the relationship between a class and an object in C#?
Explanation: A class defines the structure and behaviors, serving as a blueprint, while an object is a specific instance of that class. An object does not contain the definition, classes are not made from objects, and classes and objects are not the same.
What is the effect of declaring a method in a class as static?
Explanation: A static method belongs to the class itself and can be called without an instance. It cannot access non-static members unless those are also static, it is not restricted to the constructor, and it cannot be overridden unless specifically allowed (and static methods cannot be overridden).
Which of the following correctly declares a private integer field named 'count' in a C# class?
Explanation: 'private int count;' is the correct way to declare a private integer field. 'int private count;' has the order of the keywords reversed, 'private count int;' is not valid C# syntax, and 'private int count()' is the syntax for a method, not a field.
Which of the following correctly defines a public method named Display that returns no value in C#?
Explanation: 'public void Display() { }' is correct because 'void' specifies no return value and the syntax matches C# conventions. 'public int Display() { }' returns an integer rather than void, 'void Display(public) { }' misplaces the access modifier, and 'public Display void() { }' does not follow valid method definition syntax.
Assuming you have an object 'person' with a property 'Name', how would you access the Name property in C#?
Explanation: 'person.Name' is the correct way to access the Name property in C#. 'person-u003EName' uses pointer syntax from C++, 'person:Name' is not C# syntax, and 'person[Name]' uses bracket notation, which is used for indexers or arrays, not properties.