C++ Workshop - Week 7 (Ch. 8) - Quizzes and Extra Credit

Started by
6 comments, last by Dbproguy 15 years, 11 months ago

Quizzes and Extra Credit for Week 7.

Post your answers to the Quizzes from Week 7 here rather than in the chapter thread, so as not to spoil things for the others. Chapter 8 Quiz 1. What is a pointer? 2. What is a variables address? 3. What operator do you use to get the address of a variable? 4. How would you declare a variable which is a pointer to a character? 5. How would you assign the address of a character to a character pointer variable? 6. What is it called when you use a pointer variable to obtain the data stored at the address it holds? 7. Why must we tell the compiler what type a pointer is, if all pointers are the same size? 8. What operator do you use to indirectly get the data stored at the address held by a pointer? 9. If you allocate memory on the heap while in a function, is the data available after the function returns? 10. Why are destructors so critical? 11. How would you allocate a ‘char’ on the heap and store the address so as to access the data later? 12. How would you set the value of the data in question 11 to the character ‘c’. 13. What keyword do you use to return the memory you’ve allocated back to the operating system? 14. What is it called when you have unused memory which is not freed until the progam ends either because the program author failed to release the memory when finished, or the address to the memory was lost, making it impossible to free. 15. In addition to allocating memory for an object, what function is always called on an object after it is created with ‘new’? 16. What function is called when you use the delete operator on an object, just before the memory is freed? 17. What is the long-hand way of accessing methods or data on an object indirectly via a pointer? Show an example to illustrate. 18. What is the short-hand way of doing the same thing as in question 17. 19. What is ‘this’? 20. What is a dangling pointer? 21. What’s different between “const int*”, “int * const”, and “const int * const”? How are these read? 22. If you have a pointer to a constant object, which member functions can be called on that object? Good luck! [Edited by - jwalsh on July 29, 2006 11:39:15 AM]
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
Sorry, I posted the quiz in the quiz answer thread and told people not to answer the quiz here. =) My mistake. PLEASE, answer the quiz here.
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

1. What is a pointer?
A variable that holds a memory address.

2. What is a variables address?
The location in memory that that variable is stored in.

3. What operator do you use to get the address of a variable?
The addresss-of operator: &

4. How would you declare a variable which is a pointer to a character?
char * pChar;

5. How would you assign the address of a character to a character pointer variable?
char * pChar = &someChar

6. What is it called when you use a pointer variable to obtain the data stored at the address it holds?
Dereferencing the pointer.

7. Why must we tell the compiler what type a pointer is, if all pointers are the same size?
So that the compiler knows the size of the variable being pointed to.

8. What operator do you use to indirectly get the data stored at the address held by a pointer?
The dereference operator: *

9. If you allocate memory on the heap while in a function, is the data available after the function returns?
Yes.

10. Why are destructors so critical?
They allow objects to "clean up" any memory that was allocated on the heap.

11. How would you allocate a ‘char’ on the heap and store the address so as to access the data later?
char * pChar = new char;

12. How would you set the value of the data in question 11 to the character ‘c’.
*pChar = 'c';

13. What keyword do you use to return the memory you’ve allocated back to the operating system?
delete

14. What is it called when you have unused memory which is not freed until the progam ends either because the program author failed to release the memory when finished, or the address to the memory was lost, making it impossible to free.
A memory leak.

15. In addition to allocating memory for an object, what function is always called on an object after it is created with ‘new’?
Its constructor.

16. What function is called when you use the delete operator on an object, just before the memory is freed?
Its destructor.

17. What is the long-hand way of accessing methods or data on an object indirectly via a pointer? Show an example to illustrate.
Dereference the pointer first, then use dot notation:
(*pSomeObject).someMethod();

18. What is the short-hand way of doing the same thing as in question 17.
pSomeObject->someMethod();

19. What is ‘this’?
A pointer to the object that uses it.

20. What is a dangling pointer?
A pointer that is pointing at garbage.

21. What’s different between “const int*”, “int * const”, and “const int * const”? How are these read?
const int* is a pointer to a const int - the variable pointed at cannot be changed.
int * const is a const pointer to an int - the pointer cannot be reassigned to another variable.
const int * const is a const pointer to a const int - the pointed-at variable cannot be changed and the pointer cannot be reassigned.

22. If you have a pointer to a constant object, which member functions can be called on that object?
Only const methods.

Are there going to be any exercises for this chapter?

