This section of our 1000+ C# MCQs focuses on LINQ in C# Programming Language.
1. Assume 2 columns named as Product and Category how can be both sorted out based on first by category and then by product name?
a) var sortedProds = _db.Products.Orderby(c => c.Category)
b) var sortedProds = _db.Products.Orderby(c => c.Category) + ThenBy(n => n.Name)
c) var sortedProds = _db.Products.Orderby(c => c.Category) . ThenBy(n => n.Name)
d) all of the mentioned
View Answer
Explanation: var sortedProds = _db.Products.Orderby(c => c.Category) . ThenBy(n => n.Name).
2. Choose the wrong statement about the LINQ?
a) The main concept behind the linq is query
b) linq makes use of foreach loop to execute the query
c) It is not required that linq should make use of IEnumerable interface
d) None of the mentioned
View Answer
Explanation: LINQ at core is the query. A query specifies what data will be obtained from a data source. Query in linq is executed using foreach loop. In order for a source of data to be used by LINQ, it must implement the IEnumerable interface.
3. Choose the namespace in which the interface IEnumerable is declared?
a) System.Collections
b) System.Collections.Generic
c) Both System.Collections & System.Collections.Generic
d) None of the mentioned
View Answer
Explanation: By definition.
4. Can we use linq to query against a DataTable?
a) Yes
b) No
c) Either Yes or No
d) None of the mentioned
View Answer
Explanation: We cannot use query against the DataTable’s Rows collection, since DataRowCollection doesn’t implement IEnumerable<T>. We need to use the AsEnumerable() extension for DataTable. As an example:
var results = from myRow in myDataTable.AsEnumerable() where myRow.Field<int>("RowNo") == 1 select myRow;
5. What will be the output of the following C# code snippet?
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, -2, 3, 0, -4, 5};
var posNums = from n in nums
where n >= 0
select n;
foreach (int i in posNums)
Console.Write(i + " ");
Console.WriteLine();
Console.ReadLine();
}
}
a) 0, 1, -2, -4, 5
b) 1, 3, 0, 5
c) 1, 3, 5
d) Run time error
View Answer
Explanation: A simple linq query generated program to show a query is implemented using linq.
Output :
1, 3, 0, 5
6. Select the namespace which should be included while making use of LINQ operations?
a) System.Text
b) System.Collections.Generic
c) System.Linq
d) None of the mentioned
View Answer
Explanation: By definition.
7. What will be the output of the following C# code snippet?
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, -2, 3, 0, -4, 5 };
var posNums = from n in nums
where n % 2 ==0
select n;
Console.Write("The positive values in nums: ");
foreach (int i in posNums) Console.Write(i + " ");
Console.WriteLine();
Console.ReadLine();
}
}
a) code run successfully prints nothing
b) run time error
c) code run successfully and executes output
d) compile time error
View Answer
Explanation: -2, 0, -4
8. What will be the output of the following C# code snippet?
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, -2, 3, 0, -4, 5 };
var posNums = from n in nums
where n > -5 && n < 6
orderby n descending
select n;
Console.Write("The positive values in nums: ");
foreach (int i in posNums) Console.Write(i + " ");
Console.WriteLine();
Console.ReadLine();
}
}
a) Prints nothing code runs successfully
b) Run time error
c) Arranged in descending order code runs successfully
d) Compile time error
View Answer
Explanation: None.
Output :
5, 3, 1, 0, -2, -4
9. What will be the output of the following C# code snippet?
class Program
{
static void Main(string[] args)
{
int[] nums = { 16, 9, 25};
var posNums = from n in nums
where n > 0
select Math.Sqrt(n);
Console.Write("The positive values in nums: ");
foreach (int i in posNums) Console.Write(i + " ");
Console.WriteLine();
Console.ReadLine();
}
}
a) Code runs successfully prints nothing
b) Code runs successfully prints required output
c) Run time error
d) Compile time error
View Answer
Explanation: None.
Output :
4, 3, 5
10. What will be the output of the following C# code snippet?
class Program
{
static void Main(string[] args)
{
int[] nums = {1};
var posNums = from n in nums
wheres n > 0
select Math.Max(78, 9);
Console.Write("The largest values in nums: ");
foreach (int i in posNums) Console.Write(i + " ");
Console.WriteLine();
Console.ReadLine();
}
}
a) Code runs successfully prints nothing
b) Run time error
c) Code runs successfully prints required output
d) Compile time error
View Answer
Explanation: None.
Output :
The largest values in nums: 78
More MCQs on LINQ in C#:
Sanfoundry Global Education & Learning Series – C# Programming Language.
To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice MCA MCQs
- Apply for C# Internship
- Check MCA Books
- Practice Computer Science MCQs
- Check C# Books