AppDevelop Week 8 Material

studied byStudied by 0 people
0.0(0)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions

1 / 60

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

61 Terms

1

T or F: A class does not always require a constructor, especially if it is static.

True

New cards
2

Constructor —> Describe its function in one short sentence.

Constructors are used to initialize an instance of a class.

New cards
3

(a) What does the term “state” refer to in the context of C# programming?

(b) When does a class have a state?

(a) What does the term “state” refer to in the context of C# programming?

The data (variables & properties) stored in an object, that define its characteristics at a given time.

(b) When does a class have a state?

A class has a state if it contains instance variables (which holds information about the object)

New cards
4
class Car {
public string Model;
public int Speed;

// Constructor initializes state
public Car(string model, int speed) {
this.Model = model;
this.Speed = speed;
}
}

class Program {
static void Main() {
Car myCar = new Car("Toyota", 100);
Console.WriteLine($"Car Model: {myCar.Model}, Speed: {myCar.Speed} km/h");
}
}
  1. Identify the following in the code above:

    (a) The two instance variables

    (b) The constructor

  2. Does this class have a state? Justify your answer.

(1) Identify the following in the code above:

(a) The two instance variables are ‘model’ (string) & ‘speed’ (int)

  • These can also be referred to as the ‘state’ of the object Car

(b) The constructor is…

