Exercise: Properties

Questions for: Properties

An Employee class has a property called age and emp is reference to a Employee object and we want the statement Console.WriteLine(emp.age) to fail. Which of the following options will ensure this functionality?
A:
Declare age property with only get accessor.
B:
Declare age property with only set accessor.
C:
Declare age property with both get and set accessors.
D:
Declare age property with get, set and normal accessors.
Answer: B
No answer description is available. Let's discuss.
Which of the folowing does an indexer allow to index in the same way as an array?
  1. A class
  2. A property
  3. A struct
  4. A function
  5. An interface
A:
1, 3, 5
B:
2, 4
C:
3, 5
D:
3, 4, 5
Answer: A
No answer description is available. Let's discuss.
Which of the following is the correct way to implement a read only property Length in a Sample class?
A:
class Sample
{
    int len;
    public int Length
    {
        get
        {
            return len;
        } 
    } 
}
B:
class Sample
{
    public int Length
    {
        get
        {
            return Length;
        } 
    } 
}
C:
class Sample
{
    int len;
    public int Length
    {
        get
        {
            return len;
        } 
        set
        {
            len = value;
        } 
    } 
}
D:
class Sample
{
    int len;
    public int Length
    {
        Readonly get
        {
            return len;
        } 
    } 
}
Answer: A
No answer description is available. Let's discuss.
Suppose a Student class has an indexed property. This property is used to set or retrieve values to/from an array of 5 integers called scores[]. We want the property to report "Invalid Index" message if the user attempts to exceed the bounds of the array. Which of the following is the correct way to implement this property?
A:
class Student
{
    int[] scores = new int[5] {3, 2, 4,1, 5}; 
    public int this[ int index ]
    { 
        set
        { 
            if (index < 5)
                scores[index] = value; 
            else
                Console.WriteLine("Invalid Index");
        } 
    } 
}
B:
class Student
{
    int[] scores = new int[5] {3, 2, 4, 1, 5};
    public int this[ int index ]
    { 
        get
        { 
            if (index < 5)
                return scores[ index ]; 
            else
            { 
                Console.WriteLine("Invalid Index"); return 0; 
            } 
        } 
        set
        { 
            if (index < 5)
                scores[ index ] = value;
            else 
                Console.WriteLine("Invalid Index"); 
        } 
    } 
}
C:
class Student
{
    int[] scores = new int[5] {3, 2, 4, 1, 5}; 
    public int this[ int index ]
    { 
        get
        { 
            if (index < 5)
                return scores[ index ]; 
                else
                { 
                    Console.WriteLine("Invalid Index"); 
                    return 0; 
                } 
        } 
    } 
}
D:
class Student
{
    int[] scores = new int[5] {3, 2, 4, 1, 5}; 
    public int this[ int index ]
    { 
        get
        {
            if (index < 5)
                scores[ index ] = value; 
            else
            { 
                Console.WriteLine("Invalid Index");
            } 
        }
        set
        { 
            if (index < 5)
                return scores[ index ];
            else
            { 
                Console.WriteLine("Invalid Index");
                return 0;
            }
        }
    }
}
Answer: B
No answer description is available. Let's discuss.
An Account class has a property called accountNo and acc is a reference to a bank object and we want the C#.NET code snippet given below to work. Which of the following options will ensure this functionality?
acc.accountNo = 10; 
Console.WriteLine(acc.accountNo);
A:
Declare accountNo property with both get and set accessors.
B:
Declare accountNo property with only get accessor.
C:
Declare accountNo property with get, set and normal accessors.
D:
Declare accountNo property with only set accessor.
Answer: A
No answer description is available. Let's discuss.
Ad Slot (Above Pagination)
Quiz