Functions/Methods, Variables Scope, Try-Catch Block Flashcards

1
Q

Define a function.

A

A function is a block of code that performs a specific task and can be called independently and doesn’t need to be associated with any object or class.
Functions usually don’t require parameters (void functions) and may return a value using a return statement.

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

What is the difference between a function and a method?

A

A method is associated with an object while a function is not.

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

Define a method.

A

A method is a function that is specifically associated with an obejct, meaning it can access and modify the object’s properties when called.

Essentially, a method is a function within an object in object-oriented programming (OOP).

Methods are typically defined within a class and are called on instances of that class or the class itself (in the case of class methods).

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

A function/method is defined by:

<Access> <Return> <Method>(Parameter List) {
Method Body
}

Describe Access Specifier, Return Type, Method Name, Parameter List, and Method Body
</Method></Return></Access>

A

Access Specifier - Modifier - Specifies the visibility/accessibility and behavior attribute of the method.
// public - Accessible from any other part of the program or other programs.
// private - Accessible only within the same class.
// protected - Accessible within the same class and by derived classes.
// internal - Accessible within the same assembly but not from another assembly.

Return Type - Indicates the type of value the method will return.
// int, string, bool, double, void

Method Name - The name of the method, which is used to call. Should be descriptive and follow the naming conventions.
// Example: CalcArea

Parameter List - Lists the variable passed to the method. Specified with type followed by name. Can be empty
// Example: (int x, int y), (), (stirng name)

Method Body - Contains the executable code, including logic, conditionals, loops, and return statement.

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

A parameter is what?

A

A special kind of variable used in a function to refer to one of the pieces of data provided as input to the function.

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

When a parameter is passed to a function it is called an?

A

Argument

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

A void function requires us to return a value?

A

No

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

Static vs Public

A

static: Use when you don’t need to instantiate an object to call the method (e.g., utility functions).

public: Use when you want the method to be accessible from outside the class or assembly.

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

If a method/function has no access specifier, what is it’s default?

A

Private

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

Instance Variables

A

// Instance Variables

    // Definition: Instance variables are fields that belong to a specific instance of a class (i.e., an object). These variables are associated with an object and hold data that can be unique to that object.
    // Declaration: Instance variables are typically defined within a class, but outside any method.
    // Lifetime: They exist as long as the object they belong to is alive.
    // Access: They are accessed using an instance of the class.

    // class Car
    // {
    //     // Instance variable
    //     public string make;
    //     public string model;

    //     // Constructor to initialize the instance variables
    //     public Car(string make, string model)
    //     {
    //         this.make = make;
    //         this.model = model;
    //     }
    // }

    // class Program
    // {
    //     static void Main()
    //     {
    //         Car myCar = new Car("Toyota", "Corolla"); // Creating an instance of Car
    //         Console.WriteLine(myCar.make);  // Accessing instance variable
    //         Console.WriteLine(myCar.model); // Accessing instance variable
    //     }
    // }

    // Each instance of the Car class can have different values for make and model.
    // You need an object to access the instance variables (myCar.make).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Local Variables

A

// Local Variables

    // Definition: Local variables are variables that are defined within a method, constructor, or block of code. They can only be used within the method or block where they are declared.
    // Declaration: They are declared inside methods, loops, or conditionals.
    // Lifetime: Their lifetime is limited to the scope of the method or block in which they are defined. Once the method or block exits, the local variable is destroyed.
    // Access: They can only be accessed within the method or block where they are defined.

    // class Program
    // {
    //     static void Main()
    //     {
    //         // Local variable
    //         int sum = 0;
    //         for (int i = 0; i < 10; i++)
    //         {
    //             sum += i; // Accessing local variable within the block
    //         }
    //         Console.WriteLine(sum); // sum is accessible here because it's in the same method
    //     }
    // }

    // Local variables cannot be accessed outside their method or block.
    // They are created when the method is called and destroyed when the method exits.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Global Variables

A

