Chapter 4 Flashcards

1
Q
  1. 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
A

c. NumberStyles.Currency

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. 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.
A

d. All of the above.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What type does the “checked” block work for and what does it do?

A

Only for integer. It throws an exception when a narrowing conversion fails.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. 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.
A

b. The source and destination types must be compatible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens during a narrowing conversion that fails for floats? (Double to float)

A

The result float is set to infinity. Check for this with float.IsInfinity (static method)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. 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);
A

c. Console.WriteLine(total.ToString

(“c”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are implicit conversions allowed for narrowing conversions?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. 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}”)
A

c. String.Format(“{2}, {0}, {3}”,

“vidi”, “Venti”, “Veni”, “vici”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If x were an Int32, what would you get by calling:BitConverter.GetBytes(x)

A

An array of bytes, 4 actually.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. 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

a. i = (int)l is a narrowing conversion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How is a cast handled from float to int?

A

Truncate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. 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
A

d. TryParse

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

System.convert.toInt32 does what to a float-to-int conversion?

A

Rounds to nearest int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. The statement object obj = 72 is an example of which of the following?
    a. Explicit conversion
    b. Immutable conversion
    c. Boxing
    d. Unboxing
A

c. Boxing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Once you cast a reference type to an ancestor class, can you use it again later as the original type?

A

Yes. It never really changes, it’s a reference (address) after all. You can simply “convert” it back, and no values are lost.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. 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

a. Person alice = new Employee();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Converting to an ancestor class is what kind of conversion? (Widening or narrowing?)

A

Widening. An explicit cast is required to go the other direction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Which of the following is not a String method?
    a. IndexOf
    b. StartsWith
    c. StopsWith
    d. Trim
A

c. StopsWith

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Will the following code throw?Person person = new Person();Employee employee = (Employee) person;

A

Yes, the underlying type is actually a Person not an employee.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. 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.
A

d. Use the String class’s Space method passing it 10 as the number of spaces the string should contain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The is operator will returns true for an object if…

A

The type being checked is in the class hierarchy of the type being tested.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Which of the following statements can you use to catch integer overflow and underflow errors?
    a. checked
    b. overflow
    c. watch
    d. try
A

a. checked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will an as operator return if the types are incompatible.

A

A null object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 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.
A

c. Check the result for the value Infinity or NegativeInfinity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the three main methods for converting values?

A

Parsing methodsSystem.ConvertSystem.BitCoverter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

b. Converting a double to a float

f. Converting a double to a long
g. Converting a decimal to an integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Parsing methods of conversion only apply to what kind of input and output?

A

Strings as input and primitive data types as output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you get some parsing methods to accept non-numeric characters? Give some examples

A

System.Globalization.NumberStylesAllowLeadingSignAllowLeadingParenthesisAllowDecimalPoint

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

By default the decimal parser enables what NumberStyles?

A

Thousands and decimals (points)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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

A

c. Converting an Employee to a Person
d. Converting a Manager to a Person
e. Converting a Manager to an Employee

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

In C# U.S. formats always apply to what regardless of the locale?

A

Literal values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What kind of rounding does ToIntXX, and ToUIntX use, and how does it work?

A

Banker’s roundingRound to nearest integer, when tied go with nearest even.

19
Q

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

a. cust = person;

19
Q

How would you do a conversion where you won’t know the type until runtime?

A

Use Convert.ChangeType(value,type)

20
Q

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;

A

c. cust = (Customer)person;

20
Q

Once you have an array of bytes can you convert these into integers?

A

Yes, BitConverter has methods like ToInt

21
Q

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

a. int.Parse
b. int.TryParse

e. System.Convert.ToInt32

21
Q

Define Boxing and Unboxing

A

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.

22
Q

E9. Which of the following string methods breaks a delimited string into pieces?

a. ToArray
b. Split
c. Fields
d. Join

A

b. Split

22
Q

What are the two most common ways to engage interopperability?

A

COM InteropPlatform Invoke (PInvoke)

23
Q

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

A

c. Join

23
Q

Helper attribute for PInvoke to help manage data types

A

MarshallAs

24
Q

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

A

b. int value = (int)fvalue

24
Q

When C# does not understand a data type in a dll pulled in using COM, this type helps pacify the compiler.

A

dynamic type

25
Q

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

A

d. Type.ToInt32

25
Q

How does the dot net framework store chars?

A

UTF-16

26
Q

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

A

d. BitConverter.ToInt16

26
Q

Are c# strings immutable?

A

Yes. When you think you are manipulating them, they are actually being created and assigned to the reference.

27
Q

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

A

b. BitConverter.GetBytes

27
Q

Describe the intern pool and how It applies to strings

A

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.

28
Q

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.

A

c. The string.Format method must box its second parameter.

28
Q

What is the purpose of StringBuilder.EnsureCapacity?

A

To allow the memory allocation up front, instead of having to do it later, essentially twice.

29
Q

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.

A

d. All the above.

29
Q

A what point does StringBuilder become more efficient than concatenating strings.

A

Around 7 concatenations.

30
Q

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

a. String

30
Q

What is the benefit/limitations of StringWriter vs. StringBuilder.

A

stringWriter is only for appending. It’s faster while StringBuilder is more flexible. Also, StringWriter implements TextWriter.

31
Q

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.

A

c. It enables Visual Studio to defer type checking until run time.

31
Q

What is StringReader used for.

A

Reading strings sequentially. Plus StringReader implements TextReader.

32
Q

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

a. value.ToString(“C”)

32
Q

Can ToString be given formats?

A

Yes.

33
Q

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()

A

b. DateTime.Now.ToString(“D”)
c. DateTime.Now.ToString(“d”)
d. DateTime.Now.ToShortDateString()

33
Q

Standard Numeric Format String: C or c

A

Currency

34
Q

Standard Numeric Format String: D or d

A

Decimal (Integer Types only)

35
Q

Standard Numeric Format String: E or e

A

Scientific Notation

36
Q

Standard Numeric Format String: F or f

A

Fixed-point

37
Q

Standard Numeric Format String: G or g

A

General (fixed-point or scientific, whichever is shorter.)

38
Q

Standard Numeric Format String: N or n

A

Number (with decimal and thousands separators)

39
Q

Standard Numeric Format String: P or p

A

Percent (multiplied by 100 and % added)

40
Q

Standard Numeric Format String: X or x

A

Hexadecimal (integer types only)

41
Q

Standard DateTime Format String: d

A

short date 3/14/2014

42
Q

Standard DateTime Format String: D

A

Long Date Friday, March 14, 2012

43
Q

Standard DateTime Format String: f

A

“Full” with short timeFriday, March 14, 2012 2:15 PM

44
Q

Standard DateTime Format String: F

A

“Full” with long timeFriday, March 14, 2012 2:15:16 PM

45
Q

Standard DateTime Format String: g

A

“General” with short time3/14/2014 2:15 PM

46
Q

Standard DateTime Format String: G

A

“General” with longtime3/14/2014 2:15:16 PM

47
Q

Standard DateTime Format String: m or M

A

Month/Day

48
Q

Standard DateTime Format String: t

A

short time 2:15 PM

49
Q

Standard DateTime Format String: T

A

long time 2:15:16 PM

50
Q

Standard DateTime Format String: y or Y

A

Year/Month March, 2014

51
Q

CLR

A

Common Language Runtime (CLR) A virtual machine that manages execution of C# (and other .NET) programs.

52
Q

composite format

A

composite format A format item used by String.Format to indicate how an argument should be formatted. The basic syntax is {index[,length][:formatString]}.

53
Q

immutable

A

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.

54
Q

Unicode

A

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.

55
Q

ToUpper vs ToUpperInvariant?

A

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.