This set of C# Questions and Answers for Entrance exams on “Properties and its Applications”.
1. The correct way to implement a read only property add, in a math class is?
a)
-
class math
-
{
-
int ad;
-
public int add
-
{
-
get
-
{
-
return ad;
-
}
-
}
-
}
b)
-
class math
-
{
-
public int add
-
{
-
get
-
{
-
return ad;
-
}
-
}
-
}
c)
-
class math
-
{
-
int ad;
-
public int add
-
{
-
get
-
{
-
return ad;
-
}
-
set
-
{
-
ad = value;
-
}
-
}
-
}
d) None of the mentioned
Answer: a
Clarification: None.
2. The correct way to implement a write only property add in a math class?
a)
-
class math
-
{
-
public int add
-
{
-
set
-
{
-
add = value;
-
}
-
}
-
}
b)
-
class math
-
{
-
int ad;
-
public int add
-
{
-
set
-
{
-
ad = value;
-
}
-
}
-
}
c)
-
class math
-
{
-
int ad;
-
public int add
-
{
-
get
-
{
-
return ad;
-
}
-
set
-
{
-
ad = value;
-
}
-
}
-
}
d) None of the mentioned
Answer: b
Clarification: None.
3. Select the correct statement about properties of read and write in C#.NET?
a) A property can simultaneously be read or write only
b) A property cannot be either read only or write only
c) A write only property will only have get accessor
d) A read only property will only have get accessor
Answer: d
Clarification: None.
4. What will be the output of the following C# code?
-
class number
-
{
-
private int num1;
-
private int num2;
-
public int anumber
-
{
-
get
-
{
-
return num1;
-
}
-
set
-
{
-
num1 = value;
-
}
-
}
-
public int anumber1
-
{
-
get
-
{
-
return num2;
-
}
-
set
-
{
-
num2 = value;
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
number p = new number();
-
p.anumber = 20;
-
number k = new number();
-
k.anumber1 = 40;
-
int m = p.anumber;
-
int t = k.anumber1;
-
int r = p.anumber + k.anumber1;
-
System.Console.WriteLine("number1 = " +m);
-
System.Console.WriteLine("number2 = " +t);
-
System.Console.WriteLine("sum = " +r);
-
System.Console.ReadLine();
-
}
-
}
a) 20
b)
number1 = 30 number2 = 40 sum = 70
c)
number1 = 20 number2 = 40 sum = 60
d) Compile time error
Answer: c
Clarification: None.
Output :
number1 = 20 number2 = 40 sum = 60
5. What will be the output of the following C# code?
-
class number
-
{
-
private int num1 = 60;
-
private int num2 = 20;
-
public int anumber
-
{
-
get
-
{
-
return num1;
-
}
-
set
-
{
-
num1 = value;
-
}
-
}
-
public int anumber1
-
{
-
get
-
{
-
return num2;
-
}
-
set
-
{
-
num2 = value;
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
number p = new number();
-
number k = new number();
-
int m = p.anumber;
-
int t = k.anumber1;
-
int r = p.anumber * k.anumber1;
-
Console.WriteLine("sum = " + r);
-
Console.ReadLine();
-
}
-
}
a) 0
b) 120
c) 1200
d) Compile time error
Answer: c
Clarification: None.
Output :
6. What will be the output of the following C# code?
-
class number
-
{
-
int length = 50;
-
public int number1
-
{
-
get
-
{
-
return length;
-
}
-
set
-
{
-
length = value;
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
number p = new number();
-
p.number1 = p.number1 + 40;
-
int k = p.number1 * 3 / 9;
-
Console.WriteLine(k);
-
Console.ReadLine();
-
}
-
}
a) 0
b) 180
c) 30
d) Compile time error
Answer: c
Clarification: None.
Output :
7. What will be the output of the following C# code?
-
class number
-
{
-
int length = 60;
-
public int number1
-
{
-
get
-
{
-
return length;
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
number p = new number();
-
int l;
-
l = p.number1 + 40;
-
int k = l * 3 / 4;
-
Console.WriteLine(k);
-
Console.ReadLine();
-
}
-
}
a) 30
b) 75
c) 80
d) 0
Answer: b
Clarification: None.
Output :
8. What will be the output of the following C# code?
-
class student
-
{
-
int []scores = new int[5] {23, 42, 54, 11, 65};
-
public int this[int index]
-
{
-
get
-
{
-
if (index < 5)
-
return scores[index];
-
else
-
{
-
Console.WriteLine("invalid index");
-
return 0;
-
}
-
}
-
set
-
{
-
if (index < 5)
-
scores[index] = value;
-
else
-
Console.WriteLine("invalid index");
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
student s = new student();
-
Console.WriteLine(s[4] + 8);
-
Console.ReadLine();
-
}
-
}
a) 73
b) 37
c) 0
d) Run time error
Answer: a
Clarification: None.
Output :
9. The correct way to implement the property for which property reports the error “invalid index” if user attempts to cross bounds of the array for a student class with 5 integer arrays.
a)
-
class student
-
{
-
int []scores = new int[5] {23, 42, 54, 11, 65};
-
public int this[int index]
-
{
-
get
-
{
-
if (index < 5)
-
return scores[index];
-
else
-
{
-
Console.WriteLine("invalid index");
-
return 0;
-
}
-
}
-
set
-
{
-
if (index < 5)
-
scores[index] = value;
-
else
-
Console.WriteLine("invalid index");
-
}
-
}
-
}
b)
-
class student
-
{
-
int []scores = new int[5] {23, 42, 54, 11, 65};
-
public int this[int index]
-
{
-
get
-
{
-
if (index < 5)
-
return scores[index];
-
}
-
}
-
}
c)
-
class student
-
{
-
int []scores = new int[5]{23, 42, 54, 11, 65};
-
public int this[int index]
-
{
-
set
-
{
-
if (index < 5)
-
return scores[index];
-
else
-
{
-
Console.WriteLine("invalid index");
-
return 0;
-
}
-
}
-
}
-
}
d) None of the mentioned
Answer: a
Clarification: None.
10. What will be the output of the following C# code?
-
class student
-
{
-
int []scores = new int[3] {13, 32, 24};
-
public int this[int index]
-
{
-
get
-
{
-
if (index < 3)
-
return scores[index];
-
else
-
{
-
Console.WriteLine("invalid index");
-
return 0;
-
}
-
}
-
private set
-
{
-
if (index < 3)
-
scores[index] = value;
-
else
-
Console.WriteLine("invalid index");
-
}
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
student s = new student();
-
int[] scores1 = new int[3] {8, 19, 40};
-
for (int i = 0; i < 3; i++)
-
{
-
if (scores1[i] > s[i])
-
{
-
Console.WriteLine(" scores1 had greater value :" + scores1[i]);
-
}
-
else
-
{
-
Console.WriteLine("scores had greater value :" + s[i]);
-
}
-
}
-
Console.ReadLine();
-
}
-
}
a) 0
b) Compile time error
c) Run time error
d)
scores had greater value : 13 scores had greater value : 32 scores1 had greater value : 40
View Answer
Answer: d
Clarification: None.
Output :
scores had greater value : 13 scores had greater value : 32 scores1 had greater value : 40
C# for Entrance exams,