fundamentals Flashcards

1
Q

What are the steps involved when we run the dot Net code?

A

Any dot Net code such as C# will first convert to an IL code. IL code is a partially compiled code. This partially compiled code will then be loaded by CLR to be converted to a machine code. This machine code will be executed by the JIT.

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

What is CLR?

A

When the IL code is generated, CLR will convert it to the machine’s native language. It also does other operations such as garbage collection.

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

What is CTS?

A

Common Type System: When we write two programs, one in c# and other one in VB. we can cross use the code. To enable cross using of the code, we have CTS where the data types defined in two different languages gets compiled to a common data type.

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

What is CLS?

A

Common Language Specifications is the specification which should be followed by any dot Net language so that when once program is used in another language program it will not get into any issue.

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

Provide a demo on how IL, CLR, CTS and CLS work?

A

Create a c# program and build it.
Open the output file using ildasm.exe. You can see the IL code.
Write one more program in VB. Open in ildasm.exe.
See that the int in C3 and vb is converted to int32. This is CTS.
in C#, create two variables, v1 and V2. when we try to use these in VB, it will not work becase VB is a case-insensitive language and we have vioalated CLS.

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

What is an assembly? What are types of assembly?

A

When we build an application, we get either dll or exe. this is called assembly. Exe will have its own address space. dll has to be laoded.

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

How do you link dll in exe.

A

using dll name. add dll as a reference.

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

What is an App domain?

A

App domain is a logical isolated container inside a process address space. In app doamin, we can load a peice code/dll in a isolated manner.

We can add permission to the app domin. Example, there is a dll or class which shoould not create a file. Then we can create a permission and pass that permission in CreateDomiain method. We can also unlaod a domain.

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

What is an App domain?

A

App domain is a logical isolated container inside a process address space. In app doamin, we can load a peice code/dll in a isolated manner.

We can add permission to the app domin. Example, there is a dll or class which shoould not create a file. Then we can create a permission and pass that permission in CreateDomiain method. Then call CreateInstanceAndUnwrap(assemblyName) We can also unlaod a domain.

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

What is managed and unmanaged code?

A

Managed code runs under CLR. Unmanaged code runs under its own runtime (C++). CLR managed GC, CLS, CAS etc.

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

How to see the Garage Collector?

A

CLRProfile.exe available in MSFT site.

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

What is a GC?

A

dot Net is an managed code and CLR takes care of clearing the objects. This process is called GC.

The way it works is that it will have gen0, gen1, gen2.

Any new object created will be in gen0. GS is a background thread and it will check the gen0 bucket. If objects are there and if the objects are used then they will be moved to the Gen1 bucket, Unused objects will be deleted. Next time, when the GC checks these, if the objects in Gen1 is required then it will move to Gen2.

WHy do we need this? This will help to optimzse the GC job. GC will frequenlt chec the Gen0 bucket.

Also having too many objects in Gen2 is not good. This will increase the process memory.

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

What is IDisposable?

A

If we want to clean up the resources in dot Net class, we cannot clear it as we do it in c++. this is becasue when GC find that the class is having a destructor, it will move the objects to the Gen 1 and Gen2. Hence, we need to used IDisposable interface. Inherit from IDisposable then implement Dispose method. Inside DIspose method call GC.SupressFinalaise();

