ASP.NET Test - 9

1)   Choose the correct one.  

int[] numbers = { 9, 10, 0, 11 };
  
        var nums =
        from n in numbers
        select n + 1;
  
    foreach (var i in nums)
    {
        Console. Write (i+”,”);
    }


a. 10, 11, 1, 12
b. 10, 11, 12, 13
c. 9, 10, 0, 11
d. None of these
Answer  Explanation 

ANSWER: 10, 11, 1, 12

Explanation:
The LINQ Select statement has been used to add all the elements in an integer array by one and return the projected collection.
In the above code snippet, an integer type array, named numbers has been instantiated with {9, 10, 0, 11}. The Select operator operates on this numbers array and returns a collection in which every item is added by one.


2)   Choose the correct one.

int[] numbers = { 9, 4, 1, 3, 8, 6, 7, 2,1 };
  
    var nums = numbers.Take(3);  
  
    foreach (var n in nums)   
    {
  
        Console.WriteLine(n);
  
    }


a.
7
2
1
b.
9
4
1
c.
3
8
6
d.
1
4
9
Answer  Explanation 

ANSWER:
9
4
1

Explanation:
Take function takes a number of elements from one collection, and places them in a new collection.
This method is available in the System.Linq namespace that allows you to get the specified number of contiguous elements from the start of a sequence.
In the above code snippet, take function will takes the first three elements from array.
int[] numbers = { 9, 4, 1, 3, 8, 6, 7, 2,1 };


3)   Choose the correct one.  

int[] numbers = { 5, 4, 11, 3, 9, 8, 6, 7, 2, 0 };  
  
    var nums = numbers.Skip(4);    
  
    foreach (var n in nums)   
    {   
        Console.Write(n+”  “);   
    }


a. 9867 2 0
b. 5411398
c. 54113
d. None of the above
Answer  Explanation 

ANSWER: 9867 2 0

Explanation:
Skip method ignored a specified number of elements in a sequence and then returns the remaining elements.
In the above code snippet, Skip(4) function will ignore the first four elements from array
int[] numbers = { 5, 4, 11, 3, 9, 8, 6, 7, 2, 0 };


4)   Choose the correct one.  

int[] A = { 0, 2, 4, 5, 6, 8 };
        int[] B = { 1, 3, 5, 7, 8 };
  
        var nums = A.Union(B);   
   
    foreach (var n in nums)
    {
        Console.Write(n+”  “);
    }


a. 0 24568 13578
b. 0 2 4 5 6 8 1 3 7
c. 0 1 2 3 4 5 6 7 8
d. None of the above
Answer  Explanation 

ANSWER: 0 2 4 5 6 8 1 3 7

Explanation:
Union operator returns the union of two sequences. In the above code Union operator returns the sequence of number those are either in array A and B. It removes the duplicate entries.
The Union method will work with two collections of the same type of elements.


5)   Choose the correct one.  

int[] A = { 0, 2, 4, 5, 6, 8, 9 };
    int[] B = { 1, 3, 5, 7, 8 };
  
    IEnumerable nums = A.Except(B);
  
    foreach (var n in nums)
    {
        Console.Write(n +”  “);
    }


a. 0, 2, 4, 5, 6, 8, 9
b. 1, 3, 5, 7, 8
c. 0 2 4 6 9
d. All of the above
Answer  Explanation 

ANSWER: 0 2 4 6 9

Explanation:
In the above example the answer will be C because Except extension method produces the set difference of two sequences. The result is a collection where the second array's elements are subtracted from the first array.


6)   Which LINQ statement defines the range variable in a LINQ query?

a. from
b. select
c. join
d. where
Answer  Explanation 

ANSWER: from

Explanation:
A Linq query must begin with a from clause. The from clause specifies the following:

• The data source on which the query or sub - query will be executed.
• A local range variable that represents each element in the source.
Example:
class Demo
{
static void Main()
{
Int [ ] numbers = {15, 4, 1, 3, 9, 8, 6, 7, 2, 0, 20};

var no = from num in numbers
wherenum< 4
selectnum;

foreach (intI in no)
{
Console.Write(I + “ “);
}
}
}
// Output: 1 3 2 0


7)   Which query expression is used to limit the number of results?

a. Skip
b. Take
c. Where
d. Select
Answer  Explanation 

ANSWER: Take

Explanation:
Take function takes a number of elements from one collection, and places them in a new collection.
This method is available in the System.Linq namespace that allows you to get the specified number of contiguous elements from the start of a sequence.


8)   Which interface defines the basic extension methods for LINQ?

a. IComparable<T>
b. IList
c. IEnumerable
d. IQueryable<T>
Answer  Explanation 

ANSWER: IEnumerable

Explanation:
IEnumerable is a generic interface that defines the basic extension methods for LINQ. IEnumerable can move forward only over a collection. It is suitable for LINQ to Object and LINQ to XML queries.


9)   What LINQ expressions are used to shape results in a query?

1. where
2. select
3. join
4. group


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

ANSWER: 2, 4

Explanation:
When the linq query is executed, the select clause specifies the type of values that will be produced.
By using group clause you can group your results based on a key that you specify.


10)   What types of shapes can LINQ query results be shaped into?

1. Collections of primitive types
2. Collections of complex types
3. Single types
4. Collections of anonymous types


a. 1, 2, 4
b. 1,2,3
c. 1,3,4
d. None of the above
Answer  Explanation 

ANSWER: 1, 2, 4

Explanation:
Primitive, complex, anonymous types shapes can LINQ query results be shaped.


11)   Which LINQ statement is used to merge two data sources to perform queries?

a. where
b. select
c. join
d. group
Answer  Explanation 

ANSWER: join

Explanation:
The join clause is used to combine the elements of two different sources. The following are three most common join types:

• Inner join
• Group join
• Left outer join


12)   Which LINQ keyword is used to categorize results in a query?

a. where
b. select
c. join
d. group
Answer  Explanation 

ANSWER: group

Explanation:
By using group clause you can group your results based on a key that you specify. It can be inserted between a from clause and a select clause.


13)   Which of the following statements is true?

a. LINQ to SQL works with any database.
b. LINQ to SQL works with SQL Server.
c. LINQ to SQL is a CLR feature.
d. LINQ to SQL is a SQL Server feature.
Answer  Explanation 

ANSWER: LINQ to SQL works with SQL Server.

Explanation:
LINQ to SQL works with SQL Server and it is optimal with sql server.


14)   LINQ query can work with?

a. DataSet
b. List<T>
c. Array
d. All of the above
Answer  Explanation 

ANSWER: All of the above

Explanation:
There are three types of LINQ.
1. Linq to Objects
2. Linq to SQL
3.Linq to XML
So in the above question all options are correct.


15)   How you can merge the results from two separate LINQ queries into a single result set.

a. Use the ToList method.
b. Use the DataContractJsonSerializer class.
c. Use the XElement class.
d. Use the Concat method.
Answer  Explanation 

ANSWER: Use the Concat method.

Explanation:
Concat method is used to merge two separate lists together to form a single list.

class Program
{
static void Main()
{
List<string> list1 = new List<string>();
list1.Add( " A " );
list1.Add( " B " );

List<string> list2 = new List<string>();
list2.Add( " C " );
list2.Add( " D " );

var result = list1.Concat(list2);
List<string>finalList = result.ToList();
foreach (var entry in finalList)
{
Console.WriteLine(entry);
}
}
}


16)   Which of the following objects represents a LINQ to SQL O / R map?

a. DataSet
b. XElement
c. ObjectContext
d. DataContext
Answer  Explanation 

ANSWER: DataContext

Explanation:
A DataContext object represents a LINQ to SQL O / R map. ORM stands for Object-Relational Mapping. It is also called as O / R mapping.
DataContext class is used to connect with database, retrieve objects from it, and submit changes back to it.


17)   What is lamda expression?

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


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.


18)    Choose the correct one

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.


19)   Choose the correct one.

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.


20)   Choose the correct one

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.


21)   Choose the correct option.

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.


22)   Which of the following statement is correct?

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);


23)   What types of Objects can you query using LINQ?

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.


24)    When do LINQ queries actually run?

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.


25)   Which of the following statement / s is / are true?

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();