1. What is a pointer?
A variable type that can hold an address.

2. What is a variables address?
The memory location of that variable.

3. What operator do you use to get the address of a variable?
The & operator.

4. How would you declare a variable which is a pointer to a character?
char * pChar;


5. How would you assign the address of a character to a character pointer variable?
pChar = &aChar


6. What is it called when you use a pointer variable to obtain the data stored at the address it holds?
Dereferencing.

7. Why must we tell the compiler what type a pointer is, if all pointers are the same size?
Because C++ is a type safe language. This allows the compiler to flag problems with types that do not match at compiletime, instead of finding out at runtime.

8. What operator do you use to indirectly get the data stored at the address held by a pointer?
The * operator.

9. If you allocate memory on the heap while in a function, is the data available after the function returns?
Yes, as long as you have a pointer to that memory.

10. Why are destructors so critical?
They allow you to 'clean up' any memory that was allocated for the given class.

11. How would you allocate a ‘char’ on the heap and store the address so as to access the data later?
char * pChar = new char;


12. How would you set the value of the data in question 11 to the character ‘c’.
*pChar = 'c';


13. What keyword do you use to return the memory you’ve allocated back to the operating system?
delete


14. What is it called when you have unused memory which is not freed until the progam ends either because the program author failed to release the memory when finished, or the address to the memory was lost, making it impossible to free.
A memory leak.

15. In addition to allocating memory for an object, what function is always called on an object after it is created with ‘new’?
The constructor.

16. What function is called when you use the delete operator on an object, just before the memory is freed?
The destructor.

17. What is the long-hand way of accessing methods or data on an object indirectly via a pointer? Show an example to illustrate.
Class * pClass = new Class;(*pClass).myMethod();


18. What is the short-hand way of doing the same thing as in question 17.
Class * pClass = new Class;pClass->myMethod();


19. What is ‘this’?
A pointer to the object your using 'this' in.

20. What is a dangling pointer?
A pointer that has the address of invalid memory.

21. What’s different between “const int*”, “int * const”, and “const int * const”? How are these read?
1. “const int*” is a constant pointer to an int
2. “int * const” is a pointer to a constant int
3. “const int * const” is a constant pointer to a constant int
The variable that has const in front of it is made constant.

22. If you have a pointer to a constant object, which member functions can be called on that object?
Methods that are marked const.
1. What is a pointer? A variable that contains the memory address of another variable. This allows the variable to be used as a reference.
2. What is a variables address? A "variable's" address is the value representing the location of the variable's data within RAM.
3. What operator do you use to get the address of a variable? The address of operator a.k.a the amperstand (&).
4. How would you declare a variable which is a pointer to a character? You specify the type, followed by an asterisk, followed by the variable name. The type in this case would be char. An example using the variable name "c" may look like this: char *c;
5. How would you assign the address of a character to a character pointer variable? Using the address of operator to retrieve the address you can: Specify the pointer variable, followed by an equal sign, followed by an amperstand, immediately (no spaces) followed by the variable contianing the character.
6. What is it called when you use a pointer variable to obtain the data stored at the address it holds? Dereferencing.
7. Why must we tell the compiler what type a pointer is, if all pointers are the same size? To ensure that dereferenced pointers behave correctly, and to ensure that dynamic memory allocation is handled correctly.
8. What operator do you use to indirectly get the data stored at the address held by a pointer? The dereferencing operator, or the asterisk (*).
9. If you allocate memory on the heap while in a function, is the data available after the function returns? Yes.
10. Why are destructors so critical? Destructors clean up after an object is no longer needed in memory. For objects with dynamically allocated data members this may involve free RAM that would otherwise be left to memory leaks.
11. How would you allocate a ‘char’ on the heap and store the address so as to access the data later? Assing the return value of "new char" to a pointer.
12. How would you set the value of the data in question 11 to the character ‘c’. Follow the dereferenced pointer with an equal sign, and then 'c'.
13. What keyword do you use to return the memory you’ve allocated back to the operating system? delete.
14. What is it called when you have unused memory which is not freed until the progam ends either because the program author failed to release the memory when finished, or the address to the memory was lost, making it impossible to free. A memory leak.
15. In addition to allocating memory for an object, what function is always called on an object after it is created with ‘new’? There is no such function, however in a perfect world delete would always be called.
16. What function is called when you use the delete operator on an object, just before the memory is freed? The object destructor.
17. What is the long-hand way of accessing methods or data on an object indirectly via a pointer? Show an example to illustrate. (*pointer).Method(); (*pointer).m_dataMember;
18. What is the short-hand way of doing the same thing as in question 17. pointer->Method(); pointer->m_dataMember;
19. What is ‘this’? Within a class the "this" operator is a pointer to the class object.
20. What is a dangling pointer? A pointer that continues to reference a memory location after the deallocation of that memory.
21. What’s different between “const int*”, “int * const”, and “const int * const”? How are these read? The first defines a non-const pointer to constant data. The second defines a const pointer to non-const data. The third defines a const pointer to const data.
22. If you have a pointer to a constant object, which member functions can be called on that object? Const member functions.
Programming since 1995.
1. What is a pointer?
A variable that holds a memory address.

2. What is a variables address?
The specific location in memory that that variable is held.

3. What operator do you use to get the address of a variable?
The '&' operator.

4. How would you declare a variable which is a pointer to a character?
char *pLetter

5. How would you assign the address of a character to a character pointer variable?
pLetter = &Letter

6. What is it called when you use a pointer variable to obtain the data stored at the address it holds?
Dereferencing.

7. Why must we tell the compiler what type a pointer is, if all pointers are the same size?
For debugging purposes; if the point points to an incorrect datatype C++ will flag it as an error.

8. What operator do you use to indirectly get the data stored at the address held by a pointer?
The '*' operator.

9. If you allocate memory on the heap while in a function, is the data available after the function returns?
Yes.

10. Why are destructors so critical?
It frees up memory after we are done with an object. If it didn't do this we would have mass amount of memory leakage.

11. How would you allocate a ‘char’ on the heap and store the address so as to access the data later?
chat *pLetter = new char;

12. How would you set the value of the data in question 11 to the character ‘c’.
*pLetter = 'c';

13. What keyword do you use to return the memory you’ve allocated back to the operating system?
The 'delete' keyword.

14. What is it called when you have unused memory which is not freed until the progam ends either because the program author failed to release the memory when finished, or the address to the memory was lost, making it impossible to free.
Memory leak.

15. In addition to allocating memory for an object, what function is always called on an object after it is created with ‘new’?
The constructor.

16. What function is called when you use the delete operator on an object, just before the memory is freed?
The destructor.

17. What is the long-hand way of accessing methods or data on an object indirectly via a pointer? Show an example to illustrate.
(*pRags).GetAge();

18. What is the short-hand way of doing the same thing as in question 17.
frisky->getage();

19. What is ‘this’?
A pointer to an individual object.

20. What is a dangling pointer?
A pointer that has been deleted but hasn't been nulled.

21. What’s different between “const int*”, “int * const”, and “const int * const”? How are these read?

“const int*”
The pointer is constant and can't be changed.

“int * const”
The upcoming variable the pointer is pointing to is constant and can't be changed.

“const int * const”
Both the pointer and the data the pointer is pointing to is constant and can't be changed.

22. If you have a pointer to a constant object, which member functions can be called on that object?
Only constant member functions.
Chapter 8.

1) What is a pointer?
A) A pointer is a variable that holds a memory address.

2) What is a variable address?
A) It is the place in memory that stores the variables type.

3) What operator do you use to get the address of a varialbe?
A) You use the address-of operator ( & ).

4) How would you declare a variable which is a pointer to a character?
A) char *pChar;

5) How would you assign the address of a character to a character pointer variable?
A) char myChar;
char *pChar = &myChar

6) What is it called when you use a pointer variable to obtain the data stored at the address it holds?
A) It is called indirection.

7) Why must we tell the compiler what type a pointer is, if all pointers are the same size?
A) Because the value pointed to by the address in the pointer must be of the same type as the original type.

8) What operator do you use to indirectly get the data stored at the address held by a pointer?
A) It is called the dereferencing/indirection operator ( * ).

9) If you allocate memory on the heap while in a function, is the data available after the function returns?
A) Yes, it is still available untill you explicitly state that you are done with it by freeing it. It is however NECESSARY to free your memory once you don't need it anymore.

10) Why are destructors so critical?
A) Because it is here where you will normally delete the pointer and set it to NULL ( 0 ).

11) How would you allocate a 'char' on the heap and store the address so as to access the data later?
A) char *pChar = new char;
pChar = 0;

12) How would you set the value of the data in question 11 to the character 'c'.
A) *pChar = 'c';

