C# Programming Flashcards

1
Q

Given an example of some possible properties for the class Pet and their data types.

A
Pet{
Name : string;
Age: int;
Species: string;
Food: string
HasFur: boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why do we use classes?

A
To reduce duplicate code and allows us to quickly make objects that have similar data.
It allows us to write class specific code that dictates what should be done with this data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What two things do classes have?

A

Data - represented by fields

Behaviours - represented by methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Declare a class for Snake that has fields: Name as a string and Age as an int. 
Also write a method called feed that takes no parameters and prints the word 'Gulp!"
A
public class Snake
{   
    private string Name;
    private int Age;
    public void Feed ()
    {
      Console.WriteLine("Gulp!");
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would you initialize an object called Bane of class Dog?

A

var Bane = new Dog();

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

What is the purpose of a constructor?

A
Constructors are a method that are called when the instance of a class is created. It puts the object in an early state with empty fields.
Numerals are set to 0, characters to '\0', boolean to false and strings and classes to null.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why do we use overloading?

A

Overloading is using the same method name with different parameters or return types. That way we can control how a method deals with data based on the different data that is given to it. E.g Do x if given a name, Do y if given a number etc.

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

How is the ‘this’ keyword used in C#?

A

The ‘this’ keyword refers to the current instance of a class or struct. It refers to an object field not a local parameter so in the line:
this.price = price
this.price refers to the price field of an object
price refers to the parameter price of the method.

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

What are the two main types of visibility?

A

Public - Member can be reached from anywhere

Private - Can only be reached by members of the same class.

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

How would you set a read only field for a string Name?

A

public string Name {get; private set;}

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

How would you set a write only field for an int Age?

A

public int Age {private get; set;}

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

Can you use logic to validate object fields?

A
Yes you can, you can validate the fields by putting the logic in the set clause. E.g
public int Age { get { return age;}
set{
  if(value<0)
{Console.WriteLine("Age is less than zero")}
else
{age = value;}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you overload a method with the same data types?

E.g one overload takes in two ints and a second overload takes in to ints?

A

No. Overloads must have different parameters.

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

How are properties useful in C#?

A
Properties are used to access private variables that are located elsewhere in the program. For example
private string name;  is a field
public string Name; is a property
{
get { return name; }
set { name = value; }
}
get retrieves the private field value and set sets the value of Name to the value retrieved from the private field. We can now use Name in our program while name remains inaccessible.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Instead of a property how else could you access field data while maintaining it stays private?

A

You could create a method that updates data. For example if you had a User class with private strings for email, username, password you could create a method called registerUser that takes in a username, email and password and updates the data in an object.

public void registerUser(string username, string password, string email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is aggregation?

A

Aggregation is where we have two separate classes that reference each other. They can exists on their own but can also use data from another class. Therefore we create a reference to one class within another class.

17
Q

Why do we use OOP?

A

We use OOP so that teams can work and build large robust programs separately but still have them come together and work well together once assembled.

18
Q

What are the 4 pillars of OOP?

A

Encapsulation: Groups related objects and functions together which increases reusability and reduces complexity.

Abstraction: Hides some properties and functions from the outside to make interfacing with the classes/methods simpler and reduces the impact of change on the classes.

Inheritance: Allows us to define a base group of properties and methods for a class that every sub-class has and can use. Reduces the amount of code in a program.

Polymorphism: Allows us to remove long ‘if else’ or ‘switch’ statements. Allows us to give each instance of an object instructions on how to behave in different conditions with different parameters.

19
Q
What does the notation:
public class car:vehicle mean?
A

It means that car inherits from vehicle

20
Q

How do we express requirements in OOP design?

Give an example.

A

User stories are used to express requirements.

E.g. “As a seller, I want to list my item for sale and look at bids for my item”

21
Q

What was the old system of development?

What is the new more robust system?

A

Waterfall development was the old way.

Incremental iteration is the new way. We take on small part or system at a time then find requirements -> design system -> implement system -> test system -> refactor

22
Q

What is refactoring?

A

Refactoring is the process of modifying code so that it has the same functionality but is ready to facilitate new required functionality.

23
Q

What are the steps of OOD?

A
  1. Identify candidate classes by looking for nouns in the user story e.g Book, Car, Item
  2. Evaluate each class by given it a responsibility by looking for verbs in user stories e.g Rent, Buy, Sell
  3. Determine the relationships between classes by looking at what data is needed by each class e,g Renting a book, Buying a car
24
Q

What are the four relationships between classes in OOP and an example of each?

A
Has-a = A car has-a windscreen
Part-of = A player is part of a team
Member-of- A car is a member of the vehicle class
Uses = A car uses the road to drive on.
25
Q

What is UML used for? Draw a UML diagram for Snake class. With fields Age, Length, Name and methods Feed and Shed.

A

To show the fields and methods in a class.

Snake
int Age;
float Length;
string Name;
Feed();
Shed();
26
Q

What does the KISS method of software development pertain to?

A

Keep It Simple Stupid
Each method should do just one thing.
It should either choose and item and do something with the item OR make a decision and act on that decision.
Anything more should be a separate method.

27
Q

What sort of relationship is Inheritance?

A

Inheritance is a ‘Is-A’ relationship

28
Q

Create a public class called Dog with fields name and age and a display() method. Then create a child class of Labrador that adds a bool ‘likesFood’ and a display() method.

A
public class Dog 
{ 
  string Name;
  int Age;
public virtual void Display()
  { Console.WriteLine({Name}{Age})};
}
public class Labrador: Dog
{
 bool likesFood;
public override void Display()
{Console.WriteLine({Name}{Age}{likesFood})};
}
29
Q

What does protected visibility mean?

A

It means the variable can only be accessed within the class or by an ancestor.

30
Q

What are three commonly overriden methods and what do they do?

A

public override string ToString() - returns a string that represents an object
public override int GetHashCode() - returns a unique number for each object
public override bool Equals() - determines if two objects are equal

31
Q

What is the difference between static and non-static?

A

Static functions do not need an object to be instantiated before they are called, they will instantiate their own.

Non-static - Needs an object to be called on and don’t instantiate their own object.

32
Q
If we had classes:
class Dog(){};
class Dog:Lab(){};
class Dog:BullArab(){}'
class Dog:Poodle(){};

Could we have an array Lab [] myFavDogs = ({Lab, BullArab, Poodle});? Why/Why not?

A

You could not as they are different classes. What you could do instead is create the array from the base class e.g:

Dogs [] myFavDogs = ({Lab, BullArab, Poodle});

33
Q

Why do we use abstract classes?

A

Abstract classes are used to define a base class. They are not used to create objects for a class, they are only used to inherit data fields and behaviours from.

34
Q

Declare an interface for Felines with a method for makeSound();

A
interface IFeline{
 void makeSound();
}
35
Q

What is the difference between run-time polymorphism and compile-time polymorphism?

A

Compile-time = Static binding occurs at compile time e.g method overloading

Run-time = We find out which method is invoked at runtime e.g method overriding