26)   Trace the output
namespace A
{
    class MyClass
    {
        public void fun()
        {
            Console.WriteLine( " C # is fun " );
        }
    }
    namespace B
    {
        class MyClass
        {
            public void fun()
            {
                Console.WriteLine( " C # is interesting " );
            }
        }
    }
}
Consider the above code what will be the output of following program
class Program
    {
        static void Main(string[] args)
        {
            A.B.MyClass obj = new A.B.MyClass();

            obj.fun();

        }

    }


a. C # is interesting
b. C # is fun
c. compile time error
d. None of the above
Answer  Explanation 

ANSWER: C # is interesting

Explanation:
In the given problem there is nested namespace. The code A.B.MyClass obj = new A.B.MyClass(); will create the object that will refer to namespace B, so the inner class method will be called and answer will be
“C # is interesting”


27)   Trace the output
namespace A
{
    class MyClass
    {
        public void fun()
        {
            Console.WriteLine("C # is fun");
        }
    }
    namespace B
    {
        class MyClass
        {
            public void fun()
            {
                Console.WriteLine("C # is interesting");
            }
        }
    }
}
Consider the above code what will be the output of following program
class Program
    {
        static void Main(string[] args)
        {

            A.MyClass obj = new A.MyClass();
            obj.fun();
        }

    }


a. C # is interesting
b. C # is fun
c. cmpile time error
d. None of the above
Answer  Explanation 

ANSWER: C # is fun

Explanation:
No explanation is available for this question!


28)   Trace the output
class Myclass
{
   public  int count = 0;
   public  Myclass()
    {
        count++;
    }
}


    class Program
    {
        static void Main(string[] args)
        {
            Myclass obj1 = new Myclass();
            Console.WriteLine(obj1.count);

            Myclass obj2 = new Myclass();
            Console.WriteLine(obj2.count);

            Myclass obj3 = new Myclass();
            Console.WriteLine(obj3.count);
        }

    }


a. 1, 1, 1
b. 1, 2, 3
c. 0, 1, 2
d. All of the above
Answer  Explanation 

ANSWER: 1, 1, 1

Explanation:
In the above example three different objects are created of class Myclass. So every time count is reinitialized with zero and constructor will be called. Here count variable is non - static therefore separate copy of count will be created for each object. So the answer will be 1 1 1.


29)   An interface can contain declaration of?

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.


30)   Which of the following are correct?

1. An interface can be instantiated directly.
2. Interfaces can contain constructor.
3. Interfaces contain no implementation of methods.
4. Classes and structs can implement more than one interface.
5. An interface itself can inherit from multiple interfaces.


a. 3,4,5
b. 1,2,3
c. 2,3,4
d. All of the above
Answer  Explanation 

ANSWER: 3,4,5

Explanation:
Interfaces are like classes but we cannot provide definition of methods in interface. We can only declare the methods not implementation in interface. A class must implement all its members that is inheriting the interface.
An interface has the following properties:

• An interface cannot be instantiated directly.
• Interfaces can contain events, indexers, methods and properties.
• Interfaces contain no implementation of methods.
• Classes and structs can inherit from more than one interface.
• An interface can itself inherit from multiple interfaces.


31)   Which of the following are correct?

1. Delegates are like C++ function pointers.
2. Delegates allow methods to be passed as parameters.
3. Delegates can be used to define callback methods.
4. Delegates are not type safe.


a. 1,3,4
b. 1,2,3
c. 2,3,4
d. All of the above
Answer  Explanation 

ANSWER: 1,2,3

Explanation:
A delegate is an object that can refer to a method. It means that it can hold a reference to a method. The method can be called through this reference or we can say that a delegate can invoke the method to which it refers. The main advantage of a delegate is that it invokes the method at run time rather than compile time. Delegate in C# is similar to a function pointer in C / C++.
A delegate can call only those methods that have same signature and return type as delegate. So the option 1, 2, 3 are correct.


32)   Properties in .NET can be declare as

1. Static, Protected internal, Virtual
2. Public, internal, Protected internal
3. Only public
4. None


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

ANSWER: 1, 2

Explanation:
Properties are not variables. Properties are an extension of fields and are accessed using the same syntax. Properties cannot be passed as a ref or out parameter.A property is one or two code blocks, representing a get accessor or a set accessor or both. A property without a set accessor is considered read-only. A property without a get accessor is considered write - only.
Properties can be marked as public, private, protected, internal, protected internal, static, virtual.


