Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Properties Aptitude Questions and Answers | Set 5
C# Properties Aptitude Questions | Set 5: This section contains aptitude questions and answers on C# Properties.
Submitted by Nidhi, on April 10, 2020
1) Can we use the virtual keyword to define a property in C#.NET?
- Yes
- No
Correct answer: 1
Yes
Yes, we can use the virtual keyword to define a property.
2) What is the correct output of given code snippets?
using System;
class Employee
{
private static int empid;
public Employee()
{
empid = 101;
}
public volatile 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: 4
Syntax error
The above code will generate an error, we cannot use a volatile keyword like this.
The output would be,
The modifier `volatile' is not valid for this item
3) What is the correct output of given code snippets?
using System;
class Employee
{
private static int empid;
public Employee()
{
empid = 101;
}
public override 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: 4
Syntax error
The above code will generate an error, we cannot use override keyword like this.
The output would be,
A static member `Employee.EmpId' cannot be marked as override, virtual or abstract
`Employee.EmpId' is marked as an override but no suitable property found to override
4) Can we override a property in C#.NET?
- Yes
- No
Correct answer: 1
Yes
Yes, we can override a property in C#.NET.
5) What is the correct output of given code snippets?
using System;
abstract class Person
{
abstract public int EmpId { get; }
}
class Employee:Person
{
private int empid;
public Employee()
{
empid = 101;
}
public override int EmpId
{
get
{
return empid;
}
}
}
class program
{
static void Main(string[] args)
{
Employee Emp = new Employee();
Console.WriteLine(Emp.EmpId);
}
}
- 102
- 101
- Runtime exception
- Syntax error
Correct answer: 2
101
The above code will print "101" on the console screen.