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);
    }
}
  1. 10 20
  2. 10
  3. Syntax Error
  4. Runtime Exception

2) In C#.NET, can we inherit a structure?
  1. Yes
  2. No

3) What is the correct way to define a variable of a given structure?
struct Sample
{
	int X;
	int Y;
}
  1. Sample S(); S = new Sample();
  2. Sample S = new Sample;
  3. Sample S = new Sample();
  4. Sample S; S = new Sample;

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();
    }
}
  1. 10 20
  2. 10
  3. Syntax Error
  4. Runtime Exception

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();
    }
}
  1. 10 20
  2. 10
  3. Syntax Error
  4. Runtime Exception





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.