Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
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);
}
}
- 102
- 101
- Runtime exception
- Syntax error
Correct answer: 1
102
The above code snippets will print "102" on the console screen.
2) Suppose we have a class Employee that contains a property called "empId" with getting accessor then which option will work properly?
- Employee.empId = 101;
- Employee emp = new Employee(); emp.empId = 101;
- Employee emp = new Employee(); int id = emp.empId;
- Employee emp = new Employee(); emp.empId = emp.empId + 10;
Correct answer: 3
Employee emp = new Employee(); int id = emp.empId;
The 3rd option will work properly in case of get accessor.
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:
- Only A
- A and B
- C and D
- Only D
Correct answer: 2
A and B
In case of only set assessor, Option A and B will work properly.
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);
}
}
- 102
- 101
- Runtime exception
- Syntax error
Correct answer: 4
Syntax error
The above code snippets will generate a syntax error. Because we cannot use static properties with instance members of a class.
The output would be,
An object reference is required to access non-static member `Employee.empid'
An object reference is required to access non-static member `Employee.empid'
Static member `Employee.EmpId' cannot be accessed with an instance reference, qualify it with a type name instead
Static member `Employee.EmpId' cannot be accessed with an instance reference, qualify it with a type name instead
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);
}
}
- 102
- 101
- Runtime exception
- Syntax error
Correct answer: 1
102
The above code will print "102" on the console screen.