Warm-up Exercises
-
Explain Boolean type bool and the meaning of logical operations AND (&&), OR (||) and negation (!). Provide a small example.
Solution
Example:
- Declare a variable of type int, with value 3.
Solution
Questions
- Write a statement that initializes a variable named myHeightInMeters to your height in meters. What should be the datatype of myHeightInMeters, and why?
Solution
decimal myHeightInMeters = 1.74m;
The datatype should be decimal
because a person’s height in meters most likely needs the precision
afforded by the decimal type.
- What is wrong with the following? Will the error(s) appear at compilation time, or at execution time?
Solution
Console.ReadLine()
returns a value of type string, which cannot be
stored in an integer variable. This results in a compile time error?
- What is the difference, if any, between 3 and “3”?
Solution
3 is an integer value, and “3” is a string value.
Problems
- Declare and initialize 3 variables:
- Each variable should have a different data type
- Choose an appropriate name and value for each variable Then display the value of each variable on the screen.