C# and OOP Flashcards

1
Q

Printing a line

A

Console.WriteLine(iValue);

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

Converting number to string

A

string strValue = floatValue.ToString();

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

Casting float to int

A

int iValue = (int)fValue;
(rounds down)

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

Converting string to number

A

string strValue = “13.8”;
float fValue = float.Parse(strValue);

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

Getting a substring of a string

A

string.Substring(a, b) gives you the b characters which come after character a

“vge1974”.Substring(2, 4) = “e197”

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

Conditional statement

A

if (condition)
{
code
}
else
{
more code
}

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

Comparison operators

A

==, !=, <, >, <=, >=

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

Logical operators

A

&&, ||, !
“true”, “false”

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

Expressing x^y

A

Math.Pow(x, y)

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

While loop

A

while (condition)
{
code
}

(can control flow using “break” and “continue”)

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

Do while loop

A

do
{
code
}
while (condition)

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

For loop

A

for (setup; condition; increment)
{
code
}

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

Foreach loop

A

foreach (<type> currentItem in listOfItems)
{
code
}</type>

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

What is OOP?

A

A programming framework that facilitates the reuse and extensibility of code. Bits of source code with relevant purposes are grouped together as classes, managed as a hierarchy with inheritance. Objects are instances of a class, which is like a blueprint.

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

Advantages of OOP

A

With OOP, source code becomes easier to maintain and follow
Code can be reused through inheritance, improving efficiency and productivity
Security through encapsulation

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

Drawing classes in UML

A

Attributes
———–
Methods

The constructor is always there but not listed

16
Q

Drawing the main program itself in UML

A

Attributes
———–
Methods

17
Q

Creating a new object using the constructor

A

Cow cowName = new Cow(“Moo”, 8);

18
Q

What is method overloading?

A

When a method has different declarations for different types of input

19
Q

What are static functions and variables?

A

Ones which belong to the class, rather than the instances of the class (the objects). They are usually used to denote functions/variables that are the same across all objects of that class. They can be called without creating an object of such a class. They have to be changed from the class, not the object.
They are underlined in UML.

20
Q

Example of using a static variable

A

Student s1 = new Student();
s1.studentName = “Henry”;

Console.WriteLine(s1.studentName); // instance variable
Console.WriteLine(Student.universityName); // static variable

21
Q

What is encapsulation and why is it useful?

A

This is where a class keeps all relevant information, but only exposes necessary information to other classes.

Issues avoided by encapsulation: human errors, intentional attacks, classes which are difficult to follow

22
Q

public vs private vs protected

A

public: accessible everywhere
private: accessible only within the same class
protected: accessible only within the same class AND child classes

23
Q

Old encapsulation method using setters and getters

A

public class Cow
{
private float _fWeight;
private string _strName;

public void SetFWeight(float fWeightIn)
{
    if (fWeightIn > 0.0f)
        _fWeight = fWeightIn;
}

public float GetFWeight()
{
    return _fWeight;
} }
24
Q

New encapsulation method using get/set

A

public class Cow
{
private float _fWeight;
private string _strName;

public float WeightPublic
{
set
{
if (value > 0.0f)
_fWeight = value;
}
get
{
return _fWeight;
}
}

25
Q

The “this” keyword

A

Refers to the current instance of the class

26
Q

What is inheritance and why is it useful?

A

Inheritance is where a child class can be derived from a parent class to retain and update the variables and methods of the parent class.
The UML symbol is an arrow with a white head.

27
Q

How to show inheritance in C#?

A

Syntax:
“public class Cow : Animal”

28
Q

Using “base”

A

Allows a child class to add code to its parent’s constructor

public Cow(string strName, float fWeight, bool bMakeMilk) : base(strName, fWeight)

29
Q

UML association arrow

A

A class creates an object of another class

30
Q

UML composition arrow

A

Black diamond
Type of association, where a class A creates an object B of another class, and B is destroyed when A is destroyed. A is called the container and B is called the content.

31
Q

UML aggregation arrow

A

White diamond
Opposite of composition, where the content is not destroyed when the container is destroyed.

32
Q

Using lists in C#

A

List<type> ListName = new List<type>();</type></type>

33
Q

List operations in C#

A

lstData.Count
lstData.Add(value)
lstData.Remove(value) // removes first occurrence
lstData.RemoveAt(k) // Removes the item at this position

34
Q

What is abstraction?

A

Where a parent class designs the inputs and outputs of the methods, leaving the child class to implement the actual contents of the methods

35
Q

Abstract classes vs abstract methods

A

Abstract classes are ones which cannot be used to create objects, only to derive new classes using inheritance.

Abstract methods are unfinished methods which only specify input/output values, and are expected to be provided by the derived class. Abstract methods can only be used in abstract classes, but an abstract class does not need to contain abstract methods.

36
Q

What is an interface?

A

An EU version of an abstract class, which only has public abstract methods. A child class can implement as many interfaces as needed, as opposed to only being able to inherit one abstract class

Declaration: public interface IDrawable

37
Q

What is polymorphism?

A

Polymorphic objects can be treated as different classes, meaning they can be cast to their parent’s class (e.g. a Cow can be cast to an animal)

Polymorphic methods can be overridden or hidden: the system decides which method to call with reference to the class of the object.