🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C# Workshop - Week 4 Review Questions

Started by
9 comments, last by SamLowry 16 years, 10 months ago
Chapters 11, 13, 14, and 15 Review Questions Greetings everyone. Each week I will post review questions and exercises in the weekly thread. Please try and answer them by yourself; either as you're reading over the chapters, or afterwards if you prefer. Once you've done so, feel free to look over the answers provided by others and submit your own answers if you've not yet seen them posted. Discussion about the quiz questions and answers is encouraged for clarification. Finally, experienced C# programmers may feel free to post quiz-like questions and exercises of their own.
  1. If a block of code needs to be executed from different parts of a program, it is common to put the code in a WHAT?
  2. If a method has a return type other than void, all code paths of the method must terminate with a what?
  3. What are fields, what do they look like?
  4. When creating a static, readonly field, does the order of the keywords matter?
  5. Must a "readonly" value be initialized in its declaration statement?
  6. Must a "const" value be initialized in its declaration statement?
  7. When are const and readonly values evaluated?
  8. Can the const modifier be used on local variables? How about the readonly modifier?
  9. When a method is invoked, what data is pushed onto the stack?
  10. What two keywords can be used with parameters to ensure that any changes to the parameter variables exists after the method terminates?
  11. True for false, when using the 'ref' keyword with a parameter, the keyword must only be supplied with the method definition?
  12. When C# code is compiled to intermediate language, how does the intermediate language differentiate between 'ref' and 'out' parameters?
  13. True or False, when passing a reference type to a method, a COPY of the reference is made, preventing you from actually changing where the original reference pointed to in memory?
  14. True or False, a reference variable passed to a method points to the same location in heap memory as the original reference?
  15. What does the keyword 'params' do?
  16. Where in a method call can a 'param' parameter be located?
  17. True or false, object oriented language such as C# allow the programmer to create their own classes and structures?
  18. What does the 'public' access modifier do?
  19. True or False, the 'instance' keyword is used to indicate a field or other member can be used on class instances?
  20. Static members of a type must always be prefaced with what?
  21. Instance members of a type must always be prefaced with what, when being accesses from outside of an object?
  22. When working with structures, a field that is not explicitly set through a 'new' statement or through an assignment operator is said to be what?
  23. True or False, it's a good idea to use the 'new' keyword when creating instances of a structure? Why or Why Not.
  24. When you declare an instance of reference type without using the new statement it is said to be what?
  25. True or False, all memory allocated on the heap is automatically initialized to 0?
  26. When working with structures, allocating an array of 5 elements creates enough memory to store 5 structures. When allocating an array of 5 reference types, enough memory is allocated to store 5 what?
  27. True or False, Instances of structures require less memory and less overhead than instances of classes?
  28. True or False, Structures are more suited for light-weight objects?
  29. True or False, whenever you have a need for a new class or structure that has just a few fields, you should probably make it a structure, especially if it is likely to be used within arrays.
  30. If an array is initialized within the declaration, why is better to initialize that array as a static field rather than a local variable?
  31. Can an instance of a class or structure access the instance fields of that class?
  32. Can an instance of a class or structure access the static fields of the class?
  33. Can a static method of a class or structure access the instance fields of the class?
  34. True or False, every class or structure in the .NET Framework, including those you write yourself, has a ToString method?
  35. What is the keyword used to explicitly access the members of a base class?
  36. True or False, it's possible to inherit an explicit base class with both classes and structs?
  37. What is the implicit base class of all structs?
  38. What does the 'virtual' keyword mean?
  39. What keyword is used with the virtual method to indicate a derived class wishes to override the virtual method?
  40. True or false, when overriding a method in a derived class, everything about the method declaration, including the accessibility, return type, and parameters must match for it to be successfully overridden.
  41. Is it possible to use the override keyword on a non-virtual method?
  42. Without the use of a constructor, what's another method of initializing the value of class fields?
  43. Does the above method work for structs?
  44. What are constructors?
  45. What is a constructor called with no parameters?
  46. Is it possible to declare a parameterless constructor for struct types? Why or Why not.
  47. True or False, it is not possible to declare a constructor Private.
  48. True or false, you can declare a parametered constructor in both structs and classes.
  49. If you have a method with parameter names which are the same as those of the classes fields, which keyword do you use to indicate you wish to reference the fields of the current class. What does that keyword refer to within an instance method?
  50. What happens to the default parameterless constructor if you provide a parametered constructor for a class?
  51. What happens to the default parameterless constructor if you provide a parametered constructor for a struct?
  52. Since a constructor doesn't have a return value, what must it do to indicate failure if it's unable to initialize an object correctly.
  53. What is it called when you explicitly call one constructor from another, in order to help with object initialization?
  54. If you don’t explicitly provide a constructor initializer, what does the constructor call instead?
  55. How many static constructors can a class have?
  56. The code in a static constructor is guaranteed to be executed before what?
  57. True or false, a static constructor is a good place to put initialization of static fields?
