250+ TOP MCQs on Fundamental of LINQ and Answers

C# MCQs on fundamental of 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

Answer: c
Clarification: 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

Answer: c
Clarification: 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

Answer: b
Clarification: 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

Answer: b
Clarification: We cannot use query against the DataTable’s Rows collection, since DataRowCollection doesn’t implement IEnumerable. 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?

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int[] nums = { 1, -2, 3, 0, -4, 5};
  6.          var posNums = from n in nums
  7.                        where n >= 0
  8.                        select n;
  9.         foreach (int i in posNums)
  10.         Console.Write(i + " ");
  11.         Console.WriteLine();
  12.         Console.ReadLine();
  13.     }
  14. }

a) 0, 1, -2, -4, 5
b) 1, 3, 0, 5
c) 1, 3, 5
d) Run time error

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

Answer: c
Clarification: By definition.

7. What will be the output of the following C# code snippet?

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int[] nums = { 1, -2, 3, 0, -4, 5 };
  6.          var posNums = from n in nums
  7.                        where n % 2 ==0
  8.                        select n;
  9.             Console.Write("The positive values in nums: ");
  10.             foreach (int i in posNums) Console.Write(i + " ");
  11.             Console.WriteLine();
  12.             Console.ReadLine();
  13.         }
  14.     }

a) code run successfully prints nothing
b) run time error
c) code run successfully and executes output
d) compile time error

Answer: c
Clarification: -2, 0, -4

8. What will be the output of the following C# code snippet?

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         int[] nums = { 1, -2, 3, 0, -4, 5 };
  6.         var posNums = from n in nums
  7.                        where n > -5 && n < 6
  8.                        orderby n descending
  9.                        select n;
  10.         Console.Write("The positive values in nums: ");
  11.         foreach (int i in posNums) Console.Write(i + " ");
  12.         Console.WriteLine();
  13.         Console.ReadLine();
  14.     }
  15. }

a) Prints nothing code runs successfully
b) Run time error
c) Arranged in descending order code runs successfully
d) Compile time error

Answer: c
Clarification: None.
Output :

9. What will be the output of the following C# code snippet?

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         int[] nums = { 16,  9, 25};
  6.         var posNums = from n in nums
  7.                       where n > 0
  8.                       select Math.Sqrt(n);
  9. 
    
  10.         Console.Write("The positive values in nums: ");
  11.         foreach (int i in posNums) Console.Write(i + " ");
  12.         Console.WriteLine();
  13.         Console.ReadLine();
  14.     }
  15. }

a) Code runs successfully prints nothing
b) Code runs successfully prints required output
c) Run time error
d) Compile time error

10. What will be the output of the following C# code snippet?

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         int[] nums = {1};
  6.         var posNums = from n in nums
  7.                       wheres n > 0
  8.                      select Math.Max(78, 9);
  9.         Console.Write("The largest values in nums: ");
  10.         foreach (int i in posNums) Console.Write(i + " ");
  11.         Console.WriteLine();
  12.         Console.ReadLine();
  13.     }
  14. }

a) Code runs successfully prints nothing
b) Run time error
c) Code runs successfully prints required output
d) Compile time error

Answer: c
Clarification: None.
Output :

The largest values in nums: 78

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top