Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Structures Aptitude Questions and Answers | Set 5
C# Structures Aptitude Questions | Set 5: 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(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];
S[0] = new sample(10,20);
S[1] = new sample(30,40);
S[1].disp();
}
}
- 10 20 30 40
- 10 20
- Syntax Error
- 30 40
Correct answer: 4
30 40
The above source code will print "30 40" on the console screen.
2) What is the output of given code snippets?
using System;
struct sample
{
const int a=5;
int b;
public sample(int Y)
{
b = Y;
}
public void disp()
{
Console.WriteLine(a + " " + b);
}
}
class Employee
{
static void Main(string[] args)
{
sample S = new sample(10);
S.disp();
}
}
- 5 10
- 5 1o
- Syntax Error
- Runtime Exception
Correct answer: 1
5 10
The above source code will print "5 10" 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)
{
sample S = { 30, 40 };
Console.WriteLine(S.a + " " + S.b);
}
}
- 30 40
- 30
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
The above code will generate a syntax error.
The output would be,
Can only use array initializer expressions to assign to array types. Try using a new expression instead
4) What is the output of given code snippets?
using System;
struct sample
{
public int a=10;
public int b=20;
}
class Employee
{
static void Main(string[] args)
{
sample S;
Console.WriteLine(S.a + " " + S.b);
}
}
- 10 20
- 1o 2o
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
The above source code will generate a syntax error.
The output would be,
'sample': Structs cannot have instance property or field initializers
'sample': Structs cannot have instance property or field initializers
5) Can we create a structure within a structure in C#.NET?
- Yes
- No
Correct answer: 1
Yes
Yes, we can create structure within a structure.