C# Flashcards

0
Q

What are the 2 floating point types in C#?

A

Float - 32 bits. Literal suffixes with f, e.g. 3.5f
Double - 64 bits. By defaultreal numeric literal treated as double. To force integer to be treated as double literal suffix with d.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What is a floating point number?

A

A floating point number is a method of representing an approximation of a real number that can support a wide range of values. It is represented by a fixed number of digits and a scaled using an exponent. They can be stored efficiently in binary allowing great range but less precision.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare an array of 10 integers named numbers?

A

int[] numbers = new int[10];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the property of strings that mean that they cannot be modified?

A

Immutable. Strings objects cannot be modified after they have been created. Concatenation strings results in a new string object being created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What abstract base type of all arrays types?

A

Array. Arrays are objects in C#, unlike in C and C++ where they are just addressable contiguous areas of memory. It has several useful properties and methods defined on it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a delegate?

A

A delegate is a type-safe function pointer. The delegate type defines a method signature. Delegates do that type can reference methods with a matching signature.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define a delegate Add, that takes in two integer values as parameters

A

private delegate int add(int a, int b);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can delegates be used?

A

Callback methods
Event handlers
Anonymous methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are events?

A

Events enable a class or object to notify other classes or objects when something of interest happens. The event pattern is closely related to the Observer pattern.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Name all 4 access modifiers

A

private - restricted to containing type
public - access not restricted
protected - restricted to containing type or types that derive from it
internal - restricted to current assembly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a constant?

A

Constants are immutable types whose value is known at compile-time. Only the C# built-in types can be declared as const.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Declare a constant for the value of pie

A

const decimal pie = 3.14;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the static keyword do and how can it be used?

A

The static keyword can be used to declare a member static. It can be used on classes, fields, methods, properties, operators, events, and constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a static class and what are its properties?

A

Static classes are classes defined with the static keyword. Static classes cannot be instantiated. All of its members must be static also.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a popular example of a static class in the .NET framework?

A

The Math class. It provides utility methods for performing mathematical operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What design pattern is commonly used to ensure that only a single instance of a class can be created?

A

Singleton. There are a few ways to implement this, each of which has trade-offs. It involves making the constructor private so that instantiation can be controlled via a static method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a field?

A

A field is variable of any type that is declared directly in a class or struct. They are members of their containing type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you prevent a class from being inherited?

A

The sealed keyword prohibits a class from being inherited

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the execution entry point for a C# console application?

A

The Main method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you initiate a string without escaping each backslash?

A

You put an @ sign in front of the double-quoted string.

String ex = @”This has a carriage return\r\n”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the difference between a struct and a class?

A

Structs cannot be inherited. Structs are passed by value and not by reference. Structs are stored on the stack not the heap. The result is better performance with Structs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are namespaces, and how they are used?

A

Namespaces are used to organize classes within the .NET Framework. They dictate the logical structure of the code. They are analogous to Java packages, with the key difference being Java packages define the physical layout of source files (directory structure) while .NET namespaces do not. However, many developers follow this approach and organize their C# source files in directories that correlate with namespaces. The .NET Framework has namespaces defined for its many classes, such as System.Xml–these are utilized via the using statement. Namespaces are assigned to classes via the namespace keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is garbage collection?

A

Garbage collection is a feature of most modern object oriented languages. It is a feature of the runtime that disposes of objects that are no longer referenced

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What does type safe mean? Why is C# type safe where JavaScript for example isn’t?

A

Type safety relates to how a language enforces constraints on the kinds of operations that can be performed on a type such that only valid operations can be performed. For example, the method quack() can’t be called on a swan object that does not implement this method. JavaScript does not enforce this and would not generate an error until runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are exceptions?

A

A feature of many modern object oriented languages it is a mechanism for handling errors. Classes can throw exceptions (objects) that stop the normal execution of code and instead get passed up the calling hierarchy until they are either handled by a try catch finally block, or reach the top of the calling hierarchy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do you pass an object by reference?

A

Use either ref or out keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the difference between ref and out?

A

Both keywords indicate passing a parameter by reference. Ref is to be used for passing value in and out, whereas out is just for method to set value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the affect of modifying a value type within a method where it has been passed by reference?