Cheers! [Edited by - JWalsh on July 31, 2007 10:20:21 PM]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
The review questions for week 4 have been posted.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
I've decided to post my attempted answers to the questions. :-) Point out mistakes etc. or stuff that's not clear, if any.

The only slight doubts were around questions 42 & 53 where it wasn't quite obvious to me what the answer was supposed to be (what the question wanted.)

1. If a block of code needs to be executed from different parts of a program, it is common to put the code in a WHAT?
a. A method, also known as a function or procedure in some other languages.

2. If a method has a return type other than void, all code paths of the method must terminate with a what?
a. To ensure the result of the method when called is not undefined, all code paths must end with a return statement.

3. What are fields, what do they look like?
a. “Fields” are data members of the object or class, meaning they are object or class wide variables. Normal (that is “instance”) fields exist in the object instance and are accessible by all methods of the object. Fields which are marked with “static” are “class” fields and are accessible from static (class) methods as well as instance methods.

4. When creating a static, readonly field, does the order of the keywords matter?
a. The order does not matter.

5. Must a "readonly" value be initialized in its declaration statement?
a. No. While it may be initialized in its declaration, or alternatively in a class constructor.

6. Must a "const" value be initialized in its declaration statement?
a. Yes, because const is a compile time substitution, which means you’re in effect specifying a substation to use at compile time, whereas static readonly is compiled as a variable data member in the normal way, but with read-only semantics.

7. When are const and readonly values evaluated?
a. As mentioned, const is evaluated at compile time, while readonly is evaluated at runtime.

