C# MCQ Set 1
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
Answer: c [Reason:] 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
Answer: c [Reason:] 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
Answer: b [Reason:] 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
Answer: b [Reason:] We cannot use query against the DataTable’s Rows collection, since DataRowCollection doesn’t implement IEnumerable
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field
select myRow;
5. What will be the output of given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = { 1, -2, 3, 0, -4, 5};
-
var posNums = from n in nums
-
where n >= 0
-
select n;
-
foreach (int i in posNums)
-
Console.Write(i + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) 0, 1, -2, -4, 5
b) 1, 3, 0, 5
c) 1, 3, 5
d) Run time error
Answer
Answer: b [Reason:] 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
Answer: c [Reason:] By definition.
7. Select the output for the given code snippet:
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = { 1, -2, 3, 0, -4, 5 };
-
var posNums = from n in nums
-
where n % 2 ==0
-
select n;
-
Console.Write("The positive values in nums: ");
-
foreach (int i in posNums) Console.Write(i + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) code run successfully prints nothing
b) run time error
c) code run successfully and executes output
d) compile time error
Answer
Answer: c [Reason:] -2, 0, -4
8. Select the output for given code snippet:
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = { 1, -2, 3, 0, -4, 5 };
-
var posNums = from n in nums
-
where n > -5 && n < 6
-
orderby n descending
-
select n;
-
Console.Write("The positive values in nums: ");
-
foreach (int i in posNums) Console.Write(i + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) Prints nothing code runs successfully
b) Run time error
c) Arranged in descending order code runs successfully
d) Compile time error
Answer
Answer: c [Reason:] None.
Output :5, 3, 1, 0, -2, -4
9. Select the output for given code snippet:
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = { 16, 9, 25};
-
var posNums = from n in nums
-
where n > 0
-
select Math.Sqrt(n);
-
-
Console.Write("The positive values in nums: ");
-
foreach (int i in posNums) Console.Write(i + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) Code runs successfully prints nothing
b) Code runs successfully prints required output
c) Run time error
d) Compile time error
Answer
Answer: b [Reason:] None.
Output : 4, 3, 5
10. Select the output for given code snippet:
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = {1};
-
var posNums = from n in nums
-
wheres n > 0
-
select Math.Max(78, 9);
-
Console.Write("The largest values in nums: ");
-
foreach (int i in posNums) Console.Write(i + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) Code runs successfully prints nothing
b) Run time error
c) Code runs successfully prints required output
d) Compile time error
Answer
Answer: c [Reason:] None.
Output : The largest values in nums: 78
C# MCQ Set 2
1. Which namespace is mostly preferred for the operation of networking in C#?
a) System.Web
b) System.in
c) System.Net.Mail
d) All of the mentioned
Answer
Answer: c [Reason:] Networking support is contained in several namespaces defined by the .NET Framework.The primary namespace for networking is System.Net. It defines a large number of high-level, easy-to-use classes that support various types of operations common to the Internet.Several namespaces nested under System.Net are also provided. Example :System.Net.Mail.
2. Which of the following are the classes defined by the namespace System.Net:
a) Cookie
b) CookieContainer
c) FileWebRequest
d) All of the mentioned
Answer
Answer: d [Reason:] None.
3. Which of the following are the interfaces defined by the namespace System.Net:
a) IAuthenticationModule
b) HttpWebRequest
c) WebProxy
d) HttpResponseHeader
Answer
Answer: a [Reason:] c and d are namespaces and enumerations.
4. Which of the following are the classes that support the standard HTTP protocol
a) HttpWebRequest
b) HttpResponseHeader
c) HttpRequestHeader
d) HttpStatusCode
Answer
Answer: a [Reason:] The derived classes that support the standard HTTP protocol are HttpWebRequest and HttpWebResponse.
5. Which of the following class/classes supports the task of uploading and downloading the file:
a) WebRequest
b) WebResponse
c) WebClient
d) All of the mentioned
Answer
Answer: c [Reason:] If we only need to upload or download a file, then WebClient is often the best way to accomplish it.
6. How many ports of TCP/IP are reserved for specific protocols?
a) 10
b) 1024
c) 2048
d) 512
Answer
Answer: b [Reason:] None.
7. How many bits are present in a single IP address?
a) 8
b) 16
c) 32
d) 64
Answer
Answer: c [Reason:] None.
8. Which of the following is the full form of DNS?
a) Data Network Service
b) Data Name Service
c) Domain Network Service
d) Domian Name Service
Answer
Answer: d [Reason:] None.
9. Which of the following classes is used to encapsulate IP address and DNS?
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler
Answer
Answer: c [Reason:] InetAddress class encapsulates both IP address and DNS. We can interact with this class by using the name of an IP host.
10. Which of the following are the protocols defined by .NET runtime:
a) HTTP
b) HTTPS
c) File
d) All of the mentioned
Answer
Answer: d [Reason:] The .NET runtime defines HTTP, HTTPS, file, and FTP protocols. Thus, if we specify a URI that uses HTTP prefix, we will automatically receive the HTTP-compatible class that supports it. If we specify a URI that uses FTP prefix, we will automatically receive the FTP-compatible class that supports it.
C# MCQ Set 3
1. Which of the following is not a namespace in the .NET Framework Class Library?
a) System.Process
b) System.Security
c) System.Threading
d) System.xml
Answer
Answer: a [Reason:] None.
2. Which is the correct statement about the namespaces in C#.NET?
a) Nesting of namespaces is permitted, provided all the inner namespaces are declared in the same file
b) A namespace cannot be tested
c) There is no limit on the number of levels while nesting namespaces
d) All of the mentioned
Answer
Answer: c [Reason:] None.
3. Which among the following does not belong to the C#.NET namespace?
a) class
b) struct
c) enum
d) data
Answer
Answer: d [Reason:] None.
4. Which among the following is a correct statement about namespace used in C#.NET?
a) Classes must belong to a namespace, whereas structures need not
b) All elements of the namespace must to belong to one file
c) If not mentioned, a namespace takes the name of the current project
d) All of the mentioned
Answer
Answer: c [Reason:] None.
5. Which among the given is not a correct way to call the method Issue() defined in the code snippet given below?
-
class Book
-
{
-
public void issue()
-
{
-
/* code */
-
}
-
}
-
class Journel
-
{
-
public void issue()
-
{
-
/* code */
-
}
-
}
a) College.Lib.Book b = new College.Lib.Book();
b.issue();
b) Book b = new Book();
b.issue();
c) using College.Lib;
Book b = new Book();
b.issue();
d) All of the mentioned
Answer
Answer: b [Reason:] None.
6. Which among the following statements are not correct about a namespace used in C#.NET?
a) Nested namespaces are allowed
b) Importing outer namespaces imports inner namespace
c) Nested namespaces are allowed
d) Importing outer namespace does not import inner namespace
Answer
Answer: b [Reason:] None.
7. Which among the following is a .NET namespace?
a) System.Web
b) System.Process
c) System.Drawing2D
d) System.Drawing3D
Answer
Answer: a [Reason:] None.
8. If a class named csharp is present in namespace n1 as well as in namespace n2,then which of the following is the correct way to use csharp class?
a) class Program { static void Main(string[] args) { import n1; csharp x = new csharp(); x.fun(); import n2; csharp y = new csharp(); y.fun(); } } b) import n1; import n2; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { n1.csharp x = new n1.csharp(); x.fun(); import n2; n2.csharp y = new n2.csharp(); y.fun(); } } } c) class Program { static void Main(string[] args) { using n1; csharp x = new csharp(); x.fun(); using n2; csharp y = new csharp(); y.fun(); } } d) using n1; using n2; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { n1.csharp x = new n1.csharp(); x.fun(); import n2; n2.csharp y = new n2.csharp(); y.fun(); } } }
Answer
Answer: c [Reason:] None.
9. If ListBox is the class of System.Windows.Forms namespace.Then,correct way to create an object of ListBox class is?
a) using System.Windows.Forms;
ListBox I = new ListBox();
b) System.Windows.Forms.ListBox I = new System.Windows.Forms.ListBox();
c) using LBControl I = new System.Windows.Forms.ListBox;
d) All of the mentioned
Answer
Answer: d [Reason:] None.
10. Which among the following is the correct statement about the using statement used in C#.NET?
a) A C#.NET source code file consists of any number of using statement
b) By using ‘using’ statement it’s possible to create an alias for the namespace but not for the namespace element
c) It is permitted to define a member at namespace level using alias
d) Using statement can be placed anywhere in the C#.NET source code file
Answer
Answer: c [Reason:] None.
C# MCQ Set 4
1. For the code set given below,which of the following statements are perfectly valid?
-
public class MyContainer<T> where T: class, IComparable
-
{
-
/* insert code here */
-
}
a) Class MyConatiner requires that its type argument must implement Icomparable interface
b) There are multiple constraints on type argument to MyConatiner class
c) Compiler will report an error
d) None of the mentioned
Answer
Answer: b [Reason:] None.
2. For the code given below which statements are perfectly valid?
-
public class Csharp
-
{
-
public void subject<S>(S arg)
-
{
-
Console.WriteLine(arg);
-
}
-
}
-
class Program
-
{
-
static Void Main(string[] args)
-
{
-
Csharp c = new Csharp();
-
c.subject("hi");
-
c.subject(20);
-
}
-
}
a) Run time exception error
b) Compile time error
c) Code runs successfully and prints required output
d) None of the mentioned
Answer
Answer: c
Output : hi
20
3. Which of given statements are valid about generics in .NET Framework?
a) generics are useful in collection classes in .NET framework
b) generics delegates are not allowed in C#.NET
c) generics is a not language feature
d) all of the mentioned
Answer
Answer: a [Reason:] None.
4. Which statement is valid for the given snippet of code:
-
public class Generic<T>
-
{
-
public T Field;
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Generic<String> g = new Generic<String>();
-
g.Field = "Hi";
-
Console.WriteLine(g.Field);
-
}
-
}
a) Compile time error
b) Generic being a keyword cannot be used as a class name
c) Run time error
d) Code runs successfully
Answer
Answer: d
Output : Hello
5. Choose the output for given set of code:
-
public class Generic<T>
-
{
-
public T Field;
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Generic<int> g2 = new Generic<int>();
-
Generic<int> g3 = new Generic<int>();
-
g2.Field = 8;
-
g3.Field = 4;
-
if (g2.Field % g3.Field == 0)
-
{
-
Console.WriteLine("A");
-
}
-
else
-
Console.WriteLine("Prints nothing:");
-
Console.ReadLine();
-
}
-
}
a) Compile time error
b) A
c) Run time error
d) Code runs successfully but prints nothing
Answer
Answer: b
Output : A
6. Which of the following is a valid statement about generic procedures in C#.NET are?
a) All procedures in a Generic class are generic
b) Generic procedures should take at least one type parameter
c) Only those procedures labeled as Generic are Generic
d) None of the mentioned
Answer
Answer: b [Reason:] None.
7. For the code set given below,which of the following statements are perfectly valid?
-
public class MyContainer<T> where T: IComparable
-
{
-
/* insert code here */
-
}
a) Class MyConatiner requires that its type arguement must implement Icomparable interface
b) There are multiple constraints on type arguement to MyContainer class
c) Type arguement of class MyContainer should be Icomparable
d) None of the mentioned
Answer
Answer: a [Reason:] None.
8. Choose the statements which are valid for given code snippet:
-
public class Generic<T>
-
{
-
public T Field;
-
public void testSub()
-
{
-
T i = Field + 1;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Generic<int>g = new Generic<int>();
-
g.testSub();
-
}
-
}
a) code runs successfully but prints nothing
b) code runs successfully and prints 1
c) program will give run time error
d) compile time error
Answer
Answer: d [Reason:] compiler will give error as operator ‘+’ is not defined for types ‘T’ and ‘int’
9. Which among the given classes represents System.Collections.Generic namespace?
a) SortedDictionary
b) Sorted Array
c) Stack
d) All of the mentioned
Answer
Answer: a [Reason:] None.
10. What will be the output of given code snippet?
-
public class Generic<T>
-
{
-
Stack<T> stk = new Stack<T>();
-
public void push(T obj)
-
{
-
stk.Push(obj);
-
}
-
public T pop()
-
{
-
T obj = stk.Pop();
-
return obj;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Generic<int> g = new Generic<int>();
-
g.push("Csharp");
-
Console.WriteLine(g.pop());
-
Console.ReadLine();
-
}
-
}
a) Compile time error
b) Csharp
c) 0
d) Run time error
Answer
Answer: b
Output : Csharp
11. What will be the output of the given code snippet?
-
public class Generic<T>
-
{
-
Stack<T> stk = new Stack<T>();
-
public void push(T obj)
-
{
-
stk.Push(obj);
-
}
-
public T pop()
-
{
-
T obj = stk.Pop();
-
return obj;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Generic<string> g = new Generic<string>();
-
g.push(30);
-
Console.WriteLine(g.pop());
-
Console.ReadLine();
-
}
-
}
a) 0
b) 30
c) Runtime Error
d) Compile time Error
Answer
Answer: b [Reason:] None.
Output : 30.
12. What will be the output of the given code snippet?
-
public class Generic<T>
-
{
-
Stack<T> stk = new Stack<T>();
-
public void push(T obj)
-
{
-
stk.Push(obj);
-
}
-
public T pop()
-
{
-
T obj = stk.Pop();
-
return obj;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Generic<string> g = new Generic<string>();
-
g.push("C++");
-
Console.WriteLine(g.pop() + " ");
-
Generic<int> g1 = new Generic<int>();
-
g1.push(20);
-
Console.WriteLine(g1.pop());
-
Console.ReadLine();
-
}
-
}
a) C++
b) 20
c) C++
20
d) 0
Answer
Answer: c [Reason:] None.
Output : C++
20
C# MCQ Set 5
1. What is an iterator?
a) a method
b) an operator
c) accessor
d) all of the mentioned
Answer
Answer: d [Reason:] An iterator is a method, operator, or accessor that returns the members of a set of objects, one member at a time, from start to finish.
2. What will be the output of the given code snippet?
-
class MyClass
-
{
-
char[] chrs = { 'A', 'B', 'C', 'D' };
-
public System.Collections.IEnumerator GetEnumerator()
-
{
-
foreach (char ch in chrs)
-
yield return ch;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
MyClass mc = new MyClass();
-
foreach (char ch in mc)
-
Console.Write(ch + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) Run time error
b) Compile time error
c) Code runs successfully prints nothing
d) Code runs successfully prints A, B, C, D
Answer
Answer: d [Reason:] None.
Output: A, B, C, D
3. Choose the correct statements about part of given code defined above?
-
public System.Collections.IEnumerator GetEnumerator()
-
{
-
foreach (char ch in chrs)
-
yield return ch;
-
}
a) Definition of iterator for MyClass
b) Implements the GetEnumerator() method defined by IEnumerable
c) The yield return statement returns the next object in the collection, which in this case is the next character in chrs
d) All of the mentioned
Answer
Answer: d [Reason:] This is the definition of the iterator for MyClass. The code implicitly implements the GetEnumerator() method defined by IEnumerable. At the body of the method. It contains a foreach loop that returns the elements in chrs. It does this through the use of a yield return statement. The yield return statement returns the next object in the collection, which in this case is the next character in chrs.
4. What does the yield return statement specify in above code snippet?
a) returns the output
b) returns the next object in the collection
c) both a & b
d) none of the mentioned
Answer
Answer: b [Reason:] The yield return statement returns the next object in the collection, which in this case is the next character in chrs in above code.
5. What does the given code snippet specify?
-
class MyClass
-
{
-
char chrs = 'A' ;
-
public IEnumerator GetEnumerator()
-
{
-
for (int i = 20; i >=0; --i)
-
yield return (char)((chrs + i));
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
MyClass mc = new MyClass();
-
foreach (char ch in mc)
-
Console.Write(ch + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) A B C D E F G H I J K L M N O P Q R S T U V
b) Run time error
c) U T S R Q P O N M L K J I H G F E D C B A
d) Compile successfully prints nothing
Answer
Answer: c [Reason:] None.
Output: U T S R Q P O N M L K J I H G F E D C B A
6. What will the given code snippet specify?
-
class MyClass
-
{
-
char chrs = 'A' ;
-
public IEnumerator GetEnumerator()
-
{
-
for (int i = 20; i >=0; --i)
-
if (i == 10) yield break;
-
yield return (char)((chrs + i));
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
MyClass mc = new MyClass();
-
foreach (char ch in mc)
-
Console.Write(ch + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) Code run successfully prints nothing
b) A B C D E F G H I J K L M N O P Q R S T U V
c) U T S R Q P O N M L
d) Compile time error
Answer
Answer: c [Reason:] The code to specify stoppage of the iterator using ‘yield break’ statement When this statement executes, the iterator signals that the end of the collection has been reached, which effectively stops the iterator.
Output: U T S R Q P O N M L
7. What will be the output of the code snippet?
-
class MyClass
-
{
-
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
-
public IEnumerator GetEnumerator()
-
{
-
for (int i = 0; i < 20; i++)
-
{
-
if (a[i] % 2 == 0)
-
yield return (int)(a[i]);
-
}
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
MyClass mc = new MyClass();
-
foreach (int i in mc)
-
Console.Write(i + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) prints nothing code run successfully
b) run time error
c) code runs successfully prints even number between 1 to 20
d) compile time error
Answer
Answer: c [Reason:] None.
Output: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
8. What will be the output of the code snippet?
-
class MyClass
-
{
-
char ch = 'A';
-
public IEnumerable MyItr(int end)
-
{
-
for (int i = 0 ;i < end ;i++)
-
yield return (char)(ch + i);
-
}
-
public IEnumerable MyItr(int begin, int end)
-
{
-
for (int i = begin ;i < end ;i++)
-
yield return (char)(ch + i);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
MyClass mc = new MyClass();
-
Console.WriteLine("Iterate the first 7 letters:");
-
foreach (char ch in mc.MyItr(7))
-
Console.Write(ch + " ");
-
Console.WriteLine("n");
-
Console.WriteLine("Iterate letters from F to L:");
-
foreach (char ch in mc.MyItr(7, 12))
-
Console.Write(ch + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) Iterate the first 7 letters:
A B C D E F G
Iterate letters from F to L:
G H I J K L
b) Iterate the first 7 letters:
A B C D E F G
Iterate letters from F to L:
H I J K L
c) Run time error
d) Compile time error
Answer
Answer: b [Reason:] None.
Output: Iterate the first 7 letters:
A B C D E F G
Iterate letters from F to L:
H I J K L
9. What will be the output of the code snippet?
-
class MyClass
-
{
-
char ch = 'A';
-
int e = 4;
-
int k = 9;
-
int z = 6;
-
public IEnumerator GetEnumerator()
-
{
-
for (int i = 0; i < 26; i++)
-
{
-
if (i == e*k /z) yield break;
-
yield return (int)(ch + i);
-
}
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
MyClass mc = new MyClass();
-
foreach(int ch in mc)
-
Console.Write(ch + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) Compile time error
b) Run time error
c) 65 66 67 68 69 70
d) Code run successfully prints nothing
Answer
Answer: c [Reason:] None.
Output: 65 66 67 68 69 70
10. What are the advantages of the named iterator?
a) They allow to pass arguments to the iterator that control what elements are obtained
b) This form of iterators can be overloaded
c) Both a & b
d) None of the mentioned
Answer
Answer: c [Reason:] By definition.