Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Class & Object Aptitude Questions and Answers | Set 5
C# Class & Object Aptitude Questions | Set 5: 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?
using System;
public class Example
{
public virtual static void SayHello()
{
Console.Write("Hello World");
}
static void Main(string[] args)
{
SayHello();
}
}
- Hello World
- HelloWorld
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
In C#.NET we static member cannot be marked as a virtual.
The output would be,
static member `Example.SayHello()' cannot be marked as override, virtual or abstract
2) What is the correct output of given code snippets?
using System;
public class Example
{
public virtual void SayHello()
{
Console.Write("Hello World");
}
static void Main(string[] args)
{
Example Ob = new Example();
Ob.SayHello();
}
}
- Hello World
- HelloWorld
- Syntax Error
- Runtime Exception
Correct answer: 1
Hello World
The above code will print "Hello World" on the console screen.
3) We can use the override keyword to declare a method in a class?
- Yes
- No
Correct answer: 1
Yes
Yes, we can use override keyword to declare a method in a class.
4) What is the correct output of given code snippets?
using System;
public class Example
{
public override void SayHello()
{
Console.Write("Hello World");
}
static void Main(string[] args)
{
Example Ob = new Example();
Ob.SayHello();
}
}
- Hello World
- HelloWorld
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
The above code will generate a syntax error. Because we can override only inherited methods.
The output would be,
`Example.SayHello()' is marked as an override but no suitable method found to override
5) What is the correct output of given code snippets?
using System;
public class Example
{
public void SayHello()
{
Console.Write("Hello World");
}
static void Main(string[] args)
{
static Example Ob = new Example();
Ob.SayHello();
}
}
- Hello World
- Hello world
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
The above code will generate a syntax error. Because we cannot use a static keyword like this.
The output would be,
Unexpected symbol `static'