Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Structures Aptitude Questions and Answers | Set 3
C# Structures Aptitude Questions | Set 3: 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;
}
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: 3
Syntax Error
The given code snippet will generate a syntax error because members of structures are private by default.
2) In C#.NET, can we inherit a structure?
- Yes
- No
Correct answer: 2
No
No, In C#.Net, inheritance is not possible in structures.
3) What is the correct way to define a variable of a given structure?
struct Sample
{
int X;
int Y;
}
- Sample S(); S = new Sample();
- Sample S = new Sample;
- Sample S = new Sample();
- Sample S; S = new Sample;
Correct answer: 3
Sample S = new Sample();
The 3rd option is correct way to define a variable of structure.
4) 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(10,20);
S.disp();
}
}
- 10 20
- 10
- Syntax Error
- Runtime Exception
Correct answer: 1
10 20
The code will print "10 20" 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)
{
a = X;
}
public void disp()
{
Console.WriteLine(a + " " + b);
}
}
class Employee
{
static void Main(string[] args)
{
sample S = new sample(10);
S.disp();
}
}
- 10 20
- 10
- Syntax Error
- Runtime Exception
Correct answer: 3
Syntax Error
In C#.Net, it is mandatory to initialize all members of structure in constructor.