Quiz9 and Solutions

Quiz9 and Solutions - Computer Techniques for Electronic...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Techniques for Electronic Design II (ESE 224) Quiz for chapter 9 Period: 10 minutes Multi‐Choice Problems 1. Which of the following statements about pointer are correct? (1.5) a) A pointer is an object that holds the memory address of another object. b) The ‘&’ operator is used to get the address of an object. c) The size of a pointer depends on the base type of the pointer. d) A pointer should be initialized or assigned before being used. e) Since the name of an array is a pointer to the first element, we could point it to other objects unrelated to the array. 2. Which of the following statements about dynamic memory allocation are correct? (1.5) a) All memories used by a program should be allocated dynamically by the programmer. b) A successful call of new operator would return the ending of the allocated memory block. c) Generally, all memory allocated dynamically should be freed before the program is terminated. d) The array defined by statement ‘char test[10];’ could also be freed by delete operation. e) Dynamically allocated memory is defined at runtime. 3. Which of the following statements about standard template library (STL) are correct? (1.5) a) List is a class template whose elements are linked by pointers. b) Items of queue could be added / deleted from any position of the queue. c) The later an item is added into a stack, the earlier it would be retrieved. d) Items within a list could be of different data types. e) Programmer defined data type (i.e. class) could also be the item data type of stack. Memory Snapshot Problem int a(2) b(3); int *ptr1, *ptr2; ptr1 = &a; ptr2 = ptr1; *ptr1 = b; b = 2; 4. What is the value of the memory pointed by ptr1 and ptr2? (2) Programming Problem 5. Write a function to allocate a one‐dimensional integer array of size 5. Assign random values (less than 100) to each element, compute the sum of all elements, and free the allocated memory after computation. (3.5) Solutions 1. A, B, D 2. C, E 3. A, C, E 4. 3, 3 5. int dynamic_sum(void) { int *ptr; int sum(0); ptr = new int(5); srand(1234); for(int i = 0; i < 5; i++) { ptr[i] = rand()%99; sum += ptr[i]; } delete ptr; return sum; } ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online