魔法使いとC#のてんぷら

  • 3
    Like
  • 0
    Comment
More than 1 year has passed since last update.

魔法使いと黒猫のウィズをリリース直後からやっているてんぷらです。
C#クイズスタート☆
※答えは A. の後ろをハイライト表示

Q.1

enum Status : int
{
    Fire    = 1,
    Water   = 2,
    Thunder = 3
}

void Main()
{
    string value = Status.Water.ToString();
}

value の値は?

A.Water

Q.2

using System.Linq;

class Program
{
    void Main()
    {
        object data = "3,5,7,1,9";
        var value = ((string)data).Split(',').Select((no) => int.Parse(no)).ToArray();
    }
}

value の型は?

A.int[]

Q.3

class Program
{
    public static const int X = 1; // (1)
    public readonly int Y = 2;     // (2)

    public Program()
    {
        Y = 3; // (3)
    }
}

エラーになるのは (1)~(3) のうちどれ?

A.(1)

Q.4

public void Main()
{
    var num0  = new Number(1);
    var num1  = new Number(1);
    int value = (num0 + num0).Num;
}

struct Number
{
    public int Num { get; private set; }

    public Number(int num)
    {
        Num = num;
    }

    public static Number operator +(Number num0, Number num1)
    {
        return new Number((num0.Num + num1.Num) * 2);
    }
}

value の値は?

A.4

Q.5

class Program
{
    private Monster monster = new Monster();

    public void Main()
    {
        monster.Id = 3;
    }
}

class Monster
{
    private int id = 0;
    private string name = "";

    public int Id
    {
        get { return id; }
        set 
        {
            id = value;
            Name = "Monster_" + id;
        }
    }

    public string Name
    {
        get { return name; }
        set 
        {
            name = value;
            id = 1;
        }
    }
}

Main() メソッドが呼ばれた後、id の値は?

A.1

Q.6

void Main() 
{
    var str = "1";        // (1)
    var num = (int)str;   // (2)
    str = num.ToString(); // (3)
}

エラーになるのは (1)~(3) のうちどれ?

A.(2)

Q.7

class BaseProgram
{
    protected int value = 0;

    protected virtual void Do()
    {
        value++;
    }
}

class Program : BaseProgram
{
    public void Main()
    {
        Do();
    }

    protected override void Do()
    {
        value++;
    }
}

Main() メソッドが呼ばれた後、value の値は?

A.1

Q.8

public void Main()
{
    Func<int[], int> main = null;
    main = (args) =>
    {
        Func<int, int, int, int> parupunte = null;
        parupunte = (idx, arg, res) =>
        {
            res += (idx != 1) ? arg : (arg + arg);
            return res;
        };

        int result = 0;
        for (int i = 0; i < args.Length; i++)
        {
            result = parupunte(i, args[i], result);
        }
        return result;
    };

    int value = main(new int[] { 1, 2, 3 });
}

value の値は?

A.8

Q.9

class Program
{
    public void Main()
    {
        int value = 
            new Data(1)
            .Add(3)
            .Mul(2)
            .Sub(1)
            .Div(3)
            .Mul(5)
            .Sub(22)
            .Add(14)
            .Mul(8)
            .Sub(17)
            .Mul(2)
            .Div(4)
            .Answer();
    }
}

class Data
{
    private int num = 0;

    public Data(int num)
    {
        this.num = num;
    }

    public Data Add(int add)
    {
        num += add;
        return this;
    }

    public Data Sub(int sub)
    {
        num -= sub;
        return this;
    }

    public Data Mul(int mul)
    {
        num *= mul;
        return this;
    }

    public Data Div(int div)
    {
        num /= div;
        return this;
    }

    public int Answer()
    {
        num *= 0;
        return num;
    }
}

value の値は?

A.0

Q.10

using System.Linq;

class Program
{
    public void Main()
    {
        int[] data = { 1, 2, 3, 4, 5 };
        var value = data.Where((a) => (a > 1 && ((a % 2) == 1))).OrderByDescending((a) => a).Select((a) => a.ToString()).Aggregate((a, b) => a + "_" + b);
    }
}

value の値は?

A.5_3

Author

http://okamura0510.jp