Pytanie |
Odpowiedź |
J.1.1.1. What is the Common Intermediate Language? rozpocznij naukę
|
|
The Common Intermediate Language is a programming language that all. NET-compatible languages like C#, Visual Basic, or F# get compiled to.
|
|
|
J.1.2.2. How is it possible that a C# class can derive from, for example, an F# class? rozpocznij naukę
|
|
It is possible because both those languages are. NET compatible and they get compiled to the Common Intermediate Language.
|
|
|
J.1.3.3. Does C# compiler compile C# source code directly to binary code? rozpocznij naukę
|
|
No, it compiles it to the Intermediate Language, which is compiled to binary code by the Just-In-Time compiler in runtime.
|
|
|
J.1.4.4. How can you see the CIL code a project got compiled to? rozpocznij naukę
|
|
Some tools can decompile a *. dll file and read the CIL code. One of those tools is Ildasm.
|
|
|
J.1.5.5. What is the Just-In-Time compiler? rozpocznij naukę
|
|
Just-In-Time compiler is a feature of the Common Language Runtime (CLR), which translates the Common Intermediate Language (CIL) code to binary code during the program execution.
|
|
|
J.2.1.6 What is the Common Language Runtime (CLR)? rozpocznij naukę
|
|
The Common Language Runtime is a runtime environment that manages the execution of the. NET applications.
|
|
|
J.2.2.7 What is the difference between CLR, CLI, and CIL. rozpocznij naukę
|
|
CLR (Common Language Runtime) is an implementation of the CLI (Common Language Infrastructure). CIL is Common Intermediate Language, to which all. NET-compatible languages get compiled.
|
|
|
rozpocznij naukę
|
|
CTS is the Common Type System, which is a standardized type system for all. NET-compatible languages, which makes them interoperable - for example, we can have a C# class derived from an F# class.
|
|
|
J.2.4.9 Is the CLR the only implementation of the CLI? rozpocznij naukę
|
|
No. Anyone can create their implementation of the CLI. One of the examples is Mono Runtime.
|
|
|
J.3.1.10 What is the difference between C# and. NET? rozpocznij naukę
|
|
C# is a programming language and. NET is a framework that supports applications written in C#, as well as in other. NET compatible languages.
|
|
|
J.3.2.11 What is the difference between. NET and. NET Framework? rozpocznij naukę
|
|
. NET is a successor of. NET Framework... NET was originally named. NET Core, and it was renamed to. NET since version 5.0.
|
|
|
J.4.1.12 What is the difference between value types and reference types? rozpocznij naukę
|
|
The differences between value types and reference types are: 1. Value types inherit from System. ValueType while reference types inherit from System. Object. 2. When a value type is passed as a parameter, its copy is given to the method. When a reference type is passed as a parameter, a copy of the reference is given to the method. 3. On assignment, a variable of a value type is copied. For reference types, only a reference is copied. 4. All value types are sealed (which means, they cannot be inherited) 5. Value types are stored on the stack, reference types are stored on the heap (because of that, the Garbage Collector only cleans up reference types)
|
|
|
J.4.2.13 What will happen if you pass an integer to a method and you increase it by one in the method's body? Will the variable you passed to the method be incremented? rozpocznij naukę
|
|
The number will be increased in the scope of the method's body, but the variable outside this method will stay unmodified because a copy was passed to the method.
|
|
|
J.4.3.14 Assuming you want the modification to the integer parameter to affect the variable that was passed to a method, how would you achieve that? rozpocznij naukę
|
|
By using ref parameter. See the question "What is the difference between the "ref" and the "out" keywords?".
|
|
|
J.5.1.15 What is boxing and unboxing? rozpocznij naukę
|
|
Boxing is the process of wrapping a value type into an instance of a type System. Object. Unboxing is the opposite - the process of converting the boxed value back to a value type.
|
|
|
J.5.2.16 What is the penalty for using boxing and unboxing? rozpocznij naukę
|
|
The main penalty is performance - when boxing, a new object must be created, which involves allocating memory for it. The unboxing requires a cast which is also expensive from the performance point of view.
|
|
|
J.5.3.17 Is assigning a string to a variable of type object boxing? rozpocznij naukę
|
|
No, because string is not a value type. The point of boxing is to wrap a value type into an object (which is a reference type).
|
|
|
J.6.1.18 What are the three main types of errors? rozpocznij naukę
|
|
1. compilation errors, also known as syntax errors, reported by the compiler 2. runtime errors, thrown during program execution 3. logical errors, occurring when the program works without crashing but it does not produce a correct result
|
|
|
J.6.2.19 What type of errors do unit tests protect us from? rozpocznij naukę
|
|
Both runtime errors and logical errors.
|
|
|
J.6.3.20 What's the C#'s mechanism for handling runtime errors? rozpocznij naukę
|
|
Exceptions - they are used for handling runtime errors. See the lecture 7 “How are exceptions handled in C#?” for more information.
|
|
|
J.7.1.21 How are exceptions handled in C#? rozpocznij naukę
|
|
Exceptions are handled by try-catch-finally blocks. Try contains code that may throw exceptions, catch defines what should be done if an exception of a given type is thrown, and finally is executed no matter if the exception was thrown or not.
|
|
|
J.7.2.22 Is it possible to have multiple catch blocks after a try block? rozpocznij naukę
|
|
Yes. You can catch any number of exceptions. It is important to first catch the more specific, and then more generic exceptions.
|
|
|
J.7.3.23 How to ensure some piece of code will be called, even if an exception was thrown? rozpocznij naukę
|
|
You should put this code in the finally block.
|
|
|
J.7.4.24 What is the base type for all exceptions in C#? rozpocznij naukę
|
|
|
|
|
J.8.1.25 What are the types of access modifiers in C#? rozpocznij naukę
|
|
public, internal, protected, protected internal, private protected and private
|
|
|
J.8.2.26 What is the difference between protected and private protected access modifiers?" rozpocznij naukę
|
|
The difference is that the type defined with private protected access modifier is only available in the declaring assem. Except for that, both work similarly: the types or members can be accessed by the class they are defined in or in the derived classes.
|
|
|
J.8.3.27 What is the difference between private and private protected access modifiers? rozpocznij naukę
|
|
The type or member defined with the private access modifier can only be accessed in the containing class. If private protected access modifier would be used instead, the type or member could be also used by derived classes within the same assembly.
|
|
|
J.8.4.28 What is the difference between protected and protected internal access modifiers? rozpocznij naukę
|
|
Types or members that are protected internal work as they are both protected and internal at the same time - so within their containing assembly they are accessible by any class (that's how internal works) and outside this assembly, they are available only in derived classes (that's how protected works).
|
|
|
J.9.1.29 What are the default access modifiers in C#? rozpocznij naukę
|
|
The default access modifier at the namespace level is internal. At the class level, it is private.
|
|
|
J.9.2.30 What's the default access modifier for a class? rozpocznij naukę
|
|
Internal, unless the class is not nested. If it is, the answer is "private".
|
|
|
J. 10.1.31 What is the purpose of the "sealed" modifier? rozpocznij naukę
|
|
The sealed modifier prevents a class from being inherited, or an overridden method from further overriding.
|
|
|
J. 10.2.32 How would you prevent the class from being inherited? rozpocznij naukę
|
|
|
|
|
J. 10.3.33 How can you make the class inheritable, but prevent specific methods from being further overridden? rozpocznij naukę
|
|
By making the overridden method sealed.
|
|
|
J. 10.4.34 Can you make an abstract class sealed? rozpocznij naukę
|
|
No. The whole point of an abstract class is to inherit from it. Making it sealed makes no sense.
|
|
|
"J. 11.1.35 What is the purpose of the ""params"" keyword?" rozpocznij naukę
|
|
The “params” keyword allows us to pass any number of parameters of the same type to a method.
|
|
|
J. 11.2.36 Why must the parameter with params modifier be the last in the parameters list? rozpocznij naukę
|
|
Because if it wasn't, and there was another parameter after it, the compiler would not know if this parameter belongs to the params array or not.
|
|
|
J. 12.1.37 What is the difference between a class and a struct? rozpocznij naukę
|
|
1. Structs are value types and classes are reference types. 2. Structs can only have a constructor with parameters, and all the struct's fields must be assigned in this constructor. 3. Structs can't have explicit parameterless constructors. 4. Structs can't have destructors.
|
|
|
J. 12.2.38 What is the base type for structs? rozpocznij naukę
|
|
|
|
|
J. 12.3.39 Is it possible to inherit from a struct? rozpocznij naukę
|
|
No, all structs are sealed.
|
|
|
J. 12.4.40 How would you represent a point in the cartesian coordinate system? rozpocznij naukę
|
|
I would create a struct that has two float readonly properties - X and Y.
|
|
|
J. 13.1.41 What are partial classes? rozpocznij naukę
|
|
Partial classes are classes that are split over two or more source files. All parts are combined when the application is compiled. It is also possible to declare partial structs, interfaces, and methods.
|
|
|
J. 13.2.42 What's the use of partial classes? rozpocznij naukę
|
|
Splitting a large class into more pieces, which can be useful when many programmers contribute to the development of this class - makes the source control easier. Also, partial classes are useful when some code is automatically generated - we can have the generated code in one part, and the human-written code in another.
|
|
|
J. 13.3.43 Is it possible to create a partial struct? rozpocznij naukę
|
|
Yes. We can create partial classes, structs, and interfaces, as well as partial methods.
|
|
|
J. 14.1.44 What does the "new" keyword do? rozpocznij naukę
|
|
The "new" keyword is used in three different contexts: 1. The new operator, which creates a new instance of a type 2. The new modifier, which is used to explicitly hide a member method from a base class in the derived class 3. The new constraint, which specifies that a type argument in a generic class must have a parameterless constructor
|
|
|
J. 14.2.45 How can you create a new object of type T in a generic class? rozpocznij naukę
|
|
By adding the new constraint to the type T.
|
|
|
J. 14.3.46 How do you hide a base class member method in the derived class? rozpocznij naukę
|
|
By using the new modifier.
|
|
|
"J. 14.4.47 How many uses does the ""new"" keyword have?" rozpocznij naukę
|
|
Three - new operator, new modifier, and new constraint.
|
|
|
J. 15.1.48 What is the purpose of the "static" keyword? rozpocznij naukę
|
|
The "static" keyword can be used in two contexts: 1. static modifier - used to define static classes, as well as static members in classes, structs, and records 2. using static directive - used to reference static members without needing to explicitly specify their name every time
|
|
|
"J. 15.2.49 Let's say you have a console application, and you use Console. WriteLine"" and ""Console. ReadLine"" all the time. What can you do to make this code more concise?" rozpocznij naukę
|
|
"We can use ""using static System. Console"" and then use only ""ReadLine"" and ""WriteLine"" throughout the code."
|
|
|
"J. 15.3.50 ""Let's say you have a variable of class Person called ""john"". How would you access a static method X from the Person class?" rozpocznij naukę
|
|
"I would not use the ""john"" instance - I would just call the ""Person. X()"" since X is a static method."
|
|
|
J. 15.4.51 Are const fields static? rozpocznij naukę
|
|
Yes, they are implicitly static.
|
|
|
J. 15.5.52 Let's say you have a class that works as a collection of objects. It has an Add method that is used to add an object to this colection. How would you keep track of the count of ALL elements that have ever been added to any instance of this cls? rozpocznij naukę
|
|
We could introduce a static "Counter" field that is incremented each time the "Add" method is called.
|
|
|
J. 16.1.53 What is a static class? rozpocznij naukę
|
|
A static class is a class that cannot be instantiated and can only contain static methods. It can work as a container for methods that just operate on input parameters and do not have to get or set any internal instance fields.
|
|
|
J. 16.2.54 Can a static class have a constructor defined? rozpocznij naukę
|
|
Static class cannot have an instance constructor defined, but it can have a static constructor.
|
|
|
J. 16.3.55 What is a static constructor? HINT It's a special method used to initialize static members of a class. It's a special method used to initialize static members of a classIt's a special method used to initialize static members of a classIt's a spe method used to initialize static member EXAMPLE It's a special method used to initialize static members of a class. It's a special method used to initialize static members t's a special method used to initialize static members of a classIt's a special method used to initialize static member rozpocznij naukę
|
|
It's a special method used to initialize static members of a class. It's a special method used to initialize static members of a classIt's a special method used to initialize static members of a classIt's a special method used to initialize static member EXPLANATION It's a special method used to initialize static members of a class It's a special method used to initialize static members of a class It's a special method used to initialize static members of a class It's a special method used to initialize EXAMPLE It's a special method used to initialize static members of a class It's a special method used to initialize static members of a class It's a special method used to initialize static members of a class It's a special method used to initialize stat
|
|
|
J. 16.4.56 Is it possible to use the "this" keyword within a static method? rozpocznij naukę
|
|
No. "This" refers to the current instance, and there is no instance reference in a static method, as a static method belongs to a type as a whole, not to a specific instance.
|
|
|
J. 16.5.57 How to initialize static fields of a class? rozpocznij naukę
|
|
By using the static constructor.
|
|
|
J. 16.6.58 Is it possible to have a static constructor in a non-static class? rozpocznij naukę
|
|
Yes. A non-static class can have static members, and they can be initialized in the static constructor. In this case, the static constructor will be called before the instance constructor.
|
|
|
J. 17.1.59 What is the purpose of the ternary conditional operator? rozpocznij naukę
|
|
It's a shorter syntax for the if-else clause. It evaluates a boolean expression and returns the result of one of the two expressions, depending on whether the boolean expression evaluates to true or false.
|
|
|
J. 17.2.60 Can a ternary operator always be translated to an if-else statement? rozpocznij naukę
|
|
Yes. The ternary operator is just a shorter syntax for if-else statements.
|
|
|
J. 17.3.61 Can an if-else statement always be translated to a ternary operator? rozpocznij naukę
|
|
No. The ternary operator can only be used with an assignment to a variable, so we can't use it, for example, to call Console. WriteLine method with some argument if the condition is true, and with another, if it is false.
|
|
|
J. 18.1.62 What is the purpose of the null coalescing and null conditional operators? rozpocznij naukę
|
|
The null coalescing and null conditional operators allow us to perform some operations if a value is null, and others if it's not.
|
|
|
J. 18.2.63 Let's say you have an object and you want to call a method on it, but you are not sure whether this object is null or not. How can you conditionally create this object (if it was null) without using the "if" keyword? rozpocznij naukę
|
|
We can use the null coalescing assignment operator, like for example "(numbers?= new List<int>()). Add(5);".
|
|
|
J. 18.3.64 What is the use of the question mark in C#? rozpocznij naukę
|
|
It is used in several operators, like ternary operator, null coalescing operator, null coalescing assignment operator, and null conditional operator. Also, we can use it to declare a variable of nullable type.
|
|
|
J. 19.1.65 What is encapsulation? rozpocznij naukę
|
|
Encapsulation means bundling of data with the methods that operate on that data.
|
|
|
J. 19.2.66 Is encapsulation the same thing as data hiding? rozpocznij naukę
|
|
No. Encapsulation means storing data and methods operating on this data in a single class. Data hiding is about making members of a class non-public.
|
|
|
J. 19.3.67 What is the difference between encapsulation and abstraction? rozpocznij naukę
|
|
Abstraction is about generalization - we create abstract types that then can be made more specific in derived types. Encapsulation means storing data and methods operating on this data in one class. We can have a completely non-abstract type that is encapsulated.
|
|
|
rozpocznij naukę
|
|
LINQ is a set of technologies that allow simple and efficient querying over different kinds of data.
|
|
|
J. 21.1.69 What are extension methods? rozpocznij naukę
|
|
An extension method is a method defined outside a class, that can be called upon this class's objects as a regular member method. Extension methods allow you to add new functionality to a class without modifying it.
|
|
|
J. 21.2.70 How would you add new functionality to an existing class, without modifying this class? rozpocznij naukę
|
|
By using extension methods.
|
|
|
J. 21.3.71 What will happen if you call a member method that has the same signature as the existing extension method? rozpocznij naukę
|
|
The member method has priority and it will be the one to be called. The extension method will not be called.
|
|
|
J. 22.2.73 What is an enumerator? rozpocznij naukę
|
|
An enumerator is a mechanism that allows iterating over collection elements. It's a kind of a pointer that points to a "current" element in the collection.
|
|
|
J. 22.1.72 What is IEnumerable? rozpocznij naukę
|
|
IEnumerable is an interface that enables iterating over a collection with a foreach loop.
|
|
|
J. 22.3.74 Assuming a method returns a collection of some kind, how to best express your intent if you don't want the user to modify this collection? rozpocznij naukę
|
|
By returning it as IEnumerable or another readonly collection type.
|
|
|
J. 23.1.75 What is the difference between the equality operator (==) and Equals? rozpocznij naukę
|
|
In the most common scenario == compares objects by reference while Equals is overridden to compare them by content. Both can have custom implementation for any type, so this behavior may vary.
|
|
|
J. 23.3.77 Is it possible to use the == operator on structs? rozpocznij naukę
|
|
It is by default not supported, but we can use it if we overload it.
|
|
|
J. 23.2.76 What's the difference between comparing equality by reference and by value? rozpocznij naukę
|
|
Comparing by reference simply checks if two variables point to the same object in memory. Comparing by value checks the internal value or values of an object. For example, we can have two objects of type Point, both having X = 10 and Y = 20. Even if they are two separate objects and they live in different places in the computer's memory, the comparison by value will consider them equal, while the comparison by reference will not.
|
|
|
J. 24.1.78 What is the difference between deep copy and shallow copy? rozpocznij naukę
|
|
When creating a shallow copy, value type members will be copied, but only a reference will be copied for reference types. For deep copying also the reference types will be copied into new objects.
|
|
|
J. 24.2.79 What does the MemberwiseClone method do? rozpocznij naukę
|
|
It creates a shallow copy of an object.
|
|
|
J. 24.3.80 What's the risk of using shallow copies? rozpocznij naukę
|
|
The risk is that both the original object and the copy can hold references to the same objects. That means, when modifying the object referenced by the original, also the object referenced by the copy will be affected.
|
|
|
J. 25.1.81 What is the Garbage Collector? rozpocznij naukę
|
|
The Garbage Collector is a mechanism that manages the memory used by the application. If an object is no longer used, the GC will free the memory it occupies. The Garbage Collector is also responsible for defragmenting the application's memory.
|
|
|
J. 25.2.82 Would the Garbage Collector free up the memory occupied by an integer? rozpocznij naukę
|
|
No, since an integer is a value type and Garbage Collector only handles memory occupied by reference types. Value types are stored on the stack which has its own mechanism for freeing the memory.
|
|
|
J. 25.3.83 How to manually trigger the Garbage Collector memory collection? rozpocznij naukę
|
|
By calling GC. Collect method from System namespace.
|
|
|
J. 25.4.84 What is memory fragmentation and defragmentation? rozpocznij naukę
|
|
When pieces of memory are allocated and freed, the memory becomes defragmented, which means free memory is in small pieces rather than in one long piece. This is called memory fragmentation. If there is a need to put a big object into memory, it might be impossible to find a block of free memory that is long enough. The process of moving the pieces of allocated memory so they stick together, to create a big chunk of free memory is called defragmentation.
|
|
|
J. 25.5.85 What are memory leaks? Does the Garbage Collector guarantee protection from them? rozpocznij naukę
|
|
Memory leaks happen when memory is not freed even if an object is no longer used. No, GC does not guarantee protection from them.
|
|
|
J. 26.1.86 What are nullable types? rozpocznij naukę
|
|
Nullable type is any type that can be assigned a value of null. Nullable<T> struct is a wrapper for a value type allowing assigning null to variable of this type. For example, we can't assign null to an int, but we can to a variable of type Nullable<int>.
|
|
|
J. 26.2.87 Can a value type be assigned null? rozpocznij naukę
|
|
No, it can't. It must be wrapped in the Nullable<T> struct first if we want to make it nullable.
|
|
|
J. 26.3.88 Is it possible to have a variable of type Nullable<T> where T is a reference type? For example, Nullable<string>? rozpocznij naukę
|
|
No, the Nullable struct has a type constraint on T that requires the T to be a value type. Providing a reference type as T will result in a compilation error.
|
|
|
J. 27.1.89 What is a property? rozpocznij naukę
|
|
A property is a member that provides a mechanism of reading, writing, or computing a value of a private field.
|
|
|
J. 27.2.90 What is the difference between a property and a field? rozpocznij naukę
|
|
Properties provide a level of abstraction. They work as special methods used to access class-specific data. They do not need to expose fields - they may expose constant literals or some calculated value. Also, properties can be overridden.
|
|
|
J. 27.3.91 How can you encapsulate a field of a class? rozpocznij naukę
|
|
By hiding it behind a property.
|
|
|
J. 27.4.92 What are the benefits of using properties? rozpocznij naukę
|
|
Encapsulation, custom behavior on reading and writing, different access modifiers for reading and writing. Also, properties can be calculated basing on other data.
|
|
|
J. 28.1.93 What are generics? rozpocznij naukę
|
|
Generic classes or methods are parametrized by type - like, for example, a List<T> that can store any type of elements.
|
|
|
J. 28.2.94 What are type constraints? rozpocznij naukę
|
|
Type constraints allow limiting the usage of a generic type only to the types that meet specific criteria. For example, we may require the type to be a value type, or require that this type provides a public parameterless constructor.
|
|
|
J. 28.3.95 What is the "where" keyword used for?" rozpocznij naukę
|
|
It's used to define type constraints. Also, it is used for filtering when using LINQ.
|
|
|
J. 28.4.96 What are the benefits of using generics? rozpocznij naukę
|
|
They allow us to reduce code duplication by creating a single class that can work with any type. Reducing code duplication makes the code easier to maintain and less error-prone.
|
|
|
J. 29.1.97 What is the difference between the "const" and the "readonly" modifiers? rozpocznij naukę
|
|
1. Const fields are assigned at compile time. Readonly fields can be assigned at runtime, in the constructor. 2. Consts can only be numbers, booleans, strings, or a null reference, readonly values can be anything. 3. Consts can't be declared as static, because they are implicitly static. Readonly values can be static.
|
|
|
J. 29.2.98 Assume you need the PI number in your program. How would you define it? rozpocznij naukę
|
|
It is a bit tricky because the best answer is "I would not, I would use Math. PI that is already defined in C#". But if for some reason it would not be present, I would define it in some static class MathConsts as a const value.
|
|
|
J. 29.3.99 How can you prevent the field from being modified after it is set up in the constructor? rozpocznij naukę
|
|
|
|
|
J. 29.4.100 When do you use const, and when do you use readonly? rozpocznij naukę
|
|
I would use const when the value is known at the compile time, for example, mathematical constants like PI number should be const. I would use readonly when the value is known at runtime, but I do not wish it to change after it is assigned.
|
|
|
J. 30.1.101 What is the difference between the "ref" and the "out" keywords? rozpocznij naukę
|
|
ref passes the value type to a method by reference, which means any modifications of this value inside this method will be visible outside this method. out is a way of returning extra variables from a method.
|
|
|
J. 30.2.102 How to modify the value of value type inside the method, so this modification is visible after the method finishes? rozpocznij naukę
|
|
By passing it to the method as a ref parameter.
|
|
|
J. 30.3.103 How to return an additional piece of information from the method along with the return value? rozpocznij naukę
|
|
By using an out parameter. This is how it works with methods like "int. TryParse(string text, out bool wasParsingSuccessful)". Of course, there are other ways to achieve it - for example, you can return a Tuple instead of a simple value.
|
|
|
J. 31.1.104 What is the difference between an interface and an abstract class? rozpocznij naukę
|
|
An interface defines what set of operations will be provided by any class implementing it - it does not provide any implementation on its own. An abstract class is like a general blueprint for derived classes. It may provide implementations of methods, contain fields, etc.
|
|
|
J. 31.2.105 Why can't you specify the accessibility modifier for a method defined in the interface? rozpocznij naukę
|
|
The point of creating an interface is to specify public contract that a class implementing it will expose. Modifiers other than "public" would not make sense. Because of that, the public modifier is the default and we don't need to specify it explicitly.
|
|
|
J. 31.3.106 Why can't we create instances of abstract classes? rozpocznij naukę
|
|
Because an abstract class may have abstract methods which do not contain a body. What would happen if such a method was called? It doesn't have a body so the behavior would be undefined.
|
|
|
J. 32.1.107 What is polymorphism? rozpocznij naukę
|
|
Polym. is the provision of a single interface to entities of different types. In other words, there is a generic concept of something, and this concept can be made concrete by multiple types. All of them can be used wherever the generic concept is needed.
|
|
|
J. 32.2.108 What mechanisms in C# allow us to use polymorphism? rozpocznij naukę
|
|
Interfaces, abstract classes, inheritance, virtual methods.
|
|
|
J. 33.1.109 What's the difference between a virtual method and an abstract method? rozpocznij naukę
|
|
A virtual method is a method that may be overridden in the derived class. An abstract method must be overridden (unless the derived class is abstract itself).
|
|
|
J. 33.2.110 What is method overriding? rozpocznij naukę
|
|
It is providing a custom implementation of virtual or abstract methods in the child class.
|
|
|
J. 33.3.111 When a method must be overridden? rozpocznij naukę
|
|
In the non-abstract child class, if it was abstract in the base class.
|
|
|
J. 33.4.112 Are abstract methods virtual? rozpocznij naukę
|
|
Yes, all abstract methods are implicitly virtual.
|
|
|
J. 34.1.113 What is the method overloading? rozpocznij naukę
|
|
Method overloading is having a class with multiple methods with the same name, that differ only in parameters.
|
|
|
J. 34.2.114 If two methods have the same name and parameters but for one the last parameter is optional, which method will be used when all parameters are provided? rozpocznij naukę
|
|
The one without optional parameters has a priority.
|
|
|
J. 34.3.115 What's the difference between method overloading and method overriding? rozpocznij naukę
|
|
Method overloading is having a class with multiple methods with the same name that diff er only in parameters. Method overriding is providing a custom implementation of virtual or abstract methods in the child class.
|
|
|
J. 35.1.116 What is the difference between method overriding and method hiding? rozpocznij naukę
|
|
Method overriding happens when the derived class provides its own implementation of a virtual or abstract method from a base class. Method hiding happens when there is a method in the derived class with the same name as the method in the base class, that does not override the base class method.
|
|
|
J. 35.2.117 What keyword do you have to use to hide a base class method in the child class? rozpocznij naukę
|
|
Actually, I don't have to use any keyword - the method is hidden by default. I can do it explicitly with the "new" keyword.
|
|
|
J. 36.1.118 Does C# support multiple inheritance? rozpocznij naukę
|
|
No, C# does not support multiple inheritance. It does support implementing multiple interfaces, though.
|
|
|
J. 36.2.119 What is the "diamond problem"? rozpocznij naukę
|
|
The diamond problem is an ambiguity that arises when class D is derived from classes B and C, which both derive from class A. When both classes B and C overrode a method from class A, then it is ambiguous which one would be used when this method is called on an object of class D.
|
|
|
J. 37.1.120 What is the DRY principle? rozpocznij naukę
|
|
DRY stands for "Don't Repeat Yourself" and it means that we shouldn't have multiple places where pieces of business knowledge are defined. Also, DRY is commonly considered a rule of avoiding code duplication.
|
|
|
J. 37.2.121 What are the use cases when having code duplication is reasonable? rozpocznij naukę
|
|
First, when two pieces of code are identical but handle different business cases, especially when there is a chance that one business case will change independently from the other. Second, when the price to pay for avoiding code duplication is to create a very complex and unmanageable abstraction.
|
|
|
J. 38.1.122 What is the "magic number" antipattern? rozpocznij naukę
|
|
A magic number is an unnamed hard-coded value used directly in the code.
|
|
|
J. 38.2.123 What is a code smell? rozpocznij naukę
|
|
A code smell is a characteristic of the code that indicates some deeper problem. Code smells could be magic numbers, code duplications, large classes, etc.
|
|
|
J. 38.3.124 What's the alternative for defining constant values as consts in the source code? Where else can they be defined? rozpocznij naukę
|
|
We could define them in some kind of configuration file. It has the advantage over using consts, because changing them can be done by a non-programmer and it doesn't require recompilation and redeployment of the code.
|
|
|
J. 39.1.125 Why is using the "goto" keyword considered a bad practice? rozpocznij naukę
|
|
The goto statement transfers the program execution directly to a labeled statement. Using it is widely considered a bad practice, as it increases the complexity of the code. The flow of the program is tricky to follow, making reading and debugging the code harder. Also, it can lead to the unintentional creation of infinite loops.
|
|
|
J. 39.2.126 How to simply break out from deeply-nested loops? rozpocznij naukę
|
|
For example, by using goto. We can also use the return keyword, but sometimes it requires moving the loop to a separate method.
|
|
|
J. 39.3.127 "What are the use cases when using goto might be a good idea? rozpocznij naukę
|
|
Breaking out from nested loops, common cleanup logic, and performance optimizations.
|
|
|
J. 40.1.128 What is the "spaghetti code"? rozpocznij naukę
|
|
Spaghetti code is a pejorative term used to describe code that is messy, tangled, and hard to maintain.
|
|
|
J. 40.2.129 What is the "ravioli code"?" rozpocznij naukę
|
|
The "ravioli code" is a term that describes code that contains classes that are easy to understand in isolation, but interactions between them and the project as a whole are not.
|
|
|
J. 40.3.130 What is the "lasagna code? rozpocznij naukę
|
|
The "lasagna code" is a code whose layers are complicated and so are the interactions between them. Making a change in one layer heavily affects other layers.
|
|
|
J. 41.1.131 What is the Singleton design pattern? rozpocznij naukę
|
|
Singleton is a class that only allows creating a single instance of itself, and exposes simple access to that instance.
|
|
|
J. 41.2.132 What is a global state? rozpocznij naukę
|
|
It is any state that is reachable from any point of the application. For example, a public field in a public, static class.
|
|
|
J. 41.3.133 Why is Singleton considered an antipattern? rozpocznij naukę
|
|
Because it is a piece of the global state, and the global state is hard to control.
|
|
|
J. 41.4.134 What is the difference between the Singleton Design Pattern and the application singleton? rozpocznij naukę
|
|
An application singleton is simply a single object used in various places in the application. It does not enforce its "singletoness", unlike the Singleton design pattern. Singleton is a class that only allows creating a single instance of itself, and exposes simple access to that instance.
|
|
|
J. 42.1.135 What is the Builder design pattern? rozpocznij naukę
|
|
Builder is a design pattern that allows the step-by-step construction of complex objects.
|
|
|
J. 42.2.136 What are the benefits of using the Builder design pattern? rozpocznij naukę
|
|
It allows building objects step-by-step. It helps to enclose complex building logic in a separate class. It improves readability, especially when the constructor takes many parameters.
|
|
|
J. 42.3.137 What are the downsides of using the Builder pattern? rozpocznij naukę
|
|
It requires a lot of extra code which is at least partially duplicated with the code of the class we build. It causes a risk of omitting some required building params. Also, it creates a risk of setting some property twice by mistake, thus overwriting it.
|
|
|
J. 42.4.138 What does the with keyword do? rozpocznij naukę
|
|
It is used to create a copy of a record with some particular field or fields set to new values.
|
|
|
J. 43.1.139 What is the Adapter design pattern? rozpocznij naukę
|
|
The Adapter is a design pattern that allows converting an interface of a class to the interface expected by a client.
|
|
|
J. 43.2.140 What design pattern would you use if you had some interface incompatible with your needs, and you would like to adjust it? rozpocznij naukę
|
|
The Adapter design pattern, as it allows converting an interface of a class to the interface expected by a client.
|
|
|
J. 44.1.141 What is the Bridge design pattern? rozpocznij naukę
|
|
The Bridge design pattern allows us to split an inheritance hierarchy into a set of hierarchies. It is the implementation of the "composition over inheritance" principle.
|
|
|
J. 44.2.142 What is "composition over inheritance"? rozpocznij naukę
|
|
It is a principle that states that it is better to design polymorphic and reusable code by using composition rather than inheritance.
|
|
|
J. 45.1.143 What is the Factory Method design pattern? rozpocznij naukę
|
|
Factory Method design pattern allows us to define an interface for creating objects of a general base type, without specifying what subtype exactly will be created.
|
|
|
J. 45.2.144 What is the Static Factory Method design pattern? rozpocznij naukę
|
|
Static Factory Method is a method used to create objects. It is used as an alternative to a public constructor, mostly used to improve the readability.
|
|
|
J. 45.3.145 What are the benefits of using the Factory Method design pattern? rozpocznij naukę
|
|
Separation of concerns (in this case, separating the process of creating the object from using the object), avoiding code duplications and reducing coupling.
|
|
|
J. 45.4.146 What is the separation of concerns? rozpocznij naukę
|
|
Separation of concerns is a design principle for separating the code into distinct sections such that each section addresses a separate concern.
|
|
|
J. 46.1.147 What is the "S" in the SOLID principles? rozpocznij naukę
|
|
S in the SOLID principles stands for Single Responsibility Principle (sometimes referred to as the SRP). This principle states that a class should be responsible for only one thing. Sometimes the alternative definition is used: that a class should have no more than one reason to change.
|
|
|
J. 46.2.148 How to refactor a class that is known to be breaking the SRP? rozpocznij naukę
|
|
One should identify the different responsibilities and move each of them to separate classes. Then the interactions between those classes should be defined, ideally by one class depending on an interface that the other class implements.
|
|
|
J. 47.1.149 What is the "O" in the SOLID principles? rozpocznij naukę
|
|
O in the SOLID principles stands for Open-Closed Principle (sometimes referred to as the OCP). This principle states that modules, classes, and functions should be opened for extension, but closed for modification.
|
|
|
J. 47.2.150 What are the good reasons to modify a class, disregarding the Open-Closed Principle? rozpocznij naukę
|
|
Firstly, for bug fixing. Secondly, sometimes sticking to the OCP might be an "overkill" - when it generates huge amounts of super-abstract code that brings more complexity than the OCP reduces.
|
|
|
J. 48.1.151 What is the "L" in the SOLID principles? rozpocznij naukę
|
|
L in SOLID principles stands for Liskov Substitution Principle. This principle states that we should be able to use a derived type in place of a base without knowing it, and it should not lead to any unexpected results.
|
|
|
J. 48.2.152 Who is the author of the LSP? rozpocznij naukę
|
|
It is Barbara Liskov, the second woman in history to be awarded the Turing Award.
|
|
|
J. 49.1.153 What is the "I" in the SOLID principles? rozpocznij naukę
|
|
I in SOLID principles stands for the Interface Segregation Principle (sometimes referred to as the ISP). This principle states that the clients of an interface should not be forced to depend on methods they don't use.
|
|
|
J. 49.2.154 What is the difference between the Liskov Substitution Principle and the Interface Segregation Principle? rozpocznij naukę
|
|
The LSP tells us how to implement the interface of a base type. The ISP tells us if we should implement this interface at all.
|
|
|
J. 50.1.155 What is the "D" in the SOLID principles? rozpocznij naukę
|
|
D in SOLID principles stands for the Dependency Inversion Principle. This principle states that high-level modules should not depend on low-level modules. Both should depend on abstractions.
|
|
|
J. 50.2.156 What's the difference between Dependency Inversion and Dependency Injection? rozpocznij naukę
|
|
Dependency Injection is beyond the level of this course, but in short - it's a technique of providing dependencies to a class from the outside (usually with a constructor) instead of creating them within this class. We used it in this lecture when providing IDelivery dependency to YourStore class. To answer the question - Dependency Injection is a mechanism that allows us to provide a dependency, Dependency Inversion is a principle telling that a class should depend on abstraction. Dependency Injection allows us to provide a dependency that was "detached" when following the Dependency Inversion Principle.
|
|
|