0% found this document useful (0 votes)
5K views3 pages

C E1 Competency Test

The document contains 15 multiple choice or true/false questions related to C# 4.0 features such as generics, dynamic typing, covariance and contravariance. Key points covered include: - IEnumerable<T> is not covariant - Dynamic objects allow storing different types in a list - Extension methods are not supported on dynamic types - C# 4.0 does not support safe covariance/contravariance - Contravariance requires the out keyword for output parameters - Covariance requires the in keyword for input parameters

Uploaded by

Abhik Test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5K views3 pages

C E1 Competency Test

The document contains 15 multiple choice or true/false questions related to C# 4.0 features such as generics, dynamic typing, covariance and contravariance. Key points covered include: - IEnumerable<T> is not covariant - Dynamic objects allow storing different types in a list - Extension methods are not supported on dynamic types - C# 4.0 does not support safe covariance/contravariance - Contravariance requires the out keyword for output parameters - Covariance requires the in keyword for input parameters

Uploaded by

Abhik Test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1. An IEnumerable<int> is not an IEnumerable<object>.

(true)

2. What will be the output?


List<dynamic> list = new List<dynamic>();
list.Add(1);
list.Add("Hello World");
foreach (dynamic d in list)
{
Console.WriteLine(d.GetType());
}

System.Int32
System.String

3. Will the following code work in C# 4.0?


IList<string> strings = new List<string>();
IEnumerable<object> objects = strings;
var result = strings.Union(objects);

yes it will work with 1 warning that result is declared but never used.
although

Console.WriteLine(result)=System.Linq.Enumerable+UnionIterator2`1[System.Object]

4. Extension methods are NOT supported on dynamic types.


True

5. Will the following code work in C# 4.0?


var excel = new Application();
excel.Visible = true;
excel.Workbooks.Add();
excel.get_Range("A1").Value = "Process Name";
excel.get_Range("B1").Value = "Memory Usage";

no
error: Compilation failed: 4 error(s), 0 warnings
main.cs(12,25): error CS0246: The type or namespace name `Application' could not
be found. Are you missing an assembly reference?
main.cs(13,14): error CS0131: The left-hand side of an assignment must be a
variable, a property or an indexer
main.cs(15,31): error CS0131: The left-hand side of an assignment must be a
variable, a property or an indexer
main.cs(16,30): error CS0131: The left-hand side of an assignment must be a
variable, a property or an indexer

6. What will be the output here?


IList&lt;string&gt; strings = new List&lt;string&gt;();
IEnumerable&lt;object&gt; objects = strings;
&nbsp;&nbsp;&nbsp; Console.WriteLine(objects.GetType());

wrong copied ... no idea of solution

7. Classes can be declared variant in C# 4.0


False????

8. What will be the output for the code below?


class Foo
{
public struct FooEnumerator
{
int value;
public bool MoveNext() { return true; }

This study source was downloaded by 100000828534912 from CourseHero.com on 02-11-2022 04:41:39 GMT -06:00

https://www.coursehero.com/file/61642484/C-E1-competency-test/
public int Current { get { return value++; } }
}
public FooEnumerator GetEnumerator()
{
return new FooEnumerator();
}
static void Main(string[] args)
{
foreach (int x in (dynamic)new Foo())
{
Console.WriteLine(x);
if (x >= 10) break;
}
}
}

error:
Unhandled Exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert
type 'Foo' to 'System.Collections.IEnumerable'. An explicit conversion exists
(are you missing a cast?)
at (wrapper dynamic-method) System.Object:CallSite.Target
(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSit
e,object)
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet]
(System.Runtime.CompilerServices.CallSite site, T0 arg0) [0x00104] in
<23b73e5bdfc94c3fbbc925d9c8846791>:0
at Foo.Main (System.String[] args) [0x00042] in
<b1286fe853e743049fe8f192ff63763a>:0
[ERROR] FATAL UNHANDLED EXCEPTION:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert
type 'Foo' to 'System.Collections.IEnumerable'. An explicit conversion exists
(are you missing a cast?)
at (wrapper dynamic-method) System.Object:CallSite.Target
(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSit
e,object)
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet]
(System.Runtime.CompilerServices.CallSite site, T0 arg0) [0x00104] in
<23b73e5bdfc94c3fbbc925d9c8846791>:0
at Foo.Main (System.String[] args) [0x00042] in
<b1286fe853e743049fe8f192ff63763a>:0

9. Can we use dynamic in Lambda expressions?


No

10. Will the following code work in C# 4.0?


class Employee { }
class Manager : Employee { }
class Program
{
delegate T MyFunc<T>();
static void Main(string[] args)
{
MyFunc<Manager> mgr = () => new Manager();
MyFunc<Employee> emp = mgr;
}
}

Compilation failed: 1 error(s), 0 warnings


main.cs(16,36): error CS0029: Cannot implicitly convert type
`Program.MyFunc<Manager>' to `Program.MyFunc<Employee>'

11. C# 4.0 does NOT support safe co-and contra-variance.

This study source was downloaded by 100000828534912 from CourseHero.com on 02-11-2022 04:41:39 GMT -06:00

https://www.coursehero.com/file/61642484/C-E1-competency-test/
True

12. Will the following code work in C# 4.0?


class Employee { }
class Manager : Employee { }
class Program
{
delegate void MyAction<out T>(T a);
static void Main(string[] args)
{
MyAction<Employee> action = (emp) => { Console.WriteLine(emp); };
MyAction<Manager> mgr = action;
}
}

Compilation failed: 2 error(s), 0 warnings


main.cs(10,36): error CS1961: The covariant type parameter `T' must be
contravariantly valid on `Program.MyAction<T>(T)'
main.cs(10,23): (Location of the symbol related to previous error)
main.cs(10,36): error CS1961: The covariant type parameter `T' must be
contravariantly valid on `Program.MyAction<T>.BeginInvoke(T,
System.AsyncCallback, object)'
(1,1): (Location of the symbol related to previous error)

13. Contravariance interface and delegate must have _Out___ keyword declaration
for output parameter.

14. Covariance interface and delegate must have __In__ keyword declaration for
input parameter

15. Will the following code work in C# 4.0?


public interface IInputBoxService<out T>
{
bool ShowDialog();
T Result { get; }
}
public class IntegerInputBoxService : IInputBoxService<int>
{
}

IInputBoxService<object> service = new IntegerInputBoxService();

Compilation failed: 1 error(s), 0 warnings


main.cs(15,4): error CS1525: Unexpected symbol `IInputBoxService'

This study source was downloaded by 100000828534912 from CourseHero.com on 02-11-2022 04:41:39 GMT -06:00

https://www.coursehero.com/file/61642484/C-E1-competency-test/
Powered by TCPDF (www.tcpdf.org)

You might also like