Chapter 2 Flashcards

1
Q

What is object oriented programming?

A

A programming technique that makes use of objects. Objects are self contained data structures that consist of properties, methods and events.

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

What is an object?

A

Objects are created from templates defined by classes.

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

What are an objects properties?

A

It is the data represented by an object.

Properties are class members that can be accessed like data fields but contain code like a method.

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

What are an objects methods?

A

Methods are an objects behaviour.

They are a block of code containing a series of statements.

Method defines the actions or operations of a class.

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

What is an event?

A

An event provides communication between objects

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

What is encapsulation?

A

The concept of hiding the complexity of a data structure.

In a class it prevents users from directly modifying attributes, and forces the user to use defined functions in order to modify them (called interfaces).

Encapsulation uses access modifiers to define the access level to data using public, private, protected etc.

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

What is a class?

A

It is the template from which individual objects are created, the object is also known as the instance of a class.

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

What is an access modifier?

A

It specifies what region of the code will have access to a field e.g private, public, protected.

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

How do you declare a method?

A

A method is defined by specifying the access level (private, public), the return type (double, int), the name of the method and an optional list of parameters eg public double GetArea ()

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

What does the void keyword mean when used on a method?

A

If a method does not return a value must use the void keyword.

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

How does a method return a value?

A

A method can return a value to the code calling it. If a method does return a value it must use the return keyword in the code, the return keyword will stop the code executing and return to the calling code.

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

What is a constructor?

A

Broadly speaking, a constructor is a method in the class which gets executed when its object is created.

It is a special class method that is executed when a new instance of a class is initialized.

It must have exactly the same name as the class and does not have a return type.

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

What is a default constructor?

A

Constructors that take no arguments are default constructors.

If a class is defined without a constructor a default constructor is created that does nothing.

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

What are the 2 accessors for a property?

A

Property has 2 accessors - get and set.

Get - used to return the property value.
Set - used to assign a new value to the property.

Leaving out the set means can’t set the value of the property (read-only).
Leaving out the get means can’t get the value of the property (write-only).

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

What are auto-implemented properties?

A

Auto-implemented properties simplify property declarations - introduced in C#.

public double Length {get; set}
public double Width {get; set}

Using this means that backing fields for properties are defined behind the scenes and are not accessible by code.

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

What is the this keyword?

A

This keyword is used to access members from within constructors, instance methods and accessors.

