Sunteți pe pagina 1din 14

CSCI 270

Sample of my Exam 2 int x = 16; int * p = &x;

1. Given these declarations:

For each of these expressions, tell whether its value is an int or an address or the expression is invalid (circle your choice): int int address address invalid invalid a) x b) &p

2. What is the output of the following sequence of statements? int *a, *b, *c; a = new int; *a = 1; b = new int; *b = 2; cout << *a << << *b << endl; // output is _______ _______ c = new int; a = c; *a = *b; cout << *a << << *b << << *c << endl; // output is _______ _______ _______ 3. The only number that can be directly assigned to a pointer variable is a. -1 b. 0 c. 1 d. 9 4. Given these global declarations: struct TVShow { string title; int channel; }; typedef TVShow * TVPtr;

Write a function called GetShow, dynamically allocate a variable of type TVShow, assign to it values for your favorite show, and return the address of the structure (you can use any method you want as long as the caller has access to the address of the new structure). Assume there will be enough memory to allocate the structure (you dont have to check).

2
These declarations for a node in a singly linked list are used for questions 5 - 9. Please note that this is not a class, and there are no functions defined for manipulating a node variable. A pointer value of / in a diagram means NULL. struct node { int data; node * next; }; node *A, *B, *C, *D, *E, *F, *G;

5. Assume that a linked list has been created, but you dont know what it looks like. Write statements to do each of the following actions. You can assume that any variable you are asked to use has been assigned an appropriate value. Assume that each statement is independent of the others. a. You dont know what B is pointing to. Make B point to the same node that A is pointing to.

b. Read a value from the keyboard into the data field in the node pointed to by A.

6. Modify the diagram to show the effect of executing these statements in sequence in a program (cross out data values which are changed and rewrite the new values; show what the pointers are now pointing to): A B C | | | ________ ________ ________ ________ _________ | 2 -|-->| 5 -|-->| 4 -|-->| 6 -|-->| 8 / |

B = A; A = C->next; A->data = B->data;

Given this linked list to be used for questions 7 and 8: A | ______ | 10 -|---> B | ______ ______ | 20 -|--> | 30 -|--> C | _____ ______ |45 -|--> | 50 -|--> D | ______ _______ | 80 -|--> | 90 / |

7. Evaluate each of these expressions. It is possible that an expression may be invalid or its value unknown. Write the value of each expression, or write ? if the expression cannot be evaluated. a) A->next == B _____________ b) C->data _____________

c) D 8.

_____________

Remember that pointers E, F, and G are unassigned and available; you may use them in any way that might help you, but be sure to show how values are assigned for them. Using the original list for each problem, write code to accomplish these tasks. You are writing for this specific list with known values -- no searching is necessary. For example, if I asked you to change the value of the data in the second node so that it is double its current value, you might code: B->data = B->data * 2; because you can see that B is pointing to the second node. a) Replace the data value of 30 with the data value of the last node.

b) Create a new node, assign it a data value of 100 and insert it at the end of the list so that this new node becomes the last node.

9. A singly linked list of unknown length is pointed to by A. Write code which will delete all the nodes in the list and leave A with a value of NULL. You may use any of the other pointer variables B, C, D, E, F, and G (they currently have no values assigned).

10. In a. b. c. d. e.

a list class the _______ function is called whenever a list object is passed by value. constructor destructor operator= copy constructor copy

11. The dot operator has a lower priority than the dereference operator. a. true b. false 12. A program contains this declaration: and this function call: Write the prototype for CFunction: double x; CFunction (&x);

CSCI 270 1. int address

Possible Answers for Sample of my Exam 2 invalid a) x int address invalid b) &p

2. // output is ___1___ ___2___ // output is ___2___ __2___ __2___ 3. The only number that can be directly assigned to a pointer variable is b. 0 4. TVShow * GetShow ( ) { TVShow * ptr = new TVShow; ptr->title = Numb3rs; ptr->channel = 11; return ptr; } or void GetShow (TVShow * & ptr) { ptr = new TVShow; ptr->title = CSI; ptr->channel = 11; }

