1. The ‘ref’ keyword can be used with which among the following?
a) Static function/subroutine
b) Static data
c) Instance function/subroutine
d) All of the mentioned
View Answer
Answer: a
Explanation: None.
2. To implement delegates, the necessary condition is?
a) class declaration
b) inheritance
c) runtime polymorphism
d) exceptions
View Answer
Answer: a
Explanation: None.
3. Suppose a Generic class called as SortObjects is to be made capable of
sorting objects of any type(integer, single, byte etc). Then, which of the
following programming constructs is able to implement the comparison
function?
a) interface
b) encapsulation
c) delegate
d) attribute
View Answer
Answer: c
Explanation: None.
advertisement
4. To generate a simple notification for an object in runtime, the programming
construct to be used for implementing this idea?
a) namespace
b) interface
c) delegate
d) attribute
View Answer
Answer: c
Explanation: None.
5. Choose the incorrect statement among the following about the delegate?
a) delegates are of reference types
b) delegates are object oriented
c) delegates are type safe
d) none of the mentioned
View Answer
Answer: d
Explanation: None.
6. Which among the following is the correct statement about delegate
declaration?
delegate void del(int i);
a) on declaring the delegate, a class called del is created
b) the del class is derived from the MulticastDelegate class
c) the del class will contain a one argument constructor and an invoke()
method
d) all of the mentioned
View Answer
Answer: d
Explanation: None.
7. Which of the following is an incorrect statement about delegate?
a) a single delegate can invoke more than one method
b) delegates could be shared
c) delegates are type safe wrappers for function pointers
d) delegate is a value type
View Answer
Answer: d
Explanation: In C#, a delegate is a reference type instead of a value type as it
stores a reference to a data/method.
8. Which among the following differentiates a delegate in C#.NET from a
conventional function pointer in other languages?
a) delegates in C#.NET represent a new type in the Common Type System
b) delegates allows static as well as instance methods to be invoked
c) delegates are type safe and secure
d) none of the mentioned
View Answer
Answer: d
Explanation: None.
9. Choose the incorrect statement about delegates?
a) delegates are not type safe
b) delegates can be used to implement callback notification
c) delegate is a user defined type
d) delegates permits execution of a method in an asynchronous manner
View Answer
Answer: a
Explanation: None.
10. Which of the following statements is correct about a delegate?
a) inheritance is a prerequisite for using delegates
b) delegates are not type safe
c) delegates provides wrappers for function pointers
d) none of the mentioned
View Answer
Answer: c
Explanation: None.
1. Choose the correct way to call subroutine fun() of the sample class?
1. class a
2. {
3. public void x(int p, double k)
4. {
5. Console.WriteLine("k : csharp!");
6. }
7. }
a)
advertisement
delegate void del(int i);
x s = new x();
del d = new del(ref s.x);
d(8, 2.2f);
b)
delegate void del(int p, double k);
del d;
x s = new x();
d = new del(ref s.x);
d(8, 2.2f);
c)
x s = new x();
delegate void d = new del(ref x);
d(8, 2.2f);
d) all of the mentioned
View Answer
Answer: b
Explanation: None.
2. Which of the following is the correct way to call the function abc() of the
given class in the following C# code?
1. class csharp
2. {
3. public int abc(int a)
4. {
5. Console.WriteLine("A:Just do it!");
6. return 0;
7. }
8. }
a)
delegate void del(int a);
csharp s = new csharp();
del d = new del(ref s.abc);
d(10);
b)
csharp s = new csharp();
delegate void d = new del(ref abc);
d(10);
c)
delegate int del(int a);
del d;
csharp s = new csharp();
d = new del(ref s.fun);
d(10);
d) none of the mentioned
View Answer
Answer: c
Explanation: None.
3. Which of the following is the correct way to call the subroutine function
abc() of the given class in the following C# code?
1. class csharp
2. {
3. void abc()
4. {
5. console.writeline("A:Just do it!");
6. }
7. }
a)
csharp c = new csharp();
delegate void d = new del(ref abc);
d();
b)
delegate void del();
del d;
csharp s = new csharp();
d = new del(ref s.abc);
d();
c)
csharp s = new csharp();
delegate void del = new delegate(ref abc);
del();
d) None of the mentioned
View Answer
Answer: b
Explanation: None.
4. What will be the output of the following C# code snippet?
1. {
2. delegate void A(ref string str);
3. class sample
4. {
5. public static void fun( ref string a)
6. {
7. a = a.Substring( 7, a.Length - 7);
8. }
9. }
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. A str1;
15. string str = "Test Your C#.net skills";
16. str1 = sample.fun;
17. str1(ref str);
18. Console.WriteLine(str);
19. }
20. }
21.}
a) Test Your
b) ur C#.NET
c) ur C#.NET Skills
d) None of the mentioned
View Answer
Answer: c
Explanation: None.
5. What will be the output of the following C# code snippet?
1. using System;
2.
3. delegate string F(string str);
4. class sample
5. {
6. public static string fun(string a)
7. {
8. return a.Replace(',','-');
9. }
10. }
11. class Program
12. {
13. static void Main(string[] args)
14. {
15. F str1 = new F(sample.fun);
16. string str = str1("Test Your c#.NET skills");
17. Console.WriteLine(str);
18. }
19. }
a) Test Your
b) Test Your c#.NET skills
c) ur C#.NET Skills
d) None of the mentioned
View Answer
Answer: b
Explanation: None.
Output:
Test Your c#.NET skills
6. Choose the statements which makes delegate in C#.NET different from a
normal class?
a) Delegates in C#.NET is a base class for all delegates type
b) Delegates created in C#.NET are further not allowed to derive from the
delegate types that are created
c) Only system and compilers can derive explicitly from the Delegate or
MulticasteDelegate class
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
7. Which of the following are the correct statements about delegates?
a) Delegates can be used to implement callback notification
b) Delegates permit execution of a method on a secondary thread in an
asynchronous manner
c) Delegate is a user defined type
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
8. What will be the output of the following C# code?
1. {
2. delegate string f(string str);
3. class sample
4. {
5. public static string fun(string a)
6. {
7. return a.Replace('k', 'o');
8. }
9. }
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. f str1 = new f(sample.fun);
15. string str = str1("Test Ykur C#.NET Skills");
16. Console.WriteLine(str);
17. Console.ReadLine();
18. }
19. }
20.}
a) Test Ykur C#.NET Skills
b) Test Ykour C#.NET Skills
c) Test Your C#.NET Skills
d) Test ur C#.NET Skills
View Answer
9. Incorrect statements about delegates are?
a) Delegates are reference types
b) Delegates are object oriented
c) Delegates are type safe
d) Only one method can be called using a delegate
View Answer
Answer: d
Explanation: None.
10. Select the modifiers which control the accessibility of the delegate?
a) new
b) protected
c) public
d) all of the mentioned
View Answer
Answer: d
Explanation: By definition
1. What is meant by the term generics?
a) parameterized types
b) class
c) structure
d) interface
View Answer
Answer: a
Explanation: The term generics means parameterized types. Parameterized
types are important because they enable us to create classes, structures,
interfaces, methods, and delegates in which, the type of data upon which they
operate is specified as a parameter.
2. Are generics in C# are same as the generics in java and templates in C++?
a) Yes
b) No
c) May be
d) None of the mentioned
View Answer
Answer: b
Explanation: Although C# generics are similar to templates in C++ and generics
in Java, they are not the same as either. In fact, there are some fundamental
differences among these three approaches to generics.
3. Choose the advantages of using generics?
a) Generics facilitate type safety
b) Generics facilitate improved performance and reduced code
c) Generics promote the usage of parameterized types
d) All of the mentioned
View Answer
Answer: d
Explanation: By definition of generics.
advertisement
4. What does the following C# code block defines?
1. class Gen<T>
2. {
3. T ob;
4. }
a) Generics class declaration
b) Generic constructor declaration
c) A simple class declaration
d) All of the mentioned
View Answer
Answer: a
Explanation: class Gen<T> This defines the generics declaration where ‘T’ is the
name of the type parameter. This parameter is used as a placeholder for the
actual type that will be specified when a Gen object is created. Gen is a generic
class. T is used to declare a variable called ‘ob’.
5. What does the following C# code set defines?
1. public Gen(T o)
2. {
3. ob = o;
4. }
a) Generics class Declaration
b) Declaration of variable
c) Generic constructor declaration
d) All of the mentioned
View Answer
Answer: c
Explanation: None.
6. Select the type argument of an open constructed type?
a) Gen<int>
b) Gen<T>
c) Gen<>
d) None of the mentioned
View Answer
Answer: c
Explanation: A generic type, such as Gen<T>, is an abstraction. In C#
terminology, a construct such as Gen<T> is called an open constructed type,
because the type parameter T (rather than an actual type, such as int) is
specified.
7. Which among the given classes is present in
System.Collection.Generic.namespace?
a) Stack
b) Tree
c) Sorted Array
d) All of the mentioned
View Answer
Answer: a
Explanation: None.
8. Which of these is a correct way of defining generic method?
a) name(T1, T2, …, Tn) { /* … */ }
b) public name { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) name{T1, T2, …, Tn} { /* … */ }
View Answer
Answer: b
Explanation: The syntax for a generic method includes a type parameter, inside
angle brackets, and appears before the method’s return type. For static generic
methods, the type parameter section must appear before the method’s return
type.
9. Which of these type parameters is used for generic methods to return and
accept any type of object?
a) K
b) N
c) T
d) V
View Answer
Answer: c
Explanation: T is used for type, A type variable can be any non-primitive type
you specify: any class type, any interface type, any array type, or even another
type variable.
10. Choose the correct way to call subroutine fun() of the sample class?
1. class a
2. {
3. public void x(int p, double k)
4. {
5. Console.WriteLine("k : csharp!");
6. }
7. }
a)
delegate void del(int i);
x s = new x();
del d = new del(ref s.x);
d(8, 2.2f);
b)
delegate void del(int p, double k);
del d;
x s = new x();
d = new del(ref s.x);
d(8, 2.2f);
c)
x s = new x();
delegate void d = new del(ref x);
d(8, 2.2f);
d) all of the mentioned
View Answer
Answer: b
Explanation: None.
11. What will be the output of the following C# code snippet?
1. public class Generic<T>
2. {
3. Stack<T> stk = new Stack<T>();
4. public void push(T obj)
5. {
6. stk.Push(obj);
7. }
8. public T pop()
9. {
10. T obj = stk.Pop();
11. return obj;
12. }
13.}
14.class Program
15.{
16. static void Main(string[] args)
17. {
18. Generic<string> g = new Generic<string>();
19. g.push(40);
20. Console.WriteLine(g.pop());
21. Console.ReadLine();
22. }
23.}
a) 0
b) Runtime Error
c) 40
d) Compile time Error
View Answer
Answer: c
Explanation: None.
Output :
40
12. What will be the output of the following C# code snippet?
1. public class Generic<T>
2. {
3. Stack<T> stk = new Stack<T>();
4. public void push(T obj)
5. {
6. stk.Push(obj);
7. }
8. public T pop()
9. {
10. T obj = stk.Pop();
11. return obj;
12. }
13.}
14.class Program
15.{
16. static void Main(string[] args)
17. {
18. Generic<int> g = new Generic<int>();
19. g.push("Csharp");
20. Console.WriteLine(g.pop());
21. Console.ReadLine();
22. }
23.}
a) Compile time error
b) Csharp
c) 0
d) Run time error
View Answer
Answer: b
Explanation: None.
Output :
1. In the following C# code, which of the following statement is not correct?
1. public class MyContainer<T> where T: class, IComparable
2. {
3. /* insert code here */
4. }
a) Class MyContainer requires that its type argument must implement
Icomparable interface
b) There are multiple constraints on type argument to MyContainer class
c) Class MyContainer requires that its type argument must be a reference type
(class)
d) Compiler will report an error
View Answer
Answer: d
Explanation: None.
advertisement
2. In the following C# code, which statements are perfectly valid?
1. public class Csharp
2. {
3. public void subject<S>(S arg)
4. {
5. Console.WriteLine(arg);
6. }
7. }
8. class Program
9. {
10. static Void Main(string[] args)
11. {
12. Csharp c = new Csharp();
13. c.subject("hi");
14. c.subject(20);
15. }
16.}
a) Run time exception error
b) Compile time error
c) Code runs successfully and prints required output
d) None of the mentioned
View Answer
Answer: c
Explanation: None.
Output :
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!
hi
20
3. Which of the 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
View Answer
Answer: a
Explanation: None.
4. Which statement is valid for the following C# code snippet?
1. public class Generic<T>
2. {
3. public T Field;
4. }
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. Generic<String> g = new Generic<String>();
10. g.Field = "Hi";
11. Console.WriteLine(g.Field);
12. }
13.}
a) Compile time error
b) Generic being a keyword cannot be used as a class name
c) Runtime error
d) Code runs successfully
View Answer
Answer: d
Explanation: None.
Output :
Hello
5. What will be the output of the following C# code?
1. public class Generic<T>
2. {
3. public T Field;
4. }
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. Generic<int> g2 = new Generic<int>();
10. Generic<int> g3 = new Generic<int>();
11. g2.Field = 8;
12. g3.Field = 4;
13. if (g2.Field % g3.Field == 0)
14. {
15. Console.WriteLine("A");
16. }
17. else
18. Console.WriteLine("Prints nothing:");
19. Console.ReadLine();
20. }
21. }
a) Compile time error
b) A
c) Run time error
d) Code runs successfully but prints nothing
View Answer
Answer: b
Explanation: None.
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
View Answer
Answer: b
Explanation: None.
7. In the following C# code, which of the following statements are perfectly
valid?
1. public class MyContainer<T> where T: IComparable
2. {
3. /* insert code here */
4. }
a) Class MyConatiner requires that its type argument must implement
Icomparable interface
b) There are multiple constraints on type argument to MyContainer class
c) Type argument of class MyContainer should be Icomparable
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
8. Which of the following statements are valid in the following C# code
snippet?
1. public class Generic<T>
2. {
3. public T Field;
4. public void testSub()
5. {
6. T i = Field + 1;
7. }
8. }
9. class Program
10.{
11. static void Main(string[] args)
12. {
13. Generic<int>g = new Generic<int>();
14. g.testSub();
15. }
16.}
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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: a
Explanation: None.
10. What will be the output of the following C# code snippet?
1. public class Generic<T>
2. {
3. Stack<T> stk = new Stack<T>();
4. public void push(T obj)
5. {
6. stk.Push(obj);
7. }
8. public T pop()
9. {
10. T obj = stk.Pop();
11. return obj;
12. }
13.}
14.class Program
15.{
16. static void Main(string[] args)
17. {
18. Generic<int> g = new Generic<int>();
19. g.push("Csharp");
20. Console.WriteLine(g.pop());
21. Console.ReadLine();
22. }
23.}
a) Compile time error
b) Csharp
c) 0
d) Run time error
View Answer
Answer: b
Explanation: None.
Output :
Csharp
11. What will be the output of the following C# code snippet?
1. public class Generic<T>
2. {
3. Stack<T> stk = new Stack<T>();
4. public void push(T obj)
5. {
6. stk.Push(obj);
7. }
8. public T pop()
9. {
10. T obj = stk.Pop();
11. return obj;
12. }
13.}
14.class Program
15.{
16. static void Main(string[] args)
17. {
18. Generic<string> g = new Generic<string>();
19. g.push(30);
20. Console.WriteLine(g.pop());
21. Console.ReadLine();
22. }
23.}
a) 0
b) 30
c) Runtime Error
d) Compile time Error
View Answer
Answer: b
Explanation: None.
Output :
30.
12. What will be the output of the following C# code snippet?
1. public class Generic<T>
2. {
3. Stack<T> stk = new Stack<T>();
4. public void push(T obj)
5. {
6. stk.Push(obj);
7. }
8. public T pop()
9. {
10. T obj = stk.Pop();
11. return obj;
12. }
13.}
14.class Program
15.{
16. static void Main(string[] args)
17. {
18. Generic<string> g = new Generic<string>();
19. g.push("C++");
20. Console.WriteLine(g.pop() + " ");
21. Generic<int> g1 = new Generic<int>();
22. g1.push(20);
23. Console.WriteLine(g1.pop());
24. Console.ReadLine();
25. }
26.}
a) C++
b) 20
c)
C++
20
d) 0
View Answer
Answer: c
Explanation: None.
Output :
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
View Answer
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
View Answer
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 System.Collections & System.Collections.Generic
d) None of the mentioned
View Answer
Answer: a
Explanation: By definition.
advertisement
4. Can we use linq to query against a DataTable?
a) Yes
b) No
c) Either Yes or No
d) None of the mentioned
View Answer
Answer: b
Explanation: We cannot use query against the DataTable’s Rows collection,
since DataRowCollection doesn’t implement IEnumerable<T>. We need to use
the AsEnumerable() extension for DataTable. As an example:
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
5. What will be the output of the following C# code snippet?
Note: Join free Sanfoundry classes at Telegram or Youtube
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
View Answer
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
View Answer
Answer: c
Explanation: By definition.
7. What will be the output of the following C# 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
View Answer
Answer: c
Explanation: -2, 0, -4
8. What will be the output of the following C# 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
View Answer
Answer: c
Explanation: None.
Output :
5, 3, 1, 0, -2, -4
9. What will be the output of the following C# 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
View Answer
Answer: b
Explanation: None.
Output :
4, 3, 5
10. What will be the output of the following C# 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
View Answer
Answer: c
Explanation: None.
Output :
1. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. string[] strs = {"alpha", "beta", "gamma"};
6. var chrs = from str in strs
7. let chrArray = str.ToCharArray()
8. from ch in chrArray
9. orderby ch
10. select ch;
11. Console.WriteLine("The individual characters in sorted order:");
12. foreach (char c in chrs)
13. Console.Write(c + " ");
14. Console.WriteLine();
15. Console.ReadLine();
16. }
17.}
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
View Answer
Answer: b
Explanation: None.
Output:
aaaaabeghlmmpt
2. What will be the output of the following C# code snippet?
advertisement
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. int[] nums = { 1, -2, 3, 0, -4, 5 };
6. var posNums = nums.Where(n => n > 0).Select(r => r*2).
7. OrderByDescending(r=>r);
8. Console.Write("The positive values in nums: ");
9. foreach(int i in posNums)
10. 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 prints multiple of 2
d) compile time error
View Answer
Answer: c
Explanation: 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:
Note: Join free Sanfoundry classes at Telegram or Youtube
10 6 2
3. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. int[] nums = {3, 1, 2, 5, 4};
6. var ltAvg = from n in nums
7. let x = nums.Average()
8. where n < x
9. select n;
10. Console.WriteLine("The average is " + nums.Average());
11. Console.ReadLine();
12. }
13. }
a) Run time error
b) 3
c) 5
d) Compile time error
View Answer
Answer: b
Explanation: Built in method Avg() is used
Output:
3
4. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. Expression<Func<int, int, bool>>
6. IsFactorExp = (n, d) => (d != 0) ? (n % d) == 0 : false;
7. Func<int, int, bool> IsFactor = IsFactorExp.Compile();
8. if (IsFactor(10, 5))
9. Console.WriteLine("5 is a factor of 10.");
10. if (!IsFactor(343, 7))
11. Console.WriteLine("7 is not a factor of 10.");
12. Console.ReadLine();
13. }
14.}
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
View Answer
Answer: d
Explanation: 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<Func<int, int, bool>>
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
View Answer
Answer: b
Explanation: By definition.
6. In the following C# code, which query will work according to the set of code?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. int[] nums = { 1, -2, 3, 0, -4, 5 };
6. int len = /*_________________ */
7. Console.WriteLine("The number of positive values in nums: " + len);
8. Console.ReadLine();
9. }
10.}
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 “from n in nums where n > 0 select n.Count()” & “(from n in nums
where n > 0 select n).Count();”
View Answer
Answer: c
Explanation: None.
Output:
int len = (from n in nums where n > 0
select n).Count();
7. In the following C# code, what does the output represent?
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. int len = posNums.Count();
10. Console.WriteLine(len);
11. Console.ReadLine();
12. }
13.}
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
View Answer
Answer: c
Explanation: None.
Output:
3
8. What will be the output of the following C# 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 = nums.Where(n => n < 10).Select(r => r%3);
7. Console.Write("The values in nums: ");
8. foreach (int i in posNums) Console.Write(i + " ");
9. Console.WriteLine();
10. Console.ReadLine();
11. }
12. }
a) Compile time error
b) Run time error
c) 1 -2 0 0 -1 2
d) 2 -1 0 0 -2 1
View Answer
Answer: c
Explanation: 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
9. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. string[] strs = { ".com", ".net", "facebook.com", "google.net",
6. "test", "netflix.net", "hsNameD.com" };
7. var netAddrs = from addr in strs
8. where addr.Length > 4 && addr.EndsWith(".net",
9. StringComparison.Ordinal)
10. select addr;
11. foreach (var str in netAddrs) Console.WriteLine(str);
12. Console.ReadLine();
13. }
14.}
a) Compile time error
b) Run time error
c)
facebook.com
netflix.net
google.net
d)
google.net
netflix.net
View Answer
Answer: d
Explanation: Searches for the string which ends with .net.
Output:
google.net
netflix.net
10. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5.
6. int[] nums = { 1, -2, -3, 5 };
7. var posNums = from n in nums
8. orderby n descending
9. select n*4 / 2;
10. Console.Write("The values in nums: ");
11. foreach (int i in posNums) Console.Write(i + " ");
12. Console.WriteLine();
13. Console.ReadLine();
14. }
15. }
a) 10 2 -4 -6
b) 5 1 -2 -3
c) 1 5 -2 -3
d) Run time error
View Answer
b) Answer: a
Explanation: None.
Output:
10 2 -4 -6
1. Select the class which is the base class for all arrays in C#?
a) Array
b) Text
c) arrays
d) Both Array & Text
View Answer
Answer: a
Explanation: None.
2. Select the interfaces implemented by array class.
a) ICloneable, ICollection
b) IEnumerable, IStructuralComparable, IStructuralEquatable
c) ICloneable, ICollection, IList
d) Only IEnumerable, IStructuralComparable, IStructuralEquatable &
ICloneable, ICollection, IList
View Answer
Answer: d
Explanation: None.
3. Choose the correct statement about the IComparer interface in C#?
a) The IComparer interface is in System.Collections
b) It defines a method called Compare(), which compares the values of
two objects
c) Both The IComparer interface is in System.Collections & It defines a
method called Compare(), which compares the values of two objects
d) None of the mentioned
View Answer
Answer: c
Explanation: The IComparer interface is in System.Collections. It defines
a method called Compare(), which compares the values of two objects. It
is shown here: int Compare(object x, object y). It returns greater than
zero if x is greater than y, less than zero if x is less than y, and zero if the
two values are equal.
Advertisement
4. Choose the correct statement about the IComparer<T> interface in
C#?
a) The IComparer<T> is in System.Collections.Generic
b) It defines a generic form of Compare()
c) Only The IComparer<T> is in System.Collections.Generic
d) Both The IComparer is in System.Collections.Generic & It defines a
generic form of Compare()
View Answer
Answer: d
Explanation: IComparer<T> is in System.Collections.Generic. It defines a
generic form of Compare(), which is shown here:
int Compare(T x, T y). It works the same as its non-generic relative:
returning greater than zero if x is greater than
y, less than zero if x is less than y, and zero if the two values are equal.
5. What does the following property defined in the array class defines in
C#?
Note: Join free Sanfoundry classes at Telegram or Youtube
1. public bool IsReadOnly { get; }
a) a property is read only by nature
b) property is true if the array object is read only and false otherwise
c) value is false for arrays
d) all of the mentioned
View Answer
Answer: d
Explanation: A read-only property that is true if the Array object is read-
only and false if it is not. This value is false for arrays.
6. What does the following property define in C#?
1. public static int BinarySearch<T>(T[] array, int index, int length, T
value)
a) Searches a portion of the array specified by array for the value
specified by value
b) The search begins at the index specified by index and is restricted to
length elements. Returns the index of the first match
c) If value is not found, returns a zero value
d) All of the mentioned
View Answer
Answer: b
Explanation: Searches a portion of the array specified by array for the
value specified by value. The search begins at the index specified by
index and is restricted to length elements. Returns the index of the first
match. If the value is not found, returns a negative value. The array
should be sorted and one-dimensional.
7. What will be the output of the following C# code snippet?
1. static void Main(string[] args)
2. {
3. int[] nums = { 5, 4, 6, 3, 14, 9, 8, 17, 1, 24, -1, 0 };
4. Array.Sort(nums);
5. int idx = Array.BinarySearch(nums, 14);
6. if (idx == 9)
7. {
8. Console.WriteLine(Convert.ToBoolean(1));
9. }
10. else
11. {
12. Console.WriteLine(Convert.ToBoolean(0));
13. }
14. Console.WriteLine("Index of 14 is " + idx);
15. Console.ReadLine();
16.}
a)
True
0
b) Run time error
c)
True 9
c) None of the mentioned
View Answer
8. What will be the output of the following C# code snippet?
1. static void Main(string[] args)
2. {
3. string[] strings = {"beta", "alpha", "gamma"};
4. Console.WriteLine("Array elements: ");
5. DisplayArray(strings);
6. Array.Reverse(strings);
7. Console.WriteLine("Array elements now: ");
8. DisplayArray(strings);
9. Console.ReadLine();
10. }
11. public static void DisplayArray(Array array)
12. {
13. foreach (object o in array)
14. {
15. Console.Write("{0} ", o);
16. }
17. Console.WriteLine();
18. }
a)
Array elements:
beta alpha gamma
Array elements now:
ammag ahpla ateb
b)
Array elements:
beta alpha gamma
Array elements now:
gamma beta alpha
c)
Array elements:
beta alpha gamma
Array elements now:
gamma alpha beta
d) None of the mentioned
View Answer
Answer: c
Explanation: ‘Reverse()’ a built in method to reverse an array of string
defined in array class is used.
Output:
Array elements:
beta alpha gamma
Array elements now:
gamma alpha beta
9. Which among the following is the wrong way to define and initialize an
array of 4 integers?
a) int[] a = {25, 30, 40, 5}
b)
int[] a;
a = new int[3]
a[0] = 25
a[1] = 30
a[2] = 40
a[3] = 5
c)
int[] a
a = new int[4]{25, 30, 40, 5}
d)
int[] a
a = new int[4]
a[0] = 25
a[1] = 30
a[2] = 40
a[3] = 5
View Answer
Answer: b
Explanation: None.
10. Which method will be used to copy content from one array to
another array?
a) Copy()
b) copy()
c) Both Copy() & copy()
d) None of the mentioned
View Answer
Answer: a
Explanation: Copy() is a built-in method of array class used to copy the
elements from one array to another array
11. What will be the output of the following C# code snippet?
1. static void Main()
2. {
3. int[] nums = { 1, 2, 3, 4, 5 };
4. Console.Write("Original order: ");
5. foreach(int i in nums)
6. Console.Write(i + " ");
7. Array.Reverse(nums);
8. Console.Write("Reversed order: ");
9. foreach(int i in nums)
10. Console.Write(i + " ");
11. Console.WriteLine();
12. }
a) Run time error
b) 5, 4, 3, 2, 1
c) Compile time error
d) None of the mentioned
View Answer
Answer: b
Explanation: Reverse built in method() of array class is used to reverse
the given array.
Output:
5, 4, 3, 2, 1
1. Which mechanism among the following helps in identifying a type
during the execution of a program?
a) Reflection
b) Runtime type ID
c) Both Reflection & Runtime type ID
d) None of the mentioned
View Answer
Answer: b
Explanation: Runtime type ID is the mechanism that lets identify a type
during the execution of a program. Using Runtime type ID we can
construct and use objects at runtime.
2. Select the statement which are correct about RTTI(Runtime type
identification)?
a) It allows the type of an object to be determined during program
execution
b) It tells what type of object is being referred to by a base class
reference determined by RTTI
c) Helps in prevention of an invalid cast exception in advance
d) All of the mentioned
View Answer
Answer: d
Explanation: Runtime type identification (RTTI) allows the type of an
object to be determined during program execution. RTTI is useful for
many reasons. For example, we can discover precisely what type of
object is being referred to by a base-class reference. Another use of RTTI
is to test in advance whether a cast will succeed, preventing an invalid
cast exception.
3. Select the Keyword which supports the run time type identification?
a) is, as
b) as, typeof
c) Both is, as & as, typeof
d) Only is, as
View Answer
Answer: c
Explanation: None.
Advertisement
4. What does the following C# code signify?
expr is type
a) Determines the type of an object
b) a simple deceleration
c) Both Determines the type of an object & a simple deceleration
d) None of the mentioned
View Answer
Answer: a
Explanation: The given expression determines the type of an object using
the ‘is’ operator. Here, expr is an expression that describes an object
whose type is being tested against type. If the type of expr is the same
as, or compatible with, type, then the outcome of this operation is true.
Otherwise, it is false.
5. What will be the output of the following C# code snippet?
1. class B { }
2. class A : B { }
3. class Program
4. {
5. static void Main(string[] args)
6. {
7. A a = new A();
8. B b = new B();
9. if (a is A)
10. Console.WriteLine("a is an A");
11. if (b is A)
12. Console.WriteLine("b is an A because it is derived from A");
13. if (a is B)
14. Console.WriteLine("This won’t display -- a not derived from B");
15. Console.ReadLine();
16. }
17.}
a)
a is an A
This won’t display -- a not derived from B
b)
a is an A
b is an A because it is derived from A
c)
b is an A because it is derived from A
This won’t display -- a not derived from B
d)
"Both ""a is an A
This won’t display — a not derived from B"" & ""a is an A
b is an A because it is derived from A"""
View Answer
Answer: a
Explanation: We have to include the line ‘This won’t display — a not
derived from B’ this is because ‘a’ is object of class ‘A’ which itself is
derived from class ‘B’. So, ‘a’ is a B
Output:
a is an A
This won’t display -- a not derived from B
6. Which operator among the following supports the operation of
conversion at runtime without generating the exceptions?
a) is
b) as
c) typeof
d) all of the mentioned
View Answer
Answer: b
Explanation: By definition.
7. Which operator among the following is used to perform the operation
of boxing, unboxing, reference and identity conversions?
a) is
b) as
c) typeof
d) all of the mentioned
View Answer
Answer: b
Explanation: use the as operator, which has this general form:
expr as type
Here, expr is the expression being converted to type. If the conversion
succeeds, then a reference to type is returned. Otherwise, a null
reference is returned. The as operator can be used to perform only
reference, boxing, unboxing, or identity conversions.
8. What will be the output of the following C# code snippet?
1. class A {}
2. class B : A {}
3. class CheckCast
4. {
5. static void Main()
6. {
7. A a = new A();
8. B b = new B();
9. b = a as B;
10. b = null;
11. if(b==null)
12. Console.WriteLine("The cast in b = (B) a is NOT allowed.");
13. else
14. Console.WriteLine("The cast in b = (B) a is allowed");
15. }
16.}
a) Run time error
b) The cast in b = (B) a is NOT allowed
c) The cast in b = (B) a is allowed
d) Compile time error
View Answer
Answer: b
Explanation: since a is not a B, the cast of a to B is invalid and is
prevented by the if statement.
Output:
The cast in b = (B) a is NOT allowed
9. Which operator among the following supplies the information about
the characteristics of a typeof?
a) is
b) as
c) typeof
d) none of the mentioned
View Answer
Answer: c
Explanation: C# supplies the typeof operator. It retrieves a System.Type
object for a given type. Using this object, we can determine the type’s
characteristics.
10. What will be the output of the following C# code snippet?
1. class UseTypeof
2. {
3. static void Main()
4. {
5. Type t = typeof(StreamReader);
6. Console.WriteLine(t.FullName);
7. if(t.IsClass) Console.WriteLine("Is a class.");
8. if(t.IsAbstract) Console.WriteLine("Is abstract.");
9. else Console.WriteLine("Is concrete.");
10. }
11.}
a)
Is a class
Is abstract
b) Is abstract
c)
System.IO.StreamReader
Is a class
Is concrete
d)
Both Is a class
Is abstract & System.IO.StreamReader
Is a class
Is concrete
View Answer
Answer: c
Explanation: This program obtains a Type object that describes
StreamReader. It then displays the fullname, and determines if it is a
class and whether it is abstract.
Output:
System.IO.StreamReader
Is a class
Is concrete
1. Which feature enables to obtain information about the use and
capabilities of types at runtime?
a) Runtime type ID
b) Reflection
c) Attributes
d) None of the mentioned
View Answer
Answer: b
Explanation: Reflection is the feature that enables you to obtain
information about a type. The term reflection comes from the way the
process works: A Type object mirrors the underlying type that it
represents. Reflection is a powerful mechanism because it allows us to
learn and use the capabilities of types that are known only at runtime.
2. Choose the namespace which consists of classes that are part of .NET
Reflection API?
a) System.Text
b) System.Name
c) System.Reflection
d) None of the mentioned
View Answer
Answer: c
Explanation: Many of the classes that support reflection are part of
the .NET Reflection API, which is in the System.Reflection namespace.
eg : using System.Reflection;
3. Choose the correct statement about System.Type namespace.
a) Core of the reflection subsystem as it encapsulates a type
b) Consists of many methods and properties that can be used to obtain
information about a type at runtime
c) Both Core of the reflection subsystem as it encapsulates a type &
Consists of many methods and properties that can be used to obtain
information about a type at runtime
d) Only Consists of many methods and properties that can be used to
obtain information about a type at runtime
View Answer
Answer: c
Explanation: System.Type is at the core of the reflection subsystem
because it encapsulates a type. It contains many properties and methods
that you will use to obtain information about a type at runtime.
Advertisement
4. Choose the class from which the namespace ‘System.Type’ is derived?
a) System.Reflection
b) System.Reflection.MemberInfo
c) Both System.Reflection & System.Reflection.MemberInfo
d) None of the mentioned
View Answer
Answer: b
Explanation: Type is derived from an abstract class called
System.Reflection.MemberInfo
5. What does the following property signify?
MemberTypes MemberType
Subscribe Now: C# Newsletter | Important Subjects Newsletters
a) Helps in distinguishing kinds of members
b) Property helps in determining if member is a field, method, property
or event
c) Both Helps in distinguishing kinds of members & Property helps in
determining if member is a field, method, property or event
d) None of the mentioned
View Answer
Answer: c
Explanation: This property obtains the kind of the member. This value
indicates if the member is a field, method, property, event, or
constructor among others.
6. The property signifies “Obtains a Module object that represents the
module (an executable file) in which the reflected type resides”. Choose
the property which specifies the following statement?
a) Type DeclaringType
b) int MetadataToken
c) Module Module
d) Type ReflectedType
View Answer
Answer: c
Explanation: By definition.
7. Choose the method defined by MemberInfo.
a) GetCustomAttributes()
b) IsDefined()
c) GetCustomeAttributesData()
d) All of the mentioned
View Answer
Answer: d
Explanation: MemberInfo includes two abstract methods:
GetCustomAttributes( ) and IsDefined( ). These both relate to attributes.
The first obtains a list of the custom attributes associated with the
invoking object. The second determines if an attribute is defined for the
invoking object. The .NET Framework Version 4.0 adds a method called
GetCustomAttributesData(), which returns information about custom
attributes.
8. What does the following declaration specify?
MethodInfo[] GetMethods()
a) Returns an array of MethodInfo objects
b) Returns a list of the public methods supported by the type by using
GetMethods()
c) Both Returns an array of MethodInfo objects & Returns a list of the
public methods supported by the type by using GetMethods()
d) None of the mentioned
View Answer
Answer: c
Explanation: A list of the public methods supported by the type can be
obtained by using GetMethods(). It returns an array of MethodInfo
objects that describe the methods supported by the invoking type.
MethodInfo is in the System.Reflection namespace.
9. What does the following C# code specify?
object Invoke(object obj, object[] parameters)
a) Calling a type using invoke()
b) Any arguments that need to be passed to the method are specified in
the array parameters
c) The value returned by the invoked method is returned by Invoke()
d) All of the mentioned
View Answer
Answer: d
Explanation: Here, obj is a reference to the object on which the method
is invoked. (For static methods, you can pass null to obj.) Any arguments
that need to be passed to the method are specified in the array
parameters. If no arguments are needed, parameters must be null. Also,
parameters must contain exactly the same number of elements as the
number of arguments.
10. What does the following C# method specify?
Type[] GetGenericArguments()
a) A property defined by MemberInfo
b) Obtains a list of the type arguments bound to a closed constructed
generic type
c) The list may contain both type arguments and type parameters
d) All of the mentioned
View Answer
Answer: d
Explanation: The following method Obtains a list of the type arguments
bound to a closed constructed generic type or the type parameters if the
specified type is a generic type definition. For an open constructed type,
the list may contain both type arguments and type parameters.
1. Which among the following is not the ordered collection class?
a) BitArray
b) Queue
c) Stack
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
2. Which among the following is not an interface declared in
System.Collection namespace?
a) IDictionaryComparer
b) IEnumerable
c) IEnumerator
d) Icomparer
View Answer
Answer: a
Explanation: None.
3. Which among the following is the correct way to find out the number
of elements currently present in an ArrayListCollection called arr?
a) arr.Capacity
b) arr.Count
c) arr.MaxIndex
d) arr.UpperBound
View Answer
Answer: b
Explanation: None.
Advertisement
4. Which statement is correct in the following C#.NET code snippet?
1. Stack st = new Stack();
2. st.Push("Csharp");
3. st.Push(7.3);
4. st.Push(8);
5. st.Push('b');
6. st.Push(true);
a) Unsimilar elements like “Csharp”, 7.3, 8 cannot be stored in the same
stack collection
b) Boolean values can never be stored in Stack collection
c) Perfectly workable code
d) All of the mentioned
View Answer
Answer: c
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube
5. Which is the correct statement about an ArrayList collection that
implements the IEnumerable interface?
a) To access members of ArrayList from the inner class, it is necessary to
pass ArrayList class reference to it
b) The inner class of ArrayList can access ArrayList class members
c) The ArrayList class consist of inner class that implements the
IEnumerator interface
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
6. Which among the following is the correct way to access all the
elements of the Stack collection created using the C#.NET code snippet
given below?
1. Stack st = new Stack();
2. st.Push(10);
3. st.Push(20);
4. st.Push(-5);
5. st.Push(30);
6. st.Push(6);
a)
IEnumerable e;
e = st.GetEnumerator();
while (e.MoveNext())
Console.WriteLine(e.Current);
b)
IEnumerator e;
e = st.GetEnumerator();
while(e.MoveNext())
Console.WriteLine(e.Current);
c)
IEnumerable e;
e = st.GetEnumerable();
while(e.MoveNext())
Console.WriteLine(e.Current);
d) None of the mentioned
View Answer
Answer: b
Explanation: None.
7. The correct code to access all the elements of the queue collection
created using the following C#.NET code snippets?
1. Queue q = new Queue();
2. q.Enqueue("Harsh");
3. q.Enqueue('a');
4. q.Enqueue(false);
5. q.Enqueue(70);
6. q.Enqueue(8.5);
a)
IEnumerator e;
e = q.GetEnumerator();
while(e.MoveNext())
Console.WriteLine(e.Current);
b)
IEnumerable e;
e = q.GetEnumerator();
while(e.MoveNext())
c)
IEnumerable e
e = q.GetEnumerable();
while(e.MoveNext())
Console.WriteLine(e.Current);
d) All of the mentioned
View Answer
Answer: a
Explanation: None.
8. Which statements among the following are correct about the
Collection Classes available in Framework Class Library?
a) Elements of a collection cannot be transmitted over a network
b) Elements stored in a collection can be modified only if all the
elements are of similar types
c) Elements stored in a Collection can be retrieved but cannot be
modified
d) Collection classes make use of efficient algorithms to manage the
collection, hence improving the performance of the program
View Answer
Answer: d
Explanation: None.
9. Among the given collections which one is I/O index based?
a) ArrayList
b) List
c) Stack
d) Queue
View Answer
Answer: a
Explanation: None.
10. Which among the given statements are correct about the Stack
collection?
a) It can be used for evaluation of expressions
b) It is used to maintain a FIFO list
c) Top most element of the Stack collection can be accessed using the
Peek()
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
11. A HashTable t maintains a collection of names of states and capital
city of each state. Which among the following finds out whether “New
delhi” state is present in the collection or not?
a)
t.HasValue("New delhi");
b)
t.ContainsKey("New delhi");
c)
t.HasKey("New delhi");
d)
t.ContainsValue("New delhi");
View Answer
Answer: b
Explanation: None.
12. In which of the following collections is the I/O based on a key?
a) BitArray
b) SortedList
c) Queue
d) Stack
View Answer
Answer: b
Explanation: None.
13. The wrong statements about a HashTable collection are?
a) It is a keyed collection
b) It is a ordered collection
c) It’s not an indexed collection
d) It implements a IDictionaryEnumerator interface in its inner class
View Answer
Answer: b
Explanation: None.
1. Which of these classes contains only floating point functions?
a) Math
b) Process
c) System
d) Object
View Answer
Answer: a
Explanation: 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 following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. double x = 2.0;
6. double y = 3.0;
7. double z = Math.Pow( x, y );
8. Console.WriteLine(z);
9. Console.ReadLine();
10. }
11. }
a) 2.0
b) 4.0
c) 8
d) 8.0
View Answer
Answer: c
Explanation: None.
Output :
advertisement
8
3. What will be the output of the following C# code snippet?
Subscribe Now: C# Newsletter | Important Subjects Newsletters
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. double x = 4.772;
6. double y = 4.76;
7. double z = Math.Max(x, y);
8. Console.WriteLine(z);
9. Console.ReadLine();
10. }
11. }
a) true
b) false
c) 4.772
d) 4.76
View Answer
Answer: c
Explanation: None.
Output :
4.772
4. What is the value of double consonant ‘E’ defined in Math class?
a) approximately 3
b) approximately 3.14
c) approximately 2.72
d) approximately 0
View Answer
Answer: c
Explanation: None.
5. What will be the output of the following C# code snippet?
1. public class A
2. {
3. public int x;
4. public int y;
5. public void display()
6. {
7. Console.WriteLine(x + " " + y);
8. }
9. }
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. A obj1 = new A();
15. A obj2 = new A();
16. obj1.x = 1;
17. obj1.y = 2;
18. obj2 = obj1;
19. obj1.display();
20. obj2.display();
21. }
22. }
a) 1 2 0 0
b) 1 2 1 2
c) 0 0 0 0
d) Run time exception
View Answer
Answer: b
Explanation: None.
Output :
1212
6. What will be the output of the following C# 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. select Math.Pow(4 ,3);
8. Console.Write("The values in nums: ");
9. foreach (int i in posNums)
10. Console.Write(i + " ");
11. Console.WriteLine();
12. Console.ReadLine();
13. }
14.}
a) Run time error
b) 64
c) Compile time error
d) 81
View Answer
Answer: b
Explanation: None.
Output :
64
7. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. float x = 3.14F;
6. int y = (int)Math.Abs(x);
7. Console.WriteLine(y);
8. Console.ReadLine();
9. }
10.}
a) Compile time error
b) 3.14
c) 3
d) 4
View Answer
Answer: c
Explanation: None.
Output :
3
8. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. int x = 5;
6. int y = (int)Math.Pow(x,2);
7. int z = (int)Math.Pow(y, 2);
8. Console.WriteLine(z);
9. Console.ReadLine();
10. }
11.}
a) 25
b) 625
c) Compile time error
d) Run time error
View Answer
Answer: b
Explanation: y = 25, z = 25*25 = 625
Output :
625
9. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. int[] nums = {3 ,1 ,2 ,5 ,4};
6. var ltAvg = from n in nums
7. let x = nums.Average()
8. where n < x
9. select n;
10. Console.WriteLine("The average is " + nums.Average());
11. Console.ReadLine();
12. }
13. }
a) Run time error
b) 3
c) 5
d) Compile time error
View Answer
Answer: b
Explanation: Built in method of maths class Avg() id used
Output :
3
10. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. int y = (int)Math.Max(4,2);
6. int z = (int)Math.Pow(y, 2);
7. Console.WriteLine(z);
8. Console.ReadLine();
9. }
10.}
a) 4
b) Compile time error
c) 16
d) 89
View Answer
Answer: c
Explanation: 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
1. Which among the given classes provides types of rounding functions?
a) Math
b) Process
c) System
d) Object
View Answer
Answer: a
Explanation: None.
2. Which of these methods is a rounding function of Math class?
a) Max()
b) Min()
c) Abs()
d) Round()
View Answer
Answer: d
Explanation: Round() rounds up a variable to nearest integer.
3. Which of these classes contains only floating point functions?
a) Math
b) Process
c) System
d) Object
View Answer
Answer: a
Explanation: Math class contains all the floating point functions that are
used for general purpose mathematics methods. Example : sin(), cos(),
exp(), sqrt() etc.
Advertisement
4. Which of these method returns a smallest whole number greater than
or equal to variable X?
a) double Ciel(double X)
b) double Floor(double X)
c) double Max(double X)
d) double Min(double X)
View Answer
Answer: a
Explanation: Ciel(double X) returns the smallest whole number greater
than or equal to variable X.
5. Which of these methods return a largest whole number less than or
equal to variable X?
a) double Ciel(double X)
b) double Floor(double X)
c) double Max(double X)
d) double Min(double X)
View Answer
Answer: b
Explanation: double Floor(double X) returns a largest whole number less
than or equal to variable X.
6. Which of the following functions return absolute value of a variable?
a) Abs()
b) Absolute()
c) absolutevariable()
d) None of the mentioned
View Answer
Answer: a
Explanation: Abs() returns the absolute value of a variable.
7. What will be the output of the following C# code snippet?
1. public class A
2. {
3. public int x;
4. public int y;
5. public void display()
6. {
7. Console.WriteLine(x + " " + y);
8. }
9. }
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. A obj1 = new A();
15. A obj2 = new A();
16. obj1.x = 1;
17. obj1.y = 2;
18. obj2 = obj1;
19. obj1.display();
20. obj2.display();
21. }
22. }
a) 1 2 0 0
b) 1 2 1 2
c) 0 0 0 0
d) Run time exception
View Answer
Answer: b
Explanation: None.
Output:
1212
8. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. double x = 3.14;
6. int y = (int) Math.Abs(x);
7. Console.WriteLine(y);
8. }
9. }
a) 0
b) 3
c) 3.0
d) 3.1
View Answer
Answer: b
Explanation: None.
Output:
3
9. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. double x = 3.14;
6. int y = (int) Math.Ceiling(x);
7. Console.WriteLine(y);
8. }
9. }
a) 0
b) 3
c) 3.0
d) 4
View Answer
Answer: d
Explanation: Ceiling(double x) returns the smallest whole number
greater than or equal to variable x.
Output:
4
10. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. double x = 3.14;
6. int y = (int) Math.Floor(x);
7. Console.WriteLine(y);
8. }
9. }
a) 0
b) 3
c) 3.0
d) 4
View Answer
Answer: b
Explanation: double Floor(double X) returns the largest whole number
less than or equal to variable X. Here, the smallest whole number less
than 3.14 is 3.
Output:
3
1. Select the type of multitasking methods that exist:
a) process based
b) thread based
c) only process
d) both process & thread based
View Answer
Answer: d
Explanation: 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 A program that acts as a small unit of code that can be
dispatched by the scheduler
d) Both A feature that allows our computer to run two or more programs
concurrently & A program that acts as a small unit of code that can be
dispatched by the scheduler
View Answer
Answer: d
Explanation: 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
View Answer
Answer: a
Explanation: 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.
Advertisement
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 Enables to utilize the idle and executing time present in most
programs & Enables to utilize the idle time present in most programs
d) Only Enables to utilize the idle time present in most programs
View Answer
Answer: d
Explanation: 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
View Answer
Answer: d
Explanation: None.
6. Number of threads that exists for each of the processes that occurs in
the program:
a) at most 1
b) atleast 1
c) only 1
d) both at most 1 & atleast 1
View Answer
Answer: d
Explanation: 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
View Answer
Answer: c
Explanation: 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 following C# code snippet specify?
1. public Thread(ThreadStart start)
a) Defines a thread
b) Declaration of a thread constructor
c) Only Defines a thread
d) Only Defines a thread & Declaration of a thread constructor
View Answer
Answer: d
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: c
Explanation: In multithreading, when a thread is created, it is in the
“New” state but does not start executing until explicitly started using a
method like Start().
11. Which of these methods of Thread class is used to Suspend a thread
for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()
View Answer
Answer: a
Explanation: None.
1. Which of these keywords are used to implement synchronization?
a) synchronize
b) syn
c) synch
d) synchronized
View Answer
Answer: d
Explanation: None.
2. Which keyword is used for using the synchronization features defined
by the Monitor class?
a) lock
b) synchronized
c) monitor
d) locked
View Answer
Answer: a
Explanation: The C# keyword lock is really just shorthand for using the
synchronization features defined by the Monitor class, which is defined
in the System.Threading namespace.
3. What is synchronization in reference to a thread?
a) It’s a process of handling situations when two or more threads need
access to a shared resource
b) It’s a process by which many threads are able to access the same
shared resource simultaneously
c) It’s a process by which a method is able to access many different
threads simultaneously
d) It’s a method that allows too many threads to access any information
they require
View Answer
Answer: a
Explanation: When two or more threads need to access the same shared
resource, they need some way to ensure that the resource will be used
by only one thread at a time, the process by which this is achieved is
called synchronization.
Advertisement
4. Which method is called when a thread is blocked from running
temporarily?
a) Pulse()
b) PulseAll()
c) Wait()
d) Both Pulse() & Wait()
View Answer
Answer: c
Explanation: When a thread is temporarily blocked from running, it calls
Wait( ). This causes the thread to go to sleep and the lock for that object
to be released, allowing another thread to acquire the lock.
5. What kind of exception is being thrown if Wait(), Pulse() or PulseAll() is
called from code that is not within synchronized code?
a) System I/O Exception
b) DivideByZero Exception
c) SynchronizationLockException
d) All of the mentioned
View Answer
Answer: c
Explanation: A SynchronizationLockException will be thrown if Wait(),
Pulse(), or PulseAll() is called from code that is not within synchronized
code, such as a lock block.
6. What is mutex?
a) a mutually exclusive synchronization object
b) can be acquired by more than one thread at a time
c) helps in sharing of resource which can be used by one thread
d) all of the mentioned
View Answer
7. What is Semaphore?
a) Grant more than one thread access to a shared resource at the same
time
b) Useful when a collection of resources is being synchronized
c) Make use of a counter to control access to a shared resource
d) All of the mentioned
View Answer
Answer: d
Explanation: A semaphore is similar to a mutex except that it can grant
more than one thread access to a shared resource at the same time.
Thus, the semaphore is useful when a collection of resources is being
synchronized. A semaphore controls access to a shared resource through
the use of a counter. If the counter is greater than zero, then access is
allowed. If it is zero, access is denied.
8. Which method is used to abort thread prior to it’s normal execution?
a) sleep()
b) terminate()
c) suspend()
d) Abort()
View Answer
Answer: d
Explanation: To terminate a thread prior to its normal conclusion, use
Thread.Abort( ). Its simplest form is shown here:
public void Abort()
Abort() causes a ThreadAbortException to be thrown to the thread on
which Abort() is called. This exception causes the thread to terminate.
9. Which of these statements is incorrect?
a) By multithreading CPU idle time is minimized, and we can take
maximum use of it
b) By multitasking CPU idle time is minimized, and we can take maximum
use of it
c) Two thread in Csharp can have same priority
d) A thread can exist only in two states, running and blocked
View Answer
Answer: d
Explanation: Thread exists in several states, a thread can be running,
suspended, blocked, terminated & ready to run.
10. What is multithreaded programming?
a) It’s a process in which two different processes run simultaneously
b) It’s a process in which two or more parts of same process run
simultaneously
c) It’s a process in which many different process are able to access same
information
d) It’s a process in which a single process can access information from
many sources
View Answer
Answer: b
Explanation: Multithreaded programming a process in which two or
more parts of same process run simultaneously.
1. What is an iterator?
a) a method
b) an operator
c) accessor
d) all of the mentioned
View Answer
Answer: d
Explanation: 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 following C# code snippet?
1. class MyClass
2. {
3. char[] chrs = { 'A', 'B', 'C', 'D' };
4. public System.Collections.IEnumerator GetEnumerator()
5. {
6. foreach (char ch in chrs)
7. yield return ch;
8. }
9. }
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. MyClass mc = new MyClass();
15. foreach (char ch in mc)
16. Console.Write(ch + " ");
17. Console.WriteLine();
18. Console.ReadLine();
19. }
20. }
a) Run time error
b) Compile time error
c) Code runs successfully prints nothing
d) Code runs successfully prints A, B, C, D
View Answer
Answer: d
Explanation: None.
Output:
advertisement
A, B, C, D
3. Choose the correct statements for the following C# code?
Note: Join free Sanfoundry classes at Telegram or Youtube
1. public System.Collections.IEnumerator GetEnumerator()
2. {
3. foreach (char ch in chrs)
4. yield return ch;
5. }
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
View Answer
Answer: d
Explanation: 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 the following C# code
snippet?
1. public System.Collections.IEnumerator GetEnumerator()
2. {
3. foreach (char ch in chrs)
4. yield return ch;
5. }
6. a) returns the output
7. b) returns the next object in the collection
8. c) Both returns the output & returns the next object in the collection
9. d) none of the mentioned
10.[expand title="View Answer"] Answer: b
11.Explanation: The yield return statement returns the next object in the
collection, which in this case is the next character in chrs in the code.
12.[/expand]
13.
14.
15.5. What does the following C# code snippet specify?
16.<pre lang="csharp" line="1" cssfile="hk1_style">
17.class MyClass
18.{
19. char chrs = 'A' ;
20. public IEnumerator GetEnumerator()
21. {
22. for (int i = 20; i >=0; --i)
23. yield return (char)((chrs + i));
24. }
25.}
26.class Program
27.{
28. static void Main(string[] args)
29. {
30. MyClass mc = new MyClass();
31. foreach (char ch in mc)
32. Console.Write(ch + " ");
33. Console.WriteLine();
34. Console.ReadLine();
35. }
36.}
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
View Answer
Answer: c
Explanation: None.
Output:
UTSRQPONMLKJIHGFEDCBA
6. What will be the following C# code snippet specify?
1. class MyClass
2. {
3. char chrs = 'A' ;
4. public IEnumerator GetEnumerator()
5. {
6. for (int i = 20; i >=0; --i)
7. if (i == 10) yield break;
8. yield return (char)((chrs + i));
9. }
10.}
11.class Program
12.{
13. static void Main(string[] args)
14. {
15. MyClass mc = new MyClass();
16. foreach (char ch in mc)
17. Console.Write(ch + " ");
18. Console.WriteLine();
19. Console.ReadLine();
20. }
21.}
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
View Answer
Answer: c
Explanation: 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:
UTSRQPONML
7. What will be the output of the following C# code snippet?
1. class MyClass
2. {
3. int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20};
4. public IEnumerator GetEnumerator()
5. {
6. for (int i = 0; i < 20; i++)
7. {
8. if (a[i] % 2 == 0)
9. yield return (int)(a[i]);
10. }
11. }
12. }
13. class Program
14. {
15. static void Main(string[] args)
16. {
17. MyClass mc = new MyClass();
18. foreach (int i in mc)
19. Console.Write(i + " ");
20. Console.WriteLine();
21. Console.ReadLine();
22. }
23. }
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
View Answer
Answer: c
Explanation: None.
Output:
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
8. What will be the output of the following C# code snippet?
1. class MyClass
2. {
3. char ch = 'A';
4. public IEnumerable MyItr(int end)
5. {
6. for (int i = 0 ;i < end ;i++)
7. yield return (char)(ch + i);
8. }
9. public IEnumerable MyItr(int begin, int end)
10. {
11. for (int i = begin ;i < end ;i++)
12. yield return (char)(ch + i);
13. }
14. }
15. class Program
16. {
17. static void Main(string[] args)
18. {
19. MyClass mc = new MyClass();
20. Console.WriteLine("Iterate the first 7 letters:");
21. foreach (char ch in mc.MyItr(7))
22. Console.Write(ch + " ");
23. Console.WriteLine("n");
24. Console.WriteLine("Iterate letters from F to L:");
25. foreach (char ch in mc.MyItr(7, 12))
26. Console.Write(ch + " ");
27. Console.WriteLine();
28. Console.ReadLine();
29. }
30. }
a)
Iterate the first 7 letters:
ABCDEFG
Iterate letters from F to L:
GHIJKL
b)
Iterate the first 7 letters:
ABCDEFG
Iterate letters from F to L:
HIJKL
c) Run time error
d) Compile time error
View Answer
Answer: b
Explanation: None.
Output:
Iterate the first 7 letters:
ABCDEFG
Iterate letters from F to L:
HIJKL
9. What will be the output of the following C# code snippet?
1. class MyClass
2. {
3. char ch = 'A';
4. int e = 4;
5. int k = 9;
6. int z = 6;
7. public IEnumerator GetEnumerator()
8. {
9. for (int i = 0; i < 26; i++)
10. {
11. if (i == e*k /z) yield break;
12. yield return (int)(ch + i);
13. }
14. }
15.}
16.class Program
17.{
18. static void Main(string[] args)
19. {
20. MyClass mc = new MyClass();
21. foreach(int ch in mc)
22. Console.Write(ch + " ");
23. Console.WriteLine();
24. Console.ReadLine();
25. }
26.}
a) Compile time error
b) Run time error
c) 65 66 67 68 69 70
d) Code run successfully prints nothing
View Answer
Answer: c
Explanation: 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 They allow to pass arguments to the iterator that control what
elements are obtained & This form of iterators can be overloaded
d) None of the mentioned
View Answer
Answer: c
Explanation: By definition.
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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: c
Explanation: None.
3. Which among the following does not belong to the C#.NET
namespace?
a) class
b) struct
c) enum
d) data
View Answer
Answer: d
Explanation: None.
Advertisement
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
View Answer
Answer: c
Explanation: None.
5. Which among the given is not a correct way to call the method Issue()
defined in the following C# code snippet?
Subscribe Now: C# Newsletter | Important Subjects Newsletters
1. class Book
2. {
3. public void issue()
4. {
5. /* code */
6. }
7. }
8. class Journel
9. {
10. public void issue()
11. {
12. /* code */
13. }
14.}
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
View Answer
Answer: b
Explanation: 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
View Answer
Answer: b
Explanation: None.
7. Which among the following is a .NET namespace?
a) System.Web
b) System.Process
c) System.Drawing2D
d) System.Drawing3D
View Answer
Answer: a
Explanation: 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();
}
}
}
View Answer
Answer: c
Explanation: None.
9. If ListBox is the class of System.Windows.Forms namespace. Then, the
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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: c
Explanation: None.
1. Choose the symbol which begins a preprocessor directive in C#.NET?
a) #
b) **
c) *
d) &
View Answer
Answer: a
Explanation:
#define, #elif, #else etc.
2. What is meant by preprocessor directive in C#.NET?
a) a form of command which are interpreted by the compiler
b) a form of macros like in c and c++ not exactly same to them,
separately designed for C#.NET
c) always begins with a ‘#’ character occupies separate line of source of
code
d) all of the mentioned
View Answer
Answer: d
Explanation: Preprocessor directives are commands that are interpreted
by the compiler and affect the output or behavior of the build process.
The C# compiler does not have a separate preprocessor, like C and C++
we cannot use these directives to create macros. Preprocessing
directives are top lines in our program that start with ‘#’. The ‘#’ is
followed by an identifier that is the directive name.
Advertisement
3. What is meant by preprocessor directive #define?
a) defines a character sequence
b) helps in determining existence and non existence of a symbol
c) can be used to create function like macros as in C/C++
d) all of the mentioned
View Answer
Answer: a
Explanation: The #define directive defines a character sequence called a
symbol. The existence or nonexistence of a symbol can be determined by
#if or #elif and is used to control compilation. #define which supports
creation of function like macros in c/c++ does not support the same in
C#.
4. Select the defined preprocessor in C#.NET?
a) #define
b) #elif
c) #else
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
5. What does preprocessor directive #if and #endif explains?
a) Enables compilation of sequence of code on condition basis
b) Express results into true or false on evaluation of condition
c) If expression following #if is true then code that is between #if and
#endif is compiled otherwise skipped
d) All of the mentioned
View Answer
Answer: d
Explanation: The #if and #endif directives enable conditional compilation
of a sequence of code based upon whether an expression involving one
or more symbols evaluates to true. A symbol is true if it has been
defined. It is false otherwise. If the expression following #if is true, the
code that is between it and #endif is compiled. Otherwise, the
intervening code is skipped. The #endif directive marks the end of an #if
block.
6. What will be the output of the following C# code snippet?
1. #define pi
2. using System;
3. using System.Collections.Generic;
4. using System.Linq;
5. using System.Text;
6. using System.Threading.Tasks;
7.
8. namespace ConsoleApplication13
9. {
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. #if (!pi)
15. Console.WriteLine("i");
16. #else
17. Console.WriteLine("pi not define");
18. #endif
19. Console.WriteLine("ok");
20. Console.ReadLine();
21. }
22. }
23.}
a)
i
pi not define
b)
pi not define
ok
c)
i
ok
d) ok
View Answer
Answer: b
Explanation: The defined symbol ‘pi’ when compared as per ‘if’
condition, hence the outcome is false which results in skip of statement
and hence executes statement after #else and finally the end statement
after #endif.
Output: pi not define
ok
7. What will be the output of the following C# code snippet?
1.#define DEBUG
2.#define MYTEST
3.using System;
4.using System.Collections.Generic;
5.using System.Linq;
6.using System.Text;
7.using System.Threading.Tasks;
8.
9. namespace ConsoleApplication13
10.{
11. class Program
12. {
13. static void Main(string[] args)
14. {
15. #if (DEBUG && !MYTEST)
16. Console.WriteLine("DEBUG is defined");
17. #elif (!DEBUG && MYTEST)
18. Console.WriteLine("MYTEST is defined");
19. #elif (DEBUG && MYTEST)
20. Console.WriteLine("DEBUG and MYTEST are defined");
21. #else
22. Console.WriteLine("DEBUG and MYTEST are not defined");
23. #endif
24. Console.ReadLine();
25. }
26. }
27.}
a)
DEBUG is defined
MYTEST is defined
b)
MYTEST is defined
DEBUG and MYTEST are defined
c)
DEBUG and MYTEST are not defined
MYTEST is defined
d) DEBUG and MYTEST are defined
View Answer
Answer: d
Explanation: None.
8. What will be the output of the following C# code snippet?
1.#define DEBUG
2.#undef DEBUG
3.using System;
4.using System.Collections.Generic;
5.using System.Linq;
6.using System.Text;
7.using System.Threading.Tasks;
8.
9. namespace ConsoleApplication13
10.{
11. class Program
12. {
13. static void Main(string[] args)
14. {
15. #if (DEBUG)
16. Console.WriteLine("DEBUG is defined");
17. #elif (!DEBUG && MYTEST)
18. Console.WriteLine("MYTEST is defined");
19. #elif (DEBUG && MYTEST)
20. Console.WriteLine("DEBUG and MYTEST are defined");
21. #else
22. Console.WriteLine("DEBUG and MYTEST are not defined");
23. #endif
24. Console.ReadLine();
25. }
26. }
27.}
a)
DEBUG is defined
DEBUG and MYTEST are not defined
b) DEBUG and MYTEST are not defined
c)
MYTEST is defined
DEBUG and MYTEST are not defined
d) DEBUG is defined
View Answer
Answer: b
Explanation: #undef lets to undefine a symbol such that by using the
symbol as the expression in a #if directive, the expression will evaluate to
false i.e the symbol will be undefined in nature.
Output: DEBUG and MYTEST are not defined
9. Which preprocessor directive among the following forces the compiler
to stop the compilation?
a) #warning
b) #endregion
c) #undef
d) #error
View Answer: d
#error
10. Which among the following is not a preprocessor directive?
a) #ifdef
b) #pragma
c) #Or
d) #undef
View Answer
Answer: c
Explanation: None.
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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: b
Explanation: 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
View Answer
Answer: c
Explanation: If a static method is present in the program then it will be
executed first, then main will be executed.
Advertisement
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
View Answer
Answer: d
Explanation: 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
View Answer
Answer: d
Explanation: None.
6. What will be the output of the following C# code?
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. int volume;
7. void volume(int height, int length, int width)
8. {
9. volume = width * height * length;
10. }
11.}
12.class Prameterized_method
13.{
14. public static void main(String args[])
15. {
16. box obj = new box();
17. obj.height = 1;
18. obj.length = 5;
19. obj.width = 5;
20. obj.volume(3, 2, 1);
21. Console.WriteLine(obj.volume);
22. Console.ReadLine();
23. }
24.}
a) 0
b) 1
c) 6
d) 25
View Answer
Answer: c
Explanation: None.
Output :
6
7. What will be the output of the following C# code snippet?
1. class equality
2. {
3. int x;
4. int y;
5. boolean isequal()
6. {
7. return(x == y);
8. }
9. }
10.class Output
11.{
12. public static void main(String args[])
13. {
14. equality obj = new equality();
15. obj.x = 5;
16. obj.y = 5;
17. Console.WriteLine(obj.isequal());
18. }
19.}
a) false
b) true
c) 0
d) 1
View Answer
Answer: b
Explanation: None.
Output :
True
8. What will be the output of the following C# code snippet?
1. class equality
2. {
3. public int x;
4. public int y;
5. public Boolean isequal()
6. {
7. return (x == y);
8. }
9. }
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. equality obj = new equality();
15. obj.x = 5;
16. obj.y = 5;
17. Console.WriteLine(obj.isequal());
18. Console.ReadLine();
19. }
20. }
a) false
b) true
c) 0
d) 1
View Answer
Answer: b
Explanation: None.
Output :
True
9. What will be the output of the following C# code snippet?
1. class box
2. {
3. public int width;
4. public int height;
5. public int length;
6. public int volume1;
7. public void volume()
8. {
9. volume1 = width * height * length;
10. }
11. public void volume(int x)
12. {
13. volume1 = x;
14. }
15.}
16.class Program
17.{
18. static void Main(string[] args)
19. {
20. box obj = new box();
21. obj.height = 1;
22. obj.length = 5;
23. obj.width = 5;
24. obj.volume(5);
25. Console.WriteLine(obj.volume1);
26. Console.ReadLine();
27. }
28.}
a) 0
b) 5
c) 25
d) 26
View Answer
Answer: b
Explanation: None.
Output :
5
10. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. int x, y = 1;
6. x = 10;
7. if(x != 10 && x / Convert.ToInt32(0) == 0)
8. Console.WriteLine(y);
9. else
10. Console.WriteLine(++y);
11. Console.ReadLine();
12. }
13.}
a) 1
b) 2
c) Run time error
d) Compile time error
View Answer
Answer: b
Explanation: Both conditions for if statements are failed and hence
statement after else is executed.
Output :
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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: d
Explanation: None.
3. Which of the following are the interfaces defined by the namespace
System.Net?
a) IAuthenticationModule
b) HttpWebRequest
c) WebProxy
d) HttpResponseHeader
View Answer
Answer: a
Explanation: WebProxy and HttpResponseHeader are namespaces and
enumerations.
Advertisement
4. Which of the following are the classes that support the standard HTTP
protocol?
a) HttpWebRequest
b) HttpResponseHeader
c) HttpRequestHeader
d) HttpStatusCode
View Answer
Answer: a
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer
Answer: b
Explanation: None.
7. How many bits are present in a single IP address?
a) 8
b) 16
c) 32
d) 64
View Answer
Answer: c
Explanation: 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) Domain Name Service
View Answer
Answer: d
Explanation: None.
9. Which of the following classes is used to encapsulate IP address and
DNS?
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler
View Answer
Answer: c
Explanation: 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
View Answer
Answer: d
Explanation: 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.
1. What does URL stand for?
a) Uniform Resource Locator
b) Uniform Resource Latch
c) Universal Resource Locator
d) Universal Resource Latch
View Answer
Answer: a
Explanation: None.
2. Which of these exceptions is thrown by the URL class’s constructors?
a) URLNotFound
b) URLSourceNotFound
c) MalformedURLException
d) URLNotFoundException
View Answer
Answer: c
Explanation: None.
3. What does the following form define?
advertisement
Protocol://HostName/FilePath?Query
a) Protocol specifies the protocol being used, such as HTTP
b) HostName identifies a specific server, such as mhprofessional.com or
www.google.com
c) FilePath specifies the path to a specific file
d) All of the mentioned
View Answer
Answer: d
Explanation: By definition.
4. Which of these classes is used to encapsulate IP address and DNS?
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler
View Answer
Answer: c
Explanation: InetAddress class encapsulates both IP address and DNS, we
can interact with this class by using the name of an IP host.
5. Which of these is a standard for communicating multimedia content
over email?
a) http
b) https
c) Mime
d) httpd
View Answer
Answer: c
Explanation: MIME is an internet standard for communicating
multimedia content over email. The HTTP protocol uses and extends the
notion of MIME headers to pass attribute pairs between HTTP client and
server.
6. What does the following method specify?
public static WebRequest Create(string requestUriString)
a) Creates a WebRequest object for the URI specified by the string
passed by requestUriString
b) The object returned will implement the protocol specified by the
prefix of the URI
c) The object will be an instance of the class that inherits WebRequest
d) All of the mentioned
View Answer
Answer: d
Explanation: Creates a WebRequest object for the URI specified by the
string passed by requestUriString. The object returned will
implement the protocol specified by the prefix of the URI. Thus, the
object will be an instance of a class that inherits WebRequest. A
NotSupportedException is thrown if the requested protocol is not
available. A UriFormatException is thrown if the URI format is invalid.
7. What will be the output of the following C# code?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. int ch;
6. HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("http://www.McGraw-Hill.com");
7. HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
8. Stream istrm = resp.GetResponseStream();
9. for (int i = 1 ; ;i++)
10. {
11. ch = istrm.ReadByte();
12. if (ch == -1)
13. break;
14. Console.Write((char)ch);
15. if ((i % 400) == 0)
16. {
17. Console.Write("\nPress Enter.");
18. Console.ReadLine();
19. }
20. }
21. resp.Close();
22. }
23. }
a) html
b) text
c) html/text
d) text/html
View Answer
Answer: d
Explanation: The following program obtains the hypertext contained at a
specific website. The program displays the hypertext on the screen.
8. What will be the output of the following C# code?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. Uri obj = new Uri("https://www.sanfoundry.com/csharpmcq");
6. Console.WriteLine(obj.AbsoluteUri);
7. Console.ReadLine();
8. }
9. }
a) sanfoundry
b) sanfoundry.com
c) www.sanfoundry.com
d) https://www.sanfoundry.com/csharpmcq
View Answer
Answer: d
Explanation: AbsoluteUri is used to know the full URL of an URL object.
Output:
https://www.sanfoundry.com/csharpmcq
9. Which of these data members of HttpResponse class is used to store
the response from a http server?
a) status
b) address
c) statusResponse
d) statusCode
View Answer
Answer: d
Explanation: When we send a request to http server it responds with a
status code. This status code is stored in statusCode and the textual
equivalent is stored in reasonPhrase.
10. Which of these classes is used to access actual bits or content
information of a URL?
a) URL
b) URLDecoder
c) URLConnection
d) All of the mentioned
View Answer
Answer: d
Explanation: URL, URLDecoder and URLConnection – all these are used
to access information stored in the URL.
1. What exception is thrown if the URI format is invalid?
a) URLNotFound
b) URLSourceNotFound
c) MalformedURLException
d) UriFormatException
View Answer
Answer: d
Explanation: None.
2. What exception is thrown if the protocol supported by URI prefix is
invalid?
a) URLNotFound
b) URLSourceNotFound
c) UriFormatException
d) NotSupportedException
View Answer
Answer: d
Explanation: 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
View Answer
Answer: c
Explanation: None.
Advertisement
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
View Answer
Answer: d
Explanation: None.
5. Choose the exceptions generated by the GetReponse() method
defined by WebRequest.
a) InvalidOperationException
b) ProtocolViolationException
c) WebException
d) All of the mentioned
View Answer
Answer: d
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer
Answer: d
Explanation: None.
8. Choose the exceptions generated by the GetResponseStream()
method defined by WebRequest.
a) ProtocolViolationException
b) ObjectDisposedException
c) IOException
d) All of the mentioned
View Answer
Answer: d
Explanation: 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
View Answer
Answer: b
Explanation: None.
10. Which of these methods gives the full URL of an URL object?
a) fullHost()
b) getHost()
c) AbsoluteUri
d) toExternalForm()
View Answer
Answer: c
Explanation: None
1. Why are generics used?
a) Generics make code more fast
b) Generics make code more optimised and readable
c) Generics add stability to your code by making more of your bugs
detectable at compile time
d) Generics add stability to your code by making more of your bugs
detectable at run time
View Answer
Answer: c
Explanation: Generics add stability to your code by making more of your
bugs detectable at compile time.
2. Which of these type parameters is used for generic methods to return
and accept any type of object?
a) K
b) N
c) T
d) V
View Answer
Answer: c
Explanation: T is used for type, A type variable can be any non-primitive
type you specify: any class type, any interface type, any array type, or
even another type variable.
3. Which of these is an correct way of defining generic method?
a) name(T1, T2, …, Tn) { /* … */ }
b) public name { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) name{T1, T2, …, Tn} { /* … */ }
View Answer
Answer: b
Explanation: The syntax for a generic method includes a type parameter,
inside angle brackets, and appears before the method’s return type. For
static generic methods, the type parameter section must appear before
the method’s return type.
Advertisement
4. What will be the output of the following C# code snippet?
1. public class Generic<T>
2. {
3. Stack<T> stk = new Stack<T>();
4. public void push(T obj)
5. {
6. stk.Push(obj);
7. }
8. public T pop()
9. {
10. T obj = stk.Pop();
11. return obj;
12. }
13.}
14.class Program
15.{
16. static void Main(string[] args)
17. {
18. Generic<int> g = new Generic<int>();
19. g.push("Csharp");
20. Console.WriteLine(g.pop());
21. Console.ReadLine();
22. }
23.}
a) Compile time error
b) Csharp
c) 0
d) Run time error
View Answer
Answer: b
Explanation: None.
Output :
Csharp
5. What will be the output of the following C# code snippet?
1. public class Generic<T>
2. {
3. Stack<T> stk = new Stack<T>();
4. public void push(T obj)
5. {
6. stk.Push(obj);
7. }
8. public T pop()
9. {
10. T obj = stk.Pop();
11. return obj;
12. }
13.}
14.class Program
15.{
16. static void Main(string[] args)
17. {
18. Generic<string> g = new Generic<string>();
19. g.push(30);
20. Console.WriteLine(g.pop());
21. Console.ReadLine();
22. }
23.}
a) 0
b) 30
c) Runtime Error
d) Compile time Error
View Answer
Answer: b
Explanation: None.
Output : 30
6. What does the following C# code block define?
1. class Gen<T> {
2. T ob;
3. }
a) Generics class declaration
b) Declaration of variable
c) A simple class declaration
d) Both Generics class declaration & Declaration of variable
View Answer
Answer: d
Explanation: class Gen<T> This defines the generics declaration where ‘T’
is the name of type parameter. This parameter is used as a placeholder
for the actual type that will be specified when a Gen object is created.
Gen is a generic class. T is used to declare a variable called ‘ob’.
7. What will be the output of the following C# code snippet?
1. public class Generic<T>
2. {
3. Stack<T> stk = new Stack<T>();
4. public void push(T obj)
5. {
6. stk.Push(obj);
7. }
8. public T pop()
9. {
10. T obj = stk.Pop();
11. return obj;
12. }
13.}
14.class Program
15.{
16. static void Main(string[] args)
17. {
18. Generic<string> g = new Generic<string>();
19. g.push("C++");
20. Console.WriteLine(g.pop() + " ");
21. Generic<int> g1 = new Generic<int>();
22. g1.push(20);
23. Console.WriteLine(g1.pop());
24. Console.ReadLine();
25. }
26.}
a) C++
b) 20
c)
C++
20
d) 0
View Answer
Answer: c
Explanation: None.
Output :
C++
20
8. Select the type argument of open constructed type?
a) Gen<int>
b) Gen<T>
c) Gen<>
d) None of the mentioned
View Answer
Answer: c
Explanation: A generic type, such as Gen<T>, is an abstraction. In C#
terminology, a construct such as Gen<T> is called an open constructed
type, because the type parameter T (rather than an actual type, such as
int) is specified.
9. Choose the correct way to call subroutine fun() of the sample class?
1. class a
2. {
3. public void x(int p, double k)
4. {
5. Console.WriteLine("k : csharp!");
6. }
7. }
a)
delegate void del(int i);
x s = new x();
del d = new del(ref s.x);
d(8, 2.2f);
b)
delegate void del(int p, double k);
del d;
x s = new x();
d = new del(ref s.x);
d(8, 2.2f);
c)
x s = new x();
delegate void d = new del(ref x);
d(8, 2.2f);
d) all of the mentioned
View Answer
Answer: b
Explanation: None.
10. What does the following C# code set defines?
1. public Gen(T o) {
2. ob = o;
3. }
a) Generics class decleration
b) Decleration of variable
c) Generic constructor decleration
d) All of the mentioned
View Answer
Answer: c
Explanation: None
1. Pointer variable is used to hold the _________ of the variable.
a) Value
b) Address
c) Value and Address
d) Name of the variable
View Answer
Answer: b
Explanation: By definition.
2. Which among the given operators is referred to as ‘address of’
operator?
a) *
b) ^
c) &
d) ~
View Answer
Answer: c
Explanation: The ‘&’ is a unary operator that returns the memory
address of its operand.
For example,
int* ip;
int num = 10;
ip = #
puts into ip the memory address of the variable num. This address is the
location of the variable in the computer’s internal memory.
Advertisement
3. Choose the correct statement among the given statements?
a) Use of return statement is necessary in every function
b) Return statement may not be followed by a parenthesis
c) A program may contain more than one return statement
d) Return statement may not return a value
View Answer
Answer: a
Explanation: None.
4. What is the size of a char pointer?
a) 1 byte
b) 2 byte
c) 3 byte
d) 4 byte
View Answer
Answer: b
Explanation:
class UnsafeCode
{
unsafe static void Main()
{
char ch;
Console.WriteLine(sizeof(char));
Console.ReadLine();
}
}
The sizeof() method helps in calculating size of char pointer.
5. After incrementing a float pointer ptr by 1 it would be incremented by
__________
a) 1 byte
b) 2 bytes
c) 3 bytes
d) 4 bytes
View Answer
Answer: d
Explanation: None.
6. Which of the following job is done by the instruction ++*p for an
integer pointer p?
a) increment value contained at address p
b) increment address contained in p
c) Both increment value contained at address p and increment address
contained in p
d) neither increment value contained at address p nor increment address
contained in p
View Answer
Answer: a
Explanation:
class UnsafeCode
{
unsafe static void Main()
{
int n = 10;
int* p = &n;
Console.WriteLine(*p);
}
}
Output :
10 + 1 = 11.
7. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int a = 2;
6. int b = 4;
7. int *a1 = &a;
8. int *b1 = &b;
9. Console.WriteLine(*a1 + *b1);
10. }
11.}
a) 6
b) print garbage value
c) print -6
d) print address of b + a
View Answer
Answer: a
Explanation: The (*) operator prints the value stored at address (&) of ‘a’.
Output :
4+2=6
8. What will be the output of the following C# code segment?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int n = 10;
6. void* p = &n;
7. Console.WriteLine(*p);
8. Console.ReadLine();
9. }
10. }
a) The program will print 10
b) Run time error
c) Compile time error
d) Output is the address contained in p
View Answer
Answer: c
Explanation: The program will result in compile time error because void
pointer cannot point anywhere.
9. Which among the following is referred as an array of pointers?
a) int *p;
b) int (*)p;
c) int p[4];
d) int*[4] p;
View Answer
Answer: d
Explanation: None.
10. Among the given pointer which of the following cannot be
incremented?
a) int
b) char
c) float
d) void
View Answer
Answer: d
Explanation: None.
11. How many values can be returned from a function simultaneously
using pointers?
a) 1
b) 2
c) 3
d) as many as user wants
View Answer
Answer: d
Explanation: None.
12. Consider an integer pointer . *a.++*a will increment ___________
while *a++ will increment __________
a) value at a, address contained in a
b) value at a,value at a
c) address contained in a, address contained in a
d) address contained in a, value at a
View Answer
Answer: a
Explanation: None.
13. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int* a;
6. int a1 = 10;
7. int b1;
8. b1 = *&a1;
9. a = &b1;
10. {
11. Console.WriteLine(*a);
12. Console.ReadLine();
13. }
14. }
15.}
a) program will print garbage value
b) program will print address of a
c) program will print value of a1
d) program will print address of a1
View Answer
Answer: c
Explanation: The address of variable a1 is stored in variable b1 by making
a1 as a pointer to variable b1. Later, variable b1 address is stored in
pointer a and hence using pointer operation value of a1 is displayed in a.
Output : 10
14. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int n = 10;
6. int* p = &n;
7. int** p1 = &p;
8. int*** p2 = &p1;
9. Console.WriteLine(*p * **p1 * ***p2);
10. Console.ReadLine();
11. }
12.}
a) compile time error
b) garbage value is printed
c) program will print 1000
d) program will print 100
View Answer
Answer: c
Explanation: None.
Output :1000
15. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int* p;
6. p = (int*)(65535);
7. Console.WriteLine((uint)p);
8. Console.ReadLine();
9. }
10.}
a) compile time error
b) garbage value
c) program prints value at address 65535
d) program prints 65535
View Answer
Answer: d
Explanation: None.
Output :
65535
1. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int m = 10;
6. int *mptr = &m;
7. int **ptr = &mptr;
8. int n = 20;
9. int *nptr = &n;
10. int **prt = &nptr;
11. m = **prt + *nptr;
12. n = *mptr* **prt;
13. Console.WriteLine(n + " " + m);
14. Console.ReadLine();
15. }
16. }
a) 20 200
b) 40 200
c) 800 40
d) 40 800
View Answer
Answer: c
Explanation: None.
Output: 800 40
Advertisement
2. What will be the output of the following C# code snippet?
1. unsafe static void Main()
2. {
3. int a = 5;
4. int b = 5;
5. int c = 5;
6. int*[] ptr = new int* [3];
7. ptr[0] = &a;
8. ptr[1] = &b;
9. ptr[2] = &c;
10. for (a = 0; a < 3; a++)
11. {
12. c += *ptr[a];
13. Console.WriteLine(c);
14. }
15. Console.ReadLine();
16. }
a) 5 10
b) 10 20
c) Compile time error
d) 5 10 20
View Answer
Answer: d
Explanation: None.
Output:5 10 20
3. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int* ptrs = stackalloc int[3];
6. ptrs[0] = 1;
7. ptrs[1] = 2;
8. ptrs[2] = 3;
9. for (int i = 2; i >= 0; --i)
10. {
11. ptrs[i] = ptrs[i]* 3;
12. ptrs[i] = ptrs[i] + 4;
13. Console.WriteLine(ptrs[i]);
14. }
15. Console.ReadLine();
16. }
17.}
a) 20, 10, 7
b) 13, 10, 7
c) 6, 9, 3
d) Compile time error
View Answer
Answer: b
Explanation: None.
Output: 13, 10, 7
4. Among the given pointers which of the following cannot be
incremented?
a) int
b) char
c) float
d) void
View Answer
Answer: d
Explanation: None.
5. A structure pointer points to __________
a) first member of structure
b) first two members of structure
c) whole structure
d) only to the last member of structure
View Answer
Answer: c
Explanation: None.
6. What will be the declaration of the variable ptr as the pointer to array
of 6 floats?
a) float *ptr[6]
b) float [6]*ptr
c) float(*ptr)[6]
d) float(*ptr)(6).
View Answer
Answer: c
Explanation: None.
7. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. char[] arr = { 'A', 'B', 'C', 'D', 'E' };
6. fixed (char* P = arr)
7. {
8. int i;
9. for (i = 0 ;i < 5 ;i++)
10. if (*P % 2 == 0)
11. ++*P;
12. else
13. (*P)++;
14. Console.WriteLine(arr);
15. }
16. Console.ReadLine();
17. }
18. }
a) ACCEE
b) FBCDE
c) BBDDF
d) BBCEE
View Answer
Answer: b
Explanation: None.
Output:FBCDE
8. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int[] nums = new int[10];
6. Console.WriteLine("Index pointer like array.");
7. fixed (int* p = nums)
8. {
9. for (int i = 0 ;i <10 ;i++)
10. p[i] = i;
11. for (int i = 10 ;i >0 ;i--)
12. Console.WriteLine("p[{0}]: {1} ", i, p[i]);
13. Console.ReadLine();
14. }
15. }
16. }
a) p[10] :0, p[9] :9, p[8] :8…..p[1]:1
b) p[10] : 1, p[9] :2, p[8] :3…..p[1] :0
c) p[1] : 1, p[2] :2, p[3] :3…..p[10] :0
d) Compile time error
View Answer
Answer: a
Explanation: None.
Output:Index pointer like array:
p[10] :0, p[9] :9, p[8] :8…p[1]:1
9. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. string str = "this is a test";
6.
7. fixed (char* p = str)
8. {
9. for (int i = str.Length-1 ;p[i] != 0 ;i--)
10. Console.Write(p[i]);
11. }
12. Console.ReadLine();
13. }
14. }
a) test a is this
b) compile time error
c) tset a si siht
d) run time error
View Answer
Answer: c
Explanation: Reversal of string using pointers.
Output:tset a si siht
10. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int* p ;
6. int ch = 13*5;
7. p = &(ch);
8. Console.WriteLine(Convert.ToChar(*p));
9. if (*p == 'A')
10. Console.WriteLine(Convert.ToBoolean(1));
11. else
12. Console.WriteLine(Convert.ToBoolean(0));
13. Console.ReadLine();
14. }
15. }
a) 65 False
b) 65 1
c) A True
d) A 1
View Answer
Answer: c
Explanation: Convert.Tochar(*p) = A
Convert.ToBoolean(1) = True
Output: A
True
1. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. struct MyStruct
4. {
5. public int a;
6. public int b;
7. public int Sum()
8. {
9. return a * b;
10. }
11. }
12. unsafe static void Main()
13. {
14. MyStruct o = new MyStruct();
15. MyStruct* p;
16. p = &o;
17. p->a = 10;
18. p->b = 20;
19. Console.WriteLine("Value is " + p->Sum());
20. Console.ReadLine();
21. }
22.}
a) Compile time error
b) Run time error
c) 200
d) 30
View Answer
Answer: c
Explanation: A pointer can point to an object of a structure type as long
as the structure does not contain reference types. When we access a
member of a structure through a pointer, we must use the arrow
operator, which is –>, rather than the dot (.) operator.
Output : 200
Advertisement
2. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. struct MyStruct
4. {
5. public int a;
6. public int b;
7. public int Sum()
8. {
9. return a / b;
10. }
11. }
12. unsafe static void Main()
13. {
14. MyStruct o = new MyStruct();
15. MyStruct* p;
16. p = &o;
17. p->a = 60;
18. p->b = 15;
19. int c = 30;
20. Console.WriteLine("Value is : " + p->Sum()*c);
21. Console.ReadLine();
22. }
23.}
a) Compile time error
b) 120
c) Run time error
d) 4
View Answer
Answer: b
Explanation: None.
Output :
120
3. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int[] nums = new int[10];
6. fixed (int* p = &nums[0], p2 = nums)
7. {
8. if (p == p2)
9. Console.WriteLine("p and p2 point to same address.");
10. Console.ReadLine();
11. }
12. }
13.}
a) Run time error
b) Compile time error
c) p and p2 point to the same address
d) Only p2
View Answer
Answer: c
Explanation: None.
Output:
p and p2 point to same address
4. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. static void Main()
4. {
5. int? count = null;
6. int? result = null;
7. int incr = 10;
8. result = count + incr;
9. if (result.HasValue)
10. Console.WriteLine("result has this value: " + result.Value);
11. else
12. Console.WriteLine("result has no value");
13. Console.ReadLine();
14. }
15.}
a) Run time error
b) 0
c) Result has no value
d) Compile time error
View Answer
Answer: c
Explanation: A nullable object can be used in expressions that are valid
for its underlying type. When non-nullable and nullable types are mixed
in an operation, the outcome is a nullable value.
Output:
result has no value
5. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. static void Main()
4. {
5. int count = 100;
6. int? result = null;
7. int incr = 10;
8. result = count + incr;
9. if (result.HasValue)
10. Console.WriteLine("result has this value: " + result.Value);
11. else
12. Console.WriteLine("result has no value");
13. Console.ReadLine();
14. }
15.}
a) Run time error
b) 110
c) Result has no value
d) Compile time error
View Answer
Answer: b
Explanation: None.
Output: result has this value :
110
6. Choose the statement which defines the Nullable type Correctly:
a) A special version of a value type that is represented by a structure
b) A nullable type can also store the value null
c) Nullable types are objects of System.Nullable, where T must be a non
nullable value type
d) All of the mentioned
View Answer
Answer: d
Explanation: A nullable type is a special version of the value type that is
represented by a structure. In addition to the values defined by the
underlying type, a nullable type can also store the value null. Thus, a
nullable type has the same range and characteristics as its underlying
type. It simply adds the ability to represent a value which indicates that a
variable of that type is unassigned. Nullable types are objects of
System.Nullable<T>, where T must be a nonnullable value type.
7. What does the following code depicts?
i. System.Nullable count;
ii. bool? done;
a) Code i declares the objects of nullable of type Nullable<T> defined in
the System namespace
b) Code ii declares a nullable type in much shorter and in more
commonly used way using ‘?’
c) Both Code i declares the objects of nullable of type Nullable<T>
defined in the System namespace & Code ii declares a nullable type in
much shorter and in more commonly used way using ‘?’
d) None of the mentioned
View Answer
Answer: c
Explanation: None.
8. Which operator is commonly used to find the size of the type of C#?
a) size()
b) sizeof(type)
c) both size() & sizeof(type)
d) none of the mentioned
View Answer
Answer: b
Explanation: None.
9. What will be the output of the following C# code snippet?
1. unsafe struct FixedBankRecord
2. {
3. public fixed byte Name[80];
4. public double Balance;
5. public long ID;
6. }
7. class UnsafeCode
8. {
9. unsafe static void Main()
10. {
11. Console.WriteLine("Size of FixedBankRecord is " +
sizeof(FixedBankRecord));
12. Console.ReadLine();
13. }
14. }
a) Run time error
b) 80
c) 96
d) Compile time error
View Answer
Answer: c
Explanation: The purpose of a fixed-size buffer is to allow the creation of
a struct in which the array of elements that make up the buffer are
contained within the struct. By using a fixed-size buffer, we let the entire
array to be contained within the struct. The overall size of
FixedBankRecord is 96, which is the sum of its members.
Output :
96
10. What will be the output of the following C# code snippet?
1. class UnsafeCode
2. {
3. unsafe static void Main()
4. {
5. int* ptrs = stackalloc int[3];
6. ptrs[0] = 1;
7. ptrs[1] = 2;
8. ptrs[2] = 3;
9. for (int i = 2; i >=0; i--)
10. Console.WriteLine(ptrs[i]);
11. Console.ReadLine();
12. }
13.}
a) 3 2 1
b) 1 2 3
c) None of the mentioned
d) Run time error
View Answer
Answer: a
Explanation: Allocates memory from the stack by using stackalloc. Here,
ptrs is a pointer that receives the address of the memory that is large
enough to hold size of number of objects of type ‘int’. Here, type ‘int’ is a
non reference type. Finally, stackalloc can be used only in an unsafe
context.
Output :
321
1. Which among these access specifiers should be used for main()
method?
a) private
b) public
c) protected
d) none of the mentioned
View Answer
Answer: b
Explanation: main() method must be specified public as it called by
Csharp run time system outside of the program, by default main is
private in nature if no access specifier is used.
2. Which of these is used as default for a member of a class if no access
specifier is used for it?
a) private
b) public
c) protected internal
d) protected
View Answer
Answer: a
Explanation: None.
3. What is the process by which we can control what parts of a program
can access the members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
View Answer
Answer: c
Explanation: None.
advertisement
4. Which of these base class are accessible to the derived class
members?
a) static
b) protected
c) private
d) shared
View Answer
Answer: b
Explanation: None.
5. What will be the output of the following C# code snippet?
Note: Join free Sanfoundry classes at Telegram or Youtube
1. class access
2. {
3. public int x;
4. private int y;
5. public void cal(int a, int b)
6. {
7. x = a + 1;
8. y = b;
9. }
10. }
11. class Program
12. {
13. static void Main(string[] args)
14. {
15. access obj = new access();
16. obj.cal(2, 3);
17. Console.WriteLine(obj.x + " " + obj.y);
18. }
19. }
a) 3 3
b) 2 3
c) Run time error
d) Compile time error
View Answer
Answer: d
Explanation: ‘y’ is defined privately which cannot be accessed outside its
scope.
6. What will be the output of the following C# code snippet?
1. class access
2. {
3. public int x;
4. private int y;
5. public void cal(int a, int b)
6. {
7. x = a + 1;
8. y = b;
9. }
10. public void print()
11. {
12. Console.WriteLine(" " + y);
13. }
14.}
15.class Program
16.{
17. static void Main(string[] args)
18. {
19. access obj = new access();
20. obj.cal(2, 3);
21. Console.WriteLine(obj.x);
22. obj.print();
23. Console.ReadLine();
24. }
25.}
a) 2 3
b) 3 3
c) Run time error
d) Compile time error
View Answer
Answer: b
Explanation: None.
7. What will be the output of the following C# code snippet?
1. class sum
2. {
3. public int x;
4. public int y;
5. public int add (int a, int b)
6. {
7. x = a + b;
8. y = x + b;
9. return 0;
10. }
11.}
12.class Program
13.{
14. static void Main(string[] args)
15. {
16. sum obj1 = new sum();
17. sum obj2 = new sum();
18. int a = 2;
19. obj1.add(a, a + 1);
20. obj2.add(5, a);
21. Console.WriteLine(obj1.x + " " + obj2.y);
22. Console.ReadLine();
23. }
24.}
a) 6, 9
b) 5, 9
c) 9, 10
d) 3, 2
View Answer
Answer: b
Explanation: Here, a = 2, a + 1 = 2 + 1 = 3.
So, a = 2, b = 3.
x = 2 + 3 = 5.
y = 5 + 3 = 8.
Similarly, a = 5, b = a + 1 = 4.
y = 5 + 4 = 9.
Output :
5, 9
8. What will be the output of the following C# code snippet?
1. class static_out
2. {
3. public static int x;
4. public static int y;
5. public int add(int a, int b)
6. {
7. x = a + b;
8. y = x + b;
9. return 0;
10. }
11.}
12.class Program
13.{
14. static void Main(string[] args)
15. {
16. static_out obj1 = new static_out();
17. static_out obj2 = new static_out();
18. int a = 2;
19. obj1.add(a, a + 1);
20. obj2.add(5, a);
21. Console.WriteLine(static_out.x + " " + static_out.y );
22. Console.ReadLine();
23. }
24.}
a) 7 7
b) 6 6
c) 7 9
d) 9 7
View Answer
Answer: c
Explanation: None.
Output :
7, 9
9. Which of these access specifiers must be used for class so that it can
be inherited by another subclass?
a) public
b) private
c) both public & private
d) none of the mentioned
View Answer
Answer: a
Explanation: None.
10. Which of the following statements are incorrect?
a) public members of class can be accessed by any code in the program
b) private members of class can only be accessed by other members of
the class
c) private members of class can be inherited by a subclass, and become
protected members in subclass
d) protected members of a class can be inherited by a subclass, and
become private members of the subclass
View Answer
Answer: c
Explanation: private members of a class cannot be inherited by a
subclass.
11. What will be the output of the following C# code snippet?
1. class test
2. {
3. public int a;
4. public int b;
5. public test(int i, int j)
6. {
7. a = i;
8. b = j;
9. }
10. public void meth(test o)
11. {
12. o.a *= 2;
13. o.b /= 2;
14. }
15. }
16. class Program
17. {
18. static void Main(string[] args)
19. {
20. test obj = new test(10, 20);
21. obj.meth(obj);
22. Console.WriteLine(obj.a + " " + obj.b);
23. Console.ReadLine();
24. }
25. }
a) 20, 40
b) 40, 20
c) 20, 10
d) 10, 20
View Answer
Answer: c
Explanation: None.
Output :
20, 10
12. Accessibility modifiers defined in a class are?
a) public, private, protected
b) public, internal, protected internal
c) public, private, internal, protected internal
d) public, private, protected, internal, protected internal
View Answer
Answer: d
Explanation: None.
1. What are strings in C#?
a) a sequence of characters
b) array of characters
c) objects of built-in data type
d) a reference type
View Answer
Answer: c
Explanation: Generally, a string is defined as a sequence of characters
but it is different in C#. In c++, the string is an array of characters. In case
of C#, strings are objects of the built-in string data type. Thus, a string is
a reference type.
2. Select the namespace in which string class is built?
a) System.Text
b) System.Net
c) System.IO
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
3. Select the interfaces defined by the string class?
a) IComparable
b) IComparable<string>
c) ICloneable
d) All of the mentioned
View Answer
Answer: d
Explanation: None.
Advertisement
4. Choose the constructor type used to build strings from character array.
a) public String(value)
b) public String(char[ ] value, int startIndex, int length)
c) public String(char[ ])
d) all of the mentioned
View Answer
Answer: b
Explanation: public String(char[ ] value) – This form of constructor
constructs a string that contains characters in value
public String(char[ ] value, int startIndex, int length) -The second form
uses length characters from value, beginning at the index specified by
startIndex.
5. Select the operators used for checking the equality in strings:
a) !=
b) >
c) <
d) >=
View Answer
Answer: a
Explanation: None.
6. What does the following C# code set specifies?
1. public static int Compare(string strA, string strB)
a) Comparison is case and culture sensitive
b) Two strings A and B are compared with each other
c) Output is : >0 for (A > B), <0 for (A < B) else ‘0’ for(A=B)
d) All of the mentioned
View Answer
Answer: d
Explanation: Compares the string referred to by strA with strB. Returns
greater than zero if strA is greater than strB, less than zero if strA is less
than strB, and zero if strA and strB are equal. The comparison is case and
culture-sensitive.
7. What will be the output of the following C# code snippet?
1. static void Main(string[] args)
2. {
3. string s1 = "Hello" + "c" + "Sharp";
4. Console.WriteLine(s1);
5. Console.ReadLine();
6. }
a) Hello c Sharp
b) HellocSharp
c) Compile time error
d) Hello
View Answer
Answer: a
Explanation: Here ‘+’ operator works as concatenation for strings.
Output :
Hello c Sharp
8. Which of these operators can be used to concatenate two or more
String objects?
a) +
b) +=
c) &
d) ||
View Answer
Answer: a
Explanation: string s1 = “Hello”+ ” I ” + “Love” + ” ComputerScience “;
Console.WriteLine(s1);
Output :
Hello I Love ComputerScience.
9. What does the following C# code set specify?
1. public static int Compare(string strA, int indexA, string strB, int indexB,
int length, bool ignoreCase)
a) Comparison begins at strA[indexA] and strB[indexB] and runs for
length of characters
b) Returns output > 0 for for strA > strB else < 0 for strA < strB else if strA
= str B output is 0
c) Comparison is culture sensitive and if ignore case is true, comparison
ignores case differences
d) All of the mentioned
View Answer
Answer: d
Explanation: Compares portions of the strings referred to by strA and
strB. The comparison begins at strA[indexA] and strB[indexB] and runs
for length characters. Returns greater than zero if strA is greater than
strB, less than zero if strA is less than strB, and zero if strA and strB are
equal. If ignoreCase is true, the comparison ignores case differences.
Otherwise, case differences matter. The comparison is culture-sensitive.
10. Which string operation does the below-mentioned method define?
1. public static string Concat(string str0, string str1)
a) method returns a string
b) string str1 is concatenated to the end of str0
c) can be used to concatenate any number of strings
d) all of the mentioned
View Answer
Answer: d
Explanation: This method returns a string that contains str1
concatenated to the end of str0. Another form of Concat(), shown here,
concatenates three strings:
public static string Concat(string str0, string str1, string str2). Hence, any
number of strings can be concatenated using this method.
11. Choose the base class for string() method.
a) System.Array
b) System.char
c) System.String
d) None of the mentioned
View Answer
Answer: c
Explanation: String is an alias for the predefined “System.string” class
from which most of the string() methods are derived.
12. Did method use to remove whitespace from the string?
a) Split()
b) Substring()
c) Trim()
d) TrimStart()
View Answer
Answer: c
Explanation: Perfectly removes whitespace from string whereas
TrimStart() removes a string of characters from the end of the string.
1. What will be the output of the following C# code snippet?
1. static void Main(string[] args)
2. {
3. string s1 = "olleH";
4. string s2 = "olleh";
5. if (s1 == s2)
6. Console.WriteLine("Equal");
7. else
8. Console.WriteLine("Unequal");
9. if (s1.Equals(s2))
10. Console.WriteLine("Equal");
11. else
12. Console.WriteLine("Unequal");
13. Console.ReadLine();
14. }
a)
Equal
Unequal
advertisement
b)
Unequal
Equal
c)
Equal
Equal
Subscribe Now: C# Newsletter | Important Subjects Newsletters
d)
Unequal
Unequal
View Answer
Answer: d
Explanation: In the first comparison it is being checked if two strings are
equal or not, but in the second comparison it is checked if two string
references are equal or not. Also the length of the string and characters
match is tested for the equality of strings.
Output :
Unequal
2. What will be the output of the following C# code snippet?
1. static void Main(string[] args)
2. {
3. string s1 = " Ixg";
4. string s2 = s1.Insert(3,"i");
5. string s3 = s2.Insert(5, "o");
6. for (int i = 0; i < s3.Length; i++)
7. Console.WriteLine(s3[i]);
8. Console.ReadLine();
9. }
a) Ixgo
b) Ixig
c) Ixigo
d) Ixg
View Answer
Answer: c
Explanation: Insert() the built in method inserts characters at specified
position mentioned with index positions.
Output:
Ixigo
3. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. char []chars = {'a', 'b', 'c'};
6. String s = new String(chars);
7. Console.WriteLine(s);
8. Console.ReadLine();
9. }
10. }
a) a
b) b
c) c
d) abc
View Answer
Answer: d
Explanation: String(chars) is a constructor of class string, it initializes
string s with the values stored in character array chars,So s contains
“abc”.
Output :
Abc
4. What will be the output of given code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. char []chars = {'a', 'b', 'c'};
6. String s = new String(chars);
7. String s1 = "abcd";
8. int len1 = s1.Length;
9. int len2 = s.Length;
10. Console.WriteLine(len1 + " " + len2);
11. Console.ReadLine();
12. }
13. }
a) 4 0
b) 3 0
c) 3 4
d) 4 3
View Answer
Answer: d
Explanation: None.
Output :
43
5. What will be the output of the following C# code snippet?
1. class A
2. {
3. int i;
4. int j;
5. public A()
6. {
7. i = 1;
8. j = 2;
9. }
10. }
11. class Program
12. {
13. static void Main(string[] args)
14. {
15. A obj1 = new A();
16. Console.WriteLine(obj1.ToString());
17. Console.ReadLine();
18. }
19. }
a) True
b) False
c) String associated with obj1
d) Compile time error
View Answer
Answer: c
Explanation: ToString() is the method of class Object, since it is the
superclass of every class, every object has this method. ToString() returns
the string associated with the calling object.
Output :
ConsoleApplication19.A
6. Which of these constructors is used to create an empty String object?
a) String()
b) String(void)
c) String(0)
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
7. Which of these method of class String is used to obtain length of
String object?
a) get()
b) Sizeof()
c) Lengthof()
d) Length()
View Answer
Answer: d
Explanation: Method Length() of string class is used to get the length of
the object which invoked the method Length().
8. Choose the base class for string() method.
a) System.Array
b) System.char
c) System.String
d) None of the mentioned
View Answer
Answer: c
Explanation: String is an alias for the predefined “System.string” class
from which most of the string() methods are derived.
9. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. String c = "Hello i love Csharp";
6. Boolean var;
7. var = c.StartsWith("hello");
8. Console.WriteLine(var);
9. Console.ReadLine();
10. }
11. }
a) True
b) False
c) 1
d) Run time error
View Answer
Answer: b
Explanation: StartsWith() method is case sensitive “hello” and “Hello”
are treated differently, hence false is stored in var.
10. What is the value returned by the function CompareTo() if the
invoking string is less than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) none of the mentioned
View Answer
Answer: b
Explanation: CompareTo() function returns zero when both the strings
are equal, it returns a value less than zero if the invoking string is less
than the other string being compared and value greater than zero when
invoking string is greater than the string compared to.
11. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. String s1 = "Hello i love Csharp";
6. StringBuilder s2 = new StringBuilder(s1);
7. Console.WriteLine(s1.Equals(s2));
8. Console.ReadLine();
9. }
10.}
a) True
b) False
c) 0
d) Compile time error
View Answer
Answer: b
Explanation: Equals() compares the content of two strings. StringBuilder
class supports many methods which are useful for manipulating dynamic
strings.
Output: False
12. Which of these methods of class String is used to check whether a
given string starts with a particular substring or not?
a) StartsWith()
b) EndsWith()
c) Starts()
d) Ends()
View Answer
Answer: a
Explanation: The StartsWith() determines whether a substring exists at
the beginning of the string.
1. Which of these methods of class String is used to extract a substring
from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
View Answer
Answer: b
Explanation: None.
2. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. String s1 = "one";
6. String s2 = string.Concat(s1 + " " + "two");
7. Console.WriteLine(s2);
8. Console.ReadLine();
9. }
10.}
a) one
b) two
c) one two
d) two one
View Answer
Answer: c
Explanation: Two strings can be concatenated using Concat() method.
Output:
advertisement
one two
3. Which of these methods of class String is used to remove leading and
trailing whitespaces?
a) startsWith()
b) TrimEnd()
c) Trim()
d) TrimStart()
View Answer
Answer: c
Explanation: Removes white space from the string.
4. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. String c = " Hello World ";
6. String s = c.Trim();
7. Console.WriteLine("""+s+""");
8. Console.ReadLine();
9. }
10.}
a) ” Hello World ”
b) “HelloWorld”
c) “Hello World”
d) “Hello”
View Answer
Answer: c
Explanation: Trim() method is used to remove leading and trailing
whitespaces in a string.
Output:
"Hello World"
5. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. String s1 = "CSHARP";
6. String s2 = s1.Replace('H','L');
7. Console.WriteLine(s2);
8. Console.ReadLine();
9. }
10. }
a) CSHAP
b) CSHP
c) CSLARP
d) CSHALP
View Answer
Answer: c
Explanation: Replace() method replaces all occurrences of a single
character in invoking strings with another character. s1.Replace(‘H’,’L’)
replaces every occurrence of ‘H’ in CSHARP by ‘L’, giving CSLARP.
Output:
CSLARP
6. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. String s1 = "Hello World";
6. String s2 = s1.Substring(0, 4);
7. Console.WriteLine(s2);
8. Console.ReadLine();
9. }
10. }
a) Hello
b) Hell
c) H
d) Hello World
View Answer
Answer: b
Explanation: None.
Output:
Hell
7. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. String s = "Hello World";
6. int i = s.IndexOf('o');
7. int j = s.LastIndexOf('l');
8. Console.WriteLine(i + " " + j);
9. Console.ReadLine();
10. }
11. }
a) 9 5
b) 4 9
c) 9 0
d) 9 4
View Answer
Answer: b
Explanation: None.
Output:
49
8. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. String c = "i love Csharp";
6. bool a;
7. a = c.StartsWith("I");
8. Console.WriteLine(a);
9. Console.ReadLine();
10. }
11.}
a) true
b) false
c) 0
d) 1
View Answer
Answer: b
Explanation: StartsWith() method is case sensitive “i” and “I” are treated
differently, hence false is stored in a.
Output:
False
9. What will be the output of the following C# code snippet?
1. class Program
2. {
3. static void Main(string[] args)
4. {
5. String []chars = {"z", "x", "y", "z", "y"};
6. for (int i = 0; i < chars.Length; ++i)
7. for (int j = i + 1; j < chars.Length; ++j)
8. if(chars[i].CompareTo(chars[j]) == 0)
9. Console.WriteLine(chars[j]);
10. Console.ReadLine();
11. }
12.}
a) zx
b) xy
c) zy
d) yz
View Answer
Answer: c
Explanation: compareTo() function returns zero when both the strings
are equal. It returns a value less than zero if the invoking string is less
than the other string being compared and a value greater than zero if the
invoking string is greater than the string compared to 4
Output:
Zy
10. What will be the output of the following C# code snippet?
1. static void main(String args[])
2. {
3. char chars[] = {'a', 'b', 'c'};
4. String s = new String(chars);
5. Console.WriteLine(s);
6. }
a) a
b) b
c) ab
d) abc
View Answer
Answer: d
Explanation: None.
Output:
Abc
11. What will be the output of the following C# code snippet?
1. static void Main(string[] args)
2. {
3. string s = " i love you";
4. Console.WriteLine(s.IndexOf('l') + " " + s.lastIndexOf('o') + " " +
s.IndexOf('e'));
5. Console.ReadLine();
6. }
a) 3 5 7
b) 4 5 6
c) 3 9 6
d) 2 4 6
View Answer
Answer: c
Explanation: indexof(‘l’) and lastIndexof(‘o’) are pre-defined function
which are used to get the index of first and last occurrence of the
character pointed by l and c respectively in the given array.
Output:
3, 9, 6