CS113: Fall 1996 MIDTERM STUDY QUESTIONS This study sheet is composed of questions at various levels. It is intended to give you some practice for the midterm, but it is not intended to indicate the length (it is too long), the distribution, the emphasis, or the format of the midterm. Focus on the content of the questions; if you have the knowledge and skills to answers these questions, you will probably have the knowledge and skills to answer the questions on the midterm. You should try to solve them all, although you may not need to write out detailed solutions to every one of them. If you can not solve one, do not stay in that question for the rest of your life. Move to the following one. The problems do not necessarily get harder as you proceed from top to bottom. This review contains 2 parts: 1) A list of questions taken from the text books 2) A list of exercises G O O D L U C K !!! = = = = = = = = === 1) QUESTIONS FROM THE TEXTS =========================== Review Questions from Roberts: Chapter 2: 4, 6, 7, 16 - 23 Chapter 3: 4, 7, 9, 14 - 18, 22 Chapter 4: 2, 3, 4, 11 - 14, 17, 21, 22 Chapter 5: 2, 5, 6, 7, 9, 10, 14, 17 Chapter 7: 2 - 7, 10, 16, 17 Chapter 8: 2, 3, 4, 9, 10, 16 Chapter 9: 3, 4, 5, 8, 12, 13, 14, 16, 17, 19 Chapter 10: 7 - 11 Chapter 11: 1 - 14 Chapter 13: 1 - 17, 19 - 21, 23 - 27 Chapter 14: 1 - 10 Chapter 15: 1 -5, 7, 8, 9, 12, 15, 16, 17 Chapter 16: 2 - 7, 12 Chapter 17: 8, 9 Review Questions (RQ) and Exercises (E) from Standish 2.3: RQ 1, 3, 4, 5, 9, 12 7.3: RQ 1--4 7.6: RQ 1, 2 8.5: RQ 1 8.5: E 1--4 2) OTHER QUESTIONS AND EXERCISES ================================ Note: I may add additional questions to this list between now and the midterm. Last change: 15 Oct 9:30 am. 1. Give the function header for each of the following. a) Function hypotenuse that takes two double-precision floating point arguments side1 and side2, and returns double-precision floating point value. b) Function IntToFloat that takes an integer argument Number and returns a floating point value. 2. Label the elements of a 5-by-3 two dimensional array SALES to indicate the order in which they are set to zero by the following program segment. for (row = 0; row <= 2; row++) for (column = 0; column <= 4; column++) SALES[column][row] = 0; 3. What does the following program do? #include #include "genlib.h" #include "sipmio.h" int Mystry2 (const char *); void main( void ) { string line; printf("Enter a string: "); line = GetLine(); printf("\n%d", Mystry2(line)); } int Mystry2 (const char *s) { int x = 0; for ( ; *s != '\0'; s++) x++; return x; } 4. What does the following program do? #include #define SIZE 4 int WhatIsThis (int [], int); void main( void ) { int a[SIZE]={1,2, 3, 4}; printf("\nThe result is: %d", WhatIsThis(a, SIZE)); } int WhatIsThis (int b[], int num) { if (num == 1) return b[0]; else return b[num -1] + WhatIsThis(b, num - 1); } 3 5. Given the equation y = ax + 7, which of the following, if any, are correct C statements for this equation? Why? a) y = a * x * x * x + 7; b) y = a * x * x * (x + 7); c) y = (a * x) * x * x + 7; d) y = a * (x * x * x) + 7; e) y = a * x * (x * x + 7); Which of the correct ones, if any, is clearest? 6. What does the following program do, if the user inputs two numbers 6 and 9. #include main() { int i, j, x, y; printf ("enter two integers : "); scanf("%d%d", &x, &y); for (i=1; i<= y; i++){ for (j=1; j<= x; j++) printf("@"); printf("\n"); } } [A note on I/O: On the midterm, you will not be required to write code which uses scanf (although you may). You should be able to read scanf in its simplest forms, as in this example. For file I/O, it will suffice to use fprintf() and getc(),putc(),ungetc(). You may use the functions in simpio.h in any code you write.] 7. Write a program that reads in a binary number (as a string) and converts it to a decimal number. Your program should take care of sign of the number. If the input number is negative, the final decimal number should also be negative. You can assume that the number will be entered with no spaces. 8. Write a program that reads input characters one by one until end of file. As it reads it should convert lower case alphabets to upper case, and vice-versa, and print them. Tabs should be printed as 3 spaces. At the end it should print the number of blanks, tabs, newlines in the input data. 9. A number n is said to be a perfect number if it is equal to the sum of all its divisors, excluding itself. For example, the divisors of 6 are 1, 2, and 3. Since 6 = 1 + 2 + 3, it is a perfect number. Write a function called IsPerfect that takes an integer as argument and returns 1 if it is a perfect number and 0 otherwise. Write a main program that reads in a number n and prints all perfect numbers between 1 an n by using the function IsPerfect. 10. Which of the following you can NOT use as identifiers and why? 4th star* scanf one-to-1 11. What is the minimum size of a character array to store the following string: "\a\"This is a string\"\n" 12. What gets printed? Explain. (If you need, you can assume the address of i to be 2279468.) int i = 3, *p = &i; printf( "%d %d %d %d\n", p, *p + 7, **&p, p - (p-2)); 13. Which statements are legal, which is not, why and why not? int i = 9, a[3], *p; char b[4], *q; a = p; p = a; q = &i; q = b; q = b[2]; q = &b[2]; q = b + 2; q = &b[0] + 2; 14. Write a program to draw the following pictures. Start by writing a function that prints the n-th line of the object and use it to print the picture required. (Use for-loops, while, switch.) a) Parallelogram: ******** ******** ******** ******** b) Pyramid: 0 *1* ++2++ $$$3$$$ ****4**** +++++5+++++ $$$$$$6$$$$$$ ...... 15. Write a program that prints out the sum of the first 50 odd numbers. 16. Write a program that reading a text file counts the number of punctuation characters there are in the it. 17. What is printed by the following piece of code? void main( void ) { int x; int a[10] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3 }; int *p; p = &x; x = 5; *p = 8; p = a + 5; printf( "%d, %d\n", x, *p ); } 18. Suppose you are the client using a package for handling character stacks which exports to you only the following typedefs and function prototypes: typedef charStackCDT *charStackADT; charStackADT NewIntStack(void); char Pop(charStackADT); void Push(charStackADT, char); char StackDepth(charStackADT); bool StackIsFull (charStackADT); How can you implement _as_the_client_ functions with following prototypes? bool StackIsEmpty(charStackADT); /* this loads the stack with the characters from a string */ void LoadStack(charSTackADT, string); /* this prints a representation of the stack, but leaves the stack * itself unchanged. notice, you don't have a "peek" function. */ void DisplayStack(charSTackADT); How might the answers be different if you were the implementor rather than a client? 19. a) What is the purpose of lines like #ifndef _turtle_h #define _turtle_h #endif b) What else is #define used for?