CS 354, version A
Fall 2014
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 = _____ /   30
Q5 = _____ /   20
Total = _____ / 100







Question 1 (18 points total)
Part A (5 points) What does the the -m32 option cause the gcc compiler to do?




Part B (5 points) Write a Linux command that copies the file named input1 from directory /tmp/run1/ to the CWD (current working directory).



Part C (8 points) Name the 4 segments provided to and available to be used by every executable program.















Question 2 (12 points total) Once this C program has main()'s missing parameters, it is compiled on one of the CS department's instructional Linux machines:

#include <stdio.h>

int main(                                            ) {
   int i;

   printf("argc is %d\n", argc);
   printf("extra args are\n");

   for ( i = 1; i < argc; i++ ) {
      printf("%s\n", *(argv + i) );
   }
   return 0;
}


Part A (6 points)
On the code above, fill in the missing parameters in main()'s function header.



Part B (6 points) This program is invoked with the command line
    argvtest  xxx  -7000
What is the output of this program?


















Question 3 (20 points)
Complete this code fragment. It is to do a bitwise logical AND of each corresponding array element of arrA and arrB, placing the result into the corresponding element of arrC. The code fragment may not declare variables other than those given, and it may not use the [] operators, so array access must be done using address arithmetic.

#define ARSIZE 100
   int  arrA[ARSIZE];
   int  arrB[ARSIZE];
   int  arrC[ARSIZE];
   int  *ptr1, *ptr2;
   int  i;    

   


   for  ( i = 0;  i < ARSIZE; i++ ) {








   }




















Question 4 (30 points total)
Here is a poorly implemented C program:

#include <stdio.h>

#define MAXCHAR 12

int main(int argc, char *argv[]) {
   FILE *infile;
   char buffer[MAXCHAR];
   int  linenumber;

   infile = fopen(argv[1], "r");

   linenumber = 1;
   while (fscanf(infile, "%s\n", buffer) != EOF) {
       printf("%d. %s\n", linenumber, buffer);
       linenumber++;
   }

   return 0;
}
Part A (10 points) Assume that this program has been compiled and the executable is called badC. Here is a shell command (underlined) and its output (not underlined):
% cat ascii6
my
file has
4
lines.
What is the output of the following shell command?
% badC  ascii6












Part B (10 points) There are 2 major bugs in this program that might cause it to crash at runtime. Identify the bugs.










Part C (10 points) Write a series of Linux shell commands and their output that cause one of these crashes. Identify which of your 2 bugs caused this crash. Include all commands that will prove the bug is hit. Underline the shell commands, but also show any output generated from the shell commands (not underlined).





















Question 5 (20 points total)
Part A (5 points)
Complete this definition of a Node that could be used in a program that uses a singly linked list. Assume that each item going into the list is an integer.

typedef                       {



} Node;
Part B (10 points)
Assume that this Node definition has been used to create and initialize a singly linked list. Complete this function definition that prints each integer in the linked list to stdout, such that each integer is printed on its own line.
/*  Parameter head is a pointer to the first Node in the linked list. */
void  printlist( Node *head ){










}
Part C (5 points)
We decide to make the code that handles a singly linked list modular. Therefore, we introduce function listinitialize() to initialize an empty, singly linked list. Complete the definition of this function.
/*  Returns a pointer to the head of the new, empty list. */
Node *  listinitialize( ){




}
The code that calls this function contains
   Node *list1head;
   list1head = listinitialize();