A

Within the method the parameter is the sae memory location as outside so any modifications to it are reflected outside too.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What happens when within a method you assign a new object to a reference parameter where it is a reference type?

A

As the parameter is a effectively a pointer to a pointer, assigning a new object to it causes the variable outside of the method to point to the new object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Which access modifiers should fields generally be given?

A

Private or protected. Access to fields should be controlled via properties, methods, and indexers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Declare a field called size of type int with private accessibility

A

private int size;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the order in which fields and constructors are initialized/called?

A

Fields are initialized first. Constructors are then called beginning with the base class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is a default parameter? How are they useful?

A

Default parameters are specified with the optional keyword. They can be used in some cases instead of overloading a method. We can omit optional parameter from the method call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What keyword must be present in the definition of a method in the base class in order for a derived class to override the method?

A

The virtual keyword is specified in the base class. The derived method is specified with the overrides keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is this code of the ColorCoOrds class which derives from CoOrds class and what does it do?

public ColorCoOrds(int x, int y) : base (x, y)
{
        color = System.Drawing.Color.Red;
}
A

The ColourCoOrds class calls the constructor on its base class which accepts two integer parameters before it’s own constructor code gets executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What are optional/default parameters?

A

Optional parameters are parameters that can be omitted from method calls. In the method declaration optional parameters are declared by assigning a default value in method signature. Default values must be able to be determined at run-time, e.g. constants, null values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Where must optional/default parameters be specified in a method declaration?

A

Optional parameters must be specified after all required parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How does intellisense indicate an optional parameter?

A

Intellisense displays optional parameters in [square brackets] along with their default value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Declare a void method countdown which takes in an optional int parameter with a default of 10

A

public void Countdown(int timer = 10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What is a named argument?

A

A named argument is where the client has specified the name of the parameter in the method call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Use named arguments to call this method:

public decimal CalculateBMI(int height, int weight) {…}

A

CalculateBMI(weight: 76, height: 180);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Why are named and optional/default arguments useful together?

A

In methods where there are many parameters the use of optional/default and named arguments allow method calls without specifying values for every parameter. Especially useful with COM interfaces such as Microsoft Office

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What are the disadvantages of optional parameters?

A

Optional parameters are implemented by the compiler by inserting the default value into the calling code when no value is supplied. This means that if we make any changes to our parameters then our client code needs to be recompiled to reflect the updates values.

43
Q

What is the alternative to using overloaded methods?

A

Using optional parameters to provide default values for parameters that are not supplied by the calling code.

44
Q

What is a sealed class?

A

A sealed class is a class that cannot be inherited. It is specified using the sealed keyword. System.String is an example of sealed class. It is often done for performance or security reasons.

45
Q

What does the break statement do inside a loop?

A

It breaks out of the loop and continues at the next line following the loop

46
Q

What are the 4 iteration statements available in C#?

A

while
do…while
for
foreach

47
Q

What does a continue statement do inside a loop?

A

It stops executing the code in the current iteration and starts a new iteration.

48
Q

What are the advantages of generic collections over non-generic collections?

A

Generic collections are type-safe. They also provide better performance for value types as they do not require boxing and unboxing values.

49
Q

What are Generic Constraints?

A

Generic Constraints are constraints that allow you to specify constraints on the types that the generic class/struct can be used with. For example, a generic class be declared such that it can be created with only types that implement an interface IAnimal.

50
Q

Derive a generic class MagicHat from Queue

A
class MagicHat : Queue {
...
}
51
Q

Describe the difference between a Thread and a Process?

A

Each process provides the resources needed to execute a program. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads. A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources.

52
Q

What is a Windows Service and how does its lifecycle differ from a “standard” EXE?

A

A windows service always runs when a computer starts up (if configured). A standard exe only runs when a user logs in and stops when the user logs out.

53
Q

What is the maximum amount of memory any single process on Windows can address? Is this different than the maximum virtual memory for the system? How would this affect a system design?

A

Limits depending on OS and processor architecture. 32-bit OS and processor: 2GB. 64-bit OS and processor: 8TB.

54
Q

What is the difference between an EXE and a DLL?

A

An EXE has an entry point to start execution, a DLL doesn’t.

55
Q

What is strong-typing versus weak-typing? Which is preferred? Why?

A

These terms don’t have widely agreed definitions.

56
Q

Juxtapose the use of override with new. What is shadowing?

A

Override is used to replace the definition of a method with another in a derived class. The derived class implementation will be used no matter what. New on the other hand creates a new method which shadows the method in the base class. This new method will be called when an object of the derived class is called using it’s type, but if called via the base class type will use the base class version of the method.

57
Q

Explain the use of virtual, sealed, override, and abstract.

A
virtual - method can be overriden
sealed - class cannot be derived from
override - override a virtual method in a derived class
abstract - used on method or class. abstract class cannot be instantiated.  abstract method within abstract class provides default implementation.  abstract method within non-abstract class can only provide signature, no implementation.
59
Q

Explain the importance and use of each component of this string: Foo.Bar, Version=2.0.205.0, Culture=neutral, PublicKeyToken=593777ae2d274679d

A

This is an example of an assembly full-qualified name. The assembly name, version, culture and public key token. Each assembly is signed with a private key, the public key is used to decrypt and ensures that assembly hasn’t been tampered with.

59
Q

By what mechanism does NUnit know what methods to test?

A

Using custom attributes

60
Q

What benefit do you get from using a Primary Interop Assembly (PIA)?

A

A Primary Interop Assembly is an assembly that contains metadata about a COM library which it wraps.

61
Q

What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;}

