Krystyna Ślusarczyk Junior Level Flashcards

1
Q

What is a difference between class and a struct?

A

a. Struct is a value type, and class is a reference type.
b. Struct cannot have a destructor. THe reason is that if we have a struct whihc manages database and in destrucor we close the database. and when we create a copy of this struct when we pass this as a param, the copied struct will close the connection.
c. Struct has an implicit paramaetrised constreucotr and all the properties should be assigned in the constructor.
d. Struct cannot have parameterless constructor.
e. Struct are sealed (all valuetypes).
f. Struct

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

What is a base type for struct?

A

System.ValueType

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

Is it possible to inherit from a struct?

A

No

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

What is a Partial class?

A

Partial classes are classes which are split over two or more source files. WHen the applciation is compiled these classes are combined to one class. We can have partial class, interface, strut and methods.

Exmaple.

partial class person
{

}

partial class Person
{

}

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

What is the use of partial classes?

A

Partial classes are used to split one class into many classes which is useful when many developers contribute t the same class and merging these classes is difficult in source control/repo. Another use is that when the system generates a class then if we are too contributing to the same class, then we can use partial class.

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

What does new keyword do?

A

3 uses:

a. new operator: to invoke a constructor to create a new object
b. new modifier: explicitly hide a member of a base class in the derived class.
c. new constraint: type argument in the generic class must have a parameter-less constructor

class generic where T: new()
{
get()
{
   return new T();
}
}

we don’t know if T will have parameter-less constructor.

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

What is the purpose of static keyword?

A

2 contexts:

Static Modifiers: Used to declare a class as struct as well as static members of a class.

Static data member can be used in non-static methods.

Static function cannot use non-static members.

Static modifiers belong a type/class and not to a object.

all const members are static.

WE CANNOT HAVE A STATIC STRUCT AND RECORDS.

Static Directives: they can be used to reference a static members without explicitly specifying their name eveytime.

Example, we dont have to use Console.WriteLine everutim if we use

using static System.COnsole.

we can just use WriteLine.

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

What is a static class?

A

Static class is a class that cannot be instantiated and can only contain static members.

It can work as a container for static members that work just on input parameters and do not have to get or set instance members.

Example, if we have a calculator calss, we can make it as static. we don’t have to create multiple instances of calculator. Ifwe have different elements for difference instance then we can have non-static class.

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

Name some static class?

A

System,.Math
System.Console
Systm.Environemnt

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

Can we derive from a static class?

A

No. Static classes are sealed.

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

Can a static class has a constructor?

A

yes, it can only have staic constructor.

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

Where to initialize static memebers ?

A

In a static constructor.

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

can a non-static class have a static constructor?

A

Yes, it will be used to initialize the static members.

Static constructor will be called before the instance constructor is invoked.

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

What is a static constructor?

A

They are used to initialize static members. They should be parameter-less. They cannot be inherited. They cannot have access modifers. They cannot be called directly. Only CLR can invoke it.

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

What is a ternary conditional operator?

A

It provides a shorter syntax for if-else statement.

ternary operator always returns a value. Hence, we cannot add come expression such as console.write line

var val = dogSize > 25 ? “big” : small

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

Can all ternary operator be translated to if-else?

A

yes, we can.

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

can all else-if be converted to ternery?

A

No. ternary can only return a value.

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

What is null coalescing and null conditional operators?

A

they allow to perform if the value is null..

variable ?? “this will be executed if variable is null”

if (variable == null)
{
return variable;
}

return “this will be executed if variable is null”

null coalescing assignment operator?

if (variable == null)
{
create a variable
}

variable.Add();

(variable ??= create a vraible).Add();

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

What is null conditonal operator?

A

if (vvarible != NULL)
{
variable.method();
}

variable?.method();

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

What is the use of ? in C#?

A

it is used in ternary operator, null coleascing, null conditional and null coleascing assignment operators.

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

What are extension methods?

A

An extension method is declared outside the class and the extension method can be called upon the objects as a regualr method.

