Fall 2003 Test #2
CSE1320 Section
501
Tuesday, October 28, 2003 Dr.
Tiernan
Name: Section: 501
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 cannot post your 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 and short answers are worth varying points each. Please circle the letter indicating your choice for the answer.
1) Give at least two C commands Dr. Tiernan says NEVER (or almost never) to use and explain why {4}
2) Match the following storage classes to their descriptions {10 points total ; 2 each }
A) auto C) typedef E) extern
B) static D) register
Allows a variable to be linked between files
Can be used along with another storage class to limit access to within a single file
Default storage class for all variables and functions
Helps
make C programs more portable
Suggestion to the computer how to store a variable
3) Which of the following is not true about bit fields? {4}
A) They can only be created as parts of structs
B) They can be used with the addressing operator
C) An unnamed bit field moves the next element to an addressable boundary
D) Bit fields can be mapped over other variables to access individual bits
4) What is alpha if its definition is: char *(* alpha)(int, double [], int (*)(void)) {4}
A) alpha is a function with three parameters, int double array and function with input of void and return type of int and alpha returns a pointer to string
B) alpha is a variable of type pointer-to-pointer-to-char which returns a function
C) alpha is a pointer to a function with three parameters, int, array of double, and ptr to function with input type void and return type int and alpha returns a string
D) alpha is a string pointer with three parameters, int, double, and void
5) Give a real-life example of a stack and a queue but do NOT use the examples discussed in class. {4}
STACK:
QUEUE:
6) Define the following briefly: {12 points total ; 3 each}
scope
storage
class
global
variable
local
variable
7) Write a recursive function that implements the following mathematical function on an input of integer n: {10}
2 * pep(n - 1) - 1 for n > 0
pep( n ) = 0 for n > 0
2 * pep(n + 1) + 1 for n < 0
Write code fragments to answer the following questions:
8) Declare an enumerated type called pumbaa and give it the following values: simba, nala, mufasa, zazu, timon, and sarabi. List these values in such a way that the enumerated values match the alphabetical ordering of the elements. {6}
9) Using the type defined above, declare an array dis which is of that type and that can hold at least five elements. {4}
10) Declare a struct type neymal that contains the following: apumbaa enumerated type value, an integer to hold an age in years, a bit field of 10 bits to create masks, a single character indicating types of food eaten (‘c’ carnivore, ‘h’ herbivore, ‘o’ omnivore), and a string to hold a description. {8}
11) Using the struct type neymal defined above, write a switch statement. Use the enumerated type member as the control for the switch and the cases. Create a different case for each possible value of the enum type and for each possible value print a different combination of pieces of data from the switch. Do not print the same piece of data in more than three cases in the switch. For the default, print “I loathe the Lion King.” [Ex: for value timon you could print the age in years and the bit field value.] {12}
12) Give the declaration you would use for your main routine in order to pass command line parameters to your program and return an integer code to the OS. {6}
13) Given the function below, answer the following questions.
(Assume elemstruct is a typedef for the underlying struct for an element and elemtype is a typedef for the data value in the struct)
elemstruct *doto( elemstruct *now, elemstruct element, elemtype newvalue)
{
element.value = newvalue;
element.after = now->after;
now->after = &element;
return &element;
}
A) What abstract data type is being manipulated? {4}
B) What would be a more meaningful name for the function doto? {4}
C) Write the corresponding function to undo what function doto does to the ADT. {8}
Extra Credit questions - Worth two {2} points each unless stated otherwise.
XC1) Write a prototype declaration for the following:
An array of five pointers to functions with one integer input and a string output
XC2) Which of the following defines scope for C variables?
A) Length of time a variable exists during program
B) Size of the variable in bytes
C) Instrument that determines it properties
D) Parts of the program which can see the variable
XC3) Which of the following is not true about unions in C?
A) Only one valid member at a time
B) Members vote to make a collective decision
C) Up to programmer to distinguish meaning
D) Data in a member is only meaningful to a single member
XC4) Which of the following would be considered poor structured programming?
A) Putting an exit statement in an emergency cutoff routine to handle critical cases
B) Using a goto to change the control flow at a specific program point
C) Building a switch statement with breaks in some cases and fall through in others
D) Designing the controls of a loop such that all testing occurs in the loop header
XC5) Currently Dr. Tiernan uses obscure Star Trek and Star Wars references in C class. What other obscure pop culture references should she use and why should she use it? Relate your idea somehow to C and give the name or title of the pop culture icon.
{ ANY ANSWER will receive TWO (2) POINTS}