Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Structures Aptitude Questions and Answers | Set 4
C# Structures Aptitude Questions | Set 4: This section contains aptitude questions and answers on C# Structures.
Submitted by Nidhi, on April 10, 2020
1) What is the output of given code snippets?
using System;
struct sample
{
int a;
int b;
public sample()
{
a = 10;
b = 20;
}
public void disp()
{
Console.WriteLine(a + " " + b);
}
}
class Employee
{
static void Main(string[] args)
{
sample S = new sample();
S.disp();
}
}
- 10 20
- 10
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
In C#.Net, we cannot use an explicit parameterless constructor in C#.Net.
The output would be,
Structs cannot contain explicit parameterless constructors
2) What is the output of given code snippets?
using System;
struct sample
{
public int a;
public int b;
}
class Employee
{
static void Main(string[] args)
{
sample S = new sample();
S.a = 10;
S.b = 20;
Console.WriteLine(S.a + " " + S.b);
}
}
- 10 20
- 10
- Syntax Error
- Runtime Exception
Correct answer: 1
10 20
In C#.Net, the above code will print "10 20" on the console screen.
3) What is the output of given code snippets?
using System;
struct sample
{
public int a;
public int b;
}
class Employee
{
static void Main(string[] args)
{
const sample S;
S.a = 10;
S.b = 20;
Console.WriteLine(S.a + " " + S.b);
}
}
- 10 20
- 10
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
In C#.Net, the above code will generate a syntax error, we cannot create a const keyword like this.
The output would be,
A const field requires a value to be provided
4) What is the output of given code snippets?
using System;
struct sample
{
public int a;
public int b;
}
class Employee
{
static void Main(string[] args)
{
sample []S;
S = new sample[2];
S[0].a = 10;
S[0].b = 20;
S[1].a = 30;
S[1].b = 40;
Console.WriteLine(S[0].a + " " + S[0].b + " " + S[1].a+ " " + S[1].b);
}
}
- 10 20 30 40
- 10 20
- Syntax Error
- Runtime Exception
Correct answer: 1
10 20 30 40
In C#.Net, the above code will print "10 20 30 40" on the console screen.
5) What is the output of given code snippets?
using System;
struct sample
{
int a;
int b;
public sample(int X, int Y)
{
a = X;
b = Y;
}
public void disp()
{
Console.WriteLine(a + " " + b);
}
}
class Employee
{
static void Main(string[] args)
{
sample []S = new sample[2]{{10, 20},{30,40}};
S[0].disp();
}
}
- 10 20 30 40
- 10 20
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
In C#.Net, the above code will generate syntax error, this is not a proper way to call constructors in case of an array of structure.
The output would be,
Array initializers can only be used in a variable or field initializer. Try using a new expression instead