Part1 - Tutorials EU Flashcards

1
Q

What is a class?

A

A class is a template to create an object. It contains properties as well as methods.

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

What are the key concepts of object-oriented programming?

A

ncapsulation, abstraction, polymorphism, and inheritance are the main concepts of object-oriented programming.

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

Explain Encapsulation

A

Encapsulation is a process of wrapping function and data members together in a class. It is like a capsule, a single unit.

Encapsulation is to prevent the unauthorized or unwanted change of data from outside of the function.

For example, we define the properties of class as private. This prevents form direct access of provate members. But we can provide a way to set and get using getters or setters.

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

What is a constructor, and what are its different types?

A

A constructor is like a method with the same name as the class, but it is a unique method. Even if it is not created, the compiler creates a default constructor in memory at the time of creating an object of the class.

The constructor is used to initialize the object with some default values.
Default constructor, parameterized constructor, copy constructor, static constructor, private constructor are different constructor types.

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

What is a private constructor?

A

Private Constructor prevents the creation of classes. It also prevents the inhertance of the class.

Usually used in the Singleton pattern where constructor is a private and we will define a public method getInstance(). In this method if instance is not created we will create and return it.

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

What is a destructor in C#?

A

The Destructor clears out the memory to free up resources. It is managed automatically by the garbage collector. System.GC.collect() is called internally for this purpose. However, if required, it can be done explicitly using a destructor.

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

Is C# code managed or unmanaged code?

A

C# is managed code because Common language runtime compiles the code to Intermediate language.

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

What are value types and reference types?

A

We can categorize variables into value type and reference type.

Variables of value type contain the value directly while the variable of a reference type contains the reference of the memory address where the value is stored actually in the memory.

For example bool, byte, int, char, and decimal are value type

And String, class, delegates are examples of reference types.

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

What are namespaces, and is that compulsory?

A

A namespace is a way of organizing classes of the same group or functionality under the same name. We can call it a module. Although it is not compulsory to put class in a namespace.

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

What is an interface?

A

An interface is another form of an abstract class that has only abstract public methods, and these methods only have the declaration and not the definition. A class implementing the interface must have the implementation of all the methods of the interface.

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

ow to implement multiple interfaces with the same method name in the same class?

A

if we want to implement multiple interfaces with the same method name, then we cannot directly implement the body of the function. We have to explicitly provide the name of the interface to implement the body of the method. In this way, the compiler decides which interface methods we are referring to, and this resolves the issue.

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

What is the virtual method, and how is it different from the abstract method?

A

A virtual method must have a default implementation, and We can override this virtual method using the override keyword in the derived class.

The abstract method is without implementation, and it is created inside the abstract class only. In the case of an abstract class, the class derived from the abstract class must have an implementation of that abstract method.

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

What is method overloading and method overriding?

A

Method overloading is when we have a function with the same name but a different signature.

Method overriding is when we override the virtual method of a base class in the child class using the override keyword.

Both, method overloading and overriding are a type of polymorphism.

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

Can we use “this” with the static class?

A

No, we cannot use “this” with the static class because we can only use static variables and static methods in the static class.

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

What is the difference between constants and read-only?

A

Constant variables have to be assigned a value at the time of declaration only, and we cannot change the value of that variable throughout the program.

We can assign the value to the read-only variable at the time of declaration or in a constructor of the same class.

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

What are Abstract class?

A

AN abstract class is a partially defined parent class.

For example, we have a Customer base class. This base class has a method, applyDiscount. We have two derived class GoldCustomer and SilverCustomer. Without Abstract class, we can use overriding and make the base’s applyCoupon as virtual. But this has a drawback. What if somebody calls it. Hence we can make this as abstract method. WHen we have a abstratc method, the class should be abstract.

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

ARe abstract methods virtual?

A

Yes

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

Ca n we create an instance of an abstract class?

A

No

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

Is it compulasary to implement abstract methods?

A

Yes

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

can we write a logic in interface

A

No

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

Can we define methods as private in interface?

A

No

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

Whats the difference between a .NET and C#?

A

.NET is a framework and C# is a programming language, that’s what interviewer wants to hear.
.NET framework has collection of ready-made libraries. So for example you want List, Dictionary it has
“System.collections.dll” , you want to make HTTP calls it has “System.Net.Http.dll” and so on.
While C# is programming language it has syntax, grammar, it has those “for loops”,” IF” conditions and so
on.
C# language invokes / orchestrates .NET framework libraries to create an application.

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

