CSharp Flashcards

1
Q

Explain Indexers in C#?

A

Indexers are used for allowing the classes to be indexed like arrays. Indexers will resemble the property structure but only difference is indexer’s accessors will take parameters.

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

What is the difference between “out” and “ref” parameters in C#

A

“out” parameter can be passed to a method and it need not be initialized where as “ref” parameter has to be initialized before it is used.

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

What is Constructor Overloading in C# .net ?

A

In Constructor overloading, n number of constructors can be created for the same class. But the signatures of each constructor should vary.

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

What is Operator Overloading in C# .net ?

A

We had seen function overloading in the previous example. For operator Overloading, we will have a look at the example given below. We had defined a class rectangle with two operator overloading methods.

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

Name the compiler of C#?

A

C# Compiler is – CSC.

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

What is Unboxing in C#

A

UnBoxing – It’s completely opposite to boxing. It’s the process of converting reference type to value type. For example, int myvar2 = (int)myObj;

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

Explain Overloading in C# ?

A

When methods are created with the same name, but with different signature its called overloading.

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

Explain Static constructor in C#?

A

If the constructor is declared as static then it will be invoked only once for all number of instances of a class. Static constructor will initialize the static fields of a class.

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

What is the difference between methods – “System.Array.Clone()” and “System.Array.CopyTo()” in C#?

A

“CopyTo()” method can be used to copy the elements of one array to other: “Clone()” method is used to create a new array to contain all the elements which are in the original array.

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

What is Sealed Classes in c# ?

A

If a class is defined as Sealed, it cannot be inherited in derived class

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

Which are the loop types available in C#?

A

For, While, Do..While

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

What is Abstract Class in C#?

A

If we don’t want a class to be instantiated, define the class as abstract. An abstract class can have abstract and non abstract classes. If a method is defined as abstract, it must be implemented in derived class.

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

Different Types of C# overloading

A

Constructor, Function, Operator

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

What is an Object?

A

An object is an instance of a class. It contains real values instead of variables.

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

Can a “try” block exist without a “catch” block

A

Yes, but we must have a “finally” block

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

Explain Partial Class in C#?

A

Partial classes concept added in .Net Framework 2.0 and it allows us to split the business logic in multiple files with the same class name along with “partial” keyword.

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

Explain the types of unit test cases?

A

Positive Test Cases, Negative Test Cases, Exception Test Cases

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

What is a delegate in C#

A

Delegates are type safe pointers unlike function pointers as in C++. Delegate is used to represent the reference of the methods of some return type and parameters.

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

What are C# reference types

A

class, string, interface, object

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

What are the uses of delegates in C#

A

Callback Mechanism, Asynchronuous Processing, Abstract and Encapsulate method; Multicasting

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

What is Thread in C#?

A

Thread is an execution path of a program. Thread is used to define the different or unique flow of control. If our application involves some time consuming processes then it’s better to use Multithreading., which involves multiple threads.

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

What are the differences between events and delegates in C#?

A

Main difference between event and delegate is event will provide one more of encapsulation over delegates. So when you are using events destination will listen to it but delegates are naked, which works in subscriber/destination model.

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

Define Property in C# ?

A

Properties are a type of class member, that are exposed to the outside world as a pair of Methods. For example, for the static field Minsalary, we will Create a property as shown below. Public static double MinSalary{get { return minimumSalary;} set { minimumSalary = value}}

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

Why to use lock statement in C#?

A

Lock will make sure one thread will not intercept the other thread which is running the part of code. So lock statement will make the thread wait, block till the object is being released.

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

What is an Interface in C# ?

A

An interface is similar to a class with method signatures. There wont be any implementation of the methods in an Interface. Classes which implement interface should have an implementation of methods defined in the abstract class.

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

What are the features of C#

A

Constructors and Destructors; Properties; Passing Parameters; Arrays; Main, XML Documentation; Indexers

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

Explain Hashtable in C#?

A

It is used to store the key/value pairs based on hash code of the key. Key will be used to access the element in the collection

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

What are 2 types of errors in c#

A

Compile time error; Run time error

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

What is a const in C#

A

Keyword used for making an entity constant; the value can not be reassigned

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

List out the pre defined attributes in C#?

A

Conditional, Obsolete, Attribute Usage

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

What is the difference between “StringBuilder” and “String” in C#

