C# MCQ Set 1
1. Which of these classes contains only floating point functions?
a) Math
b) Process
c) System
d) Object
Answer
Answer: a [Reason:] Math class contains all the floating point functions that are used for geometry, trigonometry, as well as several general purpose methods. Example : sin(), cos(), exp(), sqrt() etc.
2. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
double x = 2.0;
-
double y = 3.0;
-
double z = Math.Pow( x, y );
-
Console.WriteLine(z);
-
Console.ReadLine();
-
}
-
}
a) 2.0
b) 4.0
c) 8
d) 8.0
Answer
Answer: c [Reason:] None.
Output :8
3. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
double x = 4.772;
-
double y = 4.76;
-
double z = Math.Max(x, y);
-
Console.WriteLine(z);
-
Console.ReadLine();
-
}
-
}
a) true
b) false
c) 4.772
d) 4.76
Answer
Answer: c [Reason:] None.
Output :4.772
4. What is the value of double constant ‘E’ defined in Math class?
a) approximately 3
b) approximately 3.14
c) approximately 2.72
d) approximately 0
Answer
Answer: c [Reason:] None.
5. What will be the output of the given code snippet?
-
public class A
-
{
-
public int x;
-
public int y;
-
public void display()
-
{
-
Console.WriteLine(x + " " + y);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
A obj1 = new A();
-
A obj2 = new A();
-
obj1.x = 1;
-
obj1.y = 2;
-
obj2 = obj1;
-
obj1.display();
-
obj2.display();
-
}
-
}
a) 1 2 0 0
b) 1 2 1 2
c) 0 0 0 0
d) Run time exception
Answer
Answer: b [Reason:] None.
Output : 1 2 1 2
6. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = { 1 };
-
var posNums = from n in nums
-
select Math.Pow(4 ,3);
-
Console.Write("The values in nums: ");
-
foreach (int i in posNums)
-
Console.Write(i + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) Run time error
b) 64
c) Compile time error
d) 81
Answer
Answer: b [Reason:] None.
Output :64
7. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
float x = 3.14F;
-
int y = (int)Math.Abs(x);
-
Console.WriteLine(y);
-
Console.ReadLine();
-
}
-
}
a) Compile time error
b) 3.14
c) 3
d) 4
Answer
Answer: c [Reason:] None.
Output :3
8. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int x = 5;
-
int y = (int)Math.Pow(x,2);
-
int z = (int)Math.Pow(y, 2);
-
Console.WriteLine(z);
-
Console.ReadLine();
-
}
-
}
a) 25
b) 625
c) Compile time error
d) Run time error
Answer
Answer: b [Reason:] y = 25 , z = 25*25 = 625
Output :625
9. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = {3 ,1 ,2 ,5 ,4};
-
var ltAvg = from n in nums
-
let x = nums.Average()
-
where n < x
-
select n;
-
Console.WriteLine("The average is " + nums.Average());
-
Console.ReadLine();
-
}
-
}
a) Run time error
b) 3
c) 5
d) Compile time error
Answer
Answer: b [Reason:] Built in method of maths class Avg() id used
Output :3
10. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int y = (int)Math.Max(4,2);
-
int z = (int)Math.Pow(y, 2);
-
Console.WriteLine(z);
-
Console.ReadLine();
-
}
-
}
a) 4
b) Compile time error
c) 16
d) 89
Answer
Answer: c [Reason:] Built in method of maths class, Max() is used to select maximum value among 4 and 2 and then y is squared using Pow() of math class and the value is stored in z.
Output :16
C# MCQ Set 2
1. Which of these data types can be used for a method having a return statement in it?
a) void
b) int
c) float
d) all of the mentioned
Answer
Answer: d [Reason:] None.
2. What is the process of defining more than one method in a class differentiated by parameters known as?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
Answer
Answer: b [Reason:] Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number. Example – int volume(int length, int width) & int volume(int length, int width, int height) can be used to calculate volume.
3. Which of these methods is executed first before execution of any other thing that takes place in a program?
a) main method
b) finalize method
c) static method
d) private method
Answer
Answer: c [Reason:] If a static method is present in the program then it will be executed first, then main will be executed.
4. Which of these can be used to differentiate two or more methods having same name?
a) Parameters data type
b) Number of parameters
c) Return type of method
d) All of the mentioned
Answer
Answer: d [Reason:] None.
5. Which of these data types can be used for a method having a return statement in it?
a) void
b) int
c) float
d) all of the mentioned
Answer
Answer: d [Reason:] None.
6. What will be the output of the program?
-
class box
-
{
-
int width;
-
int height;
-
int length;
-
int volume;
-
void volume(int height, int length, int width)
-
{
-
volume = width * height * length;
-
}
-
}
-
class Prameterized_method
-
{
-
public static void main(String args[])
-
{
-
box obj = new box();
-
obj.height = 1;
-
obj.length = 5;
-
obj.width = 5;
-
obj.volume(3, 2, 1);
-
Console.WriteLine(obj.volume);
-
Console.ReadLine();
-
}
-
}
a) 0
b) 1
c) 6
d) 25
Answer
Answer: c [Reason:] None.
Output :6
7. What will be the output of the given code snippet?
-
class equality
-
{
-
int x;
-
int y;
-
boolean isequal()
-
{
-
return(x == y);
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
equality obj = new equality();
-
obj.x = 5;
-
obj.y = 5;
-
Console.WriteLine(obj.isequal());
-
}
-
}
a) false
b) true
c) 0
d) 1
Answer
Answer: b [Reason:] None.
Output :true
8. What will be the output of the given code snippet?
-
class equality
-
{
-
public int x;
-
public int y;
-
public Boolean isequal()
-
{
-
return (x == y);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
equality obj = new equality();
-
obj.x = 5;
-
obj.y = 5;
-
Console.WriteLine(obj.isequal());
-
Console.ReadLine();
-
}
-
}
a) false
b) true
c) 0
d) 1
Answer
Answer: b [Reason:] None.
Output :true
9. What will be the output of the given code snippet?
-
class box
-
{
-
public int width;
-
public int height;
-
public int length;
-
public int volume1;
-
public void volume()
-
{
-
volume1 = width * height * length;
-
}
-
public void volume(int x)
-
{
-
volume1 = x;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
box obj = new box();
-
obj.height = 1;
-
obj.length = 5;
-
obj.width = 5;
-
obj.volume(5);
-
Console.WriteLine(obj.volume1);
-
Console.ReadLine();
-
}
-
}
a) 0
b) 5
c) 25
d) 26
Answer
Answer: b [Reason:] None.
Output : 5
10. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int x, y = 1;
-
x = 10;
-
if(x != 10 && x / Convert.ToInt32(0) == 0)
-
Console.WriteLine(y);
-
else
-
Console.WriteLine(++y);
-
Console.ReadLine();
-
}
-
}
a) 1
b) 2
c) Run time error
d) Compile time error
Answer
Answer: b [Reason:] Both conditions for if statements are failed and hence statement after else is executed.
Output : 2
C# MCQ Set 3
1. Select the type of multitasking methods that exist:
a) process based
b) thread based
c) only process
d) both process & thread based
Answer
Answer: d [Reason:] There are two distinct types of multitasking: process-based and thread-based.
2. Choose the correct statement about process-based multitasking:
a) A feature that allows our computer to run two or more programs concurrently
b) A program that acts as a small unit of code that can be dispatched by the scheduler
c) Only b
d) Both a & b
Answer
Answer: d [Reason:] The process-based multitasking is the feature that allows your computer to run two or more programs concurrently. For example, process-based multitasking allows you to run a word processor at the same time you are using a spreadsheet or browsing the Internet.In process-based multitasking, a program is the smallest unit of code that can be dispatched by the scheduler.
3. Choose the statements which indicate the differences between the thread based multitasking and process based multitasking:
a) Process-based multitasking handles the concurrent execution of programs
b) Process-based multitasking handles the concurrent execution of pieces of the same program
c) Thread-based multitasking handles the concurrent execution of programs
d) Thread-based multitasking deals with the concurrent execution of pieces of the same program
Answer
Answer: a [Reason:] The differences between process-based and thread-based multitasking can be summarized like this:Process-based multitasking handles the concurrent execution of programs. Thread-based multitasking deals with the concurrent execution of pieces of the same program.
4. What is the advantage of the multithreading program?
a) Enables to utilize the idle and executing time present in most programs
b) Enables to utilize the idle time present in most programs
c) Both a & b
d) Only b
Answer
Answer: d [Reason:] The principal advantage of multithreading is that it enables us to write very efficient programs because it lets us utilize the idle time that is present in most programs.
5. Select the two type of threads mentioned in the concept of multithreading:
a) foreground
b) background
c) only foreground
d) both foreground & background
Answer
Answer: d [Reason:] None.
6. Number of threads that exists for each of the processes that occurs in the program:
a) atmost 1
b) atleast 1
c) only 1
d) both a & c
Answer
Answer: d [Reason:] All processes have at least one thread for execution, which is usually called the main thread because it is the primary thread that is executed when our program begins.From the main thread, we can create other threads.
7. Choose the namespace which supports multithreading programming:
a) System.net
b) System.Linq
c) System.Threading
d) All of the mentioned
Answer
Answer: c [Reason:] The classes that support multithreaded programming are defined in the System.Threading namespace. Thus, you will usually include this statement at the start of any multithreaded program.
8. What does the given code snippet specify?
-
public Thread(ThreadStart start)
a) Defines a thread
b) Declaration of a thread constructor
c) Only a
d) Only a & b
Answer
Answer: d [Reason:] To create a thread, instantiate an object of type Thread, which is a class defined in System.Threading. The simplest Thread constructor is shown here:
public Thread(ThreadStart start)
Here, start specifies the method that will be called to begin execution of the thread.In other words, it specifies the thread’s entry point.
9. Which of these classes is used to make a thread?
a) String
b) System
c) Thread
d) Runnable
Answer
Answer: c [Reason:] The multithreading system is built upon the Thread class, which encapsulates a thread of execution. The Thread class is sealed, which means that it cannot be inherited. Thread defines several methods and properties that help manage threads.
10. On call of which type of method the new created thread will not start executing?
a) Begin()
b) Start()
c) New()
d) All of the mentioned
Answer
Answer: b [Reason:] Once created, the new thread will not start running until you call its Start() method,which is defined by Thread.
11. Which of these method of Thread class is used to Suspend a thread for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()
Answer
Answer: a [Reason:] None.
C# MCQ Set 4
1. What exception is thrown if the URI format is invalid?
a) URLNotFound
b) URLSourceNotFound
c) MalformedURLException
d) UriFormatException
Answer
Answer: d [Reason:] None.
2. What exception is thrown if the protocol supported by URI prefix is invalid?
a) URLNotFound
b) URLSourceNotFound
c) UriFormatException
d) NotSupportedException
Answer
Answer :d [Reason:] None.
3. What exception is thrown if the user does not have a proper authorization?
a) URLNotFound
b) URLSourceNotFound
c) System.Security.SecurityException
d) All of the mentioned
Answer
Answer: c [Reason:] None.
4. Choose the exceptions generated by the Create() method defined by WebRequest:
a) NotSupportedException
b) UriFormatException
c) System.Security.SecurityException
d) All of the mentioned
Answer
Answer: d [Reason:] None.
5. Choose the exceptions generated by the GetReponse() method defined by WebRequest:
a) InvalidOperationException
b) ProtocolViolationException
c) WebException
d) All of the mentioned
Answer
Answer: d [Reason:] By definition.
6. Select the properties related to the network errors generated by WebException:
a) response
b) get
c) set
d) none of the mentioned
Answer
Answer: a [Reason:] WebException has two properties that relate to network errors:
Response and Status.
We can obtain a reference to the WebResponse object inside an exception handler through the Response property. For the HTTP protocol, this object describes the error. It is defined like this:
public WebResponse Response { get; }
When an error occurs, we can use the Status property of WebException to find out what went wrong. It is defined like this:
public WebExceptionStatus Status {get; }
7. Which of these classes is used for operating on the request from the client to the server?
a) http
b) httpDecoder
c) httpConnection
d) httpd
Answer
Answer: d [Reason:] None.
8. Choose the exceptions generated by the GetResponseStream() method defined by WebRequest:
a) ProtocolViolationException
b) ObjectDisposedException
c) IOException
d) All of the mentioned
Answer
Answer: d [Reason:] None.
9. Which of these classes is used to create servers that listen to either local or remote client programs?
a) httpServer
b) ServerSockets
c) MimeHeader
d) HttpResponse
Answer
Answer: b [Reason:] None.
10. Which of these methods gives the full URL of an URL object?
a) fullHost()
b) getHost()
c) AbsoluteUri
d) toExternalForm()
Answer
Answer: c [Reason:] None.
C# MCQ Set 5
1. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
string[] strs = {"alpha", "beta", "gamma"};
-
var chrs = from str in strs
-
let chrArray = str.ToCharArray()
-
from ch in chrArray
-
orderby ch
-
select ch;
-
Console.WriteLine("The individual characters in sorted order:");
-
foreach (char c in chrs)
-
Console.Write(c + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
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
Answer
Answer: b [Reason:] None.
Output: a a a a a b e g h l m m p t
2.What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = { 1, -2, 3, 0, -4, 5 };
-
var posNums = nums.Where(n => n > 0).Select(r => r*2).OrderByDescending(r=>r);
-
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 prints multiple of 2
d) compile time error
Answer
Answer: c [Reason:] 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: 10 6 2
3. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = {3, 1, 2, 5, 4};
-
var ltAvg = from n in nums
-
let x = nums.Average()
-
where n < x
-
select n;
-
Console.WriteLine("The average is " + nums.Average());
-
Console.ReadLine();
-
}
-
}
a) Run time error
b) 3
c) 5
d) Compile time error
Answer
Answer: b [Reason:] Built in method Avg() is used
Output: 3
4. What will be the output of the given code snippet?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Expression<Func<int, int, bool>>
-
IsFactorExp = (n, d) => (d != 0) ? (n % d) == 0 : false;
-
Func<int, int, bool> IsFactor = IsFactorExp.Compile();
-
if (IsFactor(10, 5))
-
Console.WriteLine("5 is a factor of 10.");
-
if (!IsFactor(343, 7))
-
Console.WriteLine("7 is not a factor of 10.");
-
Console.ReadLine();
-
}
-
}
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
Answer
Answer: d [Reason:] 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
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
Answer
Answer: b [Reason:] By definition.
6. For the given set of codes, which query will work according to the set of code?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = { 1, -2, 3, 0, -4, 5 };
-
int len = /*_________________ */
-
Console.WriteLine("The number of positive values in nums: " + len);
-
Console.ReadLine();
-
}
-
}
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 b & c
Answer
Answer: c [Reason:] None.
Output: int len = (from n in nums where n > 0
select n).Count();
7. For the given set of code, what does the output represent?
-
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;
-
int len = posNums.Count();
-
Console.WriteLine(len);
-
Console.ReadLine();
-
}
-
}
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
Answer
Answer: c [Reason:] None.
Output: 3
8. For the given set of codes, what is the output?
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
int[] nums = { 1, -2, 3, 0, -4, 5 };
-
var posNums = nums.Where(n => n < 10).Select(r => r%3);
-
Console.Write("The values in nums: ");
-
foreach (int i in posNums) Console.Write(i + " ");
-
Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
a) Compile time error
b) Run time error
c) 1 -2 0 0 -1 2
d) 2 -1 0 0 -2 1
Answer
Answer: c [Reason:] 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