public Car(string model, int speed) {  
this.Model = model;
this.Speed = speed;

(2) Does this class have a state? Justify your answer.

  • Yes this class has a state.

    The instance variables ‘model’ & ‘speed’ represent the state of a Car object

New cards
5

class Car {
public string Model; // Instance variable (state)
public int Speed; // Instance variable (state)

// Constructor initializes state
public Car(string model, int speed) {
this.Model = model;
this.Speed = speed;
}
}

class Program {
static void Main() {
Car myCar = new Car("Toyota", 100);
Console.WriteLine($"Car Model: {myCar.Model}, Speed: {myCar.Speed} km/h");
}
}

New cards
6
class Utility {
public static void PrintMessage() {
Console.WriteLine("Hello!");
}
}

class Program {
static void Main() {
Utility.PrintMessage(); // No object creation needed
}
}

Observe the class above and determine the following…

  1. Does the class have a state? Justify your answer.

  2. Do either of the classes use a constructor? Why or why not?

(1) Does the class have a state? Justify your answer.

  • No

  • Neither of these class does not store any instance variables (states)

  • It only has a method

(2) Do either of the classes use a constructor? Why or why not?

  • No

  • Since neither of the classes in the code have a state, it does not require a constructor

New cards
7

Is the following statement True or False? Justify your answer.

“If a class only performs actions (methods) without storing any data (state), it does not need a constructor.”

True. The class may not need a constructor because there’s nothing to initialize when creating an object.

New cards
8

Constructor in C#

(a) Define.

(b) Key Characteristics.

(a) Define

  • Special method that is automatically called when an object of a class is created

  • Used to initialize objects, set default values, or execute startup logic

(b) Key Characteristics

  • Same Name as its Class — A constructor must have the same name as the class it belongs to

  • No Return Type — Unlike regular methods, constructors do not have a return type (not even void)

  • Called Automatically — A constructor is invoked (called on) automatically when an object of the class is instantiated

New cards
9

What happens if you don’t explicitly define a constructor for a class?

C# provides a default constructor

New cards
10

T or F: Regular methods in C# must have a return type.

True

New cards
11

T or F: In C#, a constructor must have the same name as the class it belongs to.

True

New cards
12

When is a Class constructor invoked/called on automatically?

When an object of the class is instantiated

New cards
13

Are default constructors parameterized or non-parameterized?

Non-parameterized

New cards
14

What are the major differences b/w parameterized & non-parameterized constructors?

Parameterized Constructor

  • Can accept parameters to initialize an object with specific values

  • Typically used to initialize object fields w/ default or predefine values

  • Automatically provide by C# if no constructor is explicitly defined by the programmer

Example Code

class Car
{
public string Brand;

// Parameterized Constructor
public Car(string brandName)
{
Brand = brandName;
}
}

class Program
{
static void Main()
{
Car myCar = new Car("Toyota");
Console.WriteLine(myCar.Brand); // Output: Toyota
}
}

Non-Parameterized Constructor

  • Accepts one or more parameters

  • Allows object properties to be set dynamically at time of object creation

  • Useful for when different objects need different initial values

New cards
15

T or F: Unlike methods, constructors do not have a return type, except for void which can be used as a return type for constructors.

False.

Constructors do not have a return type INCLUDING void.

New cards
16

Read the question commented in the code below.

class WrongExample
{
public void WrongExample() // This is intended to be a constructor, but it isn't. Why? And what is it?
{
}
}

This is not a constructor; it's a method!

This is because

New cards
17

T or F: Constructs almost always have a “Public Access Modifier”

True

New cards
18

T or F: Constructors can be public of private.

True

New cards
19

What syntax is used to create a…

(a) Parameterized Constructor

(b) Non-Parameterized Constructor

(a) Parameterized Constructor Syntax

  • A constructor that takes one or more parameters.

  • Allows setting initial values when an object is created.

class ClassName
{
// Parameterized constructor (takes arguments)
public ClassName(string param1, int param2)
{
// Use parameters to initialize properties
}
}

(b) Non-Parameterized Constructor

  • A constructor with no parameters.

  • Typically initializes object properties with default values.

class ClassName
{
// Non-parameterized constructor (no arguments)
public ClassName()
{
// Initialization code
}
}

New cards
20

Constructor overloading is used to…

Define multiple constructors (with the same name) with different parameters in the same class

New cards
21

The following sample code demonstrates constructor overloading.

class Car
{
public string Brand;

// Parameterized Constructor
public Car(string brandName)
{
Brand = brandName;
}
}

class Program
{
static void Main()
{
Car myCar = new Car("Toyota");
Console.WriteLine(myCar.Brand); // Output: Toyota
}
}

(a) Explain why this code is a demonstration of constructor overloading.

(b) Describe the differences in properties/characteristics of each constructor.

class Car
{
public string Brand;
public int Year;

// Default Constructor
public Car()
{
Brand = "Unknown";
Year = 0;
}

// Parameterized Constructor
public Car(string brandName, int year)
{
Brand = brandName;
Year = year;
}
}

class Program
{
static void Main()
{
Car car1 = new Car();
Console.WriteLine($"{car1.Brand}, {car1.Year}"); // Output: Unknown, 0

Car car2 = new Car("Ford", 2022);
Console.WriteLine($"{car2.Brand}, {car2.Year}"); // Output: Ford, 2022
}
}

This sample code demonstrates constructor overloading because…

  • the Car class has multiple constructors that all have the same name (‘Car’), but diff parameters (one class has 1 parameter, the other has 2)

  • the correct constructor is called based on how the object is instiated

  1. Default Constructor (i.e. Car() )

    • No parameters

    • Initializes Brand to “Unknown” and Year to 0

    • Used when object is created w/o passing any arguments

Car car1 = new Car();  // Calls the default constructor
  1. Parameterized Constructor (i.e. Car(string brandName, int year)

    • Takes 2 parameters:

      • String ‘brandName’

      • Integer ‘year’

    • Assigns values to both the Brand and Year fields

    • Used when specific values are provided at the time of object creation

New cards
22

Which type of constructor should you use when creating multiple objects of the same type, but you want them to have different initial values?

Parameterized Constructor

New cards
23

Does the sample code above demonstrate constructor overloading?

If so, provide a brief description of how.

class Car
{
public string Brand;
public int Year;

public Car()
{
Brand = "Unknown";
Year = 0;
}

public Car(string brand, int year)
{
Brand = brand;
Year = year;
}
}

class Program
{
static void Main()
{
Car car1 = new Car();
Car car2 = new Car("Toyota", 2023);

Console.WriteLine($"{car1.Brand}, {car1.Year}");
Console.WriteLine($"{car2.Brand}, {car2.Year}");
}
}

Yes it does demonstrate constructor overloading. See comments in the code below.

class Car
{
public string Brand;
public int Year;

// Default Constructor
public Car()
{
Brand = "Unknown";
Year = 0;
}

// Parameterized Constructor
public Car(string brand, int year)
{
Brand = brand;
Year = year;
}
}

class Program
{
static void Main()
{
Car car1 = new Car(); // Calls the default constructor
Car car2 = new Car("Toyota", 2023); // Calls the parameterized constructor

Console.WriteLine($"{car1.Brand}, {car1.Year}"); // Output: Unknown, 0
Console.WriteLine($"{car2.Brand}, {car2.Year}"); // Output: Toyota, 2023
}
}

New cards
24

Does the sample code above demonstrate constructor overloading?

If so, provide a brief description of how.

class Person
{
public string Name;
public int Age;

public Person()
{
Name = "Not Provided";
Age = 0;
}

public Person(string name, int age)
{
Name = name;
Age = age;
}
}

class Program
{
static void Main()
{
Person p1 = new Person();
Person p2 = new Person("Alice", 25);

Console.WriteLine($"{p1.Name}, {p1.Age}");
Console.WriteLine($"{p2.Name}, {p2.Age}");
}
}

Yes it does demonstrate constructor overloading. See comments in the code below.

class Person
{
public string Name;
public int Age;

// Default Constructor
public Person()
{
Name = "Not Provided";
Age = 0;
}

// Parameterized Constructor
public Person(string name, int age)
{
Name = name;
Age = age;
}
}

class Program
{
static void Main()
{
Person p1 = new Person(); // Calls the default constructor
Person p2 = new Person("Alice", 25); // Calls the parameterized constructor

Console.WriteLine($"{p1.Name}, {p1.Age}"); // Output: Not Provided, 0
Console.WriteLine($"{p2.Name}, {p2.Age}"); // Output: Alice, 25
}
}

New cards
25

Does the sample code above demonstrate constructor overloading?

If so, provide a brief description of how.

class Animal
{
public string Type;

public Animal()
{
Type = "Unknown";
}
}

class Program
{
static void Main()
{
Animal a1 = new Animal();
Console.WriteLine(a1.Type); // Output: Unknown
}
}

No this code does not demonstrate constructor overloading.

class Animal
{
public string Type;

// Only one constructor (no overloading)
public Animal()
{
Type = "Unknown";
}
}

class Program
{
static void Main()
{
Animal a1 = new Animal();
Console.WriteLine(a1.Type); // Output: Unknown
}
}

New cards
26

Does the sample code above demonstrate constructor overloading?

If so, provide a brief description of how.

class Book
{
public string Title;

// Only one constructor (no overloading)
public Book(string title)
{
Title = title;
}
}

class Program
{
static void Main()
{
Book b1 = new Book("C# Programming");
Console.WriteLine(b1.Title); // Output: C# Programming
}
}

No this code does not demonstrate constructor overloading.

class Book
{
public string Title;

// Only one constructor (no overloading)
public Book(string title)
{
Title = title;
}
}

class Program
{
static void Main()
{
Book b1 = new Book("C# Programming");
Console.WriteLine(b1.Title); // Output: C# Programming
}
}

New cards
27

Demonstrate the syntax used to create a non-parameterized constructor

New cards
28

(a) Define “Access Modifier”

(b) What is the difference b/w a public & a private access modifier”

(c) Name two other commonly used examples of access modifiers (besides public & private)

(a) Define “Access Modifier”

  • C# keyword

  • Defines visibility & accessibility of a class, method or variable

  • Controls parts of a program that can access or modify a particular piece of code

(b) What is the difference b/w a public & a private access modifier

Public access modifier —> allows a variable, method, or class can be accessed from anywhere in the program

Private access modifier —> restricts access/use of the modifier (i.e. element) within the class it belongs to

(c) Other commonly used access modifiers

  • public

  • private

  • protected

  • internal

  • protected internal

New cards
29

What is the difference b/w Public & Private Access Modifiers?

Feature

public

private

Accessibility

Anywhere in the program

Only within the same class

Used For

Members that should be accessible everywhere

Encapsulated data or internal logic

Example Usage

Class methods, properties, and constructors

Helper methods, internal logic, sensitive data

New cards
30

T or F: Encapsulation & Data Hiding are the same thing.

False.

New cards
31

Encapsulation

(a) Define

(b) Purpose

(c) Explain how its achieved through code.

(a) Define

Process of bundling data (variables) & methods (functions) that operate on the data into a single unit (a class)

(b) Purpose

To keep an object’s internal data safe from unintended changes by limiting direct access.

(c) Explain how its achieved through code.

Implemented using access modifiers like private, public, protected

New cards
32

Data Hiding


(a) Define

(b) Purpose

(c) How is it achieved in coding?

(a) Define

Restricts direct access to an object’s internal details to prevent misuse.

(b) Purpose

Protects sensitive information from being accessed and modified without authorization.

(c) How is it achieved in coding?

Achieved by making variables private or protected.

New cards
33

Example Demonstrating public vs. private

class Person
{
public string Name; // Public: Accessible from anywhere
private int age; // Private: Only accessible within this class

// Public method (can be called from outside the class)
public void SetAge(int newAge)
{
age = newAge; // Allowed (accessing private field within the same class)
}

// Public method to display age
public void DisplayAge()
{
Console.WriteLine($"Age: {age}"); // Allowed
}
}

class Program
{
static void Main()
{
Person p = new Person();
p.Name = "Alice"; // ✅ Allowed (public member)

// p.age = 25; // ❌ Not allowed (private member, cannot be accessed directly)

p.SetAge(25); // ✅ Allowed (indirectly modifying private field through a public method)
p.DisplayAge(); // Output: Age: 25
}
}

New cards
34

Name the benefits of encapsulation & data hiding.

  • Making elements private prevents direct access and modification, maintaining data integrity.

  • Public methods allow controlled access to private data members.

  • Data hiding is not foolproof but helps prevent accidental modifications.

New cards
35

this keyword

  • Refers to the current object instance

  • Distinguishes instance variables from local variables or parameters with the same name

New cards
36

What is the difference b/w static & instance classes?

Static Classes —> Cannot have instance members and does not need a constructor.

Instance Classes —> require constructors and instance methods.

New cards
37

Garbage Collector (GC)

(a) What is it?

(b) How does it work?

(a) What is it?

  • Automatic memory management feature that removes unused objects from memory to free up space & improve program/computer performance

    • Automatic —> meaning it runs automatically in the background all the time

(b) How does it work?

  • The GC scans & identifies objects in memory that are no longer in use — meaning these objects have no references pointing to them (i.e. no pointers)

  • The GC runs automatically & deletes unused objects, which frees up memory

  • GC automatically reclaims memory occupied by unreferenced objects

  • Objects are removed when a program ends, freeing memory.

  • Manual memory management can be used instead of relying on the GC.

New cards
38

T or F: It is rarely necessary to use the GC and its recommended to not use unless completely necessary.

True

New cards
39

Which method & what syntax is used to explicitly force GC to occur?

Method used for explicit GC —> GC.Collect( ):

Example:

GC.Collect();  // Forces garbage collection
GC.WaitForPendingFinalizers(); // Waits until cleanup is done

New cards
40

Name one simple/basic example where GC may be used.

In freeing up space when a device/program is in low-memory conditions.

New cards
41

For what reasons would you make a string private?

To encapsulate/data hide, which protects data from being accessed by unauthorized personnel.

New cards
42

If you have a class state with 5 variables and a 3-parameter constructor, then how many of the variables must be set/declared within the class itself?

2 of the variables must be declared/set within the class itself since they are not declared w/in the class’ constructor.

New cards
43

Name the 2 ways in which computer memory space can be freed up by altering a progrgam?

  1. Using the automatic built-in Garbage Collector

  2. Manually triggering Garbage Collection

New cards
44

Every single class inherits from the ______ class.

Every single class inherits from the object class.

***EXAM QUESTION

New cards
45

T or F: Abstraction and Abstract Classes are the same thing.

False.

Abstraction and Abstract Classes are NOT the same thing.

They are completely different, although both are parts of OOP.

***FINAL EXAM QUESTION

New cards
46

Name the 4 Pillars of OOP

  1. Inheritance

  2. Interfaces

  3. Abstract Classes

  4. Polymorphism

New cards
47

Inheritance

(a) Define

(b) What rule must all child classes of a parent class follow

(a) Define

  • A child class inherits all the variables & methods that are in its parent classes

(b) What rule must all child classes of a parent class follow (hint: think methods)

  • A child class must specialize/differ from its parent class in some way (i.e. have additional variables or methods)

New cards
48

Define & demonstrate each of the following with a sketch.

(a) Multiple Inheritance

(b) Multi-Level Inheritance

(c) Hierarchical Inheritance

(a) Multiple Inheritance

  • A class that belongs to more than one parent class

(b) Multi-Level Inheritance

  • A class that is derived from another derived class

    • Class A is derived from Class B

    • Class B is derived from Class C

    • Therefore, Class A is a “grandchild” of Class C

(c) Hierarchical Inheritance

  • Multiple subclasses inherit from the same base class

New cards
49

Which of the following is and is NOT permitted in the following languages: C++, C#, Java.

(a) Multiple Inheritance

(b) Multi-Level Inheritance

(c) Hierarchical Inheritance

(a) Multiple Inheritance

Yes

  • C++

No

  • C#

  • Java

(b) Multi-Level Inheritance

Allowed in all (C++, C# & Java)

(c) Hierarchical Inheritance

Allowed in all (C++, C# & Java)

New cards
50

In Inheritance/UML diagrams, the direction an arrow points represents…

Where a class is derived from.

New cards
51

What is Polymorphism?

A method can accept both a parent class and its derived classes.

New cards
52

What is the opposite of polymorphism?

The opposite of polymorphism is restricting methods to accept only the parent class using sealed.

New cards
53

Compare & Contrast Interfaces & Abstract Classes

Feature

Abstract Class

Interface

Contains instance variables

Yes

No

Abstract methods

Yes

Yes

Concrete methods

Yes

No

Multiple inheritance

No

Yes

New cards
54

What is Visual Paradigm?

A UML modelling tool used for creating diagrams to represent class relationships.

New cards
55
class Car
{
private string brand;

public void SetBrand(string carBrand)
{
brand = carBrand;
}

public string GetBrand()
{
return brand;
}
}

class Program
{
static void Main()
{
Car myCar = new Car();
myCar.SetBrand("Toyota");
Console.WriteLine(myCar.GetBrand());
}
}

How is encapsulation used in the code sample above? Be specific.

See the comments in the sample code below for answer:

class Car
{
private string brand; // Private variable

// Public method to set data
public void SetBrand(string carBrand)
{
brand = carBrand;
}

// Public method to get data
public string GetBrand()
{
return brand;
}
}

class Program
{
static void Main()
{
Car myCar = new Car();
myCar.SetBrand("Toyota"); // Allowed (using method)
Console.WriteLine(myCar.GetBrand()); // Output: Toyota
}
}

New cards
56
class BankAccount
{
private double balance;

public BankAccount(double initialBalance)
{
balance = initialBalance;
}

public void Deposit(double amount)
{
if (amount > 0)
balance += amount;
}

public double GetBalance()
{
return balance;
}
}

class Program
{
static void Main()
{
BankAccount account = new BankAccount(1000);
account.Deposit(500);
Console.WriteLine($"Balance: {account.GetBalance()}");
}
}

How is data hiding used in the code sample above? Be specific.

See the comments in the sample code below for answer.

class BankAccount
{
private double balance; // Private variable

public BankAccount(double initialBalance)
{
balance = initialBalance;
}

// Method to deposit money
public void Deposit(double amount)
{
if (amount > 0)
balance += amount;
}

// Method to get the balance
public double GetBalance()
{
return balance; // Can only view balance, not modify directly
}
}

class Program
{
static void Main()
{
BankAccount account = new BankAccount(1000);
account.Deposit(500);
Console.WriteLine($"Balance: {account.GetBalance()}"); // Output: Balance: 1500
}
}

New cards
57
New cards
58

(a) What is an Interface?

(b) What is the major rule when using an interface?

©

(a) What is an Interface?

  • A blueprint that defines a collection of method signatures that a class can implement

  • Specifies what methods a class will have and how they should work

(b) What is the major rule when using an interface?

When using an interface — you must provide an implementation for each method defined in the interface within the implementing class.

New cards
59
using System;

interface IVehicle
{
void StartEngine(); // Method signature
void StopEngine(); // Method signature
}

class Car : IVehicle
{
public void StartEngine()
{
Console.WriteLine("Car engine started.");
}

public void StopEngine()
{
Console.WriteLine("Car engine stopped.");
}
}

class Motorcycle : IVehicle
{
public void StartEngine()
{
Console.WriteLine("Motorcycle engine started.");
}

public void StopEngine()
{
Console.WriteLine("Motorcycle engine stopped.");
}
}

class Program
{
static void Main()
{
IVehicle myCar = new Car();
myCar.StartEngine(); // Output: Car engine started.
myCar.StopEngine(); // Output: Car engine stopped.

IVehicle myMotorcycle = new Motorcycle();
myMotorcycle.StartEngine(); // Output: Motorcycle engine started.
myMotorcycle.StopEngine(); // Output: Motorcycle engine stopped.
}
}

(a) Identify the interface and method signatures in the code above.

(b) What output will be produced from this code?

(c) Describe how the classes are implemented.

(a) Identify the interface and method signatures in the code above.

IVehivle is the interface. It defines the method signatfures StartEngine and StopEngine

(b) What output will be produced from this code?

class Program
{
static void Main()
{
IVehicle myCar = new Car();
myCar.StartEngine(); // Output: Car engine started.
myCar.StopEngine(); // Output: Car engine stopped.

IVehicle myMotorcycle = new Motorcycle();
myMotorcycle.StartEngine(); // Output: Motorcycle engine started.
myMotorcycle.StopEngine(); // Output: Motorcycle engine stopped.
}
}

(c) Describe how the classes are implemented.

  • The Car and Motorcycle classes implement this interface by providing their own definitions of these methods.

  • The Program class demonstrates creating instances of Car and Motorcycle and calling methods defined in the interface.

New cards
60

When is it best to use properties rather than private fields? How about the vice versa?

  • Use properties (public string Make { get; }) when you want controlled access (like read-only properties).

  • Use private fields (private string make;) if you just need to store data internally.

New cards
61

Properties and private fields are two different ways to…

Properties and private fields are two different ways to… declare instance variables that will be used in a constructor.

New cards
robot