A

StringBuilder is mutable, which means once object for the stringbuilder is created it can be modified either using Append, Remove or Replace. String is immutable which means we cannot modify the string object and will always create a new object in memory of type string

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

Explain Attributes in C#?

A

Attributes are used to convey the info for runtime about the behavior of elements like – “methods”, “classes”, “enums” etc. Attributes can be used to add metadata like – comments, classes, compiler instruction etc.

33
Q

What is the difference between “continue” and “break” statements in C#?

A

“continue” statement is used to pass the control to next iteration. This statement can be used with – “while”, “for”, “foreach” loops. “break” statement is used to exit the loop.

34
Q

What is a class

A

A class is the generic definition of what an object is. A Class describes all the attributes of the object, as well as the methods that implement the behavior of the member object. In other words, class is a template of an object. For ease of understanding a class, we will look at an example. In the classEmployee given below, Name and Salary are the attributes of the class Person. TheSetter andGetter methods are used to store and fetch data from the variable.

35
Q

Is C# code is unmanaged or managed code?

A

C# code is managed code because the compiler – CLR will compile the code to Intermediate Language.

36
Q

What is enum in C#?

A

enum keyword is used for declaring an enumeration, which consists of named constants and it is called as enumerator lists.Enums are value types in C# and these can’t be inherited. Below is the sample code of using Enums Eg: enum Fruits { Apple, Orange, Banana, WaterMelon};

37
Q

What is Polymorphism in C#

A

The ability of a programming language to process objects in different ways depending on their data type or class is known as Polymorphism.There are two types of polymorphism Compile time polymorphism. Best example is Overloading Runtime polymorphism. Best example is Overriding

38
Q

Explain object pool in C#?

A

Object pool is used to track the objects which are being used in the code. So object pool reduces the object creation overhead.

39
Q

Protected Access Modifier

A

Whenattribute andmethods are defined as protected, it can be accessed by any method in the inherited classes and any method within the same class

40
Q

List out some of the exceptions in C#?

A

NullReferenceException; ArgumentNullException; DivideByZeroException; IndexOutOfRangeException; InvalidOperationException; StackOverflowException

41
Q

What is the static keyword in C#

A

“Static” keyword can be used for declaring a static member. If the class is made static then all the members of the class are also made static. If the variable is made static then it will have a single instance and the value change is updated in this instance.

42
Q

What is the difference between CType and Directcast in C#?

A

CType is used for conversion between type and the expression. Directcast is used for converting the object type which requires run time type to be the same as specified type.

43
Q

Can we override private virtual method in C#

A

No we can’t override private virtual methods because they are no accessible outside the class

44
Q

What is the difference between “finalize” and “finally” methods in C#

A

Finalize - This method is used for garbage collection. So before destroying an object this method is called as part of clean up activity. Finally - This method is used for executing the code irrespective of whether an exception occurred or not.

45
Q

Why do we use “using” in C#

A

“Using” statement calls - “dispose” method internally, whenever any exception occurred in any method call and in “Using” statements objects are read only and cannot be reassignable or modifiable

46
Q

Internal Access Modifier

A

If an attribute or method is defined as Internal, access is restricted to classes within the current project assembly.

47
Q

Explain Anonymous type in C#?

A

This is being added in C# 3.0 version. This feature enables us to create an object at compile time.Below is the sample code for the same – Var myTestCategory = new { CategoryId = 1, CategoryName = “Category1”};

48
Q

Can we use “this” inside a static method in C#

A

No.

49
Q

What is Boxing in C#

A

Boxing – This is the process of converting from value type to reference type. For example, int myVar = 10; object MyObj = myVar

50
Q

What is overriding in c# ?

A

To override a base class method which is defined as virtual, Override keyword is used. In the above example, method DriveType is overridden in the derived class.

51
Q

How to check whether hash table contains specific key in C#?

A

Eg: myHashtbl.ContainsKey(“1”);

52
Q

What are the differences between public, static and void

A

Static classes/methods/variables are accessible throughout the application without creating an instance. Compiler will store the method address as an entry point. Public methods or variables are accessible throughout the application. Void is used for the the methods to indicate it will not return any value

53
Q

What is the difference between “as” and “is” operators in C#?

A

“as” operator is used for casting object to type or class. “is” operator is used for checking the object with type and this will return a Boolean value.

54
Q

Which string method is used for concatenation of two strings in c#?

A

“Concat” method of String class is used to concatenate two strings. For example, string.Concat(firstStr, secStr)

55
Q

Explain Inheritance in C#

A

In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects. In inheritance, there will be two classes - base class and derived classes. A class can inherit attributes and methods from existing class called base class or parent class. The class which inherits from a base class is called derived classes or child class.

56
Q

What are C# value types

A

decimal, int, byte, enum, double, long, float

57
Q

Explain Generics in C#

A

Generics in c# is used to make the code reusable and which intern decreases the code redundancy and increases the performance and type safety.Namespace – “System.Collections.Generic” is available in C# and this should be used over “System.Collections” types.

58
Q

What is Method Hiding in C# ?

A

If the derived class doesn’t want to use methods in the base class, derived class can implement it’s own version of the same method with same signature. For example, in the classes given below, DriveType() is implemented in the derived class with same signature. This is called Method Hiding

59
Q

Protected Internal Access Modifier

A

If an attribute or method is defined as Protected Internal, access is restricted to classes within the current project assembly and types derived from the containing class.

60
Q

Explain access modifier “protected internal” in C#

A

“protected internal” can be accessed in the same assembly and the child classes can also access these methods

61
Q

Explain the use of Virtual Keyword in C#

A

When we want to give permission to a derived class to override a method in base class, Virtual keyword is used.

62
Q

What are the collection types can be used in C#?

A

ArrayList, Stack, Queue, SortedList, HashTable, Bit Array

63
Q

Private Access Modifier

A

When a method or attribute is defined as Private, It can be accessed by any code within the containing class only

64
Q

Explain the methods and properties of Thread class in C#?

A

CurrentCulture, CurrentThread, CurrentContext, IsAlive, IsThreadPoolThread, IsBackground, Priority

65
Q

Explain Static Members in C# ?

A

If an attribute’s value had to be same across all the instances of the same class, the static keyword is used. For example, if the Minimum salary should be set for all employees in the employee class, use the following code private static double MinSalary = 30000;

66
Q

What is the difference between “dispose” and “finalize” variables in C#

A

Dispose - This method uses interface - “Idisposable” interface and it will free up both managed and unmanaged codes like - database connection, files etc. Finalize - This method is called internally unlike Dispose method which is called explicitly. It is called by the garbage collector and can’t be called from the code

67
Q

Public Access Modifier

A

When a method or attribute is defined asPublic, it can be accessed from any code in the project.

68
Q

List out the states of a thread in C#?

A

Unstarted State, Ready State, Not Runnable State, Dead State

69
Q

What is Data Encapsulation

A

Data Encapsulation is defined as the process of hiding the important fields from the end user. If we give a third party code to have complete control over the field without any validation it can adversely affect the functionality

70
Q

What is a Destructor in C# ?

A

Destructor is a special method that get invoked/called automatically whenever an object of a given class gets destroyed. Main idea behind using destructor is to free the memory used by the object.

71
Q

Can we use delegates for asynchronous method calls in C#?

A

Yes. We can use delegates for asynchronous method calls.

72
Q

What is the difference between “throw ex” and “throw”

A

“throw ex” will replace the stack trace of the exception with stack trace info of re throw point. “throw” will preserve the orginal stack trace info

73
Q

What is the difference between an Array and ArrayList in C#

A

Array stores the values or elements of same data type but an arraylist stores values of different datatypes. Array uses fixed length but arraylist does not use fixed length

74
Q

Explain String Builder class in C#

A

String Builder is a mutable string of characters. It allows us to Insert, Remove, Append and replace the characters. “ToString()” can be used to create the final string

75
Q

What is a namespace

A

Namespaces are containers for the classes. We will use namespaces for grouping the related classes in C#. “Using” keyword can be used for using the namespace in other namespace

76
Q

What is a Constructor in C# ?

A

Constructor is a special method that get invoked/called automatically, whenever an object of a given class gets instantiated

77
Q

How we can sort the array elements in descending order in C#?

A

“Sort()” method is used with “Reverse()” to sort the array in descending order.

78
Q

What is a sealed class in C#

A

Sealed class is used to prevent the class from being inherited from other classes. So “sealed” modifier also can be used with methods to avoid the methods to override in the child classes.