C# is undoubtedly one of the most interesting programming languages. And weโve already covered some of its features in our earlier posts. Today, we are presenting a C# programming test with 15 questions and answers on classes.
There are a few compelling facts that you should know about C#. First of all, the obvious one, itโs an object-oriented language and is strongly typed.
With this strict type checking feature, the bulkย ofย programming errorsย includingย runtime as well as compile time gets uncovered at a very early stage.
It saves programmers from getting into any unforeseen issue later in the project life-cycle.
Test C# Programming Skills
Before you get into aย war with theย C# programming test, buy a little time to know about the three most important data types in C#. These are none other than the trio โ Object, Var, and Dynamic types. Letโs check out what draws a distinction among them?
Object
It took birth with C# 1.0. It creates a generic entity that can hold any type. A method can accept it as an argument and also return the object type. Without casting to an actual type, you wonโt be able to use it. Itโs primarily useful when the data type of target entity is not known.
Var
Itโs relatively new and gets introduced with C# 3.0. Though, it too can store any value but requires initialization during declaration. It is type safe and doesnโt make the compiler to flag an error at runtime. Neither it supports to pass as a function argument nor can a function return it. The compiler relaxes it to work without being cast. You can utilize it to handle any of the anonymous types.
Dynamic
Itโs the newest of the C# construct which got its shape with C# 4.0. It stores values in the same manner as the legacy Var does. Itโs not type safe. And the compiler wonโt know anything about the type. There is no need to cast. But you must aware of the properties and methods the stored type supports. Otherwise, you may get into runtime issues. It is fit for situations where there are requirements to use concepts like Reflection or Dynamic languages or COM methods.
So now, you know the essentials of three object-oriented data types of C#. Please go ahead to the interview questions/answers section.
C# programming test with 15 questions and answers on classes

