✍️ Practice (Easy)
1. Is Valid
Create a function called IsValid which receives an integer and prints to console whether or not this integer is valid according to the following rule: The integer is valid if it is more than 0 and smaller than 100
👀Hints
Since the function only prints to the console and doesn't give anything back, its return type should be void:
static void IsValid(int num)
{
// check if num is more than 0 AND smaller than 100
}
💡Solution
static void IsValid(int num)
{
if (num > 0 && num < 100)
{
Console.WriteLine($"{num} is valid");
}
else
{
Console.WriteLine($"{num} is not valid");
}
}
2. Argument Order
Consider the following code:
static int WeirdFunction (int i, int j, int k)
{
return (k + i) * 2 + j;
}
What will be the output when running this:
Console.WriteLine(WeirdFunction(2, 2, 2));
Console.WriteLine(WeirdFunction(1, 2, 3));
Console.WriteLine(WeirdFunction(3, 2, 1));
👀Hints
Create a trace table to guide you:
| Call | i | j | k | Result |
|---|---|---|---|---|
WeirdFunction(2, 2, 2) | ||||
WeirdFunction(1, 2, 3) | ||||
WeirdFunction(3, 2, 1) |
💡Solution
| Call | i | j | k | Result |
|---|---|---|---|---|
WeirdFunction(2, 2, 2) | 2 | 2 | 2 | |
WeirdFunction(1, 2, 3) | 1 | 2 | 3 | |
WeirdFunction(3, 2, 1) | 3 | 2 | 1 |
3. Summing squares
Consider the following function:
static int SumSquares(int num1, int num2)
{
int result = num1 * num1 + num2 * num2;
return result;
}
What will be the output of this line of code:
Console.WriteLine(SumSquares(3, 4) + SumSquares(1, 2));
👀Hints
Try substituting the numbers directly in the function!
💡Solution
The first call to SumSquares() passed in the values 3 & 4 so inside it will do the math:
static int SumSquares(3, 4)
{
int result = 3 * 3 + 4 * 4;
return result; // 25
}
The second call to SumSquares() passed in the values 1 & 2 so inside it will do the math:
static int SumSquares(1, 2)
{
int result = 1 * 1 + 2 * 2;
return result; // 5
}
Therefore, this line of code:
Console.WriteLine(SumSquares(3, 4) + SumSquares(1, 2));
Can be rewritten as:
Console.WriteLine(25 + 5);
And finally output:
30
4. Is In Range
Create a function called IsInRange that takes three numbers. The first number will be checked to see if it is between the second number (inclusive) and the third number (exclusive).
👀Hints
The function has the below signature, fill in the missing logic!
static bool IsInRange(double num, double low, double high)
{
// check if num is between low and high
}
💡Solution
static bool IsInRange(double num, double low, double high)
{
if (num >= low && num < high)
{
return true;
}
else
{
return false;
}
}
5. Tracing
Consider the following functions:
static void Function1()
{
Console.WriteLine("Hi");
Function2();
Function3();
}
static void Function2()
{
Console.WriteLine("How are you?");
}
static void Function3()
{
Console.WriteLine("Bye");
}
What will be the output when running this:
Function1();
💡Solution
Hi
How are you?
Bye
What will be the output when running this:
Function1();
Function2();
Function3();
💡Solution
Hi
How are you?
Bye
How are you?
Bye
6. Scope
Consider the following code:
int value = 50;
static void Function1()
{
int value = 25;
Console.WriteLine(value);
}
What will be the output when running this:
Function1();
Console.WriteLine(value);
💡Solution
25
50
Inside Function1(), int value = 25; declares a brand new local variable that only exists inside the function.
static void Function1()
{
int value = 25; // local variable, only lives inside Function1
Console.WriteLine(value); // 25 (uses the local one)
}
Once Function1() finishes, its local value disappears.