Fall 2002 Test #2
CSE1320 Sections 003 and 501
Tuesday, October 8 2002 Dr. Tiernan
Name: Section:
Student ID: Keyword:
Instructions:
1. Fill in your name, student ID, and section above. Choose some keyword for yourself that I can use to anonymously post grade information. If you do not choose a keyword, I will use the last four digits of your student ID to post grade information if needed.
2. This is a closed book, closed notes, NO CALCULATOR test.
3. The test is worth a total of 100 points. The value for each question is given either at the top of that section of questions or in curly braces to the right hand side of the question. There are extra credit questions at the end of the test worth an additional 10 points total.
4. NO CHEATING!
Multiple choice questions are worth four {4} points each. Please circle the letter indicating your choice for the answer.
1. Recursion
A) is programming concept which maps to certain problem types
B) has specific operators in C
C) is the same as iteration
D) is a method for solving complex equations
2. Which of the following is NOT typically used with pointer data types?
A) &
B) |
C) *
D) ->
3. A contiguous set of memory locations belonging to the same data structure could be:
A) an array containing a single data type
B) a struct containing multiple data types
C) a dynamically allocated space
D) any of the above
4. Given the code fragment , give the values for the expressions below. {8 points total ; 2 each}
char *str1 = “This is a test.”;
char str2[] = {‘D’,’o’,’ ‘,’n’,’o ’,’t ’,’ ’,’p ’,’a ’,’s ’,’s ’,’ ’,’g ’,’o ’,’.’,’\0’};
char *str3 = str1;
char *str4 = str2;
i) strlen(str1)
ii) strcmp(str2, str1)
iii) *(str3 + 6)
iv) str4[4]
5. Write a code fragment to allocate space for 25 floating point numbers. Assign the values from 75.0 to 99.0 to the elements. Make sure to declare all variables that are used. Do not use array notation. {8}
6. Write a switch statement to do the following. Assume that the days of the week are numbered from 1 = Sunday to 7 = Saturday. Using the day of the week as the input to the switch, print “Yippee!” for Friday, Saturday and Sunday. Print “Ugh.” for Monday. Print “Working…” for all the weekdays Monday through Friday. If the input is not between 1 and 7, print a message to the user that says “Ooops.” Make sure to declare any variables you use. {9}
7. Write the necessary code to:
A) Declare a structure with the following members: a string containing the title of an artwork, a string containing the last name of the artist, a string containing the first name of the artist, a character representing one of the following (P = painting, H = photograph, R = print, S= sculpture, C= collage, D=drawing, M=mixed media, O=other), a character representing the material used (L=oil paint, T=watercolor, A=acrylic, M=marble, W=wood, B-bronze, O=other), a date of completion, a floating point number representing the price of the artwork and a pointer to another structure of this same type. {8}
B) Use the data type from part A above to declare two variables with the following values. Make sure to initialize all parts of the structure. {9}
artwork name: |
Esker Trefoil Torus |
Improvization 26 - Oars |
artist last name: |
Ferguson |
Kandinsky |
artist first name: |
Helaman |
Vassily |
artwork type: |
S |
P |
artwork media: |
B |
L |
artwork date: |
1994 |
1912 |
artwork price: |
$14,000.00 |
$22,000.00 |
C) Use the data type from part A above to declare two pointers. Set one pointer to point to the information about HelamanFerguson and one pointer to point to the information about Vassily Kandinsky. Change the pointer in the Helaman Ferguson data structure to point to the Kandinsky information. {8}
D) Write a code fragment to use the pointers from part C to print out the names and prices of the two pieces of art. {6}
8. Write a function that has two parameters, an array of floating point numbers and an integer indicating the number of values in the array. In the function, test to make sure the number of array values is greater than 0. If it is, use a do while loop to print the values in the array. If not, give a message to the user indicating an error and return the value –1. {10}
9. Use the two functions below to answer the following questions:
int ver (int stay)
{
if (stay < 2)
return stay;
else
return stay + stay + ver(stay –1) –1;
}
int disco (int go)
{
if (go = = 1)
return 1;
else if (go < 1)
return 0;
else return ver(go) * go;
}
A) What does the function disco return for an input of 4? {8}
B) Describe in words the mathematical operation performed by the function disco together with the function ver. {Hint: try inputs of 3 and 2 as additional examples to help determine the function’s actions.] {6}
10. Given the declarations below, use the addressing operator and casts to pass alpha and beta to functions uno and dos in the code fragment {8}
void main (void)
{
int uno ( int *aleph, int *barem );
int dos ( int eine[], float zwei );
int alpha[10];
int beta;
/* assume legal C code here that assigns values to alpha and beta*/
uno( , ); /*use alpha as first parameter */
dos( , ); /*and beta as second for both*/
Extra Credit questions - Worth two {2} points each.
XC1. Which of the following could be used as a string?
A) char str[]= {‘c’,’a’,’t’};
B) char *str = {‘c’,’a’,’t’};
C) char str[3] = “cat”;
D) char *str = “cat”;
XC2. Which of the following intentionally crashes your program?
A) break
B) exit
C) return
D) stop
XC3. The notation %s and ‘\0’ have to do with what C construct?
A) switch
B) special character array
C) struct
D) subroutine
XC4. Which of the following does not relate to using pointers?
A) dereference,
B) indirection
C) independence
D) addressing operator
XC5. Make a rhyme with the following:
[ANY answer will receive 2 points]
Recursion makes programs go round and round,