LINQ - ASP.NET (MCQ) questions and answers

Here, you can read LINQ multiple choice questions and answers with explanation.

1)   An interface can contain declaration of?
- Published on 31 Aug 15

a. Methods, properties, events, indexers
b. Methods
c. Static members
d. All of the above
Answer  Explanation 

ANSWER: Methods, properties, events, indexers

Explanation:
An interface can contain declaration of methods, properties, events, indexers but you cannot provide the definition.


2)   Which of the following statement / s is / are true?
- Published on 31 Aug 15

a. CommitChanges is a method of SqlDataContext.
b. CommitChanges is a method of DataContext.
c. CommitChanges is a method of DbDataContext.
d. CommitChanges is a method of OleDbData Context
Answer  Explanation 

ANSWER: CommitChanges is a method of DataContext.

Explanation:
In LINQ, CommitChanges is a method of DataContext class. DataContext class is used to connect with database, retrieve objects from it, and submit changes back to it.
Example:
DataContext context = new DataContext( “ Provide your connection string ” );
You can also create and delete database as given below.
Create database
context.CreateDatabase();

Delete database
context.DeleteDatabase();


3)    When do LINQ queries actually run?
- Published on 31 Aug 15

a. When they are iterated over in a foreachloop
b. When calling the ToArray() method on the range variable
c. When calling the ToList() method on the range variable
d. All of the above
Answer  Explanation 

ANSWER: All of the above

Explanation:
In the above question all options are correct. By default LINQ uses deferred query execution. It means that LINQ queries are always executed when you iterated the query variable, not when the query variable is created. LINQ queries actually run when they are iterated over in a foreach loop.


4)   What types of Objects can you query using LINQ?
- Published on 31 Aug 15

a. DataTables and DataSets
b. Any .NET Framework collection that implements IEnumerable(T)
c. Collections that implement interfaces that inherit from IEnumerable(T)
d. All of the above
Answer  Explanation 

ANSWER: All of the above

Explanation:
In the above question all the options are correct. Any .NET Framework collection that implements IEnumerable(T) or that inherit from IEnumerable(T) can be queried using LINQ.


5)   Which of the following statement is correct?
- Published on 31 Aug 15

a. Anonymous types are class types that derive directly from object.
b. Anonymous types are not class types that derive directly from object.
c. Anonymous types are class types that derive directly from System.Class.
d. None of the above
Answer  Explanation 

ANSWER: Anonymous types are class types that derive directly from object.

Explanation:
Anonymous types are class types that derive directly from object. You cannot cast to other type except object.Anonymous types are created by using new operator along with an object initializer.

Example:
var v = new {Website = “ CareerRide ”, Message = " Welcome " };
Console.WriteLine(v. Website + v.Message);


6)   Choose the correct option.
- Published on 31 Aug 15

a. Dynamic type is non - static type.
b. Dynamic type is static type.
c. Implicit conversion does not work with type dynamic.
d. None of the above
Answer  Explanation 

ANSWER: Dynamic type is static type.

Explanation:
If you declare anything as dynamic then its type is checked at runtime. Dynamic type is static type.


7)   Choose the correct one
- Published on 31 Aug 15

a. Variables introduced within a lambda expression are not visible in the outer method.
b. Variables introduced within a lambda expression are visible in the outer method.
c. The lambda should not contain the same number of parameters as the delegate type.
d. None of the above
Answer  Explanation 

ANSWER: Variables introduced within a lambda expression are not visible in the outer method.

Explanation:
A lambda expression is an anonymous function that you can use to create delegates
A variable, declared inside the anonymous method can’t be accessed outside the anonymous method but a variable, declared outside the anonymous method can be accessed inside the anonymous method.


8)   Choose the correct one.
- Published on 31 Aug 15

a. The return value of the lambda (if any) must be explicitly convertible to the delegate's return type
b. Each input parameter in the lambda must be implicitly convertible to its corresponding delegate parameter.
c. Lamda expression does not work with LINQ.
d. None of the above
Answer  Explanation 

ANSWER: Each input parameter in the lambda must be implicitly convertible to its corresponding delegate parameter.

Explanation:
Each input parameter in the lambda must be implicitly convertible to its corresponding delegate parameter because A delegate can refer only those methods that have same signature as delegate.


9)    Choose the correct one
- Published on 31 Aug 15

a. The lambda must contain the same number of parameters as the delegate type.
b. The lambda should not contain the same number of parameters as the delegate type.
c. The return value of the lambda (if any) must be explicitly convertible to the delegate's return type
d. None of the above
Answer  Explanation 

ANSWER: The lambda must contain the same number of parameters as the delegate type.

Explanation:
The lambda must contain the same number of parameters as the delegate type.
A delegate can refer only those methods that have same signature as delegate and lambda is nothing but anonymous function that can be used to create delegates.


10)   What is lamda expression?

1. Anonymous function
2. Can  be used to create delegates
3. Named function
4. None

- Published on 31 Aug 15

a. 1, 2
b. 1, 2, 3
c. 1, 3
d. 4
Answer  Explanation 

ANSWER: 1, 2

Explanation:
A lambda expression is an anonymous function that can be used to create delegates. There are two types of lambda expression, Expression Lambda and Statement Lambdas.

Example:
delegateint del(int i);
static void Main(string[ ] args)
{
delmyDelegate = x => x * x * x;
int j = myDelegate(4);
}
In the above given example x is the parameter that is acted on by the expression x * x * x. So the value of x will be cube of x.


1 2 3