Created
December 12, 2013 04:55
-
-
Save chintanparikh/7923360 to your computer and use it in GitHub Desktop.
C Questions for 2110
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3. Write basic linked list library functions for a doubly-linked list | |
of integers with head and tail pointers. You should include: | |
AddToFront | |
AddToBack | |
RemoveFromFront (Returns value removed) | |
RemoveFromBack (Returns value removed) | |
AddInOrder | |
Delete (Deletes first occurrence of target) | |
DeleteAll (Deletes all occurrences of target) | |
Traverse | |
FreeList | |
5. Examine the code below and fill out the table below: | |
int a; | |
static int b; | |
int main(int argc, char *argv[]) | |
{ | |
int c; | |
static int d; | |
char *p = "hello"; | |
const int x = 42; | |
int *intptr; | |
static int *intptr2; | |
intptr = malloc(100*sizeof(int)); | |
intptr2 = malloc(200*sizeof(int)); | |
... | |
<------------------------- Location ----------------------> | |
<----------- Static ---------> <---- Lifetime ---> | |
Item Code Constant Changeable Stack Heap Block Program | |
a | |
b | |
argc | |
argv | |
c | |
d | |
p | |
*p | |
i.e. "hello" | |
x | |
intptr | |
*intptr | |
intptr2 | |
*intptr2 | |
6. Write integer swap. Function will swap two integers in memory. | |
Show how to call it. For each line of code draw a picture of the stack | |
after executiuon of that line. | |
7. Given any typical sort algorithm implemented in C that sorts | |
integers convert it to operate like qsort. You don't need to | |
memorize the sort algorithm. Note: This question is not about sorting | |
but rather function pointers. | |
8. Assume you have a function: size_t getAlloc(void *ptr) that will | |
give you the size of a chunk of memory which has ben dynamically | |
allocated given a pointer to the block of memory. Write: | |
void *realloc(void *ptr, size_t newsize) | |
{ | |
} | |
Make sure your function handles the special cases: ptr = NULL and | |
size = 0; | |
9. Pointer tracing | |
Trace: | |
a b p1 p2 | |
int a = 99; | |
int b = 2; | |
int *p1 = &a; | |
int *p2 = &b; | |
p1 = p2; | |
a = 6; | |
b = 7; | |
*p2 = 67; | |
Now show the output from this statement: | |
printf("%d %d %d %d\n", a, b, *p1, *p2); | |
10. Compare and contrast the . and the -> When should each be used? | |
What is meant by a deep copy versus a shallow copy of a struct? Give an | |
example of each. | |
11. Trace the following code: | |
int arr[] = {12, 65, 78, 34, 59, 222}; | |
int arrsize = sizeof(arr)/sizeof(arr[0]); | |
int *p = arr + arrsize - 1; | |
for(i=0; i<arrsize; i++, p--) | |
{ | |
printf("%d %d &d\n", i, arr[i], *p); | |
} | |
3Pa. What does Make do. Your answer should include at least 3 major | |
things. | |
3Pb. What are the common flags used with gcc and give an example of | |
each. | |
4P. Write a macro that will produce a 15 bit GameBoy color | |
(0bbbbbgggggrrrrr) given values for red, green and blue. Your macro | |
should be written to only include the rightmost 5 bits of each of | |
the input parameters. | |
5P. Write a function that will swap two integers: | |
______ swap(____________________________________________________) | |
{ | |
} | |
6P. Explain when to use a '.' and when to use a '->' with structures. | |
Give examples. Explain what is meant by a shallow copy and a deep | |
copy. Give examples. | |
7P. Implement yoursort which will be exactly like qsort except you can | |
use whatever sort algorithm you choose. recall the prototype for | |
qsort looks like this: | |
void qsort(void *base, size_t nmemb, size_t size, | |
int(*compar)(const void *, const void *)); | |
As an example show how to sort an array of ints, an array of | |
pointers to strings (ordered by a lexical comparison of the | |
strings pointed to by the pointers), an array of structs | |
containing a field called weight and an array of pointers to | |
structs that contain a field called height. | |
8P. Assume you have a function called: | |
size_t getAllocation(void *pointer); | |
which when called passing the address of a block of memory | |
allocated by malloc will return the size of that block in bytes. | |
Write realloc making sure it handles the special cases (size = 0 | |
or pointer = NULL) | |
9P. Create structures that will allow you to implement a linked list | |
of ints. It can be singly linked list. Write a function that will | |
allow you to initialize a list, insert at the head, insert at the | |
tail, delete a node (first occurrence) and to free up the list. | |
10P. Write some code that will declare variables that illustrate all | |
the various combinations of storage class and type modifiers (all | |
will be ints): | |
-static variable visible in other files (show the main declaration | |
and also a declaration in another file) | |
-static variable visible in just one file | |
-automatic variable | |
-variable that the compiler won't let you change (initialized to 7) | |
-variable whose value might be changed by something other than your | |
program | |
-An array you want to be stored in the cartridge of a GBA. | |
2P. What is the relationship between the sizes of the basic c types | |
(char, short, int, long) in general? | |
What are the sizes of these types on the GBA? | |
Are pointers the same size as the things they point to? Explain | |
your answer. | |
3P. You come across the following line of code: | |
#define SAFEFREE(ptr) do {free(ptr); ptr = NULL} while(0); | |
Explain why the programmer did this and what problem is he trying | |
to avoid. | |
5P. Why can we not copy one array to another with a simple statement | |
but we can copy arrays that are in structs but cannot | |
automatically copy them? | |
What is/are the differences between defining structs versus | |
defining structs in typedefs them? | |
What has to be true for two structures to be considered the same | |
type | |
6P. Write a generic swap program which will swap any two data items | |
that are the same size. | |
void generic_swap(void *a, void *b, size_t size) | |
{ | |
7P. Suppose you have a UFO struct: | |
typedef struct | |
{ | |
int altitude; | |
int velocity; | |
int number_aliens; | |
char *color; | |
} UFO; | |
You have an array of these structs and you want to sort it by | |
velocity. | |
You will be using qsort: | |
void qsort(void *base, size_t nmemb, size_t size, | |
int (*compare)(const void *, const void *) ); | |
Write a compareUFO velocity function that can be passed to qsort | |
as a comparison function to help qsort sort your array. Don't | |
worry about ascending or descending...either is fine. | |
8P. Given a function: | |
size_t getAlloc(void *ptr); | |
that will return the size of the block of dynamically allocated | |
memory that ptr is pointing to that was allocated by malloc or | |
calloc, etc.; | |
Write: | |
void *myRealloc(void *ptr, size_t size); | |
Your function must correctly handle all special cases (ptr == NULL, | |
size = 0) and must NOT call realloc. | |
9P. Where are the three places the word static can be used in a c program. | |
Explain what each does. | |
To what does the term auto refer In the context of C programming)? | |
What are the 4 main areas of memory? | |
How do things get stored in each of these areas? | |
What are the two different lifetimes a variable can have? | |
What is the relationship between how a variable is declared and its lifetime? | |
10P. Answer the following briefly | |
Why would you use a function pointer? | |
Why would you use a header file? | |
What does a structure do? | |
Why would you split your code into separate C files. | |
What do you put in a header file? | |
What is the purpose of a frame pointer | |
1P. Assume that you have a function getSize: | |
unsigned int getSize(void *ptr); | |
This function will return the size of any block of memory allocated | |
by malloc, realloc, calloc, etc. | |
Using this function write realloc. Make sure your function properly | |
handles the special cases: realloc(NULL, n) and realloc(ptr, 0). | |
2P. Given the following structure: | |
typedef struct burrito | |
{ | |
int beans; | |
char rice; | |
char beef; | |
short cheese; | |
double salsa; | |
unsigned short lettuce; | |
} BURRITO; | |
Write a function that will take in an int n and allocate contiguous | |
space for n Burrito's. The function will return a pointer to the space. | |
4P. Explain what each of these C statements does: | |
LLNode * (*fp) (LLNode *, void *); | |
typedef LLNode * (*fp) (LLNode *, void *); | |
5P. Write swap | |
6P. Write a basic generic binary search tree library with the following functions: | |
insert, inorderTraversal, search, freeTree | |
The BSTNode should be like this: | |
typedef struct BSTNode | |
{ | |
void *data; | |
BSTNode *left; | |
BSTNode *right; | |
} BSTNode; | |
Note: insert, traversal and search will need to take in function | |
pointers. | |
7P. Write a function that will take in an unsigned short, an unsigned char | |
and an int. if the int is 0 then the return value of the function will | |
be the short with the char in the high order (left byte). If the | |
int is a 1 then the return value of the function will be the short with | |
the char in the low order (right byte). | |
Also write a function similar to the above idea that will take in an | |
unsigned int and and int. If the int is 0 the function will return | |
the high order byte of the short. If 1 then the low order byte will be | |
returned. | |
8P. Examine the following structures: | |
typedef struct { | |
int x; | |
int y; | |
char tag[8]; | |
} tagPoint; | |
typedef struct { | |
int x; | |
int y; | |
char *label; | |
} labelPoint; | |
tagPoint a, b; | |
a.x = 42; | |
a.y = 27; | |
strcpy(a.tag, "Point Q"); | |
b = a; | |
Draw a picture of memory showing all data items and their | |
relationship to one another. | |
labelPoint r,s; | |
char *txt = "Datum W"; | |
r.x = 19; | |
r.y = 22; | |
r.label = txt; | |
s = r; | |
Draw a picture of memory showing all data items and their | |
relationship to one another. | |
9P. Show the equivalence of for loops and while loops. | |
Why are do-while loops not equivalent? | |
What do the break and continue keywords do? | |
1P. Miscellaneous Short Answer Questions | |
a. Explain what makes void pointers unique | |
b. Is it possible to have a void ** pointer? Is it subject to the same restrictions as | |
void pointers? | |
c. Compare and contrast the * and & operators. What restrictions exist to their use? | |
d. How do you use increment and decrement operators with pointers and dereferencing? | |
What syntax rules come into play? | |
e. Which is better: | |
if(ptr != NULL) | |
or | |
if(ptr) | |
f. What goes into a header file and what does not? Why? | |
g. How many meanings are there for the keyword static? What do each of them mean? | |
h. What are the advantages of always checking the return value of malloc | |
i. When it is safe for a function to return a pointer? What things would be | |
reasonable for it to point to? What things would be bad for it to point to? | |
j. Give examples of using const with both static and auto variables. | |
Does : | |
const int *ptr; | |
mean that ptr is constant or what it is pointing to is constant? | |
Is there a way of making both these things happen? | |
6P. Write integer swap. Function will swap two integers in memory. | |
Show how to call it. For each line of code draw a picture of the stack | |
after executiuon of that line. | |
7P. Given any typical sort algorithm implemented in C that sorts | |
integers convert it to function like qsort. You don't need to | |
memorize the sort algorithm. Note: This question is not about sorting | |
but rather function pointers. | |
9P. Variable arguments (You may use the man page for varargs found here: | |
http://linux.die.net/man/3/stdarg) | |
Write a function int sumv(int n, ...) that will take in a number n and | |
then n ints (a variable number). The function will return the sum of the | |
ints not inluding n. | |
10P. Pointer tracing | |
Trace: | |
a b p1 p2 | |
int a = 99; | |
int b = 2; | |
int *p1 = &a; | |
int *p2 = &b; | |
p1 = p2; | |
a = 6; | |
b = 7; | |
*p2 = 67; | |
Now show the output from this statement: | |
printf("%d %d %d %d\n", a, b, *p1, *p2); | |
12P. Trace the following code: | |
int arr[] = {12, 65, 78, 34, 59, 222}; | |
int arrsize = sizeof(arr)/sizeof(arr[0]); | |
int *p = arr + arrsize - 1; | |
for(i=0; i<arrsize; i++, p--) | |
{ | |
printf("%d %d &d\n", i, arr[i], *p); | |
} | |
13P. What does this function do? | |
int main(int argc, char *argv[]) | |
{ | |
if (argc == 0) | |
return 0; | |
printf("%s\n", argv[0]); | |
main(--argc, ++argv); | |
} | |
Hint: Run it like this: | |
./main roses are red! | |
4P. Write qsort using any sort algorithm you wish (Bubble sort, Merge | |
sort, Shell sort, Quick sort, etc.). Show how you would use it to | |
sort an array of ints and also how you would use it to sort a | |
collection of strings given an array of pointers to the strings. | |
5P. Compare and contrast using char arrays versus char pointers and | |
malloc to store strings of characters. Discuss and give examples | |
of initialization and use. | |
6P. Is this macro correct? If not correct it | |
#define ABS(x) x>=0 ? x : -x; | |
7P. What does sizeof return? | |
Why is using sizeof important? | |
On a GBA, what is the size of a char, a short, an int, a long, a | |
float and a double? | |
8p. Compare and contrast the storage classes auto and static. Also | |
define and explain the purpose of extern, const, volatile and | |
register. | |
9P What different approaches could you use if you wanted to write a | |
function that will sum some of its arguments (and that would be a | |
variable number of arguments.) There are at least two approaches. | |
Write code examples of each. | |
9P What different approaches could you use if you wanted to write a | |
function that will sum some of its arguments (and that would be a | |
variable number of arguments.) There are at least two approaches. | |
Write code examples of each. | |
11P Write a function that will swap two pointers whose addresses are | |
passed in. | |
CS2110 Fall 2013 Practice Final | |
1. Write a macro called SCRAMBLER that will swap the first and third | |
byte of an int. | |
2. Write a function called extractByte that will be given the address of | |
an array of 16-bit shorts and an int called index. The int specifies | |
which byte of the array to extract and return. Here is the tricky | |
part: Assume this computer is Little Endian. This means the right- | |
hand byte of each short is a lower address that the left-hand byte so | |
the bytes are arranged like this: | |
Short Bytes | |
0 1 0 | |
1 3 2 | |
2 5 4 | |
But the way we want "index" to be interpreted is like this: | |
Short Bytes | |
0 0 1 | |
1 2 3 | |
2 4 5 | |
3. Write a function that will swap two integers. Write down a set of | |
rules for how a programmer could write a function that would swap | |
two of anything given the type e.g. swap two floats. | |
4. Explain the difference between shallow and deep copy using structs. | |
Create a struct where this would be an issue. Show code that would | |
shallow copy the structs and code that would deep copy the structs. | |
5. Trace the following code and show the output: | |
int x = 42; | |
int y = 38; | |
int *p1 = &x; | |
int *p2 = &y; | |
int **ppa = &p1; | |
int **ppb = &p2; | |
*p1 = 56; | |
**ppb = 22; | |
ppa = ppb; | |
p2 = p1; | |
printf("%d %d %d %d %d %d\n", x, y, *p1, *p2, **ppa, **ppb); | |
6. Consider this code: | |
FILE1.c | |
============================================================ | |
int a = 78; | |
static int b; | |
const int c = 88; | |
extern int d = 44; | |
int main() | |
{ | |
int e = 12; | |
static int f = 12; | |
int *g = malloc(40*sizeof(int)); | |
int h; | |
// More code | |
} | |
FILE2.c | |
============================================================ | |
int d; | |
int somefunc(int p) | |
{ | |
const int n = p + 6; | |
// More code | |
} | |
Answer these questions: | |
Note: Possible answers to "Where in memory?" are: | |
static (read/write) | |
static (read only) | |
stack | |
heap | |
code | |
Where in memory is the variable a? | |
Where in memory is the variable b? | |
Where in memory is the variable c? | |
Where in memory is the variable d? | |
Where in memory is the variable e? | |
Where in memory is the variable f? | |
Where in memory is the variable g? | |
Where in memory is the variable h? | |
Where in memory is the variable p? | |
Where in memory is somefunc located? | |
Where is the memory pointed to by g? | |
Which line of code is illegal? | |
What is the value of b? | |
7. Explain what each of the following statements does: | |
int (*fp) (int, int, unsigned short); | |
typedef int (*fp) (int, int, unsigned short); | |
8. Write functions that do the following things to a | |
linked list of ints: | |
add to the front | |
add to the tail | |
add in order | |
search and return a pointer to the node containing a target | |
value or NULL if not found. | |
free up the list | |
You may implement the list as you wish: singly or doubly linked | |
head pointer or head and tail pointer or list struct, etc. | |
Make sure you show all necessary structure definitions. | |
9. Write strcmp, strcat, strcpy, strdup. | |
For the purposes of this question you may look at the man pages | |
for the listed functions. | |
10. Write a function that will return a pointer to a dynamically | |
allocated array of ints. When called the function will prompt | |
the user for an int. If the user enters an int the array will be | |
resized to accomodate the new value. If the user enters end of file | |
(CTRL/D) the function will return the pointer to the array. Note: | |
the function will have no parameters. You may assume the user will | |
enter valid integer values. | |
1P. There are several different areas of memory where things may be | |
stored: Static - Code, Static - constant, Static - changeable, stack, | |
heap. Of course, there are also device registers but they generally have | |
to be accessed directly by memory so this question is more about the | |
things C does for us automatically. | |
For the above items show an example with code of how you would store | |
something in that part of memory and give the lifetime of that item. | |
Explain very briefly the purpose of this section of memory: | |
Static - Code | |
Static - Constant | |
Static - Changeable | |
Stack | |
Heap | |
2P. Malloc returns a pointer to a contiguous block of memory of a size | |
passed to malloc as an argument. Realloc can resize a previously | |
allocated block of memory (of course it doesn't really resize the | |
original block of memory), allocate a block of memory or free up a block | |
of previously allocated block of memory. | |
If you were asked to write realloc could you do it? If yes, go ahead and | |
do it. If no, what would you need? Assume you have that and then write | |
it. Make sure your realloc handles the special cases. | |
3P. Typedef a struct that will define a Binary Tree Node. It should hold | |
a void pointer to the data and pointers to the right and left children. | |
Now write a function AddInOrder that will insert nodes into the tree. | |
You can have your function take in a pointer to a compare function. | |
Finally write a function that would free up the tree. | |
6P. What is the difference between: | |
int f1(int a, char c) | |
{ | |
c = c << 8; | |
a = a | c; | |
return a; | |
} | |
int f2(int a, char c) | |
{ | |
a = a | (c<<8); | |
return a; | |
} | |
7P. Compare and contrast: | |
char *p = "Hello"; | |
and | |
char a[] = "Hello"; | |
8P. You know how to write a function that will swap two integers. | |
How would you write code to swap two pointers | |
void swapPointers( ) | |
{ | |
} | |
int main() | |
{ | |
int i; | |
int a1[] = {4, 5, 6, 7}; | |
int a2[] = {100, 200, 300, 400}; | |
int *p1 = a1; | |
int *p2 = a2; | |
/* Call swapPointers here */ | |
swapPointers( ); | |
for(i=0; i<4; i++) | |
{ | |
printf("%d\n", p1[i]); | |
} | |
for(i=0; i<4; i++) | |
{ | |
printf("%d\n", p2[i]); | |
} | |
return 0; | |
} | |
Output will be: | |
100 | |
200 | |
300 | |
400 | |
4 | |
5 | |
6 | |
7 | |
P. Write a function in C that will take in four unsigned ints: | |
target, source, width and start. The function will take the | |
rightmost 'width' bits of 'source' and insert them into 'target' | |
(replacing whatever bits are there) starting at position 'start'. | |
The bits are numbered so that the low order bit (rightmost) is | |
bit 0. | |
Example: | |
3 2 1 | |
10987654321098765432109876543210 | |
target = tttttttttttttttttttttttttttttttt | |
source = abcdefghijklmnopqrstuvwxyz123456 | |
width = 5 | |
start = 20 | |
3 2 1 | |
10987654321098765432109876543210 | |
The function will return: ttttttttttt23456tttttttttttttttt | |
8P. Write a generic swap function that will take in the addresses of | |
the two items to be swapped and the size of the items to be | |
swapped. | |
Show how you would use this function to swap two OREO structures. | |
typedef struct | |
{ | |
int top; | |
char fill; | |
int bottom; | |
} OREO; | |
9P. Assume you have a function size_t getAlloc(void *ptr) which | |
returns the the number of bytes dynamically allocated by malloc | |
that are pointed to by ptr. | |
Write realloc making sure to handle the special cases | |
void *realloc(void *ptr, size_t newsize); | |
10P. Create necessary structs to implement a linked list of integers. | |
Write a function to free up a linked list made of the structures | |
you implemented. | |
11P. Write a program that will sort a bunch of animal names and an | |
array of ints using qsort. | |
Explain what each of these is and what each of them does? | |
Note: Your answer should not include the words, "...a function | |
pointer that returns..." Function pointers don't return anything. | |
They point! | |
a.) LLNode * (*fp) (LLNode *, void *); | |
b.) typedef LLNode * (*fp_t) (LLNode *, void *); | |
12P. Explain the purpose and give an example of correct usage of each | |
of these flags for gcc | |
-g -c -o -O -E -D -l -L -I -ansi | |
-pedantic -Wall -Werror -S -save-temps | |
6P. Explain what stack smashing is and how to prevent it. | |
8P. Create a binary search tree (BST) node that will contain an int | |
(the data) and the appropriate pointers to the left and right | |
sub-trees. | |
Now write the following functions: | |
Insert: Given the root pointer of a BST and an int, inserts the | |
int into the tree maintaining the BST property of the tree. | |
Search: Given the root pointer of a BST tree and an int returns | |
a 1 if a node containing the int is in the tree and a 0 if not. | |
Inorder: Given the root pointer of a BST performs an inorder | |
traversal of the tree and prints out the value of each node. | |
FreeTree: Frees up all dynamically allocated nodes of a BST | |
given the root pointer. | |
Note: In any case where it says "Given the root pointer of a | |
BST" your code may pass in the address of the root pointer as | |
opposed to just the pointer itself. | |
10P. You will encounter many students who will say that pointers and | |
arrays are the same thing. Will you nod knowingly and say, | |
"Right on!" Or will you say, "Well, not exactly..." and then | |
proceed to explain the differences? If you choose to explain the | |
differences...what are they? Include in your answer an | |
explanation of how READ ONLY memory figures into the whole | |
scheme of things especially with literal strings. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment