Chapter 4 Flashcards
- To parse a string that might contain a currency value such as $1,234.56, you should pass the Parse or TryParse method which of the following values?
a. NumberStyles.AllowCurrencySymbol
b. NumberStyles.AllowThousands
c. NumberStyles.Currency
d. A combination of all NumberStyles values
c. NumberStyles.Currency
- Which of the following statements is true for widening conversions?
a. Any value in the source type can fit into the destination type.
b. The conversion will not result in loss of magnitude but may result is some loss of precision.
c. An explicit cast is optional.
d. All of the above.
d. All of the above.
What type does the “checked” block work for and what does it do?
Only for integer. It throws an exception when a narrowing conversion fails.
- Which of the following statements is true for narrowing conversions?
vThe conversion will not result in loss of magnitude but may result is some loss of precision.
b. The source and destination types must be compatible.
c. An explicit cast is optional.
d. A cast can convert a string into an int if the string holds numeric text.
b. The source and destination types must be compatible.
What happens during a narrowing conversion that fails for floats? (Double to float)
The result float is set to infinity. Check for this with float.IsInfinity (static method)
- Assuming total is a decimal variable holding the value 1234.56, which of the following statements displays total with the currency format $1,234.56?
a. Console.WriteLine(total.ToString());
b. Console.WriteLine(total.ToCurrency
String());
c. Console.WriteLine(total.ToString
(“c”));
d. Console.WriteLine(Format(“{0:C}”,
total);
c. Console.WriteLine(total.ToString
(“c”));
Are implicit conversions allowed for narrowing conversions?
No
- Which of the following statements generates a string containing the text “Veni, vidi, vici”?
a. String.Format(“{0}, {1}, {2}”,
Veni, vidi, vici)
b. String.Format(“{1}, {2}, {3}”,
“Veni”, “vidi”, “vici”)
c. String.Format(“{2}, {0}, {3}”,
“vidi”, “Venti”, “Veni”, “vici”)
d. String.Format(“{Veni, vidi, vici}”)
c. String.Format(“{2}, {0}, {3}”,
“vidi”, “Venti”, “Veni”, “vici”)
If x were an Int32, what would you get by calling:BitConverter.GetBytes(x)
An array of bytes, 4 actually.
- If i is an int and l is a long, which of the following statements is true?
a. i = (int)l is a narrowing conversion.
b. l = (long)i is a narrowing conversion.
c. l = (long)i could cause an integer overflow.
d. The correct way to copy i’s value into l is
l = long.Parse(i).
a. i = (int)l is a narrowing conversion.
How is a cast handled from float to int?
Truncate
- Which of the following methods is the best way to store an integer value typed by the user in a variable?
a. ToString
b. Convert
c. ParseInt
d. TryParse
d. TryParse
System.convert.toInt32 does what to a float-to-int conversion?
Rounds to nearest int.
- The statement object obj = 72 is an example of which of the following?
a. Explicit conversion
b. Immutable conversion
c. Boxing
d. Unboxing
c. Boxing
Once you cast a reference type to an ancestor class, can you use it again later as the original type?
Yes. It never really changes, it’s a reference (address) after all. You can simply “convert” it back, and no values are lost.
- If Employee inherits from Person and Manager inherits from Employee, which of the following statements is valid?
a. Person alice = new Employee();
b. Employee bob = new Person();
c. Manager cindy = new Employee();
d. Manager dan = (Manager)
(new Employee());
a. Person alice = new Employee();
Converting to an ancestor class is what kind of conversion? (Widening or narrowing?)
Widening. An explicit cast is required to go the other direction.
- Which of the following is not a String method?
a. IndexOf
b. StartsWith
c. StopsWith
d. Trim
c. StopsWith
Will the following code throw?Person person = new Person();Employee employee = (Employee) person;
Yes, the underlying type is actually a Person not an employee.
- Which of the following techniques does not create a String containing 10 spaces?
a. Set a String variable equal to a literal containing 10 spaces.
b. Use a String constructor passing it an array of 10 space characters.
c. Use a String constructor passing it the space character and 10 as the number of times it should be repeated.
d. Use the String class’s Space method passing it 10 as the number of spaces the string should contain.
d. Use the String class’s Space method passing it 10 as the number of spaces the string should contain.
The is operator will returns true for an object if…
The type being checked is in the class hierarchy of the type being tested.
- Which of the following statements can you use to catch integer overflow and underflow errors?
a. checked
b. overflow
c. watch
d. try
a. checked
What will an as operator return if the types are incompatible.
A null object.
- Which of the following techniques should you use to watch for floating point operations that cause overflow or underflow?
a. Use a checked block.
b. Use a try-catch block.
c. Check the result for the value Infinity or NegativeInfinity.
d. Check the result for Error.
c. Check the result for the value Infinity or NegativeInfinity.
What are the three main methods for converting values?
Parsing methodsSystem.ConvertSystem.BitCoverter
E1. Which of the following are narrowing conversions?
a. Converting a byte to an integer
b. Converting a double to a float
c. Converting an integer to a long
d. Converting a float to a double
e. Converting an integer to a float
f. Converting a double to a long
g. Converting a decimal to an integer
h. Converting an integer to a decimal
b. Converting a double to a float
f. Converting a double to a long
g. Converting a decimal to an integer
Parsing methods of conversion only apply to what kind of input and output?
Strings as input and primitive data types as output.
E2. Which of the following are widening conversions?
a. Converting a byte to an integer
b. Converting a double to a float
c. Converting an integer to a long
d. Converting a float to a double
e. Converting an integer to a float
f. Converting a double to a long
g. Converting a decimal to an integer
h. Converting an integer to a decimal
a. Converting a byte to an integer
c. Converting an integer to a long
d. Converting a float to a double
e. Converting an integer to a float
h. Converting an integer to a decimal
How can you get some parsing methods to accept non-numeric characters? Give some examples
System.Globalization.NumberStylesAllowLeadingSignAllowLeadingParenthesisAllowDecimalPoint
E3. If the Manager class inherits from the Employee class, and the Employee and Customer classes both inherit from the Person class, then which of the following are narrowing conversions?
a. Converting a Person to a Manager
b. Converting an Employee to a Manager
c. Converting an Employee to a Person
d. Converting a Manager to a Person
e. Converting a Manager to an Employee
f. Converting a Person to an Employee
g. Converting a Customer to an Employee
h. Converting an Employee to a Customer
a. Converting a Person to a Manager
b. Converting an Employee to a Manager
f. Converting a Person to an Employee
(In a sense you could technically consider g and h to be considered narrowing conversions, but actually they are just invalid conversions.)
By default the decimal parser enables what NumberStyles?
Thousands and decimals (points)
E4. If the Manager class inherits from the Employee class, and the Employee and Customer classes both inherit from the Person class, then which of the following are widening conversions?
a. Converting a Person to a Manager
b. Converting an Employee to a Manager
c. Converting an Employee to a Person
d. Converting a Manager to a Person
e. Converting a Manager to an Employee
f. Converting a Person to an Employee
g. Converting a Customer to an Employee
h. Converting an Employee to a Customer
c. Converting an Employee to a Person
d. Converting a Manager to a Person
e. Converting a Manager to an Employee
In C# U.S. formats always apply to what regardless of the locale?
Literal values
E5. During which of the following conversions might there be a loss of precision?
a. Converting an int to a float
b. Converting a float to a long
c. Converting a long to an int
d. Converting an int to a long
e. Converting a float to a decimal
f. Converting a decimal to a float
a. Converting an int to a float
b. Converting a float to a long
e. Converting a float to a decimal
f. Converting a decimal to a float