NEW
Font size
Worksheetsc# practice
Total questions: 11
Worksheet time: 6mins
What does this code print?
string s = null;
Console.WriteLine(s ?? "Default");
null
Default
Compilation Error
What will happen when this code runs?
int[] arr = new int[3];
Console.WriteLine(arr[3]);
0
null
Compilation Error
Runtime Exception
What is the result of the following code?
int x = 10;
if (x is int)
Console.WriteLine("Integer");
else
Console.WriteLine("Not Integer");
Integer
Not Integer
Error
Boolean
What is the output of the following code?
int a = 5;
int b = 2;
Console.WriteLine(a / b);
2.5
2
3
compile Error
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 + " ");
4 16
2 4
1 3 5
4 8
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);
}
Removes 2 from the list
Removes all even numbers
Runtime Exception
Compilation Error
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();
System.Double
double
T
3.14
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);
System.Int32
T
Int32
System.Object
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
B
A B
B A
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);
}
}
True
False
Null Reference Exception
Compilation error
Which generic constraint allows you to specify that a type must be a class?
where T : value
where T : object
where T : struct
where T : class
