CSharp Quiz
CSharp Quiz
Niels Hallenberg
IT University of Copenhagen
www.itu.dk
Why a C# Quiz
– Align C# lectures.
– Purpose of C# lectures to know the basics
necessary for F#.
– We also want to be able to explain how LINQ
works.
– The basics can be hard to comprehend
(delegates, lamda’s, extension methods, type
inference, anonymous objects, function types,…).
– There are 1000 details about C# - not important
here – we can always pose questions about C#
that we can’t answer – in that way C# is not the
most beautiful language ever created.
www.itu.dk
1
“C# will keep growing and
some day it may collapse
under its own weight”
Anders Hejlsberg
at the IT University 2007
(quoted from memory)
www.itu.dk
C# Quiz (CSharp_Quiz.sln)
Q1 The variable a.x below represents
a) an object of a class
b) an instance method
c) an instance variable
d) none of the above
class Foo {
int x = 100;
public static void Main() {
Foo f = new Foo();
Console.WriteLine(a.x);
}
}
www.itu.dk
2
C# Quiz
Q2 The statement below
a) creates a Button control
b) initializes a button control
c) instantiates button control
www.itu.dk
C# Quiz
Q3 What is true about a Constructor?
a) it is used to create and instantiate objects
b) it must have the same name as the class it is
declared within
c) it maybe overloaded
d) a constructor in a possible base class is always
called
e) a, b and c
f) all of the above
www.itu.dk
3
C# Quiz
Q4 Does the program below compile?
a) Yes public class Top {
b) No public Top(int i) {
Console.WriteLine("Top " + i);
}
}
public class Buttom : Top {
public Buttom() {
Console.WriteLine("Buttom");
}
}
class Program {
static void Main(string[] args) {
Buttom b = new Buttom();
}
}
www.itu.dk
C# Quiz
Q5 A delegate defines
a) a concept of passing methods as first class
values
b) a class that encapsulates methods
c) a means of passing collections into methods
d) a governmental representative
www.itu.dk
4
C# Quiz
Q6 Polymorphism occurs when a method in a child
class
a) override a method in the parent class by
extending the parameter list but maintains the
same return type.
b) maintain the same method signature as the
method in the parent class – however the
implementation differs.
c) override a method in the parent class by
maintaining the same parameter list but changes
the return type.
d) a and c
www.itu.dk
C# Quiz
Q7 Method Hiding – public class A {
public void foo() {
what is the output? Console.WriteLine("A.foo");
a) A.foo B.foo A.foo }
}
b) A.foo B.foo B.foo public class B : A {
public void foo() {
Console.WriteLine("B.foo");
}
The compiler gives a }
warning – why? class Program {
static void Main(string[] args) {
How to avoid the warning A a = new A();
B b = new B();
and maintain the same
a.foo();
semantics? b.foo();
((A)b).foo();
}
}
www.itu.dk
5
C# Quiz
Same as before without public class A {
warning from the public void foo() {
compiler. Console.WriteLine("A.foo");
}
}
public class B : A {
public new void foo() {
Console.WriteLine("B.foo");
}
}
class Program {
static void Main(string[] args) {
A a = new A();
B b = new B();
a.foo();
b.foo();
((A)b).foo();
}
}
www.itu.dk
C# Quiz
Q8 Method Overriding – public class A {
public virtual void foo() {
what is the output? Console.WriteLine("A.foo");
a) A.foo B.foo A.foo }
}
b) A.foo B.foo B.foo public class B : A {
public override void foo() {
This is what we normally Console.WriteLine("B.foo");
expect and also known }
from Java. }
class Program {
When you put objects of
static void Main(string[] args) {
different inherited types A a = new A();
into a collection and B b = new B();
apply a method on each a.foo();
of them, then Hiding and b.foo();
Overriding makes a huge ((A)b).foo();
difference – and is also }
confusing. }
www.itu.dk
6
C# Quiz
Q9 About abstract classes and methods. Does the
program below compile?
a) Yes
b) No public abstract class AbstractTop {
public abstract int foo(int i);
public int foo2(int i) {
Console.WriteLine("foo2: " + i);
return i;
}
}
public class NotAbstractTop : AbstractTop {
public int foo(int i) {
Console.WriteLine("foo: " + i);
return i;
}
}
www.itu.dk
C# Quiz
Q10 Can a sealed class be a base class?
a) Yes
b) No
www.itu.dk
7
C# Quiz
Q12 Say you are coding a threaded GUI application based on
Windows Forms. Can you safely update the value of a GUI
control outside the main GUI thread?
a) Yes
b) No
www.itu.dk
C# Quiz –
IEnumerable, IEnumerator, Collections
Q14 For the code below to compile and run the minimum
requirements to xs are:
a) xs implements IEnumerable foreach (var i in xs)
Console.WriteLine(i);
b) xs implements IEnumerator
c) xs implements both
8
C# Quiz – Generics
– Q16 Does the public interface IA { void fooA(); }
nonsense to the public interface IB { void fooB(); }
right compile? public class MyGeneric<T> where T : IA, IB {
T x;
a) Yes public MyGeneric(T x) {this.x = x;}
b) No public void pp() { this.x.fooA();
this.x.fooB(); }
}
public class AB : IA {
public void fooA() {Console.WriteLine("fooA");}
public void fooB() {Console.WriteLine("fooB");}
}
class Program {
static void Main(string[] args) {
AB ab = new AB();
var abInt = new MyGeneric<AB>(ab);
abInt.pp();
}
}
www.itu.dk
www.itu.dk
9
C# Quiz – Indexers and Operators
Q19 What is true about indexers?
a) You can index on integer values
b) You can index on string and integer values
c) You can overload indexers
d) You can have multidimensional indexers
e) You can index on other values than string and integers
Choose all that is true.
www.itu.dk
www.itu.dk
10
C# Quiz – Type Inference
www.itu.dk
C# Quiz – Extensions
Q26 How can you see that the code below is an extension method
and to what type and how many arguments does the method
Print take?
www.itu.dk
11
C# Quiz – Extensions
www.itu.dk
www.itu.dk
12
C# Quiz – Just Wierd
public interface I1 { }
public class Foo1 : I1 { }
public class Foo2 { }
class Program {
static void Main(string[] args) {
IEnumerable<I1> foo = new Foo1[] {new Foo1(),new Foo1()};
foreach (Foo2 x in foo) {
Console.WriteLine(x);
}
}
}
www.itu.dk
13