5. a. Make B point to the same node that A is pointing to. B = A; b. Read a value from the keyboard into the data field in the node pointed to by A. cin >> A->data; 6. B = A; A = C->next; A->data = B->data; 7. A B B C A | | | ________ ________ ________ ________ _________ | 2 -|-->| 5 -|-->| 4 -|-->| 6 -|-->| 8 2 / |

a) A->next == B ___true______ b) C->data c) D ___45_______ _? (unknown) or E = B->next; F = D->next; E->data = F->data;

8. a) Replace the data value of 30 with the data value of the last node. B->next->data = D->next->data;

b) Create a new node, assign it a data value of 100 and insert it at the end of the list so that this new node becomes the last node. E = new node; E->data = 100; E->next = NULL; D->next->next = E; or E = new node; E->data = 100; E->next = NULL; F = D->next; F->next = E;

9. A singly linked list of unknown length is pointed to by A. Write code which will delete all the nodes in the list and leave A with a value of NULL. You may use any of the other pointer variables B, C, D, E, F, and G (they currently have no values assigned). while (A != NULL) { B = A; A = A->next; delete B; }

10. d. copy constructor 11. b. false 12. Write the prototype for CFunction: void CFunction (double * xPtr);

Maliks Self-Test Questions and Answers for Pointers and Dynamic Memory 1. In C++, the ____ operator returns the address of its operand. (p.795) A: % B: @ C: & D: * 2. In C++, a pointer ____. (p.812) A: cannot be passed as a parameter to a function B: can be passed as a value or reference parameter C: can be passed as a reference parameter only D: can be passed as a value parameter only 3. Pointer variables that contain the addresses of deallocated memory spaces are called ____ pointers. (p.807) A: dangling B: void C: null D: deleted 4. Given the statements: int x; int *p; the statement: p = &x; (p.795|796) A: assigns the address of x to p B: assigns the value of x to p C: assigns the address of x to the address of p D: assigns the value of x to the address of p 5. Given the statements: int x = 25; int *p; p = &x; *p = 55; the value of x after the statements have executed is ____. (p.796) A: p B: 25 C: 55 D: *p 6. In which situation is the copy constructor not executed? (p.823) A: When, as a parameter, an object is passed by reference B: When an object is declared and initialized by using the value of another object C: When, as a parameter, an object is passed by value D: When the return value of a function is an object 7. Which of the following is the general syntax for accessing a member variable using the operator ->? (p.801) A: pointerVariableName->classMemberName B: *pointerVariableName->classMemberName C: pointerVariableName->*classMemberName D: classMemberName->pointerVariableName 8. A declaration such as: int *p; allocates memory for ____. (p.797)

A: p only B: *p only C: both p and *p D: none of the above 9. Which of the following is true regarding the statement int* p, q;? (p.795) A: only p is a pointer variable B: p and q are pointer variables C: the statement is invalid D: p and q are the same data type

10. Some programmers use the named constant ____ to initialize pointer variables. (p.803) A: ZERO B: EMPTY C: VOID D: NULL 11. Unused memory space that cannot be allocated is referred to as a(n) _____. (p.806) A: void area B: memory leak C: garbage space D: heap 12. The member access operator arrow consists of ____. (p.801) A: a "less than" sign and a hyphen B: an "equal" sign and the "greater than" sign C: an asterisk and the "greater than" sign D: a hyphen and the "greater than" sign 13. When a program requires a dynamic variable, the operator ____ is used. (p.804) A: & B: allocate C: new D: -> 14. In C++, you declare a pointer variable by using a(n) ____. (p.794) A: double colon B: set of angular brackets C: ampersand D: asterisk 15. When a program no longer needs a dynamic variable, the operator ____ is used. (p.804) A: erase B: void C: -> D: delete 16. The general syntax to declare a pointer variable is ____. (p.794) A: * dataType identifier B: dataType *identifier C: dataType &identifier D: &dataType identifier 17. When you declare a pointer variable, you specify the ____ of the value to be stored in the memory location. (p.794)

A: range B: name C: data type D: identifier 18. Variables that are created during program ____ are called dynamic variables. (p.804) A: execution B: compilation C: linking D: building Answers for Maliks Self-Test Questions over Pointers & Dynamic Memory 1. In C++, the ____ operator returns the address of its operand. (p.795) A: % B: @ C: & D: * 2. In C++, a pointer ____. (p.812) A: cannot be passed as a parameter to a function B: can be passed as a value or reference parameter C: can be passed as a reference parameter only D: can be passed as a value parameter only 3. Pointer variables that contain the addresses of deallocated memory spaces are called ____ pointers. (p.807) A: dangling B: void C: null D: deleted 4. Given the statements: int x; int *p; the statement: p = &x; (p.795|796) A: assigns the address of x to p B: assigns the value of x to p C: assigns the address of x to the address of p D: assigns the value of x to the address of p

5. Given the statements: int x = 25; int *p; p = &x; *p = 55; the value of x after the statements have executed is ____. (p.796) A: p B: 25 C: 55 D: *p 6. In which situation is the copy constructor not executed? (p.823) A: When, as a parameter, an object is passed by reference B: When an object is declared and initialized by using the value of another object C: When, as a parameter, an object is passed by value D: When the return value of a function is an object 7. Which of the following is the general syntax for accessing a member variable using the operator ->? (p.801) A: pointerVariableName->classMemberName B: *pointerVariableName->classMemberName

C: pointerVariableName->*classMemberName D: classMemberName->pointerVariableName 8. A declaration such as: int *p; allocates memory for ____. (p.797) A: p only B: *p only C: both p and *p D: none of the above 9. Which of the following is true regarding the statement int* p, q;? (p.795) A: only p is a pointer variable B: p and q are pointer variables C: the statement is invalid D: p and q are the same data type 10. Some programmers use the named constant ____ to initialize pointer variables. (p.803) A: ZERO B: EMPTY C: VOID D: NULL 11. Unused memory space that cannot be allocated is referred to as a(n) _____. (p.806) A: void area B: memory leak C: garbage space D: heap 12. The member access operator arrow consists of ____. (p.801) A: a "less than" sign and a hyphen B: an "equal" sign and the "greater than" sign C: an asterisk and the "greater than" sign D: a hyphen and the "greater than" sign 13. When a program requires a dynamic variable, the operator ____ is used. (p.804) A: & B: allocate C: new D: -> 14. In C++, you declare a pointer variable by using a(n) ____. (p.794) A: double colon B: set of angular brackets C: ampersand D: asterisk 15. When a program no longer needs a dynamic variable, the operator ____ is used. (p.804) A: erase B: void C: -> D: delete 16. The general syntax to declare a pointer variable is ____. (p.794) A: * dataType identifier B: dataType *identifier C: dataType &identifier D: &dataType identifier

9
17. When you declare a pointer variable, you specify the ____ of the value to be stored in the memory location. (p.794) A: range B: name C: data type D: identifier 18. Variables that are created during program ____ are called dynamic variables. (p.804) A: execution B: compilation C: linking D: building

Maliks Self-Test Questions for Linked Lists 1. Which of the following set of statements will correctly delete the node from a linked list that q is pointing to, assuming that p is pointing to the node before it? (p.1032) A: q->link = p->link; p->link = q; delete q; B: q = p->link; p->link = q; delete q; C: q = p->link; p->link = q->link; delete q; D: q->link = p; p->link = q->link; delete q; 2. When building a linked list forward, which of the following statement(s) correctly inserts the first node, called newNode into a nonempty list? Assume that you have two pointer variables, first and last, that are initialized to NULL, and that newNode has been initialized with data and a link value of NULL. (p.1033) A: last = newNode; B: first->link = newNode; C: last->link = newNode; last = newNode; D: first = newNode; last = newNode; 3. The linked list ADT uses a(n) ____ search in its search function. (p.1050) A: binary B: indexed C: random access D: sequential 4. A linked list is a collection of components, called ____. (p.1024) A: nodes B: links C: elements D: indices 5. A _____ linked list is a linked list in which every node has a next pointer and a back pointer. (p.1072) A: single B: doubly C: unordered

D: ordered 6. If a pointer called current has the same value as the head pointer in a list, what is the result of the statement current = current->link;? (p.1026|1027) A: current has the same link as head B: current points to the first node in the list C: current points to the second node in the list D: current points to the head pointer 7. Which of the following set of statements will correctly insert newNode into a linked list after the node that p is pointing to? (p.1030) A: p->link = newNode; newNode->link = p->link; B: newNode = p; p->link = newNode; C: newNode = p->link; p = newNode; D: newNode->link = p->link; p->link = newNode; 8. _____ deletes all of the nodes in the list, leaving the list in an empty state (p.1075) A: Destroying the list B: Initializing the list C: Opening the list D: Emptying the list 9. In a linked list of four nodes, what is the value of the following expression? head->link->link->link->info (p.1027) A: the address of the fourth node B: the data of the fourth node C: NULL D: the link of the third node

10

10. When building a linked list forward, which of the following statement(s) correctly inserts the first node, called newNode into an empty list? Assume that you have two pointer variables, first and last, that are initialized to NULL, and that newNode has been initialized with data and a link value of NULL. (p.1033) A: newNode = first; first = last; B: last = newNode; C: first = newNode; D: first = newNode; last = newNode; 11. Which of the following statements assigns the pointer q to the link of a node called newNode? (p.1030) A: newNode->setLink(q); B: newNode.link(q); C: q = newNode->link; D: newNode->link = q; 12. In computer memory, the memory addresses of nodes are in ____ format. (p.1025) A: hexadecimal B: binary C: floating point D: integer 13. Which of the following is a loop statement to correctly traverse a linked list, assuming that current has the same initial value as head? (p.1028) A: while (current != NULL) B: while (head != NULL) C: while (current != head) D: while (head != current)

11
14. A linked list in which the last node points to the first node is called a ____ linked list. (p.1083) A: doubly B: singly C: circular D: round 15. When building a linked list forward, which of the following statement(s) correctly inserts the first node, called newNode into a nonempty list? Assume that you have two pointer variables, first and last, that are initialized to NULL, and that newNode has been initialized with data and a link value of NULL. (p.1033) A: last = newNode; B: first->link = newNode; C: last->link = newNode; last = newNode; D: first = newNode; last = newNode; 16. In a linked list of four nodes, what is the value of the following expression? head->link->link->link->info (p.1027) A: the address of the fourth node B: the data of the fourth node C: NULL D: the link of the third node 17. Every node in a linked list has ____ components. (p.1024) A: one B: two C: three D: four 18. A(n) ____ is also called a sequential list. (p.1024) A: array B: linked list C: doubly linked list D: circular list 19. The link component of each node in a linked list is a(n) ____ type. (p.1025) A: array B: integer C: pointer D: string 20. The component link of the last node of a linked list should hold ____. (p.1026) A: the value NULL B: the value -1 C: an undefined value D: the address of the node before it 21. When working with linked lists, we typically use _____ notation to advance a pointer to the next node in the list. (p.1027) A: floating B: link C: dot D: arrow

Answers for Maliks Self-Test Questions over Linked Lists 1. Which of the following set of statements will correctly delete the node from a linked list that q is pointing to, assuming that p is pointing to the node before it? (p.1032) A: q->link = p->link; p->link = q; delete q; B: q = p->link; p->link = q; delete q; C: q = p->link; p->link = q->link; delete q; D: q->link = p; p->link = q->link; delete q;

