Sample Midterm #1
1. (4 points) If the current working directory is
/usr/bishop/tmp, give an absolute path name without any . or
.. directories in it for:
- ../src/./../passwd+/misc/./root.c
- /usr/holly/files/../DMS/././.././DMS/schedule
2. (5 points) If I wanted to send the output of the command
from to the file FROM.OUT, and the error messages to the
terminal, what would I type to the C shell?
3. (4 points) What command would I type to print a list of files that
have names ending in ".c" and that contain the string "<stdio.h>"?
4. (5 points) How can you delete a file named -i?
5. (8 points) How does the shell use the path variable (in C shell,
path; in Bourne shell, PATH)?
6. (15 points) What does the following print? If the ANSI standard
says that more than one output is possible, give them all and explain why more
than one is possible.
i = 2;
printf("%d %d %d\n", i++, i++, i++);
7. (14 points) Evaluate the expressions below, and give the values of
the named variables after the expression has been evaluated. If the expression
contains a syntax error, or if a value is undefined, say so. Treat each
part as separate; that is, assume the following variable values for all parts,
regardless of whether a previous part has changed them.
int a = 0, b = 4, c = 5, d = -2, x; double dx;
- x = a++ ? b++ : c++; give values of x, a, b, c
- x = (a || b++) && (c++ || a++); give values of x, a, b, c
- x = b < c < b; give value of x
- x = a + b / c; give value of x
- x = (c % d) + ((c / d) * d); give value of x
- x = dx = 3.2; give value of x and dx;
- x = a < d, --c < b; give value of x
8. (15 points) What is the output of the following program:
#include <stdio.h>
int i = 3;
void g(int i)
{
printf("%d\n", i);
{
static int i = 0;
printf("%d\n", i++);
}
printf("%d\n", i);
}
void main(void)
{
g(i);
printf("%d\n", i + i);
{
register int i;
i = 5;
printf("%d\n", i);
}
printf("%d\n", i);
g(i);
}
9. (15 points) What is the value of n after the following code
executes?
char charray[] = "A-DF123";
int n = 0, s = 1;
char *p = charray;
while(*p){
switch(*p++){
case '-':
s = -s;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
n = n * 10 + (p[-1] - '0');
break;
}
}
n = s * n;
10. (15 points) In the following:
char line[1024];
char *p;
if (fgets(p, 1024, stdin) != NULL)
for(p = line; *p; p++)
putchar(`A'<=*p && *p<=`Z' ? *p-`A'+`a' : *p);
- What happens when this portion of the program is compiled?
- What happens when this portion of the program is executed and the user
types in the string "This is ECS40" followed by a carriage return
(newline)?