Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Enumeration Aptitude Questions and Answers | Set 4
C# Enumeration Aptitude Questions | Set 4: This section contains aptitude questions and answers on C# Enumeration.
Submitted by Nidhi, on April 02, 2020
1) What is the correct output of given code snippets in C#.NET?
using System;
class program
{
enum emp_salary : int
{
emp1 = 10000,
emp2 = 15000,
emp4 = 20000
}
static void Main(string[] args)
{
int sal = (int)emp_salary.emp2;
Console.WriteLine(sal);
}
}
- 10000
- 15000
- Syntax error
- Runtime exception
Correct answer: 2
15000
The above code will print "15000" on console screen.
2) What is the correct output of given code snippets in C#.NET?
using System;
class program
{
enum emp_salary : byte
{
emp1 = 10000,
emp2 = 15000,
emp4 = 20000
}
static void Main(string[] args)
{
int sal = (int)emp_salary.emp2;
Console.WriteLine(sal);
}
}
- 10000
- 15000
- Syntax error
- Runtime exception
Correct answer: 3
Syntax error
The above code will generate syntax error because the value of enums is outside the range of byte.
3) What is the correct output of given code snippets in C#.NET?
using System;
class program
{
enum emp_salary : float
{
emp1 = 10000,
emp2 = 15000,
emp4 = 20000
}
static void Main(string[] args)
{
int sal = (int)emp_salary.emp2;
Console.WriteLine(sal);
}
}
- 10000
- 15000
- Syntax error
- Runtime exception
Correct answer: 3
Syntax error
The above code will generate syntax error because we cannot use a float with enum like this.
4) What is the correct output of given code snippets in C#.NET?
using System;
class program
{
enum emp_salary : int
{
emp1 = 10000,
emp2 = 15000,
emp4 = 20000
}
static void Main(string[] args)
{
emp_salary sal = emp_salary.emp2;
if (sal == emp_salary.emp2)
{
Console.WriteLine("15000");
}
}
}
- 10000
- 15000
- Syntax error
- Runtime exception
Correct answer: 3
15000
The above code will print "15000" on the console screen.
5) What is the correct output of given code snippets in C#.NET?
using System;
class program
{
int A = 10000;
int B = 15000;
int C = 20000;
enum emp_salary : int
{
emp1 = A,
emp2 = B,
emp4 = C
}
static void Main(string[] args)
{
emp_salary sal = emp_salary.emp2;
if (sal == emp_salary.emp2)
{
Console.WriteLine("15000");
}
}
}
- 10000
- 15000
- Syntax error
- Runtime exception
Correct answer: 3
Syntax error
The above code will generate a syntax error.