Q-1. Which of the following can be used to define the member of a class externally?
a) :
b) ::
c) #
d) none of the mentioned
[toggle title=โView answer.โ]Answer. b)[/toggle]
ย
Q-2. Which of the following operator can be used to access the member function of a class?
a) :
b) ::
c) .
d) #
[toggle title=โView answer.โ]Answer. c)[/toggle]
ย
Q-3. What will be the output of the following code snippet?
using System;
class emp
{
public string name;
public string address;
public void display()
{
Console.WriteLine("{0} is in city {1}", name, address);
}
}
class Program
{
static void Main(string[] args)
{
emp obj = new emp();
obj.name = "Akshay";
obj.address = "new delhi";
obj.display();
Console.ReadLine();
}
}
a) Syntax error
b) {0} is in city {1} Akshay new delhi
c) Akshay is in new delhi
d) Akshay is in city new delhi
e) executes successfully and prints nothing
[toggle title=โView answer.โ]Answer. d)[/toggle]
ย
Q-4. Which of the following statements are correct for the given code snippet:
shape obj;
obj = new shape();
a) creates an object of class shape.
b) To create an object of type shape on the heap or stack depending on its size.
c) create a reference obj of the class shape and an object of type shape on the heap.
d) create an object of type shape on the stack.
[toggle title=โView answer.โ]Answer. c)[/toggle]
ย
Programmers Should Also Follow the Below Post.
[alert-note]โ Check out 15 C# Questions โ For, While Loops and If Else Statements.[/alert-note]
ย
Q-5. Which of the following gives the correct count of the constructors that a class can define?
a) 1
b) 2
c) Any number
d) None of the above
[toggle title=โView answer.โ]Answer. c)[/toggle]
ย
Q-6. What will be the output of the following code snippet?
using System;
class sample
{
public static void first()
{
Console.WriteLine("first method");
}
public void second()
{
first();
Console.WriteLine("second method");
}
public void second(int i)
{
Console.WriteLine(i);
second();
}
}
class program
{
public static void Main()
{
sample obj = new sample();
sample.first();
obj.second(10);
}
}
a) second method
ย ย ย 10
ย ย ย second method
ย ย ย first method
b) first method
ย ย ย 10
ย ย ย first method
ย ย ย second method
c) first method
ย ย ย 10
d) second method
ย ย ย 10
ย ย ย first method.
[toggle title=โView answer.โ]Answer. b)[/toggle]
ย
Q-7. What will be the output of the following code snippet?
using System;
class program
{
static void Main(string[] args)
{
int num = 2;
fun1 (ref num);
Console.WriteLine(num);
Console.ReadLine();
}
static void fun1(ref int num)
{
num = num * num * num;
}
}
a) 8
b) 0
c) 2
d) 16
[toggle title=โView answer.โ]Answer. a)[/toggle]
ย
Q-8. What will be the output of the following code snippet?
using System;
class program
{
static void Main(string[] args)
{
int[] arr = new int[] {1 ,2 ,3 ,4 ,5 };
fun1(ref arr);
Console.ReadLine();
}
static void fun1(ref int[] array)
{
for (int i = 0; i < array.Length; i=i+2)
{
array[i] = array[i] + 10;
}
Console.WriteLine(string.Join(",", array));
}
}
a) 1 2 3 4 5
b) 11 12 13 14 15
c) 11 2 13 4 15
d) 1ย 3 5 7 9
[toggle title=โView answer.โ]Answer. c)[/toggle]
ย
Q-9. Which of the following statements correctly tell the differences between โ=โ and โ==โ in C#?
a) โ==โ operator is used to assign values from one variable to another variable
โ=โ operator is used to compare value between two variables
b) โ=โ operator is used to assign values from one variable to another variable
โ==โ operator is used to compare value between two variables
c) No difference between both operators
d) None of the mentioned
[toggle title=โView answer.โ]Answer. b)[/toggle]
ย
Q-10. What will be the output of the following code snippet?
using System;
class program
{
static void Main(string[] args)
{
int x = 4 ,b = 2;
x -= b/= x * b;
Console.WriteLine(x + " " + b);
Console.ReadLine();
}
}
a) 4 2
b) 0 4
c) 4 0
d) None of the mentioned
[toggle title=โView answer.โ]Answer. c)[/toggle]
ย
Q-11. What will be the output of the following code snippet?
using System;
class program
{
static void Main(string[] args)
{
int x = 8;
int b = 16;
int c = 64;
x /= c /= b;
Console.WriteLine(x + " " + b+ " " +c);
Console.ReadLine();
}
}
a) 2 16 4
b) 4 8 16
c) 2 4 8
d) 8 16 64
[toggle title=โView answer.โ]Answer. a)[/toggle]
ย
Q-12. What is the correct name of a method which has the same name as that of class and used to destroy objects?
Note โ This method gets called automatically when the application is at the final stage waiting for the process to terminate.
a) Constructor
b) Finalize()
c) Destructor
d) End
[toggle title=โView answer.โ]Answer. c)[/toggle]
ย
Q-13. What will be the output of the following code snippet?
using System;
class sample
{
int i;
double k;
public sample (int ii, double kk)
{
i = ii;
k = kk;
double j = (i) + (k);
Console.WriteLine(j);
}
~sample()
{
double j = i - k;
Console.WriteLine(j);
}
}
class Program
{
static void Main(string[] args)
{
sample s = new sample(9, 2.5);
}
}
a) 0 0
b) 11.5 0
c) Compile time error
d) 11.5 6.5
[toggle title=โView answer.โ]Answer. d)[/toggle]
ย
Donโt Miss to Check out this Awesome Post.
[alert-note]โ ย 15 C# Interview Questions that Every Beginner Should Read Once.[/alert-note]
ย
Q-14. What will be the output of the following code snippet?
using System;
class sum
{
public int value;
int num1;
int num2;
int num3;
public sum ( int a, int b, int c)
{
this.num1 = a;
this.num2 = b;
this.num3 = c;
}
~ sum()
{
value = this.num1 * this.num2 * this.num3;
Console.WriteLine(value);
}
}
class Program
{
public static void Main(string[] args)
{
sum s = new sum(4, 5, 9);
}
}
a) 0
b) 180
c) Compile time error
d) 18
[toggle title=โView answer.โ]Answer. b)[/toggle]
ย
Q-15. What will be the output of the following code snippet?
using System;
class Program
{
static void Main(string[] args)
{
int num = 5;
int square = 0, cube = 0;
Mul (num, ref square, ref cube);
Console.WriteLine(square + " & " +cube);
Console.ReadLine();
}
static void Mul (int num, ref int square, ref int cube)
{
square = num * num;
cube = num * num * num;
}
}
a) 125 & 25
b) 25 & 125
c) Compile time error
d) 10 & 15
[toggle title=โView answer.โ]Answer. b)[/toggle]
Summary โ C# programming test with 15 questions on classes
We wish youโd enjoyed running through the C# programming test. And it might get you a quick brush up of your object-oriented skills.
It would be a pleasure for us to have your feedback on the questions.
Your valuable response, a question or comments about this post are always welcome. Also, if you liked the above post, then donโt mind following us onย our social media (facebook/twitter) accounts.
Check out more CSharp Q&A on various programming topics:
1. 50 CSharp Coding Interview Questions
2. 15 CSharp Programming Interview Questions
3. 15 CSharp Interview Questions Every Programmer
4. 15 CSharp Questions on For, While and If Else
5. 15 CSharp Programming Questions on Classes
6. 20 CSharp OOPS Interview Questions Set-1
7. 20 CSharp OOPS Interview Questions Set-2
8. 25 CSharp OOPs Interview Questions Set-3
9. 35 CSharp Exception Handling Questions
Best,
TechBeamers
Hi โ This question and its options are fixed now. Thanks for reporting to us.
Q-3.
e) Akshay is in city