C# Workshop - Week 6 Review Questions

Started by
4 comments, last by Spoonbender 16 years, 8 months ago
Chapters 18 - 19 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. Why do all classes and structs automatically have "ToString" and "Equals" methods?
  2. When a new class derives from an existing class, what does it inherit?
  3. Inheritance provides a structured way to do what?
  4. All structures implicitly inherit from which base class? Can a struct inherit from any other class?
  5. True or False, as classes derive from other classes, they tend to get more general and less specific, covering a wider array of functionality.
  6. What is a 'base class'
  7. If you do not specify a base class when creating a new class, which class is assumed the base class?
  8. What is multiple inheritance? Is it supported in C#?
  9. Are constructors inherited by a derived class?
  10. When a constructor calls another constructor to help initialize the object it is called a what? If no constructors are specified to be called, which is assumed?
  11. When an object is created, which constructor in the constructors is called first, which is called last?
  12. Does a constructor with parameters automatically call the parameterless constructor of the current class?
  13. True or False, before the code in a constructor is executed, all the fields with initializers in their declaration are set.
  14. A property, field, or method which is declared protected is accessible in which classes?
  15. True or False, It's best to make as much of a class public as possible to allow easy access?
  16. C# provides implicit conversions from any type to which other types?
  17. True or False, All reference types require the same amount of space on the Stack.
  18. What is upcasting?
  19. Does upcasting happen implicitly or does it require an explicit cast.
  20. True or false, an object's run-time type is stored somewhere on the heap along with the other data about the object.
  21. True or False, an object's declared type never changes, but it's run-time (actual) type may.
  22. True or False, an object's actual type cannot be just any type, but must be either the declared type, or one of its descendants.
  23. What is downcasting?
  24. Does downcasting happen implicitly or does it require an explicit cast?
  25. When downcasting, if the actual type doesn't match the declared type or one of it's descendants at run-time, what happens?
  26. What's the major difference between the 'as' operator and traditional casting?
  27. What does the GetType method return?
  28. What's the difference between the method GetType and the typeof operator?
  29. Is it possible to check for equality between the value returned from typeof and GetType?
  30. True or false, the 'is' operator never throws an exception if given a null object, it simply returns false.
  31. What's the main difference between the 'is' operator and the GetType method when checking for equality.
  32. True or False, Methods, Properties, and Fields of a class can all be made Virtual.
  33. What is the virtual keyword used for?
  34. Is it possible to change a method's accessibility or return type when overriding a virtual method?
  35. True or False - Methods, Properties and Fields of a class can all be redefined in a base class
  36. What optional keyword is used to indicate an intentional redefinition of a base class member?
  37. What's the difference between virtual and non-virtual methods when called from a base class object? What is this technique called?
  38. The process of hooking up a virtual method with the method of the appropriate run-time type is called what?
  39. Can an abstract class object be instantiated?
  40. Can you declare variables whose declared type is an abstract class?
  41. If you want to force every derived class of an abstract class to have to implement a specific method, what keyword do you use with that method?
  42. True or false, an abstract method is implicitly virtual, and thus, the declared type of the method is never called, always the run-time type.
  43. What is a sealed class?