Using EM, we can add a new methods to the exsiting class without having its source code, rebuilding the code or inheriting the class.

To cretae an extension method, we need to create a static class and statci method. the first parameter should be this.

static class stringExtension 
{
   static string newMethod(this string, anyOtherParam)
   {

}
}

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

If a method i defined in extension class and the method is also available in the original class. Which method will be called?

A

The original method will be called.

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

How would you add a new funcitonality to an exsiting class without modifying it?

A

Extension methods.

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

Can we add extension method to System.Object?

A

Yes

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

What is nullable tyepes?

A

Nullable types are any type can be assigned a value of NULL.

By default reference can be assigned NULL but value types cannot be assigned null.

The reason we need nullable is that sometimes in DB we may have null values

Example:

Nullable i = null;

Nullable has provided two functions:

HasValue : this property will check if the var is null.
Value: this property will return the value.

We cannot use it like normal int.

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

IS this correct?

Nullable str = null;

A

No, Nullable types have strict type check. It works only with the value type.

27
Q

Can a value type be assigned null?

A

no, we have to use nullable.

28
Q

What is the difference between == and Equals?

A

For the reference type, both == and Equals compare it by reference. But these will be overiden hence the behavior may change. For example, String has overridden == and Equals to compare it by value. Arrays has not overidden and hence it compares it by ref.

For value type, we cant use ==. == needs to explicitly overriden. and Equals compare value type by value.

29
Q

What are the differences between const and readonly?

A

const can assigned during compile time. we should know the value before.

readonly is assigned during runtime in the constructor or during declaration.

const can be string, boolean, int and null references. readonly can be of any type.

const cant be static as it is explicitly static.

Readonly can be marked as static.

30
Q

What is const?

A

Const is used to declare a variable which are not supposed to change and the value is know at the compile time.

We have to assign a value at the declaration.

31
Q

What is readonly?

A

Readonly is used to declare a member whose value is not supposed to change after it has been set once.

32
Q

When to use const and when to use readonly?

A

I will use const if I know the value during compile time and I will use readonly when the value is known at the compile time.

33
Q

What is Params keyword?

A

Params keyword allows to send any number of parameters of same type to a method.

Without params keyword:

 static int add(int[] param)
        {
            var res = 0;
            foreach(var number in param)
            {
                res += number;
            }
            return res;
        }

static void Main(string[] args)
{

        add(new int[] { 1, 2, 3, 4, 5 });

}

With params keyword:

        static int add(params int[] param)
        {
            var res = 0;
            foreach(var number in param)
            {
                res += number;
            }
            return res;
        }
        static void Main(string[] args)
        {
            add(new int[] { 1, 2, 3, 4, 5 });
            add(1, 2, 3, 4, 5 );
        }

NOTE BOTH ARE VALID.

Params parameter should always be a last parameter in the formal parameter list.

34
Q

why Params parameter should always be a last parameter in the formal parameter list?

A

Because if wasn’t and we pass multiple paramaters, the compiler will not know where the parameter belongs to the params or not.

35
Q

What are Generics?

A

Generic class or method are parameterized by type. It allws us to write much less code because we dont have to write separate class for each type.

    class IntGen
    {
        int add(int a, int b)
        {
            return 0;
        }
    }
    class DoubleGen
    {
        double add(double a, double b)
        {
            return 0;
        }
    }
    class generics where T : struct
    {
        void add(T a, T b)
        {
    }
}
36
Q

What is type constriants?

A

Type constraints allows us to limit the usage of generic type to only for types that meet specific criteria. Exmaple, the type should be of valueType

class generics where T : class

class generics where T : new T()

class generics where T : struct

class generics where T : IsomeInterface

37
Q

What is the usage of where keyword?

A

Type Constraints.

38
Q

What are the benefits of Generics?

A

They allow us to reduce code duplication.

39
Q

What is the difference between virtual and an abstract method.

A

The virtual method is a method that might be overridden in the inheriting class.

The virtual method is a method that must be overridden in the inheriting class.

40
Q

Can properties, indexes be made virtrul?

A

Yes

