C# Flashcards

1
Q

Convert an int to a string.

A

Convert.ToString(myInt);

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

Convert an int to double.

A

Convert.ToDouble(myInt);

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

Convert double to int.

A

Convert.ToInt32(myDouble);

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

Convert bool to string.

A

Convert.ToString(myBool);

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

What is the ternary operator syntax?

A

variable = (condition) ? expressionTrue ; expressionFalse;

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

What is the syntax for a switch statement?

A
switch(expression)
{
case x:
//code block ;
break;
base y:
//code block ;
break;
default:
//code block ;
break;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does a while loop work?

A

It loops through a block of code as long as a specified condition is true.

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

What is the syntax for a while loop?

A
while (condition)
{
//code block to be executed;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does a do/while loop work?

A

The loop will execute the code block once before checking the condition is true. Will repeat as long as the condition is true.

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

What is the syntax for a basic do/while loop?

A
do
{
//code block to be executed;
}
while (condition);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why would you use a for loop?

A

If you know exactly how many times you want to loop through a block of code.

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

Explain each part of the basic for loop below.

for (statement 1; statement 2; statement 3)
{
// code block to be executed ;
}
A

Statement 1: executed one time before the execution of the code block. Sets up the variable before the loop starts (int i=0).
Statement 2: defines the condition for executing the code block. Defines the condition for the loop to run (i <5)
Statement 3: is executed every time after the code block has been executed. Increases the value each time the loop has been executed (i++)

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

What is the foreach loop used on?

A

Used to loop through elements in an array.

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

How would you set up a basic foreach loop?

A
foreach (type variableName in arrayName)
{
// code block to be executed;
}

Example:

string[] cars = {"Volvo", "BMW", "Ford"};
foreach (string i in cars)
{
Console.WriteLine(i);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are break statements used in and what do they do?

A

They terminate the closest enclosing loop or switch statement.

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

What does the continue statement do and where would you use it?

A

Continue breaks one iteration in a loop.

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

What is an Array?

A

A data structure that can store multiple variables of the same type.

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

Declare an array.

A

datatype [] variableName;

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

Call the Sort function on the following array.

string[ ] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};

A

Array.Sort(cars);

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

What namespace is needed for array methods Min, Max, and Sum?

A

System.Linq

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

Create and array of 4 elements without adding any elements.

A

string[] variable name = new string[4];

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

How do you set a default parameter?

A

MyMethod(string country = “Norway”)

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

What is method overloading?

A

Multiple methods with same name and different parameters.

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

What is a class?

A

A template for objects. An object constructor, or a blueprint for creating objects.

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

What is an object?

A

An instance of a class. Inherits all variables and methods from class.

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

Create and object from the Car class.

A
class Car
{
string color ="red";
static void Main(string[] args)
{
Car variableName = new Car();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Initialize the following array with any number of elements.

int[] array3;

A

array3 = new int[numberOfElements]{1,2,3,4,5,6};

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

Declare and initialize an array without the new keyword.

A

int[] array1 = {1,2,3,4,5};

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

What does CLR stand for and what does it do?

A

CLR = Common Language Runtime.

It translates source code into a form of bytecode known as CIL (Common Intermediate Language). Part of the .Net framework.

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

What is an access modifier?

A

A keyword that sets the access level/visibility for classes, fields, methods and properties.

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

What does the public keyword do?

A

Makes the code accessible for all classes.

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

What does the private keyword do?

A

Code is only accessible in same class.

33
Q

What does the protected keyword do?

A

Code is accessible within the same class, or in a class that is inherited from that class.

34
Q

What does the internal keyword do?

A

Code is accessible within its own assembly, but not from another assembly.

35
Q

What is Encapsulation?

A

The process of making sure that “sensitive” data is hidden from users.

36
Q

How does one achieve “Encapsulation”?

A
  1. ) Declare fields/variables as private.

2. ) Provide ‘public’ ‘get’ and ‘set’ methods, through properties, to access and update the value of a private field.

37
Q

What are the two methods of properties?

A

‘get’ and ‘set’

38
Q

What is the List Class Add method syntax and What does it do?

A

listName.Add()

Adds an object to the end of a list.

39
Q

Declare a list.

A

List<> myList = new List<>();

40
Q

1 What is the List Class AddRange syntax? What does it do?

A

listName.AddRange(varArr);

Adds the elements of the specified collection to the end of the List.

41
Q

2 What is the List Class ForEach syntax? What does it do?

A

listName.ForEach(Action );
Performs the specified action on each element of the List.

The parameter is the action delegate to perform on each element of the list.

*I think this could also be a lambda expression.

42
Q

3 What is the List Class Remove syntax? What does it do?

A

listName.Remove(“Compsognathus”);
Removes the first occurrence of a specific object from the List.

Returns true if item is removed; otherwise, false. Also returns false if item was not found in the list.

43
Q

4 What is the List Class Reverse syntax? What does it do?

A

ListName.Reverse();

Reverses the order of the elements in the List or a portion of it.

44
Q

5 What is the List class Sort Method syntax and what does it do?

A

dinosaurs.Sort(0, herbivores, dc);
dinosaurs.Sort();
Sorts the elements or a portion of the elements in the List using either the specified or default IComparer implementation or a provided Comparison delegate to compare list elements.

45
Q

6 What is the List class Contains Method syntax and what does it do? BONUS what is the exists method and how does it differ?

A

listName.Contains(“seat”)
Determines whether an element is in the List. Returns a boolean.

dinosaurs.Exists(EndsWithSaurus)
Determines whether the List contains elements that match the conditions defined by the specified predicate. Returns a boolean.

46
Q

7 What is the List class Find syntax and what does it do?

A

listName.Find(X=> x.PartName.Contains(“seat”))
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List.
Returns
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

47
Q

8 What is the List Class Insert syntax and what does it do?

A
listName.Insert(2, new Part() { PartName = "brake lever", PartId = 1834 });
Inserts an element into the List at the specified index.
48
Q

9 What is the List Class RemoveAt syntax and what does it do?

A

This will remove the part at index 3. listName.RemoveAt(3);

Removes the element at the specified index of the List.

49
Q

10 What is the List Class GetRange syntax and what does it do?

A

Creates a shallow copy of a range of elements in the source List.
A shallow copy of a range of elements in the source List.
GetRange (int index, int count);

50
Q

What is the String Class Contains syntax and what does it do?

A

bool b = s1.Contains(s2);
This method performs an ordinal comparison beginning at the first character position and continuing to the last. It returns a boolean.

51
Q

What is the String Class IndexOf syntax and what does it do?

A

myString.IndexOf(“about”);
Returns the zero-based index position of the value parameter from the start of the current instance if that string is found, or -1 if it is not. If value is Empty, the return value is startIndex.

52
Q

What is the String Class Insert syntax and what does it do?

A

myString.Insert(istartIndex, stringAdd);

A new string that is equivalent to this instance, but with value inserted at position startIndex.

53
Q

What is the String Class Join syntax and what does it do?

A

String.Join(separator, values);
Concatenates the elements of a specified array or members of a collections, using the specified separator between each element or member.

54
Q

What is the String Remove syntax and what does is it do?

A

myString.Remove(startIndex,numberOfElements);
Returns a new string with characters deleted from the startindex to the end of the string, if no second argument is passed.

55
Q

What is the String Replace Method syntax and what does it do?

A

myString.Replace(oldValue, newValue);
Returns a string that is equivalent to the current string except that all instances of oldValue are replaced with newValue.

56
Q

What is the String Split Method Syntax and what does it do?

A

myString.Split(delimiter);

Splits a string into a maximum number of substrings based on specified delimiting characters and, optionally, options.

57
Q

What is the String Substring Method Syntax and what does it do?

A

myString.Substring(startIndex, numberOfCharacter);
Retrieves a substring from this instance. The substring starts at a specified character position and continues for the number of characters specified. If no second argument is passed, it will continue to the end o f the string.

58
Q

What is a lambda expression and what is its syntax?

A

x=>x*x

This is an anonymous function.

59
Q

Declare and initialize a 2 dimensional array.

A
int [,] 2dArray = {
{2,3},
{4,5},
{6,7}
};
60
Q

What is the syntax for changing a string to uppercase?

A

someText.ToUpper();

61
Q

What is the syntax for changing a string to lowercase?

A

someText.ToLower();

62
Q

What is the syntax to find the last index of something in a string?

A

someText.LastIndexOf(“i”);

63
Q

What is the syntax for the String Trim method? What does it do?

A

someText.Trim();

Removes white spacing from a string.

64
Q

What is the syntax for the String method that checks for null or empty?

A

String.IsNullOrEmpty(someText);

Useful for checking user input.

65
Q

What is the syntax for the String method that checks for null or white space?

A

String.IsNullOrWhiteSpace(someText);

Useful for checking user input.

66
Q

What is the method for converting a number to a String?

A

intNumber.ToString();

67
Q

What is the syntax for the Int32 Parse method and what does it do?

A

Int32.Parse(“stringNumber”);

Converts the string representation of a number to its 32-bit integer equivalent.

68
Q

Use Convert to change a string to an int. Bonus What will this return if null or empty?

A

Convert.ToInt32(StringNumber);

0

69
Q

What namespace is needed in order to use the StringBuilder class?

A

System.Text;

70
Q

How do you create a new StringBuilder object?

A

StringBuilder newString = new StringBuilder();

71
Q

What is the StringBuilder Append syntax?

A

newString.Append(‘char’, int);

The second paramater is how many times the appended character is added. Appends to the end.

72
Q

What is the StringBuilder Replace syntax?

A

newString.Replace(“Thing to replace”, “Thing replacing”);

73
Q

What is the StringBuilder Remove syntax?

A

newString.Remove(start index, length);

Returns a reference to this instance after the excise operation has completed.

74
Q

What is the StringBuilder Insert syntax?

A

newString.Insert(index to insert, thing to insert);

75
Q

What does OOP stand for?

A

Object-Oriented Programming

Creates objects that contain both data and methods.

76
Q

What is procedural programming?

A

Focused on writing procedures or methods that perform operations on the data.

77
Q

What is the Average method? What class is it under?

A

grades.Average();

Enumerable

78
Q

What namespace is needed to utilize Enumerable methods?

A

System.Linq

79
Q

What is the Cast method and what does it do?

A

fruits.Cast();

Casts the elements of an IEnumerable to the specified type.