0

C# Programming

Description: This Test consists of conceptual questions related to C# language for CS and GATE aspirants.
Number of Questions: 15
Created by:
Tags: Csharp gate cs Programming
Attempted 0/15 Correct 0 Score 0

What will be the output of the following program? using System; class Program { static void Main(string[] args) { String[] s = new String[2]; s[0] = Hello; s[1] = How are you!; foreach (string name: s) { Console.WriteLine( + name); } Console.ReadLine(); } }

  1. HelloHow are you

  2. Hello

  3. It will not compile

  4. It will give exception

  5. None of the above


Correct Option: C
Explanation:

The code will not compile as there is an error in the syntax of foreach loop, there should be foreach (string name in s) instead of foreach (string name: s).

What will be the output of the following code? using System;class A { public void DemoA() { Console.WriteLine(Method of class A); } } class B : A { public void DemoB() { Console.WriteLine(Method of class B); } } class Test { static void Main (string[] args) { A a=new B(); a.DemoA(); a.DemoB(); } }

  1. Method of class AMethod of class B

  2. Method of class BMethod of class B

  3. Method of class AMethod of class A

  4. Compilation error

  5. None of the above


Correct Option: D
Explanation:

The code will not compile as we have an object of type B but base class A does not have the definition of method DemoB of the B class. Hence a.DemoB() is illegal but a.DemoA() is legal.

What is the error in the following code, if any? using System;class Demo { private string name; public void print() { Console.WriteLine(nMy name is + name); } public string Name { get { return name; } set { name = value; } } class Program { static void Main(string[] args) { Demo d = new Demo(); Console.Write(Enter your name:t); d.name = Console.ReadLine(); d.print(); Console.ReadLine(); } } }

  1. Private variable is not accessible in subclass.

  2. There is a syntax error in get property.

  3. It will give exception.

  4. The code will compile with no error.

  5. None of the above


Correct Option: D
Explanation:

Yes, It will compile properly as we get and set the property for the private variable so it is accessible now in subclass too.

Which of the following is/are the true statement(s)?

  1. Indexer modifier can not be private.

  2. Indexer can have static and instance members.

  3. Properties can have instance and static members.

  4. A get accessor of an indexer has no parameter.

  5. All of the above.


Correct Option: C
Explanation:

This is true about properties.

Which of the following is/are the example of boxing in C#?

  1. object o=255;int x= (int)o;

  2. int x=255;object o=x;

  3. int i = 255;object o = i;i = Convert.ToInt32(o);

  4. both (2) and (3).

  5. none of the above


Correct Option: D
Explanation:

Yes, both are the example of boxing.

Which of the following is/are true about the following code? using System;struct A { public int x; public int y; public A(int x, int y) { this.x = x; this.y = y; } public override string ToString() { return ( + x + , + y + ); } } class Test { static void Main() { A a; A b = new A(); a.x = 2; a.y = 4; Console.WriteLine(a={0}, a); Console.WriteLine(b={0}, b); } }

  1. It will print (2,4) and (0,0) as output.

  2. It will print (2,0) and (0,4) as output.

  3. It will not compile because we can not create the object of structure without using new keyword.

  4. It will not compile because we can not instantiate a structure.

  5. None of the above


Correct Option: A
Explanation:

This output will be produced. Struct A is instantiated two times and the objects a and b will define the values accordingly.

Which of the following is the incorrect declaration of the delegate in C# language?

  1. delegate int ThisDelegate (int x)

  2. public delegate int ThisDelegate (String x)

  3. public delegate char ThisDelegate (String x)

  4. delegate char ThisDelegate (String)

  5. none of the above


Correct Option: D
Explanation:

This declaration is incorrect as there is no parameter name defined for the String variable.

What is the error in the following code? using System; class Employee { private string[]name = new string[10]; public static string this[int index] { get { return name[index]; } set { name[index] = value; } } } class Test { public static void Main() { Employee emp = new Employee(); emp[0] = Sachin; emp[1] = Rohit; emp[2] = Amit; Console.WriteLine(Emploees are:); for (int i = 0; i < 3;i++) { Console.WriteLine(emp[i]); } Console.ReadLine(); } }

  1. The private modifier can not be used before array declaration.

  2. It will throw an exception.

  3. The static modifier is invalid in the code.

  4. The 'this' keyword is invalid in the code.

  5. No error


