CS 354, version A Spring 2015 | Name:___________________ Section:________________ ID:___________________ | |
Exam 1 | ||
No electronic devices may be used while taking this exam.
No calculators, no cell phones.
Each student is allowed one 8.5 by 11 inch sheet of paper with handwritten notes.
Show all work, and do any/all calculations on the exam. Extra scratch paper may not be used. Partial credit will be given based on work shown. |
Exam Score Q1 = _____ / 18 Q2 = _____ / 12 Q3 = _____ / 20 Q4 = _____ / 10 Q5 = _____ / 20 Q6 = _____ / 20 Total = _____ / 100 |
Question 1 (18 points total)
Part A (5 points)
A C program is to be compiled using the same command line options
as were used for assignment 1.
The C source code file is new.c
,
and new
is to be the file name of the resulting executable.
Write the Linux shell command to do the compilation.
Part B (5 points)
In a single sentence identify the difference between the game and
game2 programs.
Part C (8 points)
Name the 4 segments provided to and available to be used by every executable program.
Question 2 (12 points total, 4 points each part)
This C program is compiled on one of the CS department's instructional Linux
machines:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int i, intvalue;
if ( argc != 4 ) {
printf("usage: argvtest int1 int2 int3\n");
exit(0);
}
for ( i = 1; i < argc; i++ ) {
intvalue = atoi(argv[i]);
printf("%d\n", intvalue );
}
return 0;
}
Part A
This program is invoked with the command line
argvtest 12 -8 -354
What is the output of this program?
Part B
This program is invoked with the command line
argvtest 6789 abc 26
What is the output of this program?
Part C
This program is invoked with the command line
argvtest 13
What is the output of this program?
Question 3 (20 points)
Consider this C program.
#include <stdio.h>
#define ARSIZE 5
int main(int argc, char *argv[]) {
int arrA[ARSIZE];
int * arrB[ARSIZE];
int i;
for ( i = 0; i < ARSIZE; i++ ) {
*(arrB + i) = arrA + i;
}
for ( i = 0; i < ARSIZE; i++ ) {
*(arrB[i]) = i * 2;
}
return 0;
}
Fill in this diagram to show the contents of the two arrays,
once the program has completed.
Use arrows to show pointer values, and leave any uninitialized
elements blank.
-------------------------------
| | | | | | arrA
| | | | | |
-------------------------------
-------------------------------
| | | | | | arrB
| | | | | |
-------------------------------
Question 4 (10 points)
Write a fragment of C code with a declaration and
one statement that contains a buffer overflow error.
Question 5 (20 points total, 2 points each)
Circle each true statement.
a[i]
is equivalent to
*(a + i)
&(a[i])
is equivalent to
a + i
In this declaration, the type of
x
is pointer to an
int
, and the type of
y
is int
.
int * x, y;
For the declaration
int * x, y;
x
is pointer to an
int
, and the type of
y
is pointer to an int
.
ptr->next
is the same as
(*ptr).next
ptr->next
is the same as
ptr.next
main()
, will result in a compiler warning or error.
int array[20]; array[100] = -1;
main()
, will result in a compiler warning or error.
int array[6]; int index = 3; array[index - 4] = index;
main()
, will result in a compiler warning or error.
typedef struct node { int value; struct node *next; } Node; Node *ptr; ptr = NULL; ptr->intvalue = 300;
x
does not change,
because C implements call by value parameters.
int x; x = 20; doubleit(x); . . void doubleit(int y) { y = y * 2; }
Question 6 (20 points total)
A C program contains this definition of a Node
structure
that is used to create a singly linked list.
typedef struct node {
int value;
struct node *next;
} Node;
Part A (5 points)
Use the Node
definition to complete this debugging
function that prints each value
field of the
nodes in the linked list, one per line.
Parameter head
is a pointer to the head of the linked list.
void printNodes( Node *head ) {
}
Part B (5 points)
In your implementation of printNodes()
,
what happens if the list is empty?