C# Questions & Answers – Fundamentals of Class

This section of our 1000+ C# multiple choice questions focuses 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
View Answer

Answer: a
Explanation: An Object reference is required for non static field, method or property. i.e

advertisement
advertisement
                      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
View Answer

Answer: b
Explanation: By definition.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

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
View Answer

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 
       }
}
advertisement

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

advertisement
  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
View Answer

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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: d
Explanation: 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
Explanation: 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 :

           10 20
           5  125
 
 

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();
View Answer

Answer: a
Explanation: None.

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

Answer: c
Explanation: 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
View Answer

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

harsh is in new delhi

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
View Answer

Answer: c
Explanation: 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
View Answer

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

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

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.