C# Questions & Answers – Operation with LINQ

This section of our 1000+ C# MCQs focuses on operations with Linq in C# Programming Language.

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         string[] strs = {"alpha", "beta", "gamma"};
  6.         var chrs = from str in strs
  7.                    let chrArray = str.ToCharArray()
  8.                    from ch in chrArray
  9.                    orderby ch
  10.                    select ch;
  11.         Console.WriteLine("The individual characters in sorted order:");
  12.         foreach (char c in chrs) 
  13.         Console.Write(c + " ");
  14.         Console.WriteLine();
  15.         Console.ReadLine();
  16.     }
  17. }

a) a a l h a b g m m a p e t a
b) a a a a a b e g h l m m p t
c) a g h l m m p t a a a a b e
d) Run time error
View Answer

Answer: b
Explanation: None.
Output:

 a a a a a b e g h l m m p t

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

advertisement
advertisement
  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int[] nums = { 1, -2, 3, 0, -4, 5 };
  6.          var posNums = nums.Where(n => n > 0).Select(r => r*2).
  7.                        OrderByDescending(r=>r);
  8.          Console.Write("The positive values in nums: ");
  9.          foreach(int i in posNums) 
  10.          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 prints multiple of 2
d) compile time error
View Answer

Answer: c
Explanation: We had created the queries by using query method such as Where() and Select(). This creates a query called posNums that creates a sequence of positive values in nums in descending order using the method OrderByDescending().
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
 10 6 2

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

advertisement
  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int[] nums = {3, 1, 2, 5, 4};
  6.          var ltAvg = from n in nums
  7.                      let x = nums.Average()
  8.                      where n < x
  9.                      select n;
  10.          Console.WriteLine("The average is " + nums.Average());
  11.          Console.ReadLine();
  12.      }
  13.  }

a) Run time error
b) 3
c) 5
d) Compile time error
View Answer

Answer: b
Explanation: Built in method Avg() is used
Output:

advertisement
3

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         Expression<Func<int, int, bool>>
  6.         IsFactorExp = (n, d) => (d != 0) ? (n % d) == 0 : false;
  7.         Func<int, int, bool> IsFactor = IsFactorExp.Compile();
  8.         if (IsFactor(10, 5))
  9.         Console.WriteLine("5 is a factor of 10.");
  10.         if (!IsFactor(343, 7))
  11.         Console.WriteLine("7 is not a factor of 10.");
  12.         Console.ReadLine();
  13.     }
  14. }

a) Compile time error
b) Run time error
c)

5 is a factor of 10
7 is not a factor of 10

d) 5 is a factor of 10
View Answer

Answer: d
Explanation: The current program has introduced the concept of expression tree. An expression tree is a representation of a lambda expression as data. The program illustrates the two key steps in using an expression tree. First, it creates an expression tree by using this statement:

Expression<Func<int, int, bool>>
IsFactorExp = (n, d) => (d != 0) ? (n % d) == 0 : false;

Second, this constructs a representation of a lambda expression in memory.
Output:

5 is a factor of 10

5. Choose the namespace in which Expression trees are encapsulated?
a) System.Linq
b) System.Linq.Expressions
c) System.Text
d) System.Collections.Generic
View Answer

Answer: b
Explanation: By definition.

6. In the following C# code, which query will work according to the set of code?

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         int[] nums = { 1, -2, 3, 0, -4, 5 };
  6.         int len = /*_________________ */
  7.         Console.WriteLine("The number of positive values in nums: " + len);
  8.         Console.ReadLine();
  9.     }
  10. }

a)

from n in nums where n > 0
select n

b)

from n in nums where n > 0
select n.Count()

c)

(from n in nums where n > 0
select n).Count();

d) Both “from n in nums where n > 0 select n.Count()” & “(from n in nums where n > 0 select n).Count();”
View Answer

Answer: c
Explanation: None.
Output:

            int len = (from n in nums where n > 0
                    select n).Count();

7. In the following C# code, what does the output represent?

  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.         int len = posNums.Count();
  10.         Console.WriteLine(len);
  11.         Console.ReadLine();
  12.     }
  13. }

a) Execution of code with nothing being printed
b) Execution of code with printing all numbers
c) Execution of code with counting total numbers greater than zero
d) Run time error
View Answer

Answer: c
Explanation: None.
Output:

 3

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 = nums.Where(n => n < 10).Select(r => r%3);
  7.          Console.Write("The values in nums: ");
  8.          foreach (int i in posNums) Console.Write(i + " ");
  9.          Console.WriteLine();
  10.          Console.ReadLine();
  11.      }
  12.  }

a) Compile time error
b) Run time error
c) 1 -2 0 0 -1 2
d) 2 -1 0 0 -2 1
View Answer

Answer: c
Explanation: Query solved using lambda expression. The code “var posNums = nums.Where(n => n < 10).Select(r => r%3)” creates a query called posNums that creates a sequence of the values less than 10 in nums.
Output:

 1 -2 0 0 -1 2

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         string[] strs = { ".com", ".net", "facebook.com", "google.net", 
  6.                          "test", "netflix.net", "hsNameD.com" };
  7.         var netAddrs = from addr in strs
  8.                        where addr.Length > 4 && addr.EndsWith(".net",
  9.                        StringComparison.Ordinal)
  10.                        select addr;
  11.         foreach (var str in netAddrs) Console.WriteLine(str);
  12.         Console.ReadLine();
  13.     }
  14. }

a) Compile time error
b) Run time error
c)

   facebook.com
   netflix.net
   google.net

d)

   google.net
   netflix.net
View Answer
Answer: d
Explanation: Searches for the string which ends with .net.
Output:

        google.net
        netflix.net
 
 

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

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

a) 10 2 -4 -6
b) 5 1 -2 -3
c) 1 5 -2 -3
d) Run time error
View Answer

Answer: a
Explanation: None.
Output:

 10 2 -4 -6

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.

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.