13) What keyword do you use to return the memory you've allocated back to the operating system?
A) You use the keyword 'delete'.

14) What is it called when you have unused memory which is not freed untill the program ends either because the program author failed to release the memory when finished, or the address to the memory was lost, making it impossible to free.
A) It is called memory leakage.

15) In addition to allocation memory for an object, what function is always called on an object after it is created with 'new'?
A) The constructor is called.

16) What function is called when you use the delete operator on an object, just before the memory is freed.
A) The destructor is called.

17) What is the long-hand way of accessing methods or data on an object indirectly via a pointer? Show an example to illustrate?
A) You must first dereference the pointer and call the dot operator on the object pointed to by the pointer. You then use the dereferenced value-the value being pointed to-along with the dot operator to access the members of the object. An example might be (*pPoint).GetAge();

18) What is the short-hand way of doing the same thing as in question 17?
A) It is the use of the class member access operator ( -> ), wich is created by typing the dash ( - ) immediatly followed by the greater-than symbol ( > ), C++ treats this as a single symbol.
And example might be pPoint->GetAge();

19) What is 'this'?
A) It is a "hidden" parameter for every class member function, it is a pointer that points to it's individual object.

20) What is a dangling pointer?
A) A dangling pointer is a pointer that is deleted with the delete keyword but NOT set to NULL after it's deleted. When you then try to use that pointer again without reassigning it, the result is unpredictable and, if you are lucky, your program will crash.

21) What's different between "const int*", "int * const", and "const int* const"? How are these read?
A) const int* = pointer to constant integer, it means that your integer value cannot be changed, but the pointer can point to another memory address.
int * const = constant pointer to integer, it means that your value can be altered, but not the address the pointer points to.
const int * const = constant pointer to a constant integer, it means that you cannot alter the address the pointer points to and you cannot alter the value of the integer.

22) If you have a pointer to a constant object, which member functions can be called on that object?
A) Only member functions which are also constant.


[Edited by - J0Be on June 3, 2007 2:57:54 PM]
1. What is a pointer?
A variable, which points to the address of another variable.

2. What is a variables address?
The address to the location in memory which the variable is stored.

3. What operator do you use to get the address of a variable?
The address-of operator (&)

4. How would you declare a variable which is a pointer to a character?
char * pPointer = &charVar

5. How would you assign the address of a character to a character pointer variable?
pPointer = &charVar

6. What is it called when you use a pointer variable to obtain the data stored at the address it holds?
indirection

7. Why must we tell the compiler what type a pointer is, if all pointers are the same size?
So it knows what kind of variable it's pointing to?

8. What operator do you use to indirectly get the data stored at the address held by a pointer?
The dereference operater (*)

9. If you allocate memory on the heap while in a function, is the data available after the function returns?
No. The data is still private.

10. Why are destructors so critical?
If not used, memory leaks will happen and basically you're memory will be as if it is leaking away.

11. How would you allocate a ‘char’ on the heap and store the address so as to access the data later?
char * pCharVar = new char;

12. How would you set the value of the data in question 11 to the character ‘c’?
*pCharVar = 'c';

13. What keyword do you use to return the memory you’ve allocated back to the operating system?
delete

14. What is it called when you have unused memory which is not freed until the program ends either because the program author failed to release the memory when finished, or the address to the memory was lost, making it impossible to free.
A memory leak.

15. In addition to allocating memory for an object, what function is always called on an object after it is created with ‘new’?
The default constructor.

16. What function is called when you use the delete operator on an object, just before the memory is freed?
The object's destructor.

17. What is the long-hand way of accessing methods or data on an object indirectly via a pointer? Show an example to illustrate.
Using the class member access operator (->)

18. What is the short-hand way of doing the same thing as in question 17.
Creating it on the free store

19. What is ‘this’?
It's the keyword which refers to the object's name in a class.

20. What is a dangling pointer?
A pointer which was deleted but not set to null.

21. What’s different between “const int*”, “int * const”, and “const int * const”? How are these read?
- The first, the pointer is a pointer to a constant integer, the value pointed can't be changed.
- The second, the pointer is a constant pointer to an integer, the value pointed can be changed but the variable can't point anywhere else.
- The third, the pointer is a constant pointer and points to a constant integer, so neither value can be changed.

22. If you have a pointer to a constant object, which member functions can be called on that object?
constant member functions.
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life

This topic is closed to new replies.

Advertisement