Ch9 | Verified Test Bank – Structures, Enumerated Types And - Test Bank | Visual C# 5e by Tony Gaddis. DOCX document preview.
Starting Out with Visual C#, 5e (Tony Gaddis)
Chapter 9 Structures, Enumerated Types and Dictionaries
TRUE/FALSE
1. The fields contained in a structure must all be of the same data type.
2. If you find yourself using a structure for more than simply encapsulating a set of variables into a single item, you should probably use a class.
3. A structure declaration does not create anything in memory until you create a new instance of the structure.
4. Structures are value types so a structure object contains all of its data at the variable's memory location.
5. When you create a structure object with a simple declaration, the object's fields are initialized to their default values.
6. Once you have an instance of a structure, you can access its fields using the dot operator.
7. You can use the assignment operator (=) to assign one structure object to another.
8. Unlike other types of objects, you cannot pass a structure object as an argument to a method.
9. You cannot perform comparison operations directly on structure objects.
10. When you create a structure array, each element of the array is a structure instance and the fields of each instance are initialized to either zero or null (for fields that are reference variables).
11. When you add a structure instance to a List, the List will contain a copy of the object.
12. Like strings, enumerators must be enclosed in quotation marks.
13. When you declare an enumerated type, the enumerators are automatically assigned integer values, starting with 0, unless you provide other specific values.
14. Enumerators and enum variables have a ToString method.
15. You can assign an enumerator directly to an int variable.
16. You can specify specific values to be assigned to the enumerators in an enumerated data type.
17. Because enumerators represent integer values, they can be used in a loop to step through the elements of an array.
18. Given the following declarations:
enum Days { Mon, Tue, Wed, Thur, Fri, Sat };
Days day1 = Days.Tue;
Days day2 = Days.Thur;
The following Boolean expression is valid:
day1 < day2
19. Given the following declarations:
enum Days { Mon, Tue, Wed, Thur, Fri, Sat };
Days day1 = Days.Tue;
Days day2 = Days.Thur;
The following Boolean expression is valid:
day1 < Days.Mon
20. Given the following declarations:
enum Days { Mon, Tue, Wed, Thur, Fri, Sat };
Days day1 = Days.Tue;
int n = 3;
The following Boolean expression is valid:
day1 < n
21. The DateTime structure stores information about a time interval.
22. The hour property of the DateTime structure stores the hour as an integer between 1 and 24.
23. To get the current date and time you can use the DateTime.Now expression.
24. The TimeSpan structure allows you to create objects that store information about an amount of time such as 7 days or 15 minutes.
25. You can add a TimeSpan structure to a DateTime object to get another DateTime object that is later than the first one, but you cannot subtract a TimeSpan structure from a DateTime object.
26. In a dictionary, each element is associated with two values: a key and a value.
27. Given the following declaration for a Dictionary, the key is a string.
Dictionary<int, string> students = new Dictionary<int, string>();
28. Given the following declaration for a Dictionary, the name of the Dictionary is students.
Dictionary<int, string> students = new Dictionary<int, string>();
29. The Add method allows you to add an element to a Dictionary by specifying the element's value; the key is added automatically.
30. To retrieve a value from a Dictionary you only need to specify the Dictionary name and the element's key.
MULTIPLE CHOICE
1. A(n) __________ is an object you can create that contains one or more variables known as fields.
a. | file | b. | structure | c. | list | d. | container |
2. Before you can use a structure to store data, you must create a(n) __________ of the structure in memory.
a. | copy | b. | instance | c. | identity | d. | field |
3. Given the code sample shown, which of the following statements declares a Pet structure named hound and initializes the object's fields with their default values?
struct Pet
{
public string name;
public int age;
public string type;
}
a. | new Pet = hound; |
b. | Pet hound = new Pet(); |
c. | hound = new Pet(); |
d. | Pet hound; |
4. Given the code sample shown, which of the following statements assigns the string "Beagle" to the hound object's type field?
struct Pet
{
public string name;
public int age;
public string type;
}
Pet hound = new Pet();
a. | hound = new type("Beagle"); |
b. | type = "Beagle"; |
c. | hound.type = "Beagle"; |
d. | Pet.hound.type("Beagle"); |
5. Given the code sample shown, which of the following statements assigns the string "Pickles" to the name member of cats[2]?
struct Pet
{
public string name;
public int age;
public string type;
}
const int SIZE = 4;
Pet[] cats = new Pet[SIZE];
a. | "Pickles" = cats[2]; |
b. | cats[2].name = "Pickles"; |
c. | cats.name[2] = "Pickles"; |
d. | cats[2].name("Pickles"); |
6. Given the code sample shown, which of the following statements adds the Pet structure named parrot to the List object named birdList?
struct Pet
{
public string name;
public int age;
public string type;
}
List<Pet> birdList = new List<Pet>();
Pet parrot = new Pet();
a. | birdList.Add(parrot); |
b. | Add(birdList.parrot); |
c. | birdList[parrot]; |
d. | birdList[0] = parrot; |
7. Assuming that cat1 and cat2 are both instances of the Pet structure, which of the following statements copies cat1 to cat2?
a. | cat1 = cat2; | c. | cat1(cat2); |
b. | cat2 = cat1; | d. | cat1 = new cat2; |
8. Structure objects are normally passed __________ to a method.
a. | by reference | b. | globally | c. | by value | d. | locally |
9. In order to compare two structure objects you must compare the individual __________ of each object.
a. | variables | b. | fields | c. | data types | d. | objects |
10. When the items in two data structures are related by their indexes it is said that a(n) __________ exists between the data structures.
a. | exclusive association | c. | parallel relationship |
b. | logical connection | d. | binary link |
11. A(n) __________ is a data type that you can create by specifying a set of symbolic names that belong to that data type.
a. | class | c. | namespace |
b. | structure | d. | enumerated data type |
12. In the following line of code the identifiers Vanilla, Strawberry, and Chocolate which appear inside the braces are known as __________.
enum Flavor {Vanilla, Strawberry, Chocolate}
a. | constants | b. | references | c. | mnemonics | d. | enumerators |
13. Given the following code sample, which of the following statements assigns the value Flavor.Vanilla to the iceCreamFlavor variable?
enum Flavor {Vanilla, Strawberry, Chocolate}
Flavor iceCreamFlavor;
a. | iceCreamFlavor.Vanilla; |
b. | iceCreamFlavor = Flavor.Vanilla; |
c. | iceCreamFlavor(Flavor.Vanilla); |
d. | iceCreamFlavor = Flavor[0]; |
14. Given the following code sample, which of the following strings will be displayed in the message box?
enum Flavor {Vanilla, Strawberry, Chocolate}
Flavor iceCreamFlavor = Flavor.Strawberry;
MessageBox.Show(iceCreamFlavor + " is my favorite!");
a. | Flavor.Strawberry is my favorite! |
b. | iceCreamFlavor is my favorite! |
c. | Strawberry is my favorite! |
d. | iceCreamFlavor + is my favorite! |
15. Given the following code sample, which of the following statements assigns the Flavor enumerated type value to the integer variable n?
enum Flavor {Vanilla, Strawberry, Chocolate}
a. | int n = Flavor.Strawberry.ToInteger(); |
b. | int n = Strawberry.IntValue; |
c. | int n = (Flavor) Strawberry; |
d. | int n = (int) Flavor.Strawberry; |
16. You can convert an enumerator to its underlying integer type by using a(n) __________.
a. | binary operator | c. | reference variable |
b. | cast operator | d. | assignment statement |
17. Which of the following is not a valid enumerated type declaration?
a. | enum Iron {Melting = 2795, Boiling = 4982} |
b. | enum SpeedLimit {City = 35, Highway = 65} |
c. | enum Temp {Cold = 20, Hot = 90} |
d. | enum Voltage {Orange = 3.3, Green = 5.0} |
18. Enumerators and enum variables can be compared directly with __________ values.
a. | int | b. | decimal | c. | double | d. | string |
19. What is the date set by the following DateTime instance named dt?
DateTme dt = new DateTime(2018, 3, 4);
a. | March 4, 2018 | c. | March 3, 2018 |
b. | April 3, 2018 | d. | April 4, 2018 |
20. Which of the following statements will give you the current date but not the time?
a. | DateTime thisDay = DateTime.Now; |
b. | DateTime thisDay = DateTime.Today; |
c. | DateTime thisDay = DateTime.currentDate; |
d. | any of these will work |
21. To compare two DateTime objects to see if one date is later than the other, you use __________.
a. | a loop | c. | the TimeSpan structure |
b. | logic operators | d. | relational operators |
22. Given the following code, the futureDate object will be set to __________.
DateTime dt = new DateTime(2019, 7, 12);
TimeSpan tspan = new TimeSpan(7, 0, 0, 0);
DateTime futureDate = dt + tspan;
a. | July 19, 2019 | c. | December 14, 2019 |
b. | December 12, 2019 | d. | July 12, 2026 |
23. Given the following code, the tspan variable will be set to __________.
DateTime date1 = new DateTime(2019, 7, 27);
DateTime date2 = new DateTime(2019, 7, 12);
TimeSpan tspan = date1 - date2;
a. | 39 days | c. | 1 month and 8 days |
b. | 15 days | d. | 0 |
24. When an element is stored in a Dictionary, the keys must be __________.
a. | in ascending order | c. | sequential |
b. | in descending order | d. | unique |
25. When an element is stored in a Dictionary, it is stored as an object of the __________ type.
a. | key | c. | KeyValuePair |
b. | value | d. | int |
26. You can use the __________ method to determine whether a specific key exists in a dictionary.
a. | ContainsValue | c. | TryGetalue |
b. | ContainsKey | d. | ElementAt |
27. You can use the __________ method to get a value you are looking for if you pass in the value's key.
a. | ContainsValue | c. | TryGetValue |
b. | ContainsKey | d. | ElementAt |
28. You can use the __________ method to determine whether a specific element exists as a KeyValuePair in a dictionary.
a. | ContainsValue | c. | Contains |
b. | ContainsKey | d. | ElementAt |
29. The KeyValuePair type is a(n) __________ that has two properties: the Key and the Value.
a. | struct | c. | Dictionary |
b. | variable | d. | array |
30. You can use a(n) __________ to iterate over all the KeyValuePair elements in a Dictionary.
a. | foreach loop | c. | ContainsPair method |
b. | if-else structure | d. | array |