wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

c# practice

Total questions: 11

Worksheet time: 6mins

Name
Class
Date
1.

What does this code print?

string s = null;

Console.WriteLine(s ?? "Default");

a)

null

b)

Default

c)

Compilation Error

2.

What will happen when this code runs?

int[] arr = new int[3];

Console.WriteLine(arr[3]);

a)

0

b)

null

c)

Compilation Error

d)

Runtime Exception

3.

What is the result of the following code?

int x = 10;

if (x is int)

Console.WriteLine("Integer");

else

Console.WriteLine("Not Integer");

a)

Integer

b)

Not Integer

c)

Error

d)

Boolean

4.

What is the output of the following code?

int a = 5;

int b = 2;

Console.WriteLine(a / b);

a)

2.5

b)

2

c)

3

d)

compile Error

5.

What will be the result?

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Where(n => n % 2 == 0).Select(n => n * n);

foreach (var num in result)

Console.Write(num + " ");

a)

4 16

b)

2 4

c)

1 3 5

d)

4 8

6.

What happens when this is executed?

List<int> list = new List<int> { 1, 2, 3, 4 };

foreach (var item in list)

{

if (item == 2)

list.Remove(item);

}

a)

Removes 2 from the list

b)

Removes all even numbers

c)

Runtime Exception

d)

Compilation Error

7.

Output of this generic method?

public class MyClass<T>

{

public T Value { get; set; }

public MyClass(T value) { Value = value; }

public void PrintType() => Console.WriteLine(typeof(T));

}

new MyClass<double>(3.14).PrintType();

a)

System.Double

b)

double

c)

T

d)

3.14

8.

What is the output of the following code?

public class Test

{

public void Display<T>(T value)

{

Console.WriteLine(typeof(T));

}

}

new Test().Display(123);

a)

System.Int32

b)

T

c)

Int32

d)

System.Object

9.

What will this code print?

class A

{

public A() { Console.Write("A "); }

}

class B : A

{

public B() { Console.Write("B "); }

}

class Program

{

static void Main() => new B();

}

a)

A

b)

B

c)

A B

d)

B A

10.

What is the output of the following memory-related code?

class Program

{

static void Main()

{

WeakReference wr = new WeakReference(new object());

GC.Collect();

Console.WriteLine(wr.IsAlive);

}

}

a)

True

b)

False

c)

Null Reference Exception

d)

Compilation error

11.

Which generic constraint allows you to specify that a type must be a class?

a)

where T : value

b)

where T : object

c)

where T : struct

d)

where T : class