C# Programming
This Test consists of conceptual questions related to C# language for CS and GATE aspirants.
Questions
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();
}
}
- HelloHow are you
- Hello
- It will not compile
- It will give exception
- None of the above
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();
}
}
- Method of class AMethod of class B
- Method of class BMethod of class B
- Method of class AMethod of class A
- Compilation error
- None of the above
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();
}
}
}
- Private variable is not accessible in subclass.
- There is a syntax error in get property.
- It will give exception.
- The code will compile with no error.
- None of the above
Which of the following is/are the true statement(s)?
- Indexer modifier can not be private.
- Indexer can have static and instance members.
- Properties can have instance and static members.
- A get accessor of an indexer has no parameter.
- All of the above.
Which of the following is/are the example of boxing in C#?
- object o=255;int x= (int)o;
- int x=255;object o=x;
- int i = 255;object o = i;i = Convert.ToInt32(o);
- both (2) and (3).
- none of the above
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);
}
}
- It will print (2,4) and (0,0) as output.
- It will print (2,0) and (0,4) as output.
- It will not compile because we can not create the object of structure without using new keyword.
- It will not compile because we can not instantiate a structure.
- None of the above
Which of the following is the incorrect declaration of the delegate in C# language?
- delegate int ThisDelegate (int x)
- public delegate int ThisDelegate (String x)
- public delegate char ThisDelegate (String x)
- delegate char ThisDelegate (String)
- none of the above
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();
}
}
- The private modifier can not be used before array declaration.
- It will throw an exception.
- The static modifier is invalid in the code.
- The 'this' keyword is invalid in the code.
- No error
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]);
}
}
}
- 0
- 20
- 40
- Compilation error
- None of the above
Which of the following is/are the false statement(s)?
- Unboxing can throw InvalidCastException at runtime.
- In boxing, a copy of the value type is taken from the heap to the stack.
- In unboxing, a copy of the value type is taken back from the heap to the stack.
- Casting does not physically move or operate on the object.
- Both (2) and (4).
Which of the following statement(s) is/are false?
- A structure is a value type.
- A structure may contain constructor.
- A structure can not be inherited.
- A structure can not implement a interface.
- None of the above
Which of the following code(s) has error?
- 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(); } }
- 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(); } }
- 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(); } }
- none of the above
- both (2) and (3)
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();
}
}
- Protected members can not be accessed in the subclass.
- Base class can not be inherited.
- Base can class can not be a type of sealed.
- The code will produce exception.
- No error
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();
}
}
- The Demo in base class
- The Demo in child class
- Compilation error.
- The code will compile properly but will not produce any output.
- None of the above
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();
}
}
- It will print the method of the base class.
- It will print the method of the child class.
- It will not compile because base class method is not marked as virtual.
- It will not compile because child class method is not marked as override.
- None of the above