C# Multiple Choice Questions – LINQ

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

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

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

Answer: b
Explanation: By definition.
advertisement
advertisement

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

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

Note: Join free Sanfoundry classes at Telegram or Youtube
  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
View Answer

Answer: b
Explanation: A simple linq query generated program to show a query is implemented using linq.
Output :

advertisement
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

Answer: c
Explanation: By definition.
advertisement

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
View Answer

Answer: c
Explanation: -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
View Answer

Answer: c
Explanation: None.
Output :

5, 3, 1, 0, -2, -4

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
View Answer

Answer: b
Explanation: None.
Output :

4, 3, 5

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
View Answer

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.