C# Set 7 (30 mcqs)

1. Correct way to implement a read only property add in a math class?
a)
1. class math
2. {
3.     int ad;
4.     public int add
5.     {
6.         get
7.         {
8.             return ad;
9.         }
10.     }
11.
12. }
b)
1. class math
2. {
3.    public int add
4.    {
5.        get
6.        {
7.            return ad;
8.        }
9.    }
10.}
c)
1. class math
2. {
3.     int ad;
4.     public int add
5.     {
6.         get
7.         {
8.             return ad;
9.         }
10.         set
11.         {
12.             ad = value;
13.         }
14.     }
15. }
d) None of the mentioned

2. Correct way to implement a write only property add in a math class?
a)
1. class math
2. {
3.     public int add
4.     {
5.         set
6.         {
7.              add = value;
8.          }
9.      }
10.  }
b)
1. class math
2. {
3.     int ad;
4.     public int add
5.     {
6.         set
7.         {
8.             ad = value;
9.         }
10.     }
11. }
c)
1.class math
2.{
3.    int ad;
4.    public int add
5.    {
6.        get
7.        {
8.            return ad;
9.        }
10.        set
11.        {
12.            ad = value;
13.        }
14.    }
15.}
d) None of the mentioned

3. Select the correct statement about properties in C#.NET?
a) A property can simultaneously be read or write only
b) A property can be either read only or write only
c) A write only property will have only get accessor
d) A read only property will have only get accessor

4. What will be the output of following snippet of code?
1.     class number
2.     {
3.   private int num1;
4.   private int num2;
5.   public int anumber
6.   {
7.       get
8.       {
9.           return num1;
10.       }
11.       set
12.       {
13.           num1 = value;
14.       }
15.   }
16.   public int anumber1
17.   {
18.       get
19.       {
20.           return num2;
21.       }
22.       set
23.       {
24.           num2 = value;
25.       }
26.   }
27.     }
28.     class Program
29.     {
30.   public static void Main(string[] args)
31.   {
32.       number p = new number();
33.       p.anumber = 20;
34.       number k = new number();
35.       k.anumber1 = 40;
36.       int m = p.anumber;
37.       int t = k.anumber1;
38.       int r = p.anumber + k.anumber1;
39.       Console.WriteLine(“number = ” +m);
40.       Console.WriteLine(“number = ” +t);
41.       Console.WriteLine(“sum = ” +r);
42.       Console.ReadLine();
43.   }
44.     }
a) 0
b) Compile time error
c) 60
d) None of the above mentioned

5. What will be the output of following snippet of code?
1.class number
2.{
3.    private int num1 = 60;
4.    private int num2 = 20;
5.    public int anumber
6.    {
7.        get
8.        {
9.            return num1;
10.        }
11.        set
12.        {
13.            num1 = value;
14.        }
15.    }
16.    public int anumber1
17.    {
18.        get
19.        {
20.            return num2;
21.        }
22.        set
23.        {
24.            num2 = value;
25.        }
26.    }
27.}
28.class Program
29.{
30.    public static void Main(string[] args)
31.    {
32.        number p = new number();
33.        number k = new number();
34.        int m = p.anumber;
35.        int t = k.anumber1;
36.        int r = p.anumber * k.anumber1;
37.        Console.WriteLine(“sum = ” + r);
38.        Console.ReadLine();
39.    }
40.}
a) 0
b) 120
c) 1200
d) Compile time error

6. What will be the output of following snippet of code?
1.     class number
2.     {
3.   int length = 50;
4.   public int number1
5.   {
6.       get
7.       {
8.           return length;
9.       }
10.       set
11.       {
12.           length = value;
13.       }
14.   }
15.     }
16.     class Program
17.     {
18.   public static void Main(string[] args)
19.   {
20.       number p = new number();
21.       p.number1 = p.number1 + 40;
22.       int k = p.number1 * 3 / 9;
23.       Console.WriteLine(k);
24.       Console.ReadLine();
25.   }
26.     }
a) 0
b) 180
c) 30
d) Compile time error