// Global Variables

    // Definition: Global variables are variables that are accessible throughout the entire program. In C#, global variables as they are typically understood in other languages don't exist. However, you can simulate global variables using static fields in a class.
    // Declaration: In C#, a global variable can be simulated by declaring a static field inside a class. This variable is accessible anywhere in the program as long as the class is accessible.
    // Lifetime: Static fields exist for the duration of the application.
    // Access: Can be accessed by other methods or classes using the class name.

    // class Program
    // {
    //     // Simulating a global variable with a static field
    //     public static int globalVar = 10;

    //     static void Main()
    //     {
    //         Console.WriteLine(globalVar);  // Accessing global variable
    //         SomeMethod();
    //     }

    //     static void SomeMethod()
    //     {
    //         Console.WriteLine(globalVar);  // Accessing global variable in another method
    //     }
    // }

    // The globalVar is a static variable and can be accessed across methods and even other classes (if it's public or properly exposed).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Field

A

// Fields

    // Definition: Fields are variables that are declared inside a class or struct but outside any methods. They hold data that is typically shared across all instances of the class (if static) or tied to individual instances (if not static).
    // Declaration: Fields are declared directly in the class body, outside methods, and can have any access modifier (e.g., public, private, etc.).
    // Lifetime: Non-static fields exist as long as the object instance exists. Static fields exist for the lifetime of the application.

    // class Car
    // {
    //     // Field
    //     public string color;
    //     private int year;

    //     // Constructor to initialize the fields
    //     public Car(string color, int year)
    //     {
    //         this.color = color;
    //         this.year = year;
    //     }

    //     public void DisplayCarInfo()
    //     {
    //         Console.WriteLine($"Color: {color}, Year: {year}");
    //     }
    // }

    // Fields store data for an object or class.
    // They can be accessed using an instance (for instance fields) or the class name (for static fields).
    // They have a longer lifetime than local variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Out of Scope Variables

A

// Out of Scope Variables

    // Definition: A variable is said to be out of scope when it is no longer accessible because its scope has ended. This happens when:
    // A local variable is defined inside a block (such as a loop or a conditional) and is accessed outside that block.
    // A variable goes out of scope when the method or block that declared it completes execution.

    // void Method()
    // {
    //     int x = 10; // x is in scope inside this method
    // }
    // x is out of scope here because it was defined in the Method scope
    // Console.WriteLine(x);  // Error: x is out of scope

    // Variables only exist within the scope in which they are declared.
    // Once out of scope, you cannot access the variable anymore.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Scope Variable

A

// Scope Variables

    // Definition: Scope refers to the region of code where a variable is accessible. It defines where the variable can be referenced or modified.
        // Block Scope: Variables declared inside a block (e.g., within a loop or conditional) are only accessible within that block.
        // Method Scope: Variables declared inside a method can only be accessed within that method.
        // Class Scope: Instance variables and fields have class-level scope, meaning they are accessible by any method inside the class.
        // Global Scope (for static variables): Static fields have global scope within the class but can be accessed anywhere in the program if public or accessible.

    // class Program
    // {
    //     // Class-level scope
    //     public static int classVariable = 100;

    //     static void Main()
    //     {
    //         // Method-level scope
    //         int localVariable = 10;

    //         if (localVariable > 5)
    //         {
    //             // Block-level scope
    //             int blockVariable = 20;
    //             Console.WriteLine(blockVariable);  // Accessing variable in block scope
    //         }
    //         // blockVariable is out of scope here
    //         // Console.WriteLine(blockVariable);  // Error: blockVariable is out of scope
    //     }
    // }

    // The scope of a variable determines where you can access or modify it.
    // Variables can be scoped at the block level, method level, class level, or even global (static fields).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Summary:

Local variables are defined within a function or block and only accessible there.
Global variables are declared outside any function, accessible throughout the program.
Scope refers to the region where a variable is accessible in the code, typically determined by where it’s declared.
Out of scope variables are those that are not accessible from the current point in the program due to scope restrictions.
Field variables are typically used in object-oriented programming, referring to variables associated with an instance of a class (often called instance variables).
Instance variables are variables that are tied to a specific instance of a class and can store unique data for that object.

17
Q

What is a try-catch block and give an example.

A

// Try Catch block

    // is used to handle exeptions (runtume errors) that may occur during the execution of the program. It allows you to catch exceptions and take appropiate actions, such as logging the error, displaying a message ot the user, or recovering from the error.

    // Example
    try
    {
        int number1 = 10;
        int number2 = 0;
        int result = number1 / number2; // This will throw a DivideByZeroException
        Console.WriteLine(result);
    }
    catch (DivideByZeroException ex)
    {
        // Handle DivideByZeroException
        Console.WriteLine("Error: You can't divide by zero!");
        Console.WriteLine("Exception Message: " + ex.Message);
    }