Search Placementyogi.com
 

C# Enumerations - Advanced .NET Objective Questions

1

What is the output of the following C# program?

using System;
public class Yogi
{
    enum week
    {
        Monday,
        Tuesday = 2,
        Wednesday,
        Thursday = 5,
        Firday,
        Saturday,
        Sunday
    }
    public static void Main(String[] args)
    {
        Console.Write((int)week.Monday + " ");
        Console.Write((int)week.Saturday);
    }
}
A.

1 6

B.

1 7

C.

0 6

D.

0 7

E.

None of these

See Answer & Explanation Lets Discuss
Correct answer is : D
Explanation

If first element of an enum is not assigned then it is cosidered as 0. In C# if an enum element is not assigned a value then it is considered one more than its previous element's value.

Here we will have:

Monday = 0
Wednesday = 3
Friday = 6
Saturday = 7
Sunday = 8
 
2

What is the output of the following C# program?

using System;
public class Yogi
{
    enum week
    {
        Monday,
        Tuesday = 2,
        Wednesday,
        Thursday,
        Firday,
        Saturday,
        Sunday
    }
    public static void Main(String[] args)
    {
        week.Monday = 1;
        Console.Write((int)week.Wednesday);
    }
}
A.

2

B.

3

C.

Compilation Error

D.

None of these

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

Elements of an enum cannot be assigned values outside of enum block.

The error thrown will be as follows:

The left-hand side of an assignment must be a variable, property or indexer

 
3

What is the output of the following C# program?

using System;
public class Yogi
{
    enum fruit
    {
        apple,
        orange,
        mango
    }
    public static void Main(String[] args)
    {
        fruit  c = fruit.mango;
        Type t = c.GetType();
        string[] s;
        s = Enum.GetNames(t);
        Console.WriteLine(s[0]);
    }
}
A.

apple

B.

orange

C.

mango

D.

None of these

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

Enume.GetNames(Type enumType) gets names of all the elements in the enum.

Console.WriteLine(s[1]) prints orange

Console.WriteLine(s[2]) prints mango
 
 

Banner to LogicGuns.com

Hide Page Information

C# Enumerations - Advanced Objective Questions and Answers with Explanation

Here you can find advanced level C# enumerations interview questions with answers and explanation.

Why Enumerations in C#?

In this section you can learn and practice different types of advanced level questions with answers on enumerations to improve your skills in order to face the interviews, competitive examinations and various entrance tests with full confidence.

Where can I get C# Enumerations Advanced Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved advanced level enumerations questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download quiz questions with answers as PDF files and eBooks.

Where can I get Advanced Level C# Enumerations Interview Questions and Answers (objective type, multiple choice)?

Here you can find objective type advanced enumerations questions and answers for interview and entrance examination. Multiple choice and TRUE or FALSE type and YES or NO type questions are also provided.