C# Set 5 (30 mcqs)

1. Correct statement about C#.NET code given below is?
1.struct book
2.{
3.    private String name;
4.    private int pages;
5.    private Single price;
6.}
7.book b = new book();
a) New structure can be inherited from struct book
b) When the program terminates, variable b will get garbage collected
c) The structure variable ‘b’ will be created on the stack
d) When the program terminates, variable b will get garbage collected

2. Correct statement about the C#.NET code given below is?
1.class trial
2.{
3.    int i;
4.    float d;
5.}
6.struct sample
7.{
8.    private int x;
9.    private Single y;
10.    private trial z;
11.}
12.sample s = new sample();
a) trial object referred by z created on the stack
b) z is created on the heap
c) Both s and z will be created on the heap
d) s will be created on the stack

3. Choose correct statement which support facts that why C# doesnot allow creation of empty structures?
a) C#.NET supports creation of abstract user-defined data types using structures
b) By having empty structures,it would mean that the new data types has no data associated with which donot make any sense in C#.NET
c) Basic reason to create structures is inability to represent real life objects using standard data types offered by the language
d) All of the above mentioned

4. Choose correct statement about structures why they are defined as value types but not reference typeS?
a) Since space required for structure variables is allocated on stack which is a form of memory that is automatically available when a variable to be used is in scope.
b) Structures generally used to represent user defined data types that consists of small amount of data in them hence using stack for deceleration of such variables is not problem.
c) All of the mentioned
d) None of the mentioned

5. Choose correct statement about structures in C#.NET?
a) Structures can be declared within a procedure
b) Structs can implement an interface but they cannot inherit from another struct
c) Struct members cannot be declared as protected
d) A structure can be empty

6. The correct way to define a variable of type struct abc among following is?
1. struct abc
2. {
3.     public string name;
4.     protected internal int age;
5.     private Single sal;
6. }
a) abc e = new abc();
b) abc e;
c) abc e;
e = new abc;
d) abc e = new abc;

7. Which is the correct way to settle down values into the structure variable ‘e’ defined as?
1. struct emp
2. {
3.     public String name;
4.     public int age;
5.     public Single sal;
6. }
7. emp e = new emp();
a) e.name = “Ankit”;
e.age = 24;
e.sal = 200;
b) With emp e
{
.name = “Ankit”;
.age = 24;
.sal = 200;
}
c) name = “Ankit”;
age = 24;
sal = 200;
d) All of the above mentioned

8. When does structure variable get destroyed?
a) When no reference refers to it,it will get garbage collected
b) Depends on either it is created using new or without new operator
c) As variable goes out of the scope
d) Depends on either we free it’s memory using free() or delete()

9. Calculate number of bytes a structure variable s occupy in memory if it is defined as below shown?
1.  class abc
2.  {
3.      int i;
4.      Decimal d;
5.  }
6.  struct sample
7.  {
8.     private int x;
9.     private Single y;
10.     private trial z;
11.  }
12.  sample s = new sample();
a) 24 bytes
b) 8 bytes
c) 16 bytes
d) 12 bytes

10. Which is the correct output for the C#.NET program code given below?
1.{
2.    struct abc
3.    {
4.        public int i;
5.    }
6.    class Program
7.    {
8.        static void Main(string[] args)
9.        {
10.            sample a = new sample();
11.            a.i = 10;
12.            fun(ref a);
13.            Console.WriteLine(a.i);
14.        }
15.        public static voidn fun(ref sample x)
16.        {
17.            x.i = 20;
18.            Console.WriteLine(x.i);
19.        }
20.    }
21.}
a) 10 10
b) 20 10
c) 10 20
d) 20 20

11. 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

12. Choose the correct statements about the LINQ?
a) The main concept behind the linq is query
b) linq make 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

13. Choose the namespace in which the interface IEnumerable is declared?
a) System.Collections
b) System.Collections.Generic
c) Both a & b
d) None of the mentioned

14. Can we use linq to query against a DataTable?
a) Yes
b) No
c) Either a or b
d) None of the mentioned

15. What will be the output of given 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 >= 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

16. 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

17. Select the output for given 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 execute output
d) compile time error

18. Select the output for given 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) Print nothing code run successfully
b) Run time error
c) Arranged in descending order code run successfully
d) Compile time error

19. Select the output for given 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 run successfully print nothing
b) code run successfully print required output
c) Run time error
d) Compile time error

20. Select the output for given 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 run successfully print nothing
b) Run time error
c) code run successfully print required output
d) Compile time error

21. What is an iterator?
a) a method
b) an operator
c) accessor
d) Both a & b

22. What will be the output of given 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 run successfully prints nothing
d) Code run successfully prints A, B, C, D

23. Choose the correct statements about part of given code part of the code defined above?
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) Both b & c

24. What does the yield return statement specifies in above code snippet?
a) returns the output
b) returns the next object in the collection
c) Both a & b
d) None of the mentioned

25. What will be the given code snippet specifies?
1.class MyClass
2.{
3.    char chrs = ‘A’ ;
4.    public IEnumerator GetEnumerator()
5.    {
6.        for (int i = 20; i >=0; –i)
7.        yield return (char)((chrs + i));
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) 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

26. What will be the given code snippet specifies?
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

27. What will be the output of 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) print nothing code run successfully
b) run time error
c) code run successfully prints even number between 1 to 20
d) Compile time error

28. What will be the output of 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:
A B C D E F G
Iterate letters from F to L:
G H I J K L
b) Iterate the first 7 letters:
A B C D E F G
Iterate letters from F to L:
H I J K L
c) Run time error
d) Compile time error

29. What will be the output of 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

30. What are the advantages of the named iterator?
a) They allow to pass arguments to the iterator that control what elements are obtained
b) This form of iterators can be overloaded
c) Both a & b
d) None of the mentioned

Answers

1-c 2-d 3-d 4-c 5-b,c
6-a,b 7-a 8-c 9-d 10-d
11-c 12-a,b 13-b 14-b 15-b
16-c 17-c 18-c 19-b 20-c
21-a,b,c 22-d 23-a,b,c 24-b 25-c
26-c 27-c 28-b 29-c 30-a,b
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *