C# Flashcards

1
Q

What is C#?

A

C# is a modern, general-purpose, object-oriented programming language developed by Microsoft. C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages on different computer platforms and architectures.

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

What is Jagged Array in C#?

A

A Jagged array is an array of arrays.

You can initialize a jagged array as −

int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};
Where, scores is an array of two arrays of integers - scores[0] is an array of 3 integers and scores[1] is an array of 4 integers.

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

In how many ways you can pass parameters to a method?

A

There are three ways that parameters can be passed to a method −

Value parameters − This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

Reference parameters − This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.

Output parameters − This method helps in returning more than one value.

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

Can you return multiple values from a function in C#?

A

Yes! Using output parameters. A return statement can be used for returning only one value from a function. However, using output parameters, you can return two values from a function.

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

What is the difference between ref and out parameters?

A

Output parameters are similar to reference parameters, except that they transfer data out of the method rather than into it. Reference parameter copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.

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

What is namespace in C#?

A

Namespaces are used to organize classes and control the scope of a class.

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

What is the purpose of using statement in C

A

using keyword is used to include a namespace in the program. A program generally has multiple using statements.

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

What are value types in C#

A

Value type variables can be assigned a value directly. They are derived from the class System.ValueType.

The value types directly contain data. Some examples are int, char, and float, which stores numbers, alphabets, and floating point numbers, respectively. When you declare an int type, the system allocates memory to store the value.

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

What are reference types in C#

A

The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.

In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are: object, dynamic, and string.

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

Which class acts as a base class for all the data types in .net?

A

The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.

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

What is boxing in C#?

A

When a value type is converted to object type, it is called boxing.

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

What is unboxing in C#?

A

When an object type is converted to a value type, it is called unboxing.

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

What are dynamic type variables in C#

A

You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at run-time.

Syntax for declaring a dynamic type is −

dynamic = value;
For example,

dynamic d = 20;

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

What is the difference between dynamic type variables and object type variables?

A

Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at run time.

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

What are pointer types in C#?

A

Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as the pointers in C or C++.

Syntax for declaring a pointer type is −

type* identifier;
For example

char* cptr;
int* iptr;

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

What is the is operator for?

A

is operator determines whether an object is of a certain type.

If( Ford is Car) // checks if Ford is an object of the Car class.

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

What is the as operator for?

A

as operator casts without raising an exception if the cast fails.

Object obj = new StringReader("Hello");
StringReader r = obj as StringReader;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is encapsulation?

A

Encapsulation is defined ‘as the process of enclosing one or more items within a physical or logical package’. Encapsulation, in object oriented programming methodology, prevents access to implementation details.

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

How encapsulation is implemented in C#?

A

Encapsulation is implemented by using access specifiers.

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

What is the purpose of an access specifier in C#?

A

An access specifier defines the scope and visibility of a class member.

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

What is scope of a public member variable of a C# class?

A

Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.

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

What is scope of a private member variable of a C# class?

A

Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.

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

What is scope of a protected member variable of a C# class?

A

Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance.

24
Q

What is scope of a Internal member variable of a C# class?

A

Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.

25
Q

What is scope of a Protected Internal member variable of a C# class?

A

The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance.

26
Q

What are nullable types in C#?

A

C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values.

For example, you can store any value from -2,147,483,648 to 2,147,483,647 or null in a Nullable variable. Similarly, you can assign true, false, or null in a Nullable variable.

27
Q

What is the use of Null Coalescing Operator (??) in C#?

A

The null coalescing operator is used with the nullable value types and reference types. It is used for converting an operand to the type of another nullable (or not) value type operand, where an implicit conversion is possible.

If the value of the first operand is null, then the operator returns the value of the second operand, otherwise it returns the value of the first operand.

28
Q

Can you create a function in C# which can accept varying number of arguments?

A

By using the params keyword, a method parameter can be specified which takes a variable number of arguments or even no argument.

29
Q

Can you pass additional type of parameters after using params in function definition?

A

No! additional parameters are not permitted after the params keyword in a method declaration. Only one params keyword is allowed in a method declaration.

30
Q

Which class acts as a base class for all arrays in C#?

A

The Array class is the base class for all the arrays in C#. It is defined in the System namespace. The Array class provides various properties and methods to work with arrays.

31
Q

How to sort an array in C#?

A

Using Array.sort(array) function. It sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.

32
Q

How to sort an array in C# in descending order?

A

First sort the array using Array.sort(array) then reverse the same using Array.reverse(array) method.

33
Q

What is a structure

A

In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.

Structures are used to represent a record. To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program.

34
Q

What are the differences between a class and structure

A

Classes and Structures have the following basic differences:

classes are reference types and structs are value types.

structures do not support inheritance.

structures cannot have default constructor.

35
Q

What is a enumeration in C#?

A

An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.

C# enumerations are value data type. In other words, enumeration contains its own values and cannot inherit or cannot pass inheritance.

36
Q

What is the default access for a class?

A

Default access specifier for a class type is internal.

37
Q

What is the default access for a class member?

A

Default access for the members is private.

38
Q

What is inheritance?

A

One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and speeds up implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.

39
Q

Is multiple inheritance supported in C#?

A

No! C# does not support multiple inheritance.

40
Q

How to inherit a class in C#?

A

A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces. The syntax used in C# for creating derived classes is as follows:

 class 
{
   ...
}
class  : 
{
   ...
}
41
Q

What is polymorphism?

A

The word polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as ‘one interface, multiple functions’.

42
Q

What is the difference between static polymorphism and dynamic polymorphism?

A

Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.

43
Q

What is the difference between static polymorphism and dynamic polymorphism?

A

Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.

44
Q

How C# supports static polymorphism?

A

two techniques to implement static polymorphism. They are:

Function overloading

Operator overloading

45
Q

What is early binding?

A

The mechanism of linking a function with an object during compile time is called early binding. It is also called static binding.

46
Q

What is function overloading?

A

You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.

47
Q

How C# supports dynamic polymorphism?

A

Dynamic polymorphism is implemented by abstract classes and virtual functions.

48
Q

What is a sealed class?

A

When a class is declared sealed, it cannot be inherited.

49
Q

How will you create sealed abstract class in C#?

A

No! It can not be created as abstract classes cannot be declared sealed.

50
Q

What are virtual functions in C#?

A

When you have a function defined in a class that you want to be implemented in an inherited class(es), you use virtual functions. The virtual functions could be implemented differently in different inherited class and the call to these functions will be decided at runtime.

51
Q

Is operator overloading supported in C#?

A

You can redefine or overload most of the built-in operators available in C#. Thus a programmer can use operators with user-defined types as well.

Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. Similar to any other function, an overloaded operator has a return type and a parameter list.

52
Q

What is an interface?

A

An interface is defined as a syntactical contract that all the classes inheriting the interface should follow. The interface defines the ‘what’ part of the syntactical contract and the deriving classes define the ‘how’ part of the syntactical contract.

Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. It often helps in providing a standard structure that the deriving classes would follow.

53
Q

What is a preprocessor directives in C#?

A

The preprocessor directives give instruction to the compiler to preprocess the information before actual compilation starts.

All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not statements, so they do not end with a semicolon (;).

54
Q

What is the use of conditional preprocessor directive in C#?

A

You can use the #if directive to create a conditional directive. Conditional directives are useful for testing a symbol or symbols to check if they evaluate to true. If they do evaluate to true, the compiler evaluates all the code between the #if and the next directive.

55
Q

Which class acts as a base class for all exceptions in C#?

A

C# exceptions are represented by classes. The exception classes in C# are mainly directly or indirectly derived from the System.Exception class. Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemException classes.

56
Q

What is the difference between System.ApplicationException class and System.SystemException class?

A

The System.ApplicationException class supports exceptions generated by application programs. Hence the exceptions defined by the programmers should derive from this class.

The System.SystemException class is the base class for all predefined system exception.