C# Questions & Answers on Fundamentals of Class for Freshers

1. Select output for the following set of 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

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 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. Select the output for the following set of 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

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 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: 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 the access to that particular code and data.”

a) Abstraction
b) Polymorphism
c) Inheritance
d) Encapsulation
Answer: d

Explanation: By definition.
8. Select the output for the following set of 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
Answer: c

Explanation: Member fucntion() ‘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();
Answer: a

10. The data members of a class by default are ?

a) protected, public
b) private, public
c) private
d) public
Answer: c

11. Select the output for the following set of 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

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 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
13. The output of code is ?
  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

Explanation: object of class test should be declared as test t = new test();
test t = new test();
t.print();
Console.ReadLine();

Related

Java Multiple Choice Questions & Answers on Implementing Runnable interface for Threads for Freshers

1. Which of these method is used to implement Runnable interface? a) stop()b) run()c) runThread()d) stopThread() Answer: b Explanation: To implement Runnable interface, a class needs only to impl...

C Programming Multiple Choice Questions and Answers on Bit-fields for Freshers

1. What is the correct syntax to initialize bit-fields in an structure? a) struct temp { unsigned int a : 1; }s; b) struct temp { unsigned int a = 1; }s; c) st...

CSS Multiple Choice Questions & Answers on CSS3 Transitions for UI Elements for Freshers

1. Which of the following selector is used to select and style when you place mouse over it? a) focusb) hoverc) moused) all of the mentioned Answer: b 2. Which of the following method increases o...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item