Bird
0
0

You want to create a class Book with a public string field Title and a constructor that sets Title. Which is the correct declaration?

hard🚀 Application Q8 of 15
C Sharp (C#) - Classes and Objects
You want to create a class Book with a public string field Title and a constructor that sets Title. Which is the correct declaration?
Apublic class Book { public string Title; public Book(string title) { Title = title; } }
Bclass Book { string Title; void Book(string title) { Title = title; } }
Cpublic class Book { private string Title; public void Book(string title) { Title = title; } }
Dpublic class Book { public string Title; public void Book(string title) { Title = title; } }
Step-by-Step Solution
Solution:
  1. Step 1: Check field and constructor declaration

    The field Title is public, and the constructor has the same name as the class without a return type.
  2. Step 2: Identify correct constructor syntax

    public class Book { public string Title; public Book(string title) { Title = title; } } correctly declares a public constructor Book(string title) that sets the field.
  3. Final Answer:

    public class Book { public string Title; public Book(string title) { Title = title; } } -> Option A
  4. Quick Check:

    Constructor name = class name, no return type [OK]
Quick Trick: Constructor name matches class name, no return type [OK]
Common Mistakes:
MISTAKES
  • Using void for constructor
  • Making field private without accessor
  • Omitting public keyword on class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes