250+ TOP MCQs on Fundamentals of Class and Answers

C# multiple choice questions on fundamentals of class in C# Programming Language.

1. What will be the output of the following C# code?

  1.  class sample
  2.  {
  3.      public int i;
  4.      public int[] arr = new int[10];
  5.      public void fun(int i, int val)
  6.      {
  7.          arr[i] = val;
  8.      }
  9.  }
  10.  class Program
  11.  {
  12.      static void Main(string[] args)
  13.      {
  14.          sample s = new sample();
  15.          s.i = 10;
  16.          sample.fun(1, 5);
  17.          s.fun(1, 5);
  18.          Console.ReadLine();
  19.      }
  20.  }

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
Clarification: 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
Clarification: By definition.

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

Answer: c
Clarification: 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
Clarification: 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?

  1.   class sample
  2.   {
  3.       public int i;
  4.       public int j;
  5.       public void fun(int i, int j)
  6.       {
  7.           this.i = i;
  8.           this.j = j;
  9.       }
  10.   }
  11.   class Program
  12.   {
  13.       static void Main(string[] args)
  14.       {
  15.           sample s = new sample();
  16.           s.i = 1;
  17.           s.j = 2;
  18.           s.fun(s.i, s.j);
  19.           Console.WriteLine(s.i + " " + s.j);
  20.           Console.ReadLine();
  21.       }
  22.   }

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
Clarification: Variable ‘i’ and ‘j’ declared with scope public in sample class are accessed using object of class ‘sample’ which is ‘s’.
Output:

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
Clarification: By definition.

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
Clarification: By definition.

8. What will be the output of the following C# code?

  1.   class z
  2.   {
  3.       public int X;
  4.       public int Y;
  5.       public  const int c1 = 5;
  6.       public  const int c2 = c1 * 25;
  7.       public void set(int a, int b)
  8.       {
  9.           X = a;
  10.           Y = b;
  11.       }
  12. 
    
  13.   }
  14.   class Program
  15.   {
  16.       static void Main(string[] args)
  17.       {
  18.           z s = new z();
  19.           s.set(10, 20);
  20.           Console.WriteLine(s.X + " " + s.Y);
  21.           Console.WriteLine(z.c1 + " " + z.c2);
  22.           Console.ReadLine();
  23.       }
  24.    }

a)

10 20
5  25

b)

20 10
25 5

c)

10 20
5 125

d)

20 10
125 5

View Answer

Answer: c
Clarification: Member function() ‘set’ is accessed using object of class ‘z’ values are passed as parameter to ‘a’ and ‘b’. Since, variable ‘c1’ and ‘c2’ are public data member of class ‘z’. They are accessed using classname.
Output :

 

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
Clarification: None.

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

Answer: c
Clarification: None.

11. What will be the output of the following C# code?

  1.   class z
  2.   {
  3.       public string name1;
  4.       public string address;
  5.       public void show()
  6.       {
  7.           Console.WriteLine("{0} is in city{1}", name1, " ", address);
  8.       }
  9.   }
  10.   class Program
  11.   {
  12.       static void Main(string[] args)
  13.       {
  14.           z n = new z();
  15.           n.name1 = "harsh";
  16.           n.address = "new delhi";
  17.           n.show();
  18.           Console.ReadLine();
  19.       }
  20.   }

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
Clarification: Member function show() accessed using object of class ‘z’ which is ‘n’ as object.member().
Output :

12. What does the following C# code imply?

      csharp abc;
      abc = new charp();

a) Object creation on class csharp
b) Create an object of type csharp on heap or on stack depending on the size of object
c) create a reference c on csharp and an object of type csharp on heap
d) create an object of type csharp on stack

Answer: c
Clarification: None.

13. What will be the output of the following C# code?

  1.    class test
  2.    {
  3.        public void print()
  4.        {
  5.            Console.WriteLine("Csharp:");
  6.        }
  7.    }
  8.    class Program
  9.    {
  10.        static void Main(string[] args)
  11.        {
  12.            test t;
  13.            t.print();
  14.            Console.ReadLine();
  15.        }
  16.    }

a) Code runs successfully prints nothing
b) Code runs and prints “Csharp”
c) Syntax error as t is unassigned variable which is never used
d) None of the mentioned

Answer: c
Clarification: object of class test should be declared as test t = new test();

             test t = new test();
             t.print();
             Console.ReadLine();

Leave a Reply

Your email address will not be published. Required fields are marked *