C# Questions & Answers on Fundamental of LINQ for Freshers

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

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
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 a & b
d) None of the mentioned
Answer: b

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

Explanation: 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(“RowNo”) == 1
select myRow;
5. What will be the output of given 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
Answer: b

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

Explanation: By definition.
7. Select the output for the given 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
Explanation: -2, 0, -4
8. Select the output for given 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
Output :5, 3, 1, 0, -2, -4
9. Select the output for given 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
Answer: b
Output : 4, 3, 5
10. Select the output for given 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
Output : The largest values in nums: 78

Related

Java Multiple Choice Questions & Answers on Java.lang package Miscellaneous Math Methods & StrictMath Class for Freshers

1. Which of these class contains all the methods present in Math class? a) SystemMathb) StrictMathc) Compilerd) ClassLoader Answer: b Explanation: SystemMath class defines complete set of mathema...

Java Multiple Choice Questions & Answers on Event Listeners Interfaces for Freshers

1. Which of these packages contains all the event handling interfaces? a) java.langb) java.awtc) java.awt.eventd) java.event Answer: c 2. Which of these interfaces handles the event when a compon...

Java Multiple Choice Questions & Answers on MouseEvent, TextEvent & WindowEvent Class for Freshers

1. Which of these events is generated when the a window is closed? a) TextEventb) MouseEventc) FocusEventd) WindowEvent Answer: d Explanation: A WindowEvent is generated when a window is opened, ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item