7. What will be the output of following snippet of code?
1.class number
2.{
3.    int length = 60;
4.    public int number1
5.    {
6.        get
7.        {
8.            return length;
9.        }
10.    }
11.}
12.class Program
13.{
14.    public static void Main(string[] args)
15.    {
16.        number p = new number();
17.        int l;
18.        l = p.number1 + 40;
19.        int k = l * 3 / 4;
20.        Console.WriteLine(k);
21.        Console.ReadLine();
22.    }
23.}
a) 30
b) 75
c) 80
d) 0

8. What will be the output of following snippet of code?
1.class student
2.{
3.    int []scores = new int[5] {23, 42, 54, 11, 65};
4.    public int this[int index]
5.    {
6.        get
7.        {
8.            if (index < 5)
9.            return scores[index];
10.            else
11.            {
12.                Console.WriteLine(“invalid index”);
13.                return 0;
14.            }
15.        }
16.        set
17.        {
18.            if (index < 5)
19.            scores[index] = value;
20.            else
21.            Console.WriteLine(“invalid index”);
22.        }
23.    }
24.}
25.class Program
26.{
27.    public static void Main(string[] args)
28.    {
29.        student s = new student();
30.        Console.WriteLine(s[4] + 8);
31.        Console.ReadLine();
32.    }
33.}
a) 73
b) 37
c) 0
d) Run time error

9. Correct way to implement the property for which property reports the error “invalid index” if user attempts to cross bounds of the array for a student class with 5 intger arrays.
a)
1.     class student
2.     {
3.   int []scores = new int[5] {23, 42, 54, 11, 65};
4.   public int this[int index]
5.   {
6.       get
7.       {
8.           if (index < 5)
9.           return scores[index];
10.           else
11.           {
12.               Console.WriteLine(“invalid index”);
13.               return 0;
14.           }
15.       }
16.       set
17.       {
18.           if (index < 5)
19.           scores[index] = value;
20.           else
21.           Console.WriteLine(“invalid index”);
22.       }
23.   }
24.     }
b)
1.     class student
2.     {
3.   int []scores = new int[5] {23, 42, 54, 11, 65};
4.   public int this[int index]
5.   {
6.       get
7.       {
8.           if (index < 5)
9.           return scores[index];
10.           else
11.           {
12.               Console.WriteLine(“invalid index”);
13.               return 0;
14.           }
15.       }
16.   }
17.     }
c)
1.     class student
2.     {
3.   int []scores = new int[5]{23, 42, 54, 11, 65};
4.   public int this[int index]
5.   {
6.       set
7.       {
8.           if (index < 5)
9.           return scores[index];
10.           else
11.           {
12.               Console.WriteLine(“invalid index”);
13.               return 0;
14.           }
15.       }
16.   }
17.     }
d) None of the above mentioned
View Answer
Answer: a, b
Explanation : None.
10. What will be the output of following snippet of code?
1.     class student
2.     {
3.   int []scores = new int[3] {13, 32, 24};
4.   public int this[int index]
5.   {
6.       get
7.       {
8.           if (index < 3)
9.           return scores[index];
10.           else
11.           {
12.               Console.WriteLine(“invalid index”);
13.               return 0;
14.           }
15.       }
16.       private  set
17.       {
18.           if (index < 3)
19.           scores[index] = value;
20.           else
21.           Console.WriteLine(“invalid index”);
22.       }
23.   }
24.     }
25.     class Program
26.     {
27.   public static void Main(string[] args)
28.   {
29.       student s = new student();
30.       int[] scores1 = new int[3] {8, 19, 40};
31.       for (int i = 0; i < 3; i++)
32.       {
33.           if (scores1[i] > s[i])
34.           {
35.               Console.WriteLine(” scores1 had greater value :” + scores1[i]);
36.           }
37.           else
38.           {
39.               Console.WriteLine(“scores had greater value :” + s[i]);
40.           }
41.       }
42.       Console.ReadLine();
43.   }
44.     }
a) 0
b) Compile time error
c) Run time error
d) scores had greater value : 13
scores had greater value : 32
scores1 had greater value : 40

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

12.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 = nums.Where(n => n > 0).Select(r => r*2).OrderByDescending(r=>r);
7.       Console.Write(“The positive values in nums: “);
8.       foreach(int i in posNums)
9.       Console.Write(i + ” “);
10.       Console.WriteLine();
11.       Console.ReadLine();
12.   }
13.     }
a) code run successfully prints nothing
b) run time error
c) code run successfully prints multiple of 2
d) compile time error

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

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

