C# Programming Questions and Answers Part-11

1. What will be the output of the following C# code?
class sample
{
public int i;
public int[] arr = new int[10];
public void fun(int i, int val)
{
arr[i] = val;
}
}
class Program
{
static void Main(string[] args)
{
sample s = new sample();
s.i = 10;
sample.fun(1, 5);
s.fun(1, 5);
Console.ReadLine();
}
}
a) sample.fun(1, 5) will not work correctly
b) s.i = 10 cannot work as i is ‘public’
c) sample.fun(1, 5) will set value as 5 in arr[1]
d) s.fun(1, 5) will work correctly

Answer: a
Explanation: An Object reference is required for non static field, method or property. i.e
sample s = new sample();
s.i = 10;
sample.fun(1, 5);
sample.fun(1, 5);
Console.ReadLine();

2. Which of the following is used to define the member of a class externally?
a) :
b) ::
c) #
d) none of the mentioned

Answer: b
Explanation: By definition.

3. The operator used to access member function of a class?
a) :
b) ::
c) .
d) #

Answer: c
Explanation: objectname.function name(actual arguments);

4. What is the most specified using class declaration?
a) type
b) scope
c) type & scope
d) none of the mentioned

Answer: c
Explanation: General form of class declaration in C# is :
class class_name
{
member variables
variable1;
variable2;
variableN;
method1(parameter_list)
{
method body
}
method2(parameter_list)
{
method body
}
methodN(parameter_list)
{
method body
}
}

5. What will be the output of the following C# code?
class sample
{
public int i;
public int j;
public void fun(int i, int j)
{
this.i = i;
this.j = j;
}
}
class Program
{
static void Main(string[] args)
{
sample s = new sample();
s.i = 1;
s.j = 2;
s.fun(s.i, s.j);
Console.WriteLine(s.i + " " + s.j);
Console.ReadLine();
}
}
a) Error while calling s.fun() due to inaccessible level
b) Error as ‘this’ reference would not be able to call ‘i’ and ‘j’
c) 1 2
d) Runs successfully but prints nothing

Answer: c
Explanation: Variable ‘i’ and ‘j’ declared with scope public in sample class are accessed using object of class ‘sample’ which is ‘s’.
Output:
1 2

6. Which of the following statements about objects in “C#” is correct?
a) Everything you use in C# is an object, including Windows Forms and controls
b) Objects have methods and events that allow them to perform actions
c) All objects created from a class will occupy equal number of bytes in memory
d) All of the mentioned

Answer: d
Explanation: All of the mentioned

7. “A mechanism that binds together code and data in manipulates, and keeps both safe from outside interference and misuse. In short it isolates a particular code and data from all other codes and data. A well-defined interface controls access to that particular code and data.”
a) Abstraction
b) Polymorphism
c) Inheritance
d) Encapsulation

Answer: d
Explanation: Encapsulation

8. The data members of a class by default are?
a) protected, public
b) private, public
c) private
d) public

Answer: c
Explanation: private

9. Correct way of declaration of object of the following class is?
class name
a) name n = new name();
b) n = name();
c) name n = name();
d) n = new name();

Answer: a
Explanation: name n = new name();

10. What will be the output of the following C# code?
class z
{
public string name1;
public string address;
public void show()
{
Console.WriteLine("{0} is in city{1}", name1, " ", address);
}
}
class Program
{
static void Main(string[] args)
{
z n = new z();
n.name1 = "harsh";
n.address = "new delhi";
n.show();
Console.ReadLine();
}
}
a) Syntax error
b) {0} is in city{1} harsh new delhi
c) harsh is in new delhi
d) Run successfully prints nothing

Answer: c
Explanation: Member function show() accessed using object of class ‘z’ which is ‘n’ as object.member().
Output :
harsh is in new delhi