Solutions for those exercises.
Warm-up Exercises
-
Write an explicit conversion from a
double
variablemyDoubleVar
to anint
variable calledmyIntVar
. You don’t need to re-declare those variables. AssumingmyDoubleVar
’s value is5.89
, what value would be stored inmyIntVar
? -
Declare and initialize two integer variables,
x
andy
. Choose any values you want. Then write necessary commands to display the result of the following 3 mathematical operations: addition, multiplication, and remainder (modulo) ofx
andy
.An example of output, with
x
set to 5 andy
set to 3:5 + 3 = 8 5 * 3 = 15 5 % 3 = 2
An example of output, with
x
set to 4 andy
set to 2:4 + 2 = 6 4 * 2 = 8 4 % 2 = 0
-
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.
Questions
-
What distinguishes the
decimal
datatype from other numeric types? What happens when adecimal
value is used with theC
format specifier? -
What can you learn from this UML class diagram?
-
Can conversion between two number types change value of a number? Provide an example case.
-
Assume you have an
int
variable namedmyAge
whose value is24
. What would be displayed on the screen by the following?Console.WriteLine($"{myAge * 2}");
-
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;
-
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;
-
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;
-
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;
-
If one of the operator’s operand is of type
float
and the other is of typeint
, what will be the type of the result of the operation? -
What is the return type of the operation 12.4 * 3?
-
Give the values of
a
andb
after the following four instructions have been executed.int a, b; a = 4; b = a * 3 + 1; a /= 2;
-
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.decimal a = 2.5M; int b = a / 2;
-
What is the return type of the operation 12.4 * 3?
Problems
-
Write down, on a piece of paper, a fully compilable program that initializes an int variable named
persons
with the value 5, anint
variable namedbottles
with the value 3, and adouble
variable namedlitersPerBottle
with the value 1.5. Initialize a variable namedlitersPerPerson
with 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.