Correct Option: C
Explanation:

We can not use static modifier in indexer declaration as static methods can not be used with a particular instance of the class and the indexer uses the instances of the class.

What will be the output of the following code? using System;abstract class Base { protected int number; public abstract int this[int index]; class Child : Base { public override int this[int index] { get { return number; } set { number = value; } } } class Test { public static void Main() { Child obj = new Child(); obj[0] = 20; obj[1]=40 Console.WriteLine(The value is: + obj[0]); } } }

  1. 0

  2. 20

  3. 40

  4. Compilation error

  5. None of the above


Correct Option: D
Explanation:

It will not compile because there are get and set attribute missed after the indexer's declaration. There should be public abstract int this[int index]{ get; set;} code like to remove this error.

Which of the following is/are the false statement(s)?

  1. Unboxing can throw InvalidCastException at runtime.

  2. In boxing, a copy of the value type is taken from the heap to the stack.

  3. In unboxing, a copy of the value type is taken back from the heap to the stack.

  4. Casting does not physically move or operate on the object.

  5. Both (2) and (4).


Correct Option: B
Explanation:

This is false as boxing refers to take the value type from the stack to the heap.

Which of the following statement(s) is/are false?

  1. A structure is a value type.

  2. A structure may contain constructor.

  3. A structure can not be inherited.

  4. A structure can not implement a interface.

  5. None of the above


Correct Option: D
Explanation:

This is a false statement as a struct can implement the interfaces.

Which of the following code(s) has error?

  1. using System;using System.Collections.Generic;class A{ public void Demo() { Console.WriteLine(The Demo is called); }} class Test { static void Main(string[] args) { A a=new A(); a.Demo(); } }

  2. using System;using System.Collections.Generic;class A{ protected void Demo() { Console.WriteLine(The Demo is called); }} class Test { static void Main(string[] args) { A a=new A(); a.Demo(); } }

  3. using System;using System.Collections.Generic;class A{ protected void Demo() { Console.WriteLine(The Demo is called);}}class B:A { public B() { Demo(); } static void Main(string[] args) { B b=new B(); } }

  4. none of the above

  5. both (2) and (3)


Correct Option: B
Explanation:

This code will give compile error protected members can be accesses either in the same class or in the base class of that class. These protected members cannot be accessed outside the class.

What is the error in the following code? using System;using System.Collections.Generic;sealed class A { protected void Demo() { Console.WriteLine(The Demo is called); } } class B:A { static void Main(string[] args) { B a = new B(); a.Demo(); } }

  1. Protected members can not be accessed in the subclass.

  2. Base class can not be inherited.

  3. Base can class can not be a type of sealed.

  4. The code will produce exception.

  5. No error


Correct Option: B
Explanation:

Yes, class A is of sealed type and we can not inherit a sealed class.

What will be the output of the following code? using System;using System.Collections.Generic;class A { public void Demo() { Console.WriteLine(The Demo in base class); } } class B : A { public override void Demo() { Console.WriteLine(The Demo in child class); } } class test { static void Main(string[] args) { B b = new B(); b.Demo(); } }

  1. The Demo in base class

  2. The Demo in child class

  3. Compilation error.

  4. The code will compile properly but will not produce any output.

  5. None of the above


Correct Option: C
Explanation:

Yes, the code will give compilation error as there is 'virtual' keyword missing in the method of the base class, hence it will not be inherited.

Which of the following is true about the following code? using System;using System.Collections.Generic;class A { public void Demo() { Console.WriteLine(The Demo in base class); } } class B : A { public new void Demo() { Console.WriteLine(The Demo in child class); } } class test { static void Main(string[] args) { A b = new B(); b.Demo(); } }

  1. It will print the method of the base class.

  2. It will print the method of the child class.

  3. It will not compile because base class method is not marked as virtual.

  4. It will not compile because child class method is not marked as override.

  5. None of the above


Correct Option: A
Explanation:

Yes, the base class method is called as we used the new keyword in the child class, so the method can not be override. Hence the method of the B class would not be called.

- Hide questions