41
Q

Can we make virtual method in the derived class as virtual?

A

No..

42
Q

Can we make virtual method as static?

A

No

43
Q

What is method overridding?

A

It is a machanism of providing custom implementation for the virtual or abstract method in the child class.

44
Q

When a method must be overriden?

A

WHen we derive form the abstratc class

45
Q

What is method overloading?

A

Method overloading is a machenishm where a class has methods with same name but differs with the paramaters.

Method overloading can have:

a. 2 or many methods with same name and parameters of different datataype.
a. 2 or many methods with same name and parameters with different sequence of paramaters.
c. one method with ref and another without ref can be overloaded.

Method overlaoding cannot have

paramerts with ref and out… IL code witl be same for both ref and out.

methods with difference return type cannot be overloaded.

Also one method with optionla nd another without optional paramert can be overloaded.

46
Q

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?

A

one without optional parameter.

47
Q

“What’s the difference between method overloading and method overriding?”

A

method overloading is having a class with multiple methods with the same name that differ only by parameters.

Method overriding is providing a custom implementation of virtual or abstract method.

48
Q

What is method overriding and method hiding?

A

Method overriding is a process where derived class will override the method of the base class to have its custom implementation.

Method overriding happens when the derived class provides its own implementation of a virtul or abstract methods from a base class.

In Method hiding, the derived class will have same same as in the base class but it will not override the implementation of the base class. we use new keyword to override. But, if we omit override keyword, it is automatically hidden.

Below the exmaple:

public class Animal
    {
        public virtual void Get()
        {
            Console.WriteLine("Animal");
        }
    }
    public class Tiger : Animal
    {
        public override void Get()
        {
            Console.WriteLine("Tiger");
        }
    }
    public class Lion : Animal
    {
        public void Get()
        {
            Console.WriteLine("Lion");
        }
    }
Animal ani = new Animal();
ani.Get();
Animal lion = new Lion();
lion.Get();
Animal tiger = new Tiger();
tiger.Get();
49
Q

What is the use of method hiding?

A

Say, we have to use some open source library and we will derives from its class.

50
Q

What keyword do you have to use to hide a base class method in the child class?

A

We should omit override keyword. we can add new keyword.

51
Q

Does C# supports multiple inhritance?

A

No, C# does not support multiple inheritance as it will lead to diamond problem.

52
Q

What is diamond problem?

A

diamond problem arises when class D derives from class b and c which in turns derives from A. This creates ambiguity. WHen class b and c overrides a method of class A then it creates an ambiguity when we call the method using the Class D’s object.

53
Q

What is a property?

A

A property is a member that provides a mechanism for reading ,writing, or computing a value for a private field.

54
Q

What area advantages of a property?

A

Fields exposes inner implementation of a class. Properties hide it.

Fields cannot have custom behavior on reading and writing - properties can.

Properties can have different access modifier for read and write. Field cannot.

Properties can be overridden.

55
Q

How can you encapsulate a field of a class?

A

using property

56
Q

What is a sealed modifier?

A

Sealed is used to prevent a class to be inherited or to prevent a overridden method to further override.

Example:

    public sealed class A
    {
        public virtual void method(); 
    }
    public class b : A
    {
        public override sealed method();
    }

To make a method sealed, overridden is mandatory.

57
Q

How would you prevent a class from being inherited?

A

use sealed

58
Q

“How can you make the class inheritable but prevent specific methods from being further overridden?”

A

By making overriding method sealed

59
Q

Can you make an abstract class, sealed?”

A

No

60
Q

How are exceptions handled in C#?

A

using try, catch and finally block.

Try contains code that may throw exception.

Catch defines what should be done if an excpetion of given type is thrown.

finally is executed no matter if the exception is thrown or not

61
Q

Is it possible to have multiple catch blocks after a try block?

A

Yes, but the most specific catch should be the first.

62
Q

“How to ensure some piece of code will be called,

even if an exception was thrown?”

A

fianly

63
Q

“What is the base type for all exceptions in C#?”

A

It is System.Exception