A

When you use throw e, the original stack trace is lost. When you use throw; the original stack trace is preserved.

62
Q

Explain what’s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful?

A

The first constructor is calling the parameter-less constructor. It is useful for providing multiple constructors with different parameters.

63
Q

What is the difference between typeof(foo) and myFoo.GetType()?

A

typeof(foo) - returns System.Type. typeof takes a type name.
GetType - returns System.Type of current instance.
Is - checks whether type is in same inheritance tree

64
Q

What is this? Can this be used within a static method?

A

this is a keyword that refers to the current instance of the object.

65
Q

How many processes can listen on a single TCP/IP port?

A

One.

66
Q

What is the GAC? What problem does it solve?

A

Global Assembly Cache.

67
Q

Describe the difference between Interface-oriented, Object-oriented and Aspect-oriented programming.

A

?

68
Q

Describe what an Interface is and how it’s different from a Class.

A

?

69
Q

What is Reflection?

A

?

70
Q

Can DateTimes be null?

A

DateTimes are structs so are value types and therefore cannot be null. DateTimes that have not been initialized will have the minimum value. Nullable types allow us to assign null.

71
Q

How does the generational garbage collector in the .NET CLR manage object lifetime? What is non-deterministic finalization?

A

?

72
Q

What is the difference between Finalize() and Dispose()?

A

?

74
Q

How is the using() pattern useful? What is IDisposable? How does it support deterministic finalization?

A

?

75
Q

What can an interface define signatures for?

A

methods
properties
indexers
events

76
Q

What is polymorphism?

A

It means being able to use different forms of a type without regard to implementation details.

77
Q

What is the Liskov Substitution Principle?

A

Simply put, this means that an instance of a class should function as an instance of its super class—it should be possible to use it in methods instead of the super class without any exceptions or side effects. It also reflects how a class hierarchy should ideally operate, in that a base class should capture the behaviour and properties that all its subclasses have in common.

78
Q

We have a Workshop class that has 2 methods Repair Coachwork and Repair Wheels that take in a HorseDrawnVehicle class. We have Carriage, Wagon, and Sleigh that all derive from HorseDrawnVehicle however Sleigh doesn’t have wheels but HorseDrawnVehicle does. What principle does this break and why?

A

Liskov Subsitution Principle. Sleigh does not fit into the model as it does not have wheels and therefore has undesirable side effects. To fix, modify class hierarchy to include HorseDrawnWheeledVehicle which the others derive from. Repair Wheels method take in this type as parameter.

79
Q

What should we favour, inheritance or delegation?

A

Delegation. Often more flexible.

80
Q

How can events allow us to cancel actions?

A

We can have a class that raises an event prior to the main event which has a class that derives from CancelEventArgs. Based on info in this class the client can cancel the event by setting cancel parameter to true.

81
Q