15. Choose the namespace in which Expression trees are encapsulated:
a) System.Linq
b) System.Linq.Expressions
c) System.Text
d) System.Collections.Generic

16. For the given set of 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 b & c

17. For the given set of code what is output representing?
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

18. For the given set of code what is output?
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

19. For the given set of code what is output?
1.    class Program
2.    {
3.  static void Main(string[] args)
4.  {
5.      string[] strs = { “.com”, “.net”, “facebook.com”, “google.net”, “test”, “netflix.net”, “hsNameD.com” };
6.      var netAddrs = from addr in strs
7.                     where addr.Length > 4 && addr.EndsWith(“.net”,
8.                     StringComparison.Ordinal)
9.                     select addr;
10.      foreach (var str in netAddrs) Console.WriteLine(str);
11.      Console.ReadLine();
12.  }
13.    }
a) Compile time error
b) Run time error
c) facebook.com
netflix.net
google.net
d) google.net
netflix.net

20. For the given set of code what is output?
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

21. Which operator among following signifies the destructor operator?
a) ::
b) :
c) ~
d) &.

22. A method called by clients of a class to explicitly release any resources like network,connection,open files etc.When the object is no longer required?
a) Finalize()
b) End()
c) Dispose()
d) Close()

23. Name a method same name as that of class which is used to destroy objects also called automatically when application is finally on process of being getting terminated.
a) Constructor
b) Finalize()
c) Destructor
d) End

24. Which of the following statements are correct?
a) There is one garbage collector per program running in memory
b) There is one comman garbage collector for all programs
c) To garbage collect an object set all references to it to null
d) We have to specify run the garbage collector after executing VISUAL STUDIO.NET

25. Operator used to free the memory when memory is allocated ?
a) new
b) free
c) delete
d) None of the mentioned

26. Select correct statement about destructor in C#?
a) A class can have one destructor only
b) Destructors cannot be inherited or overloaded
c) Destructors can have modifiers or parameters
d) All of above mentioned

27. Output from following set of code ?
1.class sample
2.{
3.    int i;
4.    double k;
5.    public sample (int ii, double kk)
6.    {
7.        i = ii;
8.        k = kk;
9.        double j = (i) + (k);
10.        Console.WriteLine(j);
11.    }
12.    ~sample()
13.    {
14.        double j = i – k;
15.        Console.WriteLine(j);
16.    }
17.}
18.class Program
19.{
20.    static void Main(string[] args)
21.    {
22.        sample s = new sample(8, 2.5);
23.        Console.ReadLine();
24.    }
25.}
a) 0 0
b) 10.5 0
c) Compile time error
d) 10.5 5.5

28. What is return type of destructor ?
a) int
b) void
c) float
d) None of the mentioned.

29. What is output for following set of Code ?
1. class box
2. {
3.     public int volume;
4.     int width;
5.     int height;
6.     int length;
7.     public box ( int w, int h, int l)
8.     {
9.         width = w;
10.         height = h;
11.         length = l;
12.     }
13.    public ~box()
14.    {
15.        volume = width * length * height;
16.
17.    }
18.
19.}
20.class Program
21.{
22.    static void Main(string[] args)
23.    {
24.        box b = new box(4, 5, 9);
25.        Console.WriteLine(b.volume);
26.        Console.ReadLine();
27.    }
28.
29.}
a) 0
b) 180
c) Compile time error
d) None of the mentioned

30. Output from selective set of code is :
1.class box
2.{
3.    public int volume;
4.    int width;
5.    int height;
6.    int length;
7.    public box ( int w, int h, int l)
8.    {
9.        this.width = w;
10.        this.height = h;
11.        this.length = l;
12.    }
13.    ~ box()
14.    {
15.        volume = this.width * this.length * this.height;
16.        console.writeline(volume);
17.    }
18.}
19.class Program
20.{
21.    public  static void Main(string[] args)
22.    {
23.        box b = new box(4, 5, 9);
24.        Console.ReadLine();
25.    }
26.}
a) 0
b) Code execute successfully but print nothing
c) Compile time error
d) 180

Answers

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

Leave a Comment

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