C# Properties Aptitude Questions and Answers | Set 4

C# Properties Aptitude Questions | Set 4: 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;
        }
        set
        {
            empid = value;
        }
    }
}

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) Suppose we have a class Employee that contains a property called "empId" with getting accessor then which option will work properly?
  1. Employee.empId = 101;
  2. Employee emp = new Employee(); emp.empId = 101;
  3. Employee emp = new Employee(); int id = emp.empId;
  4. Employee emp = new Employee(); emp.empId = emp.empId + 10;

3) Suppose we have a class Employee that contains a property called "empId" with set accessor then which option will work properly?
    A.	Employee.empId = 101;
    B.	Employee emp = new Employee();  emp.empId = 101;
    C.	Employee emp = new Employee();  int id = emp.empId; 
    D.	Employee emp = new Employee();  emp.empId = emp.empId + 10; 

Options:

  1. Only A
  2. A and B
  3. C and D
  4. Only D

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

class Employee
{
    private int empid;

    public Employee()
    {
        empid = 101;
    }
    public static 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);
    }
}
  1. 102
  2. 101
  3. Runtime exception
  4. Syntax error

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

class Employee
{
    private static int empid;

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

class program
{
    static void Main(string[] args)
    {
        Employee.EmpId = 102;
        Console.WriteLine(Employee.EmpId);
    }
}
  1. 102
  2. 101
  3. Runtime exception
  4. Syntax error





Comments and Discussions!

Load comments ↻





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