C# Properties Aptitude Questions and Answers | Set 3

C# Properties Aptitude Questions | Set 3: This section contains aptitude questions and answers on C# Properties.
Submitted by Nidhi, on April 10, 2020

1) What is the correct output of given code snippets?
using System;

class Employee
{
    private int empid;

    public Employee()
    {
        empid = 101;
    }
    public int EmpId
    {
        get
        {
            return empid;
        }
    }
}

class program
{
    static void Main(string[] args)
    {
        Employee emp = new Employee();

        emp.EmpId = 102;
        Console.WriteLine(emp.EmpId);
    }
}
  1. 102
  2. 101
  3. Runtime exception
  4. Syntax error

2) What is the correct output of given code snippets?
using System;

class Employee
{
    private int empid;

    public int EmpId
    {
        get
        {
            return empid;
        }
    }
}

class program
{
    static void Main(string[] args)
    {
        Employee emp = new Employee();

        Console.WriteLine(emp.EmpId);
    }
}
  1. null
  2. 0
  3. Runtime exception
  4. Syntax error

3) Which keyword is used in set accessor to set the value into data members?
  1. value
  2. val
  3. assign
  4. new

4) Which keyword is used to get accessor to get value from data members?
  1. value
  2. val
  3. assign
  4. no need to use any keyword

5) What is the correct output of given code snippets?
using System;

class Employee
{
    private int empid;

    public Employee()
    {
        empid = 101;
    }
    private int EmpId
    {
        get
        {
            return empid;
        }
        set
        {
            empid = value;
        }
    }
}

class program
{
    static void Main(string[] args)
    {
        Employee emp = new Employee();

        emp.EmpId = 102;
        Console.WriteLine(emp.EmpId);
    }
}

Options:

  1. 102
  2. 101
  3. Runtime exception
  4. Syntax error





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.