Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
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);
}
}
- 102
- 101
- Runtime exception
- Syntax error
Correct answer: 4
Syntax error
The above code snippets will generate error, because it is read only property.
The output would be,
Property or indexer `Employee.EmpId' cannot be assigned to (it is read-only)
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);
}
}
- null
- 0
- Runtime exception
- Syntax error
Correct answer: 2
0
The code snippet will print "0" on the console screen.
3) Which keyword is used in set accessor to set the value into data members?
- value
- val
- assign
- new
Correct answer: 1
value
To set value in data members, we need to use value keyword in the set accessor.
4) Which keyword is used to get accessor to get value from data members?
- value
- val
- assign
- no need to use any keyword
Correct answer: 4
no need to use any keyword
In get accessor, there is 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:
- 102
- 101
- Runtime exception
- Syntax error
Correct answer: 4
Syntax error
The above code snippets will generate error because we cannot access private property outside the class.