8. Can the const modifier be used on local variables? How about the readonly modifier?
a. Yes it can. Because const is only in effect a substitution, you can expect it to work anywhere.
b. No it cannot. Readonly (and static) has their meanings intrinsically tied to the class and or object itself, so trying to apply it to a local variable is essentially meaningless (at least, for C#, in any case.)

9. When a method is invoked, what data is pushed onto the stack?
a. It’s parameters and local variables.

10. What two keywords can be used with parameters to ensure that any changes to the parameter variables exists after the method terminates?
a. ref and out.

11. True or false, when using the 'ref' keyword with a parameter, the keyword must only be supplied with the method definition?
a. True. This is unlike most other languages where only the parameter name needs be specified. The reason this is done is to act as a warning to the programmer to expect the parameter to be changed by the method being called.

12. When C# code is compiled to intermediate language, how does the intermediate language differentiate between 'ref' and 'out' parameters?
a. The IL does not differ. (But the handling of the two in terms of allowed coding constructs is slightly different.)

13. True or False, when passing a reference type to a method, a COPY of the reference is made, preventing you from actually changing where the original reference pointed to in memory?
a. True.

14. True or False, a reference variable passed to a method points to the same location in heap memory as the original reference?
a. True.

15. What does the keyword 'params' do?
a. It allows you to construct methods with an arbitrary number of the same type of parameters, which is accessible in the method as an array of the type.

16. Where in a method call can a 'param' parameter be located?
a. As part of the array of params.

17. True or false, object oriented language such as C# allow the programmer to create their own classes and structures?
a. Yes.

18. What does the 'public' access modifier do?
a. Makes a member accessible to any other class or object.

19. True or False, the 'instance' keyword is used to indicate a field or other member can be used on class instances?
a. False. Members are assumed to be instance members unless specifically stated otherwise with the static modifier.

20. Static members of a type must always be prefaced with what?
a. Strictly speaking they are not required not be prefaced with anything, but they should however be prefaced with an appropriate access modifier. A struct containing only private data members is about as useful as a blow up dart board.

21. Instance members of a type must always be prefaced with what, when being accesses from outside of an object?
a. They must be prefaced by “public” to make them visible to code not part of the class/object.

22. When working with structures, a field that is not explicitly set through a 'new' statement or through an assignment operator is said to be what?
a. uninitialized

23. True or False, it's a good idea to use the 'new' keyword when creating instances of a structure? Why or Why Not.
a. True. To ensure all the data members are properly initialized to predictable/sane/usable values.

24. When you declare an instance of reference type without using the new statement it is said to be what?
a. uninitialized

25. True or False, all memory allocated on the heap is automatically initialized to 0?
a. True.

26. When working with structures, allocating an array of 5 elements creates enough memory to store 5 structures. When allocating an array of 5 reference types, enough memory is allocated to store 5 what?
a. References. (Pointers, if you like...)

27. True or False, Instances of structures require less memory and less overhead than instances of classes?
a. True. (The easy way to see this is to consider an array of structs, vs. Array of similar objects. With the array of objects you have both a set of references taking up space as well as the objects themselves, whereas with the array of structs they exist in one contiguous “block” on the stack with no explicit references needing to be stored.)

28. True or False, Structures are more suited for light-weight objects?
a. True

29. True or False, whenever you have a need for a new class or structure that has just a few fields, you should probably make it a structure, especially if it is likely to be used within arrays.
a. True

30. If an array is initialized within the declaration, why is better to initialize that array as a static field rather than a local variable?
a. To avoid repeatedly incurring the initialization cost of the array every time the method is called.

31. Can an instance of a class or structure access the instance fields of that class?
a. Yes.

32. Can an instance of a class or structure access the static fields of the class?
a. Yes.

33. Can a static method of a class or structure access the instance fields of the class?
a. No. That is nonsensical. Suppose there’s 10 instances of an object. If now a class method wants to access an instance member, which of the 10 should the compiler use?

34. True or False, every class or structure in the .NET Framework, including those you write yourself, has a ToString method?
a. True. This is because it’s inherited from the base class of all objects, the class System.Object.

35. What is the keyword used to explicitly access the members of a base class?
a. The keyword “base”

36. True or False, it's possible to inherit an explicit base class with both classes and structs?
a. False. It is only allowed with classes.

37. What is the implicit base class of all structs?
a. System.Object

38. What does the 'virtual' keyword mean?
a. It means subclasses of the class containing the virtual method, may override the virtual method’s behaviour.

39. What keyword is used with the virtual method to indicate a derived class wishes to override the virtual method?
a. Override

40. True or false, when overriding a method in a derived class, everything about the method declaration, including the accessibility, return type, and parameters must match for it to be successfully overridden.
a. True.

41. Is it possible to use the override keyword on a non-virtual method?
a. No.

42. Without the use of a constructor, what's another method of initializing the value of class fields?
a. Defining them in the declaration. (Or use constructor initializer to call the base class constructor?)

43. Does the above method work for structs?
a. No, structs do not need it, as their field members are initialised automatically.

44. What are constructors?
a. They are special methods that are called when an object or struct is created to initialise the object/struct being created.

45. What is a constructor called with no parameters?
a. It’s called a “parameterless” constructor.

46. Is it possible to declare a parameterless constructor for struct types? Why or Why not.
a. No. Structs do not allow any type of explicit constructor to be declared. This is to keep a structs initialization cost low and predictable.

47. True or False, it is not possible to declare a constructor Private.
a. False. It is possible to have private constructors that are used internally, e.g. by static methods of the class.

48. True or false, you can declare a parametered constructor in both structs and classes.
a. True.

49. If you have a method with parameter names which are the same as those of the classes fields, which keyword do you use to indicate you wish to reference the fields of the current class. What does that keyword refer to within an instance method?
a. The keyword “this”. In an instance method it refers to the object instance.

50. What happens to the default parameterless constructor if you provide a parametered constructor for a class?
a. It disappears

51. What happens to the default parameterless constructor if you provide a parametered constructor for a struct?
a. It is added to the struct and the parameterless constructor remains.

52. Since a constructor doesn't have a return value, what must it do to indicate failure if it's unable to initialize an object correctly.
a. Throw an exception.

53. What is it called when you explicitly call one constructor from another, in order to help with object initialization?
a. Constructor initializer? (Constructor chaining?)

54. If you don’t explicitly provide a constructor initializer, what does the constructor call instead?
55. How many static constructors can a class have?
a. Exactly one.

56. The code in a static constructor is guaranteed to be executed before what?
a. Before the first static method or instance constructor is called.

57. True or false, a static constructor is a good place to put initialization of static fields?
a. True
ByteJuggler, Thanks for posting your answers. I just finished these and it was nice having something to compare them to. I came up with a couple different answers than you.

11. True or false, when using the 'ref' keyword with a parameter, the keyword must only be supplied with the method definition?
a. False. The ‘ref’ keyword must be specified in the method call and method definition.

20. Static members of a type must always be prefaced with what?
a. The keyword static

25. True or False, all memory allocated on the heap is automatically initialized to 0?
a. False. Heap memory is automatically initialized to null

37. What is the implicit base class of all structs?
a. System.ValueType


I think your answer is right on question 25. When reviewing this I found "Memory allocated from the heap is always initialized to zero" on page 88. This has me somewhat confused because aren't objects held on the heap and they are initialized to null, I thought.
Heh. Only about 175 views of the review questions, with potential duplicates. And 450 people signed up for the workshop...

Someone's not doing their homework. [lol]

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote: Original post by JWalsh
Heh. Only about 175 views of the review questions, with potential duplicates. And 450 people signed up for the workshop...

Someone's not doing their homework. [lol]

Cheers!


Well, I really appreciate you taking the time to set up these questions and the workshop in general. I'm sure this has taken a huge amount of your time. Some people may be trying to catch up to here. I got in a couple weeks late and just finished week 4. Now I'm off to attempt project 1. YAY!!!
Chapters 11, 13, 14, and 15 Review Questions

Greetings everyone. Please look over the following answers and make sure it matches with yours. If it doesn't and you're confused about why, please post your questions here.
  1. If a block of code needs to be executed from different parts of a program, it is common to put the code in a WHAT?
    Method

  2. If a method has a return type other than void, all code paths of the method must terminate with a what?
    They must terminate with a return statement which returns an object of the proper type.

  3. What are fields, what do they look like?
    Fields look like local variables, except they are not defined inside of a method. They are defined inside the class but outside of all methods, and they can be accessed by any method in the class.

  4. When creating a static, readonly field, does the order of the keywords matter?
    No. both static readonly and readonly static is fine.

  5. Must a "readonly" value be initialized in its declaration statement?
    No. It can be initialized in a constructor as well.

  6. Must a "const" value be initialized in its declaration statement?
    Yes.

  7. When are const and readonly values evaluated?
    Const is evaluated at compile-time, readonly is evaluated at run-time.

  8. Can the const modifier be used on local variables? How about the readonly modifier?
    Const can be used on local variables. Readonly cannot.

  9. When a method is invoked, what data is pushed onto the stack?
    The parameters, and all local variables defined within the method

  10. What two keywords can be used with parameters to ensure that any changes to the parameter variables exists after the method terminates?
    out and ref

  11. True for false, when using the 'ref' keyword with a parameter, the keyword must only be supplied with the method definition?
    False. It must be specified when the method is invoked as well.

  12. When C# code is compiled to intermediate language, how does the intermediate language differentiate between 'ref' and 'out' parameters?
    It doesn’t. 'ref' and 'out' are syntactic sugar part of the C# language. In IL they are the same.

  13. True or False, when passing a reference type to a method, a COPY of the reference is made, preventing you from actually changing where the original reference pointed to in memory?
    True.

  14. True or False, a reference variable passed to a method points to the same location in heap memory as the original reference?
    True.

  15. What does the keyword 'params' do?
    It allows you to send a variable list of arguments to a method

  16. Where in a method call can a 'param' parameter be located?
    It must be the last parameter.

  17. True or false, object oriented language such as C# allow the programmer to create their own classes and structures?
    True.

  18. What does the 'public' access modifier do?
    It allows members to be accessed from outside of the class or structure that contains them.

  19. True or False, the 'instance' keyword is used to indicate a field or other member can be used on class instances?
    False. There is no 'instance' keyword.

  20. Static members of a type must always be prefaced with what?
    The name of the type for which they belong, followed by a period.

  21. Instance members of a type must always be prefaced with what, when being accesses from outside of an object?
    The name of an object.

  22. When working with structures, a field that is not explicitly set through a 'new' statement or through an assignment operator is said to be what?
    Uninitialized

  23. True or False, it's a good idea to use the 'new' keyword when creating instances of a structure? Why or Why Not.
    True. Creating it with 'new' zeroes out the memory.

  24. When you declare an instance of reference type without using the new statement it is said to be what?
    Uninitialized.

  25. True or False, all memory allocated on the heap is automatically initialized to 0?
    True.

  26. When working with structures, allocating an array of 5 elements creates enough memory to store 5 structures. When allocating an array of 5 reference types, enough memory is allocated to store 5 what?
    5 References.

  27. True or False, Instances of structures require less memory and less overhead than instances of classes?
    True.

  28. True or False, Structures are more suited for light-weight objects?
    True.

  29. True or False, whenever you have a need for a new class or structure that has just a few fields, you should probably make it a structure, especially if it is likely to be used within arrays.
    True.

  30. If an array is initialized within the declaration, why is better to initialize that array as a static field rather than a local variable?
    So they only get initialized once.

  31. Can an instance of a class or structure access the instance fields of that class?
    Yes.

  32. Can an instance of a class or structure access the static fields of the class?
    Yes.

  33. Can a static method of a class or structure access the instance fields of the class?
    No.

  34. True or False, every class or structure in the .NET Framework, including those you write yourself, has a ToString method?
    True.

  35. What is the keyword used to explicitly access the members of a base class?
    base

  36. True or False, it's possible to inherit an explicit base class with both classes and structs?
    False

  37. What is the implicit base class of all structs?
    System.ValueType

  38. What does the 'virtual' keyword mean?
    This means that any derived class can supersede the provided implementation of the method with virtual keyword by providing their own.

  39. What keyword is used with the virtual method to indicate a derived class wishes to override the virtual method?
    override.

  40. True or false, when overriding a method in a derived class, everything about the method declaration, including the accessibility, return type, and parameters must match for it to be successfully overridden.
    True.

  41. Is it possible to use the override keyword on a non-virtual method?
    No.

  42. Without the use of a constructor, what's another method of initializing the value of class fields?
    You can initialize them in the declaration statement.

  43. Does the above method work for structs?
    No.

  44. What are constructors?
    Constructors are methods which are executed automatically when an object is created.

  45. What is a constructor called with no parameters?
    A parameterless constructor. It's also frequently called the default constructor.

  46. Is it possible to declare a parameterless constructor for struct types? Why or Why not.
    No. One is already provided by the compiler.

  47. True or False, it is not possible to declare a constructor Private.
    False.

  48. True or false, you can declare a parametered constructor in both structs and classes.
    True.

  49. If you have a method with parameter names which are the same as those of the classes fields, which keyword do you use to indicate you wish to reference the fields of the current class. What does that
    keyword refer to within an instance method?
    this. It refers to the current object.

  50. What happens to the default parameterless constructor if you provide a parametered constructor for a class?
    It disappears, and cannot be called. You must explicitly provide your own if you wish to have a parameterless constructor.

  51. What happens to the default parameterless constructor if you provide a parametered constructor for a struct?
    Nothing.

  52. Since a constructor doesn't have a return value, what must it do to indicate failure if it's unable to initialize an object correctly.
    It must throw an exception.

  53. What is it called when you explicitly call one constructor from another, in order to help with object initialization?
    A constructor initializer.

  54. If you don’t explicitly provide a constructor initializer, what does the constructor call instead?
    The parameterless constructor of that class's base class.

  55. How many static constructors can a class have?
    One.

  56. The code in a static constructor is guaranteed to be executed before what?
    Any instance constructor and before any static member of the class is accessed.

  57. True or false, a static constructor is a good place to put initialization of static fields?
    True.
Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote:
#20
Static members of a type must always be prefaced with what?
The name of the type for which they belong, followed by a period.


I don't understand the answer to this question. Can someone please show me by example?

Thanks in advance.
I rate users. Its just not having much impact as my rating is low...
Quote: Original post by foolios
Quote:
#20
Static members of a type must always be prefaced with what?
The name of the type for which they belong, followed by a period.


I don't understand the answer to this question. Can someone please show me by example?

Thanks in advance.


public class ClassName{    public static string Version = "3.12";        public override string ToString(){        return( "ClassName [" + ClassName.Version + "]" );    }}// later...ClassName ClassInstance = new ClassName();Console.WriteLine( ClassInstance );     // 'ClassName [3.12]'Console.WriteLine( ClassName.Version ); // '3.12'


Though this isn't actually correct. Within the class itself (the ToString above) the ClassName qualifier isn't required (at least in MSVC# 2005).
So the answer should read:
The name of the class to which they belong, followed by a period.
Right?
I rate users. Its just not having much impact as my rating is low...

This topic is closed to new replies.

Advertisement