Where is this pattern commonly used and why?

Action temp = Foo;
if (temp != null)
temp();

A

When we are raising events. It does 2 things; ensures it is not a null delegate and ensures that it is thread safe.

82
Q

How does this pattern avoid a race condition?

Action temp = Foo;
if (temp != null)
temp();

A

Delegates are immutable reference types and therefore assigning to a temp variables creates a copy of the delegate objects. If we didn’t take a copy another thread could set to null after checking it is non-null.

83
Q

What are the 2 basic ways to handle an event in .NET?

A

Attach an event handler through use of delegation

Override a virtual method of the base class

84
Q

What is “Move Accumulation to Collecting Parameter”?

A

This is a refactoring pattern where there is a single bulky method that accumulates information to a local variable. This refactoring pattern refactors to instead use several private methods that gather discrete pieces of info which we call passing in our variable as a Collecting Parameter to collect our data.

85
Q

What is a constructor initializer? What is the syntax?

A

It is an optional clause defined on a constructor. It is used to call either another constructor within the same class using : this(…) or a constructor in the base class using : base(…). It is executed prior to the constructor and if not specified an implicit initializer of the form base() is provided.

86
Q

What is Razor?

A

Razor is a view-engine. A view engine is a pluggable template syntax engine.

87
Q

What does the acronym SOLID represent?

A

5 Main OO Design Principles:

S - Single Responsibility Principle
O - Open/Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle
88
Q

What does the principle “Encapsulate what varies” mean?

A

Those areas where there is likely to be change we should spend time in designing a solution that encapsulates those areas such that they are the only area of code that require change when it does happen

89
Q

Why should we favour composition over inheritance?

A

Composition is more flexible….

90
Q

Why should we program to interfaces rather than implementations?

A

Interfaces define behaviour rather than implement it. If we define the behaviour correctly we can swap the implementations in and out without having to change other areas of code.

91
Q

What does “don’t call us, we’ll call you” mean to you as a design principle?

A

92
Q

Explain Abstraction

A

93
Q

Explain Encapsulation

A

94
Q

Explain Polymorphism

A

95
Q

Explain Inheritance

A

96
Q

Explain the Open-Closed Principle

A

Classes should be open for extension, but closed for modification. Classes should be easily extendable to incorporate new behaviour without modifying existing code. The Observer pattern achieves this by allowing us to add new Observers at any time without having to change the Subject.

97
Q

What is the Dependency Inversion Principle?

A

Depend upon abstractions. Do not depend upon concrete classes. High-level components should not rely on low-level components, they should both depend on abstractions. Where Pizza is an abstract class, concrete classes that derive from it depend on the abstraction as does PizzaStore. PizzaStore should not create the concrete classes otherwise this would violate the principle. Factory pattern can fix. Similar to “Program to an interface…” but relates specifically to abstraction both ways.

98
Q

What guidelines can help you avoid violating the Dependency Inversion Principle?

A
No variable should hold a reference to a concrete class.
No class should derive from a concrete class
No method should override an implemented method of any of its base classes.
(It's completely ok to break these guidelines as every single program does but prompt to think about design)
99
Q

What is the Simple Factory idiom?

A

It’s not actually the factory pattern but is commonly used.

100
Q

What is the Factory Method Pattern?

A

Factory Method lets subclasses decide what objects to create. With the abstract PizzaStore class, it is the Creator where NYPizzaStore and other stores implement their own version of the createPizza() method. They create Pizza objects, the Product. Pizza is an abstract class. Class hierarchies are parallel.

101
Q

What is common to all factory patterns?

A

They encapsulate object creation.

102
Q

What is the Abstract Factory Pattern?

A

103
Q

What is an Extension Method?

A

An extension method is a method defined outside of a given type but can be called as if it was part of the type. They are defined as static methods but can be used only on instances of the type they are defined for.

104
Q

Define an extension method WordCount for String class in a static class MagicString.

A
public static class MagicString {
  public static int WordCount(this String str) {
    // return word count
  }
}
105
Q

Can extension methods be used to add methods to sealed classes?

A

Yes.

106
Q

How are Extension Methods implemented by the compiler?

A

The compiler passes in the object that the extension method is being called on to the static method that is defined as the extension method.