Warm-up Exercises
- Write an explicit conversion from a
doublevariablemyDoubleVarto anintvariable calledmyIntVar. You don’t need to re-declare those variables. AssumingmyDoubleVar’s value is5.89, what value would be stored inmyIntVar?
Solution
myIntVar = (int)myDoubleVar;
The value stored in myDoubleVar would be 5.
-
Declare and initialize two integer variables,
xandy. Choose any values you want. Then write necessary commands to display the result of the following 3 mathematical operations: addition, multiplication, and remainder (modulo) ofxandy.An example of output, with
xset to 5 andyset to 3:5 + 3 = 8 5 * 3 = 15 5 % 3 = 2An example of output, with
xset to 4 andyset to 2:4 + 2 = 6 4 * 2 = 8 4 % 2 = 0Solution
int x = 6; int y = 8; Console.WriteLine($"{x} + {y} = {x + y}"); Console.WriteLine($"{x} * {y} = {x * y}"); Console.WriteLine($"{x} % {y} = {x % y}"); -
Write a command that performs implicit conversion between two numeric datatypes. Then, write a command that performs explicit conversion between two numeric datatypes. Add a comment above both commands to clearly indicate which command is implicit and which one is explicit.
Solution
// Implicit Conversion: from int to double int num = 23; double bigNum = num; // Explicit Conversion: from double to int double myDouble = 54.873 int myInt = (int)myDouble;
Questions
-
What distinguishes the
decimaldatatype from other numeric types? What happens when adecimalvalue is used with theCformat specifier?Solution
The
decimaltype is the most precise of the numeric types, and it can be used with theCformat specifier to display values representing amounts of currency. -
What can you learn from this UML class diagram?
Solution
This diagram shows a class called "Contact", which contains a private variable of type `string` called "name" and two public methods. The method called "SetName" has a `void` return type and has a parameter of type `string` called "accountName". The method called "GetName" returns a `string` value and has no parameters. -
Can conversion between two number types change value of a number? Provide an example case.
Solution
Yes. For example, when
floats,doubles, anddecimals are converted toints, the value must be truncated (shortened). For example, the value4.876f, when converted to aint, will become 4. -
Assume you have an
intvariable namedmyAgewhose value is24. What would be displayed on the screen by the following?Console.WriteLine($"{myAge * 2}");Solution
48 -
Give the values of a and b after the following four instructions have been executed.
int a, b; a = 2; b = a * 2 + 1; a -= 1;Solution
The value of
ais1and the value ofbis5. -
Give the values of c and d after the following four instructions have been executed.
int c = 3, d; d = 2 + c; c = d * 2; d += 2;Solution
The value of
cis10, and the value ofdis ‘7’. -
Is there an error in the following code? Explain the error or give the value of b after the second statement is executed.
float a = 3.7f; int b = (int)a;Solution
The value of
bis3. -
Is there an error in the following code? Explain the error or give the value of b after the second statement is executed.
decimal a = 1.6M; int b = (int)a + a;Solution
The code produces an error because the second statement performs a math operation resulting in a decimal value and attempts to assign said value to an integer variable.
-
If one of the operator’s operand is of type
floatand the other is of typeint, what will be the type of the result of the operation?Solution
The result of the operation will be of type
float. -
What is the return type of the operation 12.4 * 3?
Solution
The return type of the operation is
float. -
Give the values of
aandbafter the following four instructions have been executed.int a, b; a = 4; b = a * 3 + 1; a /= 2;Solution
The variable
ahas value 2, and the variablebhas value 13. -
There is an error in the following code, at the second line. Explain the error and how you could fix this using a cast operator, without changing the datatype of the
bvariable.decimal a = 2.5M; int b = a / 2;Solution
Since the operation
a / 2results in adecimalvalue, attempting to store it in anintvariable results in an error. This can be fixed by casting the value ofa, like so:int b = (int)a / 2; -
What is the return type of the operation 12.4 * 3?
Solution
This will evaluate to a
doublevalue.
Problems
-
Write down, on a piece of paper, a fully compilable program that initializes an int variable named
personswith the value 5, anintvariable namedbottleswith the value 3, and adoublevariable namedlitersPerBottlewith the value 1.5. Initialize a variable namedlitersPerPersonwith the value resulting from a mathematical expression calculating the number of liters that each person would receive if split equitably, and make sure that this variable is of the type which can properly store the resulting value. Write a statement that displays its value.Place a delimited comment with your name and the date of writing at the top of the program.
Solution
/* * Student Name * 2025/03/15 */ int persons = 5; int bottles = 3; double litersPerBottle = 1.5; double litersPerPerson = (bottles * litersPerBottle) / persons; Console.Write($"Each person would receive {litersPerPerson} liters.");