Class: No. Name: Part1. True/False Questions (30pts) 1. Function fscanf cannot be used to read data from the standard input. ANS: False. Function fscanf can be used to read from the standard input by including the pointer to the standard input stream, stdin, in the call to fscanf. 2. You must explicitly use fopen to open the standard input, standard output and standard error streams. ANS: False. These three streams are opened automatically by C when program execution begins. 3. A program must explicitly call function fclose to close a file. ANS: False. The files will be closed when program execution terminates, but all files should be explicitly closed with fclose. 4. If the file position pointer points to a location in a sequential file other than the beginning of the file, the file must be closed and reopened to read from the beginning of the file. ANS: False. Function rewind can be used to reposition the file position pointer to the beginning of the file. 5. Function fprintf can write to the standard output. 6. Data in sequential-access files are always updated without overwriting other data. ANS: False. In most cases, sequential file records are not of uniform length. Therefore, it is possible that updating a record will cause other data to be overwritten. 7. It is not necessary to search through all the records in a random-access file to find a specific record. 8. Records in random-access files are not of uniform length. ANS: False. Records in a random-access file are normally of uniform length. 9. Function fseek may only seek relative to the beginning of a file. ANS: False. It is possible to seek from the beginning of the file, from the end of the file and from the current location in the file. 10. A self- referential structure is used to form dynamic data structures. 11. Function malloc is used to dynamically allocate memory. 12. A stack is a specialized version of a linked list in which nodes can be inserted and deleted from anywhere of the list. ANS: False. A stack is a specialized version of a linked list in which nodes can be inserted and deleted only from the start of the list. 13. A NULL pointer normally indicates the end of a data structure. 14. A queue is referred to as a FILO data structure. ANS: False. A queue is referred to as a FIFO data structure. 15. The pointer to the next node in a linked list is referred to as a link. 16. Function free is used to reclaim dynamically allocated memory. 17. A queue is a specialized version of a linked list in which nodes can be inserted only at the start of the list and deleted only from the end of the list. 18. A stack is referred to as a LIFO data structure because the first node inserted is the last node removed. ANS: False. A stack is referred to as a LIFO data structure because the last node inserted is the first node removed. 19. The nodes of a binary tree contain two link members. 20. The first node of a tree is the root node. Part2. Multiple Choice Questions (40pts) 1. Which of the following is false? a) Storage of data in memory variables is temporary; all such data is lost when a program terminates. b) Computers store files on primary storage devices, typically disk storage devices. c) Files are used for permanent retention of data (typically large amounts of data). d) Storage of data in memory arrays is temporary; all such data is lost when a program terminates. 2. Which statement is true? a) The smallest data item in a computer is called a byte. b) Digits, letters and special symbols are referred to as memory words. 1
c) Fields are composed of records. d) Bytes are composed of bits. 3. Function reads one character from a file. a) fgetcharacter b) fgetc c) fgetchar d) fgetbyte 4. Function feof. a) forces an end-of-file condition b) determines whether the end-of-file indicator is set for a file c) sets the end-of-file indicator for a file d) flushes the contents of the file from the current position to the end 5. If an error occurs while opening a file in any mode, fopen. a) causes program termination b) returns NULL c) issues the message can t open file d) diagnoses the error, waits five minutes and retries Ans: (b) 6. Function fscanf is equivalent to function scanf, except that fscanf. a) can have only a single argument. b) can read only from standard streams. c) can read only from open streams. d) receives as an argument a file pointer for the file from which the data is read. 7. The rewind statement causes. a) the disk to stop spinning, then spin in reverse until it repositions to the beginning of the file b) the contents of the file to be erased c) a program s file position pointer to be repositioned to the beginning of the file (i.e., byte 0) d) works with tape files but not disk files 8. Which of the following is false? (a) Data can be inserted in a randomly accessed file without destroying other data in the file (b) There is more than one way to implement randomly accessed files. (c) Randomly accessed files cannot be accessed directly without searching through other records. (d) Data stored previously can be updated or deleted without rewriting the entire file. 9. What is the significance of the 1 in the following statement? fwrite( &number, sizeof( int ), 1, fptr ); (a) It specifies that the file is to be opened for updating. (b) It specifies the number of elements in the array that should be written to disk. (c) It specifies the byte size of the element being written to disk (d) none of these 10. Which statement is true? a) fseek searches for a record by its record key. b) fseek sets the file position pointer to a specific byte location in the file. c) fseek moves the read-write head on the disk to the location on disk that corresponds to a specific location in a file. d) fseek must immediately follow each call to fread or fwrite. 2
11. represent waiting lines; insertions are made at the back (also called the tail) and deletions are made from the front (also called the head) of a. a) Linked lists b) Queues c) Stacks d) Binary trees 12. facilitate high-speed searching and sorting of data, efficient elimination of duplicate items and compiling expressions into machine language. a) Linked lists b) Queues c) Stacks d) Binary Trees 13. A self-referential structure contains a member that points to. (a) integer, a structure of the same structure type (b) pointer, an integer (c) integer, an integer (d) pointer, a structure of the same structure type 14. Function malloc takes as an argument the number of bytes to be allocated, and returns a pointer of type to the allocated memory. a) NULL b) void c) void * d) NULL * 15. If no memory is available malloc returns a pointer. a) self b) NULL c) void d) NULL * 16. Not returning dynamically allocated memory when it is no longer needed can cause a system to run out of memory prematurely. This is called a(n). a) outage b) memory hole c) memory access violation d) memory leak 17. is not an advantage of linked lists when compared to arrays. (a) Dynamic memory allocation (b) Efficient insertion and deletion (c) Direct access to any list element (d) Efficient use of memory 18. Which of the following statements about stacks is incorrect? (a) stacks can be implemented using linked lists. (b) stacks are first in, first-out (FIFO) data structures. (c) new nodes can only be added to the top of the stack. 3
(d) the last node (the bottom) of a stack has a null (zero) link. 19. A queue receives the following commands (in pseudo-code): enqueue 4, 6, 8, 3, 1 dequeue 3 elements enqueue 3, 1, 5, 6 dequeue 2 elements What number is at the front of the queue? (a) 3 (b) 4 (c) 5 (d) 6 ANS: (a) 20. Select the incorrect statement. Binary trees (regardless of the order in which the values are inserted into the tree) (a) always have multiple links per node. (b) can be sorted efficiently. (c) always have the same shape for a particular set of data. (d) are nonlinear data structures. Part3. Questions and Answers (30pts) 1. Write a single statement to accomplish each of the following. Assume that each of these statements applies to the same program. (10pts) a) Write a statement that opens the file "oldmast.dat" for reading and assigns the returned file pointer to ofptr. ANS: ofptr = fopen( "oldmast.dat", "r" ); b) Write a statement that opens the file "newmast.dat" for writing (and creation) and assigns the returned file pointer to nfptr. ANS: nfptr = fopen( "newmast.dat", "w" ); c) Write a statement that reads a record from the file "oldmast.dat". The record consists of integer accountnum, string name and floating-point currentbalance. ANS: fscanf( ofptr, "%d%s%f", &accountnum, name, ¤tbalance ); d) Write a statement that writes a space-separated record to the file "newmast.dat". The record consists of the integer accountnum, string name and floating-point currentbalance. ANS: fprintf( nfptr, "%d %s %f", accountnum, name, currentbalance ); 2. Please write four functions GetNode, FreeNode, PrintList, and FreeList that can manage the following list data structure. Note: If we call PrintList(rootList); The output should be 100-->235-->440-->888 Node *GetNode() Node *pnode; pnode = (Node*) malloc(sizeof(node)); if(pnode) pnode->nextnode = NULL; return pnode; 4 void FreeNode(Node *ptr) free(ptr);
void PrintList(Node *ptrroot) void FreeList(Node *ptrroot) while(ptrroot!= NULL) Node* ptemp; printf("%d%s", ptrroot->data, ptrroot->nextnode? "-->" : ""); while(ptrroot!= NULL) ptrroot = ptrroot->nextnode; ptemp = ptrroot->nextnode; FreeNode(ptrRoot); printf("\n"); ptrroot = ptemp; 3. Please write down the following binary tree s preorder, inorder, and postorder traversal sequences. (10pts) inorder: A B C D E F G H I preorder: F B A D C E G I H postorder: A C E D B H I G F 5