Warm-up Exercises
- Write an explicit conversion from a double variable myDoubleVar to an int variable called myIntVar. You don’t need to re-declare those variables. Assuming myDoubleVar’s value is 5.89, what value would be stored in myIntVar?
Solution
myIntVar = (int)myDoubleVar;
The value stored in myDoubleVar
would
be 5
.
- Declare and initialize two integer variables, x and y. Choose any values you want. Then write necessary commands to display the result of following 3 mathematical operations: addition, multiplication, and remainder (modulo) of x and y.
EXAMPLES OF OUTPUT
x = 5 and y = 3
x = 4 and y = 2
Solution
- 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
Questions
-
Comment briefly on the type decimal and format C it can be used with in a C# code.
Solution
The decimal type is the most precise of the numeric types, and it can be used with the C format specifier to display values representing amounts of currency.
-
What can you learn from this UML class diagram?
Account | |
---|---|
- name : string | |
+ SetName(accountName : string) | |
+ GetName() : string |
Solution
-
Can conversion between two number types change value of a number? Provide an example case.
Solution
Yes, in the case of floats being converted into integers, the value must be truncated. For example, the value 4.876f, when converted into a integer, will become 4.
-
Assume you have an int variable named myAge whose value is 24. 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.
Solution
The value of a
is 1
and the value of b
is 5
.
- Give the values of c and d after the following four instructions have been executed.
Solution
The value of c
is 10
, and the value of d
is ‘7’.
- Is there an error in the following code? Explain the error or give the value of b after the second statement is executed.
Solution
The value of b
is 3
.
- Is there an error in the following code? Explain the error or give the value of b after the second statement is executed.
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 float and the other is of type int, 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
a
andb
after the following four instructions have been executed.
Solution
The variable a
= 2, and the variable b
= 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
b
variable.
Solution
Since the operation a / 2
results in a decimal value, attempting to
store it in an int variable results in an error. This can be fixed by
casting the value of a
, like so: int b = (int)a / 2;
- What is the return type of the operation 12.4 * 3?
Solution
double
Problems
- Write down, on a piece of paper, a fully compilable program that
initializes an int variable named
persons
with the value 5, an int variable namedbottles
with the value 3, and a double variable namedlitersPerBottle
with the value 1.5. Initialize a variable namedlitersPerPerson
with the value resulting from a math 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.