Good Luck! [Edited by - JWalsh on August 7, 2007 3:14:18 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
My answers are here!
I found this weeks question to be quite tough, I'm not sure if I have a firm enough grasp of the concepts this week.

Need more clarification on the following:

2. When a new class derives from an existing class, what does it inherit?
It inherits everything from the base class except its constructors.

3.Inheritance provides a structured way to do what?
To avoid rewriting code.???

4.All structures implicitly inherit from which base class? Can a struct inherit from any other class?
Object, No.

6. What is a 'base class'?
The class from which all other classes can be derived from.

10. When a constructor calls another constructor to help initialize the object it is called a what? If no constructors are specified to be called, which is assumed?
It is called a constructor initializer, if no constructors are specified then the base classes parameterless constructor is assumed.

12.Does a constructor with parameters automatically call the parameterless constructor of the current class?
No it doesn’t.

13. True or False, before the code in a constructor is executed, all the fields with initializers in their declaration are set.
???I assume yes.

16.C# provides implicit conversions from any type to which other types?
Any ancestral types.

20. True or false, an object's run-time type is stored somewhere on the heap along with the other data about the object.
True.

21. True or False, an object's declared type never changes, but it's run-time (actual) type may.
True.

29. Is it possible to check for equality between the value returned from typeof and GetType?
Yes.

31. What's the main difference between the 'is' operator and the GetType method when checking for equality.
GetType only returns the type that the object is, is will return true if the object is of that type or of any type derived from that type.

35. True or False - Methods, Properties and Fields of a class can all be redefined in a base class
False only methods and properties can be redefined in a base class.

37. What's the difference between virtual and non-virtual methods when called from a base class object? What is this technique called?
When called from a base class object virtual methods called the most derived version of the method, with non virtual methods the method called is the method associated with the declared type.
The technique is called polymorphism.

38. The process of hooking up a virtual method with the method of the appropriate run-time type is called what?
Late-binding.
How about them apples?

When a new class derives from an existing class, what does it inherit?
All the non-private methods, properties and fields.


Something that I am trying to understand about why only non-private methods, properties and fields are inherited. Why is this so?

It appears that since most fields and such are private by default and since most people try to enforce encapsulation by making things private unless something makes it necessary to not do so. That these practices will make most classes not inheritance friendly. Or why not?

So this also needs to be taken into consideration when you are making a class, that you may need to make fields public because there may be want for it by some other user to inherit from it.

This seems like a contradiction of sorts because we want to protect fields but then we also want to make our code as reuseable as possible.

How do we deal with such complications?
I rate users. Its just not having much impact as my rating is low...
Quote:Original post by foolios

When a new class derives from an existing class, what does it inherit?
All the non-private methods, properties and fields.


Something that I am trying to understand about why only non-private methods, properties and fields are inherited. Why is this so?

It appears that since most fields and such are private by default and since most people try to enforce encapsulation by making things private unless something makes it necessary to not do so. That these practices will make most classes not inheritance friendly. Or why not?

So this also needs to be taken into consideration when you are making a class, that you may need to make fields public because there may be want for it by some other user to inherit from it.

This seems like a contradiction of sorts because we want to protect fields but then we also want to make our code as reuseable as possible.

How do we deal with such complications?


Inheritance isn't everything. The first priority of classes is to let you create robust abstractions whose internals you need not to know about in order to interact with them correctly.

Using inheritance correctly is far from easy. If overriding some members here and there were the solution to safely reuse code, it'd be indeed a good thing to open up as much as possible for inheritance. But that's simply not the case.

You have to take into account two sides:

  • the creator of the superclass;

  • the user of the superclass which will define a subclass and override members.



Opening too much has adverse effects for both sides. The creator of the superclass will need to take into account that every overridable member can be overriden, and that his class must still work if that happens. So, he wants structure/order: he will want to set some things in stone about his class, things he knows for sure will not be interferred with externally, and he can use these guarantees to write a correctly behaving class. He just wants something to count on, and hence will make them hidden/un-overridable.

The subclasser wants structure too: he doesn't want the superclass to break because of his interference, but he also doesn't want to have to know about the internals of the superclass. The more things open up for him, the more he has to know about.

Technically, it is indeed better to open up as much as possible for inheritance, but it needs to happen in a very controlled way. The more you open up, the more complex the class becomes to define and use. When you write a class, you must assume that it just isn't safely subclassable. Only when explicitly thinking of where and how to allow inheritance and designing it carefully, you will end up with a inheritance-friendly class. That's the reason C# doesn't make your methods virtual by default (contrary to java or Eiffel): the language designers acknowledged the fact that overridability needs to be carefully planned.

[opinion]
I dislike inheritance very much and I would prefer to get completly rid of it, as it makes designing your classes simply too complex. It simply gets absurd once you start explicitly working with contracts. In my opinion, there are far easier, robuster ways of reusing code.
[/opinion]
Quote:Original post by foolios
Quote:
When a new class derives from an existing class, what does it inherit?
All the non-private methods, properties and fields.


Something that I am trying to understand about why only non-private methods, properties and fields are inherited. Why is this so?

It appears that since most fields and such are private by default and since most people try to enforce encapsulation by making things private unless something makes it necessary to not do so. That these practices will make most classes not inheritance friendly. Or why not?

Because the most fundamental goal with classes is to hide the internals of the class. Even derived classes shouldn't need to know everything about their base classes. Of course, sometimes you want the derived class to be able to access properties, fields or methods of the base class, and so they have to be made non-private.
But generally, the base class provides some kind of abstraction by hiding its inner workings, and the derived class should usually use that abstraction, rather than poking through the base class's internals.

And of course, when you need to expose bits of the base class to the derived class, you make the field/property/method protected, because you probably still don't want it to be public, and private is too restrictive.

Quote:So this also needs to be taken into consideration when you are making a class, that you may need to make fields public because there may be want for it by some other user to inherit from it.

Yes. You have to consider which parts of the class should be completely abstracted away (make them private. No one else has any business messing with them), which parts make up the public interface (make them public, so they can be used from outside the class), and finally, which parts aren't part of the public interface, but should still be available when extending the class through inheritance.

This topic is closed to new replies.

Advertisement