33)   Which of the following statements are correct?

1. Indexers enable objects to be indexed in a similar manner to arrays.
2. The this keyword is used to define the indexers.
3. Indexers can be overloaded.
4. Indexer cannot be used in interface


a. 1, 2
b. 3
c. 1,2,3
d. None of the above
Answer  Explanation 

ANSWER: 1,2,3

Explanation:
The indexer has following properties.
• Indexers enable objects to be indexed in a similar manner to arrays.
• A get accessor returns a value. A set accessor assigns a value.
• The this keyword is used to define the indexers.
• The value keyword is used to define the value being assigned by the set indexer.
• Indexers can be overloaded.
Example:
classIndDemo
{
privateint [ ] arr = new int[3];
publicint this[int i]
{
get
{
returnarr[i];
}
set
{
arr[i] = value;
}
}
}

class Program
{
static void Main(string[] args)
{
IndDemoobj = new IndDemo();
obj[0] = 10;
Console.WriteLine(obj[0]);
}
}


34)   The best way for handling exception when dealing with a database connection?

a. Implement a try / catch block that catches System.Exceptions.
b. Implementing custom error page.
c. Implement a try / catch block that catches individual exception types, such as SQLException.
d. Display an error message.
Answer  Explanation 

ANSWER: Implement a try / catch block that catches System.Exceptions.

Explanation:
System.Exception is base class for all type of exceptions. This class can handle any type of exception. So for handling exception while working with database connectivity use try/catch block that catches System.Exceptions.


35)   For building new types at runtime which class can be used?

a. System
b. System.Object
c. System.NewClass
d. System.Reflection.Emit.
Answer  Explanation 

ANSWER: System.Reflection.Emit.

Explanation:
For building new types at runtime System.Reflection.Emit namespace will be used. This namespace provides you a number of classes which you can use to build your type. Some of the important classes in this namespace are

• AssemblyBuilder
• ModuleBuilder
• ConstructorBuilder
• MethodBuilder
• EventBuilder
• PropertyBuilder


36)   Reflection can be used when?

a. To access attributes in your program's metadata
b. To create type at compile time
c. To access attributes at compile time.
d. None of the above
Answer  Explanation 

ANSWER: To access attributes in your program's metadata

Explanation:
Reflection is used toaccess attributes in your program's metadata. In .NET the metadata contains the type information of method, property, event, delegate, and enumeration. CLR use this metadata to load and execute code.


37)   Choose the correct one

1) XML serialization serializes the public fields and properties of an object.
2) XML serialization serializes the private fields and properties of an object.
3) XML serialization serializes only the public methods.


a. Only 1
b. Only 2
c. Only 1 & 2
d. All of the above
Answer  Explanation 

ANSWER: Only 1

Explanation:
XML serialization can serialize only public data. You cannot serialize private data.

Example:
FileStreamfs = new FileStream(" MyFile.XML ", FileMode.Create);
XmlSerializerobj = new XmlSerializer(typeof(DateTime));
obj.Serialize(fs, System.DateTime.Now);
fs.Close();


38)   Choose the correct one

1) Sealed class can be declared as abstract
2) Abstract class cannot be declared as abstract
3) Abstract class can  be declared as abstract


a. Only 1
b. Only 2
c. Only 1 & 2
d. All of the above
Answer  Explanation 

ANSWER: Only 1

Explanation:
If you declare any class as a sealed class then this class cannot be inherited. A sealed class cannot be used as a base class. So, it cannot also be an abstract class.


39)   A write - only property can be specified if the following is present.

a. The set modifier only
b. The get modifier only
c. Both the modifiers
d. None of the modifiers
Answer  Explanation 

ANSWER: The set modifier only

Explanation:
A property is called as write-only property if it has set modifier only. Properties can be think as an extension of fields. A property is one or two code blocks, representing a get accessor or a set accessor or both.


40)   An indexer is

a. A class
b. A structure
c. An enumeration
d. A special type of property
Answer  Explanation 

ANSWER: A special type of property

Explanation:
An indexer is a special type of property. this keyword is used to define the indexers. A get accessor returns a value. A set accessor assigns a value.