public class hey : IDisposable
{
   ~hey()
     {}
 dispose(){
    GC.SupressFinalise()
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a strong reference and weak reference.

A

Suppose I have an exe and a dll. I will add a reference of the dll to the exe and invoke the class method of the dll.

The problem is that I can create a same dll and class name and replace this dll. The exe will load this dll.

To avoid this, we will use signing.

In weak reference, the dlls are laoded based on the class name and namespace names.

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

Why is a byte datatype?

A

Each characters are represented as 0 or 1 (bits). Byte is 2^8. 8 bits is 1 byte. Max - 255 and Min 0. Its unsigned/

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

Why is a byte datatype?

A

Each characters are represented as 0 or 1 (bits). Byte is 2^8. 8 bits is 1 byte. MaxValue - 255 and Min 0. Its unsigned/

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

What is difference between float, double and decimal?

A

float - 7 decimals
double - 16
decimal - 29

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

Tell about numeric daatypes?

A

two types
decimal - float, double, decimal
without decimals - int16, int 32 int64

without decimals we have unsigned version also

19
Q

Wha is difference between shot int and long?

A

short int16
int short32
long int64

20
Q

What is difference between shot int and long?

A

short int16
int short32
long int64

21
Q

How to convert string to int in c#?

A

Convert.ToInt16

22
Q

What is implicit and explicit casting?

A

In implicit casting the casting happens automatically. .Net allows it only if there is no data loss. Example, double d = 1;

In explicait casting, .Net wont allow it but we can force it to cast.

int e - 10.45;

23
Q

What is difference between casting and conversion?

A

In conversion we use c#’s Convert function

24
Q

What is the difference between string and String?

A

string is the alias of Staring. We can use any but prefer string. String.concat()

25
Q

Why do you say strings are immutable?

A

STring are objects. Unlike primitive datatype which are struct.
When we create a string

string d = “bharath”;
d=’amarnath’;

the second line will cerate a new object. and old object will be put for GC.

create a for loop, assign the string new GUIld Guid.NewGuid.ToString()

In Task manager you can see that process memory has increased.

Noe that if the string in the for loop is same then there will be no increase

26
Q

What is string interning?

A

Note that if the string in the for loop is same then there will be no increase.

27
Q

Stringuilder and string/

A

Strings are immutable and explain interning.

so we can use stringbuild

STringBuilder a = new StringBulder();
a.Append("sdfsdf");

this will not create a new object.

28
Q

Why strings are immutable?

A

For Thread safety

29
Q

What is checked and unchecked keywords?

A

int i = int.maXValue;
int y = int.MaxValue;

int z = i + y;

The above code wont show any exception but the value will be incorrect for z due to overflow.

int z = checked(i+z);

What is unchecked; It is same as default. But

const int i = int.maXValue;
const int y = int.MaxValue;

int z = i + y;

the above code will generate exception. If we remove this then we will use.

30
Q

What is a heap and stack?

A

When we create a variable we can either create it either in the stack or heap.

int i = 5;
int y= i;

In the above code, there are two placeholder will be created in the stack. one for i and another y. It is a assigment operator and makes a copy of that.

But,

class a = new a();

class b = a;

Here one object is created and stored it in the heap. It will create a reference in the stack. for class b it will create another reference and point to the same object in the heap.

31
Q

What is boxing and unboxing?

A

Boxing: When we assign a value from vlaue to reference then boxing happens.

int i = 0;

object a = i;

When boxing happens the value 0 is stored in heap and a refers to that value.

Boxing is implicit.

Unboxing: WHen we assign the reference to value then unboxing happens.

object a = 9;
int b= (int)a;

When unboxing happens, value is unboxed from heap and stored in stack

Boxing and unboxing should be avoided because it decreases the performance.

32
Q

What is out and ref?

A

addOne(int x, int res){
res += x;
}

addOne(7,0);

here the value is passed as value.

addOne(int x,ref int res){
res += x;
}

int result = 6;
addOne(7,ref result);

Here we area passing as reference.

In out, we have to initialize it in the method.

addOne(int x,outint res){
res += x;
}

int result = 6;
addOne(7,out result);

caller to callee - pass be value;

caller to callee and callee to caller : ref

callee to caller : out

33
Q

What si difference between const and readonly?

A

const is a compile time constant.

readonly is a runtime const.

example:

class program{
   public static const name = "bharath";
   public static readonly age;

program(){
age = 34;
}
}

readonly can be assigned only once.

34
Q

What is IS and AS keyword?

A

IS: TO ceheck if the object IS of type.

AS: used to convert object AS string (any other datatype

object str = “string”;

if(str is string)

AS

string eWhat = str as string;

35
Q

What is #Debug

A

The part of the code in #Debug will not be compiled.

36
Q

What is serialization and deserialization?

A

Serialization: Converting an object to a stream of bytes is called Serialization. After serialization, we can send this to a network, store it in a DB and file.

To serializa we have to call Serialize(). This method is in IFormatter.

IFormatter iBin = new BinaryFormatter();
Stream streamobj = new FileStream(@"C:/bharath.txt", ...);

iBin.Serialize(streamobj, obj);
streamobj.close();

class should be marked as Serializale.

37
Q

What is difference between GetType and Typeof?

A

Using both we can get the type pf an object. But in typeof we can do it without creating an object.

Customer cust1 = new Customer();
Customer cust2 = new Customer()

Employer emp1 = new Employer();

if(cust1.GetObject() == cust2.GetObject())

what if cust1 and cust2 are not there, we need to create a new object only to check. Hence, we can use typeof

if(typeof(Employer) == emp1.GetType())

38
Q

What is a NuGet?

A

NuGet is a platform where developers can share the code. Previously, we had to use dll. But, NuGet had made it simpler.

We can search and add a NuGet using NuGet manager.

We can host NuGets using nuget.org We can also host it privately.

39
Q

What is early binding and late binding?

A

In Early binding, It recognizes and checks the methods, or properties during compile time. In this binding, the compiler already knows about what kind of object it is and what are the methods or properties it holds, here the objects are static objects. The performance of early binding is fast and it is easy to code. It decreases the number of run-time errors.

In Dynamic or late binding it is done during runtime.

use dynamic and refelctions

40
Q

Question: How will you differentiate between a Class and a Struct?

A

Answer: Although both class and structure are user-defined data types, they are different in several fundamental ways. A class is a reference type and stores on the heap. Struct, on the other hand, is a value type and is, therefore, stored on the stack. While the structure doesn’t support inheritance and polymorphism, the class provides support for both. A class can be of an abstract type, but a structure can’t. All members of a class are private by default, while members of a struct are public by default. Another distinction between class and struct is based on memory management. The former supports garbage collection while the latter doesn’t.

41
Q

Question: What are Namespaces in C#?

A

Use of namespaces is for organizing large code projects. The most widely used namespace in C# is System. Namespaces are created using the namespace keyword. It is possible to use one namespace in another, known as Nested Namespaces.

42
Q

Explain Reflection in C#.

A

The ability of code to access the metadata of the assembly during runtime is called Reflection. A program reflects upon itself and uses the metadata to:

Inform the user, or
Modify the behaviour
The system contains all classes and methods that manage the information of all the loaded types and methods. Reflection namespace. Implementation of reflection is in 2 steps:

Get the type of the object, then
Use the type to identify members, such as properties and methods

43
Q

How can we mark a method as deprecated?

A

We have to use Obsolete attribute

[Obsolete(“this method is not used”, true)];

true means it will be shown as error

1st param is also optional.

This attribute will show as warning and if we want to show as error pass true

44
Q

What is the difference between Build VS Rebuild Vs Clean?

A

Rebuild = clean + build.

But in Rebuild it happens in sequence. SUppose project 1 is having dependency then the dependent project will be cleaned and built then the next dependent.

For clean and build, it will clean all the projects and then build.