What is an interface?

A

Interface is a contract and by having a contract we have better control on change management and breaking things.

Interfaces support multiple inheritance and this helps to add new methods without affecting the old interface.

24
Q

Can we create instance of an interfaces?

A

No

25
Q

can we do multiple inheritace with abstract classes?

A

No

26
Q

If I want to change the interfaces what is the best practice?

A

Multiple inheirtance

27
Q

WHy do we need construcitr?

A

COnstructor is a special class which gets automatically invoked when a instance of a class is created.

28
Q

In parent child whihc construcor fires first

A

First the base constructor is called then the derived construcotr is called.

29
Q

How are initializers executed?

A

First the derived then base

30
Q

How are static constructors are executed?

A

First static derived ctor -> static base ctor-> base ctor-> derived ctor.

31
Q

Can we overlaod stayic methods?

A

yes

32
Q

can we override static?

A

NO

33
Q

What are static class?

A
A static class can only contain static data members, static methods, and a static constructor.
It is not allowed to CREATE OBJECTS of the static class.
Static classes are SEALED, means one cannot inherit a static class from another class.
A static class is declared with the help of STATIC keyword.
34
Q

What is difference between ref and out?

A

Ref and out pass by ref.

Out only from callee to caller. Out needs to be initialized in caller function.

ref value retains in the caller function.

35
Q

What is shadowing or method hiding?

A

The child class methods will be hidden form the parent class.

  public class baseClass
    {
        public virtual void print()
        {
            Console.WriteLine("Base class");
        }
    }
    public class derivedClass : baseClass
    {
        public void print()
        {
            Console.WriteLine("Derived class");
        }
    }

baseClass b = new derivedClass();

        b.print();
            derivedClass d = new derivedClass();
            d.print();
36
Q

What is a sealed class?

A

Class which cannot be inheirted

37
Q

Can we create an object of a sealed class?

A

Yes

38
Q

What is nested class?

A

A class inside a class

39
Q

Uses of nested class?

A

Logically group classes. The class we are nesting is only useful inside an enclosing class.

40
Q

Can nested class access the vaiableds of outer class?

A

No! but we can pass the instanse and use it.

41
Q

can a nested class use any access modifiers?

A

Yes. To hide use private.

42
Q

What are partial class?

A

Partial classes can be written in many physical files and while compiling it compiles into one file.

43
Q

What are extension methods?

A

If we have to extend the existing class methods, then we use extension methods.

To create an extension method,
Create a static class
Inside this static class, create a static method. This method is an extension method. 
The extension method should take class name that is extending as it's first parameter. But we should also pass "this " along with the class name.
44
Q

What is nullable?

A

Data types in C# are of two types: Value Type and Reference Type; Value types cannot have Null values, but for reference types can have null value.

However, sometime we need to have null values for value types. For example, we may have a class and this class maps to a table in the database. When we map a class to the database, each property of a class will be a column in the table. And a field in the table can be NULL. But if C# does not allow values to be NULL, then we will end up having default values in the fields of the database instead of NULL.

45
Q

WHAT IS THE DIFFERENCE BETWEEN VAR AND DYNAMIC IN C#?

A

VAR - The type of the variable is decided by the compiler at compile time. DYNAMIC - The type of the variable is decided by the compiler at run time.

46
Q

How do you define const?

A

const float f = 3.14f;

47
Q

WHat is the range of BYTE?

A

0-255

48
Q

What is checked keyword?

A
checked
{
   byte number = 255;
number = number+1;
}
49
Q

How do you convert string to any primitive type?

A

primitiveType.Parse()

or

Convert.ToByte()
ToIn64
ToInt32

50
Q

Types of operators?

A
Arthmetic operators: + - / * ++ -- %
Cpmparision operators: == !=
Assignment : = += -=
Logical operators: && || !
Bitwise Operators
51
Q

How do you create an array in C#?

A

int[] nubers = new int[3] { 1 ,2 ,3}

52
Q

What is a verbatim string?

A

var stringVar = @”C:\PF\mcafee”

53
Q

how to use format?

A

String.Format(“{0} {1}”, firstName, lastName);

54
Q

array to string?

A

string.join(“,”, numbers);

55
Q

What are the differences between struct and classes?

A

Strut are value type. Classes are reference type.

members are public by default. Class members are private by defualt.

56
Q

How to read each character of a string?

A

var name = “bharath”

foreach(var c in name)

57
Q

how to generate random number?

A

var rand = new Random()

rand.Next(min,max)