12

2. When building a linked list forward, which of the following statement(s) correctly inserts the first node, called newNode into a nonempty list? Assume that you have two pointer variables, first and last, that are initialized to NULL, and that newNode has been initialized with data and a link value of NULL. (p.1033) A: last = newNode; B: first->link = newNode; C: last->link = newNode; last = newNode; D: first = newNode; last = newNode; 3. The linked list ADT uses a(n) ____ search in its search function. (p.1050) A: binary B: indexed C: random access D: sequential 4. A linked list is a collection of components, called ____. (p.1024) A: nodes B: links C: elements D: indices 5. A _____ linked list is a linked list in which every node has a next pointer and a back pointer. (p.1072) A: single B: doubly C: unordered D: ordered 6. If a pointer called current has the same value as the head pointer in a list, what is the result of the statement current = current->link;? (p.1026|1027) A: current has the same link as head B: current points to the first node in the list C: current points to the second node in the list D: current points to the head pointer 7. Which of the following set of statements will correctly insert newNode into a linked list after the node that p is pointing to? (p.1030) A: p->link = newNode; newNode->link = p->link; B: newNode = p; p->link = newNode; C: newNode = p->link; p = newNode; D: newNode->link = p->link; p->link = newNode; 8. _____ deletes all of the nodes in the list, leaving the list in an empty state (p.1075) A: Destroying the list B: Initializing the list C: Opening the list D: Emptying the list

9. In a linked list of four nodes, what is the value of the following expression? head->link->link->link->info (p.1027) A: the address of the fourth node B: the data of the fourth node C: NULL D: the link of the third node

13

10. When building a linked list forward, which of the following statement(s) correctly inserts the first node, called newNode into an empty list? Assume that you have two pointer variables, first and last, that are initialized to NULL, and that newNode has been initialized with data and a link value of NULL. (p.1033) A: newNode = first; first = last; B: last = newNode; C: first = newNode; D: first = newNode; last = newNode; 11. Which of the following statements assigns the pointer q to the link of a node called newNode? (p.1030) A: newNode->setLink(q); B: newNode.link(q); C: q = newNode->link; D: newNode->link = q; 12. In computer memory, the memory addresses of nodes are in ____ format. (p.1025) A: hexadecimal B: binary C: floating point D: integer 13. Which of the following is a loop statement to correctly traverse a linked list, assuming that current has the same initial value as head? (p.1028) A: while (current != NULL) B: while (head != NULL) C: while (current != head) D: while (head != current) 14. A linked list in which the last node points to the first node is called a ____ linked list. (p.1083) A: doubly B: singly C: circular D: round 15. When building a linked list forward, which of the following statement(s) correctly inserts the first node, called newNode into a nonempty list? Assume that you have two pointer variables, first and last, that are initialized to NULL, and that newNode has been initialized with data and a link value of NULL. (p.1033) A: last = newNode; B: first->link = newNode; C: last->link = newNode; last = newNode; D: first = newNode; last = newNode; 16. In a linked list of four nodes, what is the value of the following expression? head->link->link->link->info (p.1027) A: the address of the fourth node B: the data of the fourth node C: NULL

D: the link of the third node 17. Every node in a linked list has ____ components. (p.1024) A: one B: two C: three D: four 18. A(n) ____ is also called a sequential list. (p.1024) A: array B: linked list C: doubly linked list D: circular list 19. The link component of each node in a linked list is a(n) ____ type. (p.1025) A: array B: integer C: pointer D: string 20. The component link of the last node of a linked list should hold ____. (p.1026) A: the value NULL B: the value -1 C: an undefined value D: the address of the node before it 21. When working with linked lists, we typically use _____ notation to advance a pointer to the next node in the list. (p.1027) A: floating B: link C: dot D: arrow

14

S-ar putea să vă placă și