Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Class & Object Aptitude Questions and Answers | Set 4
C# Class & Object Aptitude Questions | Set 4: 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
{
virtual private int X;
private int Y;
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
- Hello World
- HelloWorld
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
The above code will generate a syntax error.
The output would be,
The modifier `virtual' is not valid for this item
2) What is the correct output of given code snippets?
using System;
public class Example
{
private int X;
private int Y;
Example()
{
this.X = 1;
this.Y = 2;
}
static void Main(string[] args)
{
Example Ob = new Example();
Console.WriteLine("{0}, {1}", Ob.X, Ob.Y);
}
}
- 1, 2
- 12
- Syntax Error
- Runtime Exception
Correct answer: 1
1, 2
The above code will print (1, 2) on the console screen.
3) What is the correct output of given code snippets?
using System;
public class Example
{
private int X;
private int Y;
public Example()
{
this.X = 1;
this.Y = 2;
}
public static ShowData()
{
Console.WriteLine("{0}, {1}", this.X, this.Y);
}
static void Main(string[] args)
{
Example Ob = new Example();
Ob.Show();
}
}
- 1, 2
- 12
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
The above code will generate a syntax error.
The output would be,
Class, struct, or interface method must have a return type
4) What is the correct output of given code snippets?
using System;
public class Example
{
public Example Method1()
{
Console.Write("####, ");
return this;
}
public Example Method2()
{
Console.Write("@@@@, ");
return this;
}
public Example Method3()
{
Console.Write("$$$$, ");
return this;
}
static void Main(string[] args)
{
Example Ob = new Example();
Ob.Method1().Method2().Method3();
}
}
- ####, @@@@, $$$$,
- $$$$, @@@@, ####,
- Syntax Error
- Runtime Exception
Correct answer: 1
####, @@@@, $$$$,
The above code will print (####, @@@@, $$$$,) on console screen.
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)
{
this.Method();
}
}
- Hello World
- Hello world
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
In C#.NET we cannot use "this" reference in a static method.
The output would be,
Keyword `this' is not valid in a static property, static method, or static field initializer
Type `Example' does not contain a definition for `Method' and no extension method `Method' of type `Example' could be found. Are you missing an assembly reference?
(Location of the symbol related to previous error)