Final 3
Final 3
Final Exam
Term: Fall 2018 (Sep4-Dec4)
Student ID Information
Grade Table
Course ID: CSCI 2132 Question Score
4 /7
Time Period: Start: 15:30
End: 18:30 5 /4
8 /9
Number of Exam Pages: 21 pages
(including this cover sheet) 9 /8
Additional
11 /8
16 /10
17 /12
Σ /163
CSCI 2132, 6 Dec 2018, Final Exam Page 2 of 21
b) (2 points) The cut command is used to display first n lines from a text file.
d) (2 points) The calloc C function is used to make a system call to the kernel of the
operating system.
e) (2 points) The gdb command break is used to mark a line in the program where
program execution will stop and give us a chance to examine the state of the variables.
f) (2 points) The following C code “char *f="%d\n"; printf(f, 10);” is valid and
prints the number 10, followed by a newline character.
CSCI 2132, 6 Dec 2018, Final Exam Page 3 of 21
A. strcmp(i, s1);
B. strcpy(s2, s1);
C. strcat(s1, s2+i);
D. i = strlen(s1);
b) (3 points) Unix has a quite general concept of a file, including several different file
types. Which of the following is NOT a file type in Unix?
A. symbolic link
B. job
C. socket
D. directory
c) (3 points) If p and q are pointers to int, pointing to elements of an array, which of
the following statements is not valid:
A. p = q + 2;
B. *p = p - q;
C. *p = p + q;
D. *p = *p + *q;
d) (3 points) Which of the following lines is a correct way to use the function scanf?
4. (7 points) Program Output (strings). What is the output of the following program?
Include notes or diagrams that justify your answer.
#include <stdio.h>
#include <string.h>
int main() {
char s[90];
char *p = s;
strcpy(s, "12345-");
strcat(p, "ABCDE");
p += 6;
printf("%d %d %d\n", strlen(s), strlen(p), p-s);
*(--p) = ’\0’; p+=1;
printf("(%s) (%s)\n", s, p);
strcat(s,"XYZ");
printf("(%s) (%s) (%s)\n", s, p+3);
return 0;
}
5. (4 points) Program Output (strcmp). What is the output of the following pro-
gram? Include notes or diagrams that justify your answer.
#include <stdio.h>
#include <string.h>
int main() {
char s[9] = "abc-78";
char *p = "abf-192";
6. (10 points) Single command line. For each of the following questions, write a
single Unix command line to perform the task required. You can use pipes and the list
of allowed commands are: cut, ls, grep, egrep, sort, uniq, wc
a) (3 points) Print a list of files in the directory ../dir1 that have names starting with
an uppercase letter and ending with ‘.txt’.
b) (3 points) The file words.txt contains one word in each line. Print the number of
words in this file that are six or seven characters long, start with ‘p’ or ‘b’ and end with
‘ing’.
b) (4 points) The file games.txt contains a list of hockey games in the following format
“team1 :team2 =score1 :score2 ” in each line (example: Jets:Sabres=3:2). If the first
team in each game is the home team, and the second team is the visiting team, write a
command to print out all visiting teams, sorted without repetition.
CSCI 2132, 6 Dec 2018, Final Exam Page 8 of 21
If the function prints the number −20, what is the value of N? Explain.
CSCI 2132, 6 Dec 2018, Final Exam Page 9 of 21
b) (5 points) When we submit changes in ‘git’ to a remote repository, we use one addi-
tional command compared to when doing the same operation in ‘svn’. This is related to
an essential difference between ‘git’ and ‘svn’. Briefly explain this difference.
CSCI 2132, 6 Dec 2018, Final Exam Page 10 of 21
b) (5 points) Briefly describe what the realloc function does and its time efficiency.
CSCI 2132, 6 Dec 2018, Final Exam Page 12 of 21
b) (4 points) The following function reads some characters from the standard input and
stores them in a string on a heap without wasting space. Fill in the missing code and
explain how much of input it will store.
char *f() {
int ch, size = 100, count=0;
char *b = malloc( size );
while (EOF != (ch = getchar())) {
b[count++] = ch;
}
b[count] = ’\0’;
b = _________________ ( b, ______________________ );
/* Fill in blanks */
return b;
}
//How much input is read?
CSCI 2132, 6 Dec 2018, Final Exam Page 13 of 21
struct node {
char *team; /* team is the team name */
int points; /* points are accumulated team points */
struct node *next; /* next is pointer to next node */
};
(a) (4 points) Write a C function teamPrint that takes a pointer to the above node
structure and prints a line with the team name, the number of points, and a new line.
For multiple lines to be aligned, print team name to at least 12 characters width, and
points to at least 2 digits width, with a space between them. An output example is
“ Maple_Leafs 4”. The start of the function is:
void teamPrint(struct node *n) {
(b) (9 points) Write the C function teamNew which allocates a node structure, sets the
team name team to given name tname, sets points to 0, and the next pointer to NULL.
The team name should also be allocated on heap and copied from the given name since
the given name may have temporary life span. No error checking is required. The
function returns the pointer to the new node. The start of the function is:
struct node *teamNew(char *tname) {
CSCI 2132, 6 Dec 2018, Final Exam Page 16 of 21