Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Class & Object Aptitude Questions and Answers | Set 3
C# Class & Object Aptitude Questions | Set 3: This section contains aptitude questions and answers on C# Class & Object.
Submitted by Nidhi, on April 12, 2020
1) What are the correct statements about given code snippets?
public class Example
{
private int X;
public float Y;
private void method1()
{
Console.WriteLine("{0},{1}", X, Y);
}
public void method2()
{
Console.WriteLine("{0},{1}", X, Y);
}
}
- No error
- Y cannot be declared as public
- Method1() should be static
- Method1() cannot access X
Correct answer: 1
No error
The above code does not produce any error.
2) Can we create a static class in C#.NET?
- Yes
- No
Correct answer: 1
Yes
Yes, we can create a static class in C#.NET.
3) What is the correct output of given code snippets?
using System;
public static class Example
{
private void SayHi()
{
Console.WriteLine("Hiii");
}
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
- No error
- SayHi() must be static
- Cannot create static class
- SayHi() should be called in Main() method
Correct answer: 2
SayHi() must be static
In C#.NET, every static class can contain static members only.
The output would be,
`Example.SayHi()': cannot declare instance members in a static class
4) What is the correct output of given code snippets?
using System;
public static class Example
{
private int X;
private int Y;
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
- No error
- Compilation Error
- Cannot create a static class
- Runtime Exception
Correct answer: 2
Compilation Error
In C#.NET, every static class can contain static members only.
The output would be,
`Example.X': cannot declare instance members in a static class
`Example.Y': cannot declare instance members in a static class
5) Can we create a virtual class in C#.NET?
- Yes
- No
Correct answer: 2
No
No, we cannot create a virtual class in C#.NET.