private double length;
private double width;
public Rectangle (double l, double w)
{
	this.length = l;
	this.width = w;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is constructor overloading?

A

A class can have multiple constructors that take multiple arguments.

It is often useful to have more than one way to initialise an object, by using multiple constructors.

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

What is a method’s signature?

A

A method’s name, parameter list and order of the data types of the parameters.

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

What is a delegate?

A

Delegates are a special type used to encapsulate a method with a specific signature.

Delegates are defined using the delegate keyword.

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

What are events?

A

Events are a way for a class to notify other classes or objects when something of interest happens.

The class that sends information is publisher, class that receives is subscriber e.g. click event in GUI. Events aren’t limited to GUI.

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

What do events normally need?

A

Delegate that connects event with its handler method.

Class that contains event data - derived from EventArgs class.

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

What is a namespace?

A

Namespaces allow you to organise code and create unique class names.

Defined using the namespace keyword.

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

What are static members?

A

Static members belong to a class itself rather than individual object type.

Static member referenced using class name e.g. Rectangle.ShapeName.

Can’t use this keyword on static member only use this on instance objects.

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

What is a value type?

A

Value types directly store data within an allocated block of memory, dependent on the type e.g. declaring an int causes compiler to allocate 4 bytes of memory.

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

What is a reference type?

A

Reference types store a reference to a memory location e.g. when declare circle variable does not allocate memory to hold circle, allots a small area of memory that holds the address (reference) to another block of memory that holds the circle object.

Memory for circle object is only allocated when new keyword is used to create object.

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

What are structs?

A

Structs create user defined types that consist of small groups of related fields. Used to define simple types, if complex use a class.

Defined using keyword struct.

Structs are similar to classes in that they can have methods, properties etc.

They are value types as opposed to classes which are reference types.

Struct cannot inherit from another class or struct unlike a class.

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

What is the heap?

A

The heap is the memory available to the program at runtime for dynamic memory allocation.

Memory allocated in heap is reclaimed by the garbage collector when object is no longer in use.

28
Q

What is the stack?

A

Some data items are created on the stack or call stack e.g. parameters, local variables.

These are reclaimed when the stack unwinds.

29
Q

What does the public access modifier mean?

A

This is the least restrictive access modifier and means that access is not restricted.

30
Q

What does the private access modifier mean?

A

This is the most restrictive access modifier and access is restricted to the containing class.

31
Q

What does the protected access modifier mean?

A

Access is restricted to the containing class and to any class that is derived from the containing class

32
Q

What does the internal access modifier mean?

A

Access is restricted to the code in the same assembly.

33
Q

What does the protected internal access modifier mean?

A

Access is restricted to any code in same assembly and only derived classes in another assembly.

34
Q

What is inheritance?

A

Inheritance allows us to develop a class once and then reuse that code over and over as the basis of new classes.

A class that inherits is called the derived class and the class for creating is the base class.

Structs do not support inheritance.

35
Q

How do you define a derived class?

A

To define a derived class, put a colon after the derived class name followed by the base class name e.g. Rectangle: Polygon.

36
Q

What is an abstract class?

A

Abstract classes provide a common definition of a base class that can be shared by multiple derived classes, there is no code in an abstract base class but will have code in derived class.

37
Q

What is a sealed class?

A

Sealed classes provide complete functionality but cannot be used as a base class.

Sealed classes are defined when implementation is complete and do not want it to be inherited, using sealed keyword.

Can also mark selected member within a class as sealed, to stop them being overridden.

38
Q

What is casting?

A

C# allows you to cast an object to any of its base types. This is an operation that checks whether it is safe to convert one type to another before it does the conversion. You prefix the object variable with the name of the type in parentheses.

Object o = new Rectangle (10,20); // rectangle object assigned to Object class then cast back as rectangle object

39
Q

What happens if object being cast is not compatible?

A

The runtime will check if if type o is compatible with rectangle class, if not it will throw a System.InvalidCastException. However this can be avoided using is and as operators.

40
Q

What is the is operator?

A

Is operator - checks whether cast should be allowed before performing cast, only performs cast if o contains a Rectangle object (see below), used in if statement to return true or false.

if (o is rectangle)
{
Rectangle r = (Rectangle) o;
}

41
Q

What is the as operator?

A

As operator - similar to the is operator but returns null instead of raising an error.

Rectangle r = o as Rectangle;
if(r != null)
{
	// do something
}

If cast not possible r is assigned a value of null and no exceptions will be raised.

42
Q

What is polymorphism?

A

Polymorphism is the ability of derived classes to share common functionality with base classes but still define their own unique behaviour.

43
Q

What is a virtual member?

A

Base class has a virtual member, by using the virtual keyword means it can be overridden in derived class. Can’t use override in derived class unless use virtual keyword in base class.

44
Q

What does the override keyword do?

A

Override keyword replaces a base class member in a derived class.

Override takes priority instead of the base class definition of the member and will always take over from base class member.

45
Q

What does the new keyword do?

A

New keyword creates a new member of the same name in the derived class and hides the base class implementation.

The new keyword creates a new definition of the member and base class member is hidden. If derived class is cast to an instance of the base class, hidden members of the class can still be called.

46
Q

What is an interface?

A

Interfaces are used to establish contracts through which objects can interact with each other without knowing the implementation details.

Interfaces are a set of signatures for methods, properties, delegates, events and indexes. Does not contain any code or data, specifies the methods and properties that a class that inherits must provide.

Common example is IComparable namespace, this has a single method that accepts an object and returns an int.

47
Q

Where is a class stored in memory?

A

A class is allocated in the heap, as it is a reference type.

48
Q

Where is a structure allocated in memory?

A

A struct is allocated to the stack as it is a value type.

49
Q

You want to restrict the access for a method to the containing class or to a class that is derived from the containing class. Which access modifier should you use for this method?

a. public
b. private
c. protected
d. internal

A

c. protected

50
Q

In a class, you defined a method called Render. This method provides functionality to render bitmap files on the screen. You would like the derived classes to supersede this functionality to support the rendering of additional image formats. You also want the Render method of the derived classes to be executed even if a derived class is cast as the base class. Which keyword should you use with the definition of the Render method in the base class?

a. abstract
b. virtual
c. new
d. overrides

A

b. virtual

51
Q

You defined a class AdvMath that defines advanced mathematical functionality. You do not want the functionality of this class to be inherited into derived classes. What keyword should you use to define the AdvMath class?

a. sealed
b. abstract
c. private
d. internal

A

a. sealed

52
Q

You need to provide query functionality to several of your classes. Each class’s algorithm for the query will likely be different. Also, not all the classes have an “is-a” relationship with each other. How should you support this functionality?

a. Add the query functionality to a base class with public access modifier.
b. Have all the classes inherit from an abstract base class and override the base-class method to provide their own query functionality.
c. Have all the classes inherit from a base class that provides the query functionality.
d. Create a common interface that is implemented by all the classes.

A

d. Create a common interface that is implemented by all the classes.

53
Q

Which of the following class elements should you use to define the behavior of a class?

a. Method
b. Property
c. Event
d. Delegate

A

a. Method

54
Q
You are writing code for a class named Product. You need to make sure that the data members of the class are initialized to their correct values as soon as you create an object of the Product class. The initialization code should be always executed. What should you do?
a.	Create a static method in the Product class to initialize data members.

b. Create a constructor in the Product class to initialize data members.
c. Create a static property in the Product class to initialize data members.
d. Create an event in the Product class to initialize data members.

A

b. Create a constructor in the Product class to initialize data members.

55
Q
You are creating a new class named Square that is derived from the Polygon class. The Polygon class has the following code:
class Polygon
{
    public virtual void Draw()
    {
        // additional code...
    }
}
	The Draw method in the Square class should provide new functionality but also hide the Polygon class implementation of the Draw method. Which code segment should you use to accomplish this?
a.	
class Square : Polygon
{
    public override void Draw()
    {
        // additional code ...
    }
}
b.	
class Square : Polygon
{
    public new void Draw()
    {
        // additional code ...
    }
}
c.
class Square : Polygon
{
    public virtual void Draw()
    {
        // additional code ...
    }
}
d.
class Square : Polygon
{
    public static void Draw()
    {
        // additional code ...
    }
}
A
b.	
class Square : Polygon
{
    public new void Draw()
    {
        // additional code ...
    }
}
56
Q
You are writing code for a new method named Process:
void Process(object o)
{

}
The code receives a parameter of type object. You need to cast this object into the type Rectangle. At times, the value of o that is passed to the method might not be a valid Rectangle value. You need to make sure that the code does not generate any System.InvalidCastException errors while doing the conversions. Which of the following lines of code should you use inside the Process method to accomplish this goal?
a. Rectangle r = (Rectangle) o;
b. Rectangle r = o as Rectangle;
c. Rectangle r = o is Rectangle;
d. Rectangle r = (o != null) ? o as rectangle : (Rectangle) o;

A

b. Rectangle r = o as Rectangle;

57
Q

You are writing code to handle events in your program. You define a delegate named RectangleHandler like this:
public delegate void RectangleHandler(Rectangle rect);
You also create a variable of the RectangleHandler type as follows:
RectangleHandler handler;
Later in the program, you need to add a method named DisplayArea to the method invocation list of the handler variable. The signature of the DisplayArea method matches the signature of the RectangleHandler method. Any code that you write should not affect any existing event-handling code. Given this restriction, which of the following code should you write?
a. handler = new RectangleHandler(DisplayArea);
b. handler = DisplayArea;
c. handler += DisplayArea;
d. handler -= DisplayArea;

A

c. handler += DisplayArea;

58
Q

A(n) ______ is a blueprint of an object.

A

class

59
Q

A class that does not provide a complete implementation must be declared with the keyword ______.

A

abstract

60
Q

Classes that want to support comparison must implement the IComparable interface and then provide a body for the ______ method.

A

CompareTo

61
Q

You can use the ______ operator to check whether it is legal to cast one type to another type.

A

is

62
Q

Three main features of an object-oriented programming language are ______, ______, and ______.

A

encapsulation, inheritance, and polymorphism

63
Q

You can use _______ to group related classes in order to reduce name collisions.

A

namespaces

64
Q

The ______ keyword refers to the current instance of a class.

A

this

65
Q

A(n) ______ is a type that references a method.

A

delegate

66
Q

A(n) ______ is a value type, whereas a(n) _______ is a reference type.

A

struct, class

67
Q

You can use the _______ keyword to declare a member that belongs to the class itself rather than to a specific object.

A

static