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
What kind of rounding does ToIntXX, and ToUIntX use, and how does it work?
Banker’s roundingRound to nearest integer, when tied go with nearest even.
E6. Suppose the Customer class inherits from the Person class, cust is a Customer variable that holds a reference to a Customer object, and person is a Person variable that holds a reference to a Person object. Then which of the following statements will fail at design time?
a. cust = person;
b. cust = person as Customer;
c. cust = (Customer)person;
d. person = cust;
e. person = cust as Person;
f. person = (Person)cust;
a. cust = person;
How would you do a conversion where you won’t know the type until runtime?
Use Convert.ChangeType(value,type)
E7. Suppose the Customer class inherits from the Person class, cust is a Customer variable that holds a reference to a Customer object, and person is a Person variable that holds a reference to a Person object. Then which of the following statements will fail at run time?
a. cust = person;
b. cust = person as Customer;
c. cust = (Customer)person;
d. person = cust;
e. person = cust as Person;
f. person = (Person)cust;
c. cust = (Customer)person;
Once you have an array of bytes can you convert these into integers?
Yes, BitConverter has methods like ToInt
E8. Which of the following methods enables a program to parse an integer entered in a TextBox by the user?
a. int.Parse
b. int.TryParse
c. string.Parse
d. string.ParseInt
e. System.Convert.ToInt32
a. int.Parse
b. int.TryParse
e. System.Convert.ToInt32
Define Boxing and Unboxing
Boxing is converting a value type into an object or an interface that is supported by the value’s type. Unboxing is the converting a boxed value back into its original value.
E9. Which of the following string methods breaks a delimited string into pieces?
a. ToArray
b. Split
c. Fields
d. Join
b. Split
What are the two most common ways to engage interopperability?
COM InteropPlatform Invoke (PInvoke)
E10. Which of the following string methods combines an array of values into a single delimited string?
a. FromArray
b. Fields
c. Join
d. Merge
c. Join
Helper attribute for PInvoke to help manage data types
MarshallAs
E11. Which of the following statements correctly casts a float into an int?
a. int value = (float)fvalue
b. int value = (int)fvalue
c. int value = ToInt(fvalue)
d. float value = (int)fvalue
b. int value = (int)fvalue
When C# does not understand a data type in a dll pulled in using COM, this type helps pacify the compiler.
dynamic type
E12. Which of the following methods does not convert a floating point value into an integer type value?
a. Convert.ChangeType
b. Convert.ToInt16
c. Convert.ToInt32
d. Type.ToInt32
d. Type.ToInt32
How does the dot net framework store chars?
UTF-16
E13. Which of the following methods lets you retrieve four 16-bit values that are packed into a 64-byte value returned by an API function?
a. BitConverter.ToInt
b. BitConverter.FromInt64
c. (int)
d. BitConverter.ToInt16
d. BitConverter.ToInt16
Are c# strings immutable?
Yes. When you think you are manipulating them, they are actually being created and assigned to the reference.
E14. Which of the following methods converts a 32-bit integer value into an array of bytes?
a. BitConverter.ToBytes
b. BitConverter.GetBytes
c. BitConverter.ToArray
d. ToArray
b. BitConverter.GetBytes
Describe the intern pool and how It applies to strings
Each new string created is an entry into the intern pool. The intern pool holds a reference to each unique string. Since strings are I’m unable concatenating strings results in a lot of overhead, as each concatenation involves a new string creation and another entry into the intern pool.
E15. In the following two statements, where does boxing occur?
int i = 1337;
Console.WriteLine(string.Format(“i is: {0}”, i));
a. Storing 1337 in an int requires that 1337 be boxed.
b. The string.Format method must box its first parameter.
c. The string.Format method must box its second parameter.
d. The Console.WriteLine method must box the result of the string.Format method.
c. The string.Format method must box its second parameter.
What is the purpose of StringBuilder.EnsureCapacity?
To allow the memory allocation up front, instead of having to do it later, essentially twice.
E16. After executing the following two C# statements, which of the following is true?
int i = 10;
object iObject = i;
a. Variable i holds the value 10.
b. Variable iObject holds the value 10.
c. If you later change the value of iObject, the value of i remains unchanged.
d. All the above.
d. All the above.
A what point does StringBuilder become more efficient than concatenating strings.
Around 7 concatenations.
E17. When working with API functions, a variable with a name that begins with lpsz has what data type?
a. String
b. 32-bit integer
c. Character array
d. Boolean
a. String
What is the benefit/limitations of StringWriter vs. StringBuilder.
stringWriter is only for appending. It’s faster while StringBuilder is more flexible. Also, StringWriter implements TextWriter.
E18. What purpose does a dynamic data type serve?
a. It enables the program to store more than one kind of data in a variable.
b. It enables the program to convert a value from one type to another automatically without casting.
c. It enables Visual Studio to defer type checking until run time.
d. It prevents type checking.
c. It enables Visual Studio to defer type checking until run time.
What is StringReader used for.
Reading strings sequentially. Plus StringReader implements TextReader.
E19. Which of the following methods formats the variable amount as currency?
a. value.ToString(“C”)
b. value.ToString(“$”)
c. value.ToString(“$#,###.00”)
d. value.ToCurrency()
a. value.ToString(“C”)
Can ToString be given formats?
Yes.
E20. Which of the following methods converts the current date into a string that is appropriate for the user’s locale?
a. DateTime.Now.ToString(“mm/dd/yy”)
b. DateTime.Now.ToString(“D”)
c. DateTime.Now.ToString(“d”)
d. DateTime.Now.ToShortDateString()
b. DateTime.Now.ToString(“D”)
c. DateTime.Now.ToString(“d”)
d. DateTime.Now.ToShortDateString()
Standard Numeric Format String: C or c
Currency
Standard Numeric Format String: D or d
Decimal (Integer Types only)
Standard Numeric Format String: E or e
Scientific Notation
Standard Numeric Format String: F or f
Fixed-point
Standard Numeric Format String: G or g
General (fixed-point or scientific, whichever is shorter.)
Standard Numeric Format String: N or n
Number (with decimal and thousands separators)
Standard Numeric Format String: P or p
Percent (multiplied by 100 and % added)
Standard Numeric Format String: X or x
Hexadecimal (integer types only)
Standard DateTime Format String: d
short date 3/14/2014
Standard DateTime Format String: D
Long Date Friday, March 14, 2012
Standard DateTime Format String: f
“Full” with short timeFriday, March 14, 2012 2:15 PM
Standard DateTime Format String: F
“Full” with long timeFriday, March 14, 2012 2:15:16 PM
Standard DateTime Format String: g
“General” with short time3/14/2014 2:15 PM
Standard DateTime Format String: G
“General” with longtime3/14/2014 2:15:16 PM
Standard DateTime Format String: m or M
Month/Day
Standard DateTime Format String: t
short time 2:15 PM
Standard DateTime Format String: T
long time 2:15:16 PM
Standard DateTime Format String: y or Y
Year/Month March, 2014
CLR
Common Language Runtime (CLR) A virtual machine that manages execution of C# (and other .NET) programs.
composite format
composite format A format item used by String.Format to indicate how an argument should be formatted. The basic syntax is {index[,length][:formatString]}.
immutable
immutable A data type is immutable if its value cannot be changed after it has been created. The String class is immutable. String methods that seem to modify a String, such as Replace and ToUpper, actually replace the String with a new value containing the modified contents.
Unicode
Unicode is a standard for encoding characters used by scripts in various locales around the world. It enables a program to display English, Chinese, Kanji, Arabic, Cyrillic, and other character sets. The .NET Framework uses the UTF-16 encoding, which uses 16 bits to represent each character.
ToUpper vs ToUpperInvariant?
SomeString.ToUpper will upcase based on the culture that the user is running in.
SomeString.ToUpperInvariant will up-cased based on English but not country based.
Need to say upcase in accordance with some culture.