Docsity

CMSC 106 Exam 1 - Fall 2007 - Prof. Jandelyn Dawn Plane, Exams of Computer Science

Toggle sidebar
bg1
Name (printed):
Student ID #:
Section # (or TA’s:
name and time)
CMSC 106 Exam #1 Fall 2007
Do not open this exam until you are told. Read these instructions:
This is a closed book, closed notes exam. No calculators or other aids are allowed. If you have a
question during the exam, please raise your hand. Each question’s value is written next to its number.
Watch the number of points for each question to be sure you are not spending too much time on any
particular question. It is not necessary to do the problems in the order presented – the order of presentation
was selected to give extra space to questions that may need that space. (In particular - the page after a
programming question is blank so you can flow onto the second page if needed.)
No one may leave before the exam is over.
There are no syntax errors in any of the C code on this exam. If you don’t recognize a C construct, do the
best you can.
pages numbered 1-8, 6 problems, 100 points, 60 minutes.
Please write legibly. Credit cannot be given for any parts of answers which cannot be read.
Good luck!
1 2 3 4 5 6 Total
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download CMSC 106 Exam 1 - Fall 2007 - Prof. Jandelyn Dawn Plane and more Exams Computer Science in PDF only on Docsity!

Name (printed):

Student ID #:

Section # (or TA’s: name and time)

CMSC 106 Exam #1 Fall 2007

Do not open this exam until you are told. Read these instructions:

This is a closed book, closed notes exam. No calculators or other aids are allowed. If you have a question during the exam, please raise your hand. Each question’s value is written next to its number. Watch the number of points for each question to be sure you are not spending too much time on any particular question. It is not necessary to do the problems in the order presented – the order of presentation was selected to give extra space to questions that may need that space. (In particular - the page after a programming question is blank so you can flow onto the second page if needed.)

No one may leave before the exam is over.

There are no syntax errors in any of the C code on this exam. If you don’t recognize a C construct, do the best you can.

pages numbered 1-8, 6 problems, 100 points, 60 minutes.

Please write legibly. Credit cannot be given for any parts of answers which cannot be read.

Good luck!

1 2 3 4 5 6 Total

  1. [18 pts.] Short answer:

a. Give the UNIX command which will move you into directory where the class example source files are. You may assume you are in your home directory when you give the command.

b. Write the UNIX command which will allow you to display the contents of the file named this.file to the screen one page at a time. The file named this.file is in your home directory. You do not know which directory you are in as you give the command.

c. Give the command you would type that would compile the program that is named this.c into an executable file named that. The file named this.c is in the current working directory and the executable file can be put in the same directory.

d. There are certain things that are usually at the very top of the source file. i. These often begin with a the # character. What is the purpose of the # character in this context?

ii. One of these is #include - what is the purpose of this?

iii. Another is #define - what is the purpose of this?

e. Give an example of an escape sequence which could appear within a printf string and tell its purpose.

f. Give an example of a format specifier which could appear within a printf string and tell its purpose.

  1. [20 pts.] Write the complete C program which will request a single integer from the user. If that integer is positive it will then say “Thank you” and then request a character, but if the integer was not positive the program will end immediately with the message “Invalid Input”. Once the character is read, the program will print “Invalid Input” if the character was not a, b, c or d (either upper or lowercase should be accepted), but if the character is one of those listed it should print “Thank you”. It should then print the character entered the number of time indicated by the integer. The dividing lines appear in this output just as a division between the different runs of the program. If the program did not run completely and had to end with “Invalid Input”, it should return any negative value to the operating system. If the program did run completely and ended by printing the character the correct number of times, it should return a 0 to the operating system.

Enter an Integer: - Invaid Input


Enter an Integer: 2 Thank you Enter a Character: x Invalid Input


Enter an Integer: 3 Thank You Enter a Character: a Thank you aaa


Page Blank to leave extra room for the programming question.

  1. [12 pts.] For each set of variable values given below, tell which statement will be performed (just give the number). If more than one statement would be done be sure to list all statements that would be performed in the order they would be performed. You just need to give the statement’s number. If no statement would be performed, write “none”. Be careful, the indenting may not be correct!

if (a < b) if (c < d) statement1; else if (a <= c && a != d) if (a == c) statement2; else statement3; else if (c > d) statement4; else statement5;

a. a = 2, b = 5, c = 3, d = 6

b. a = 3, b = 4, c = 2, d = 1

c. a = 1, b = 1, c = 1, d = 1

d. a = 3, b = 4, c = 5, d = 2

  1. [20 pts.] Give the output of the following loops. If a loop never executes or has no output then write “none”; if a loop is infinite show the output of the first three iterations and then write “in- finite”. In this output, spaces do not matter - just have the correct values on the correct lines. Be careful - indenting may not be correct!

a. i = 0; while (i < 10){ printf("%d - ",i++); i++; }

b. i = 10; j=2; while (i>j*2) printf("%d %d\n",j,i); i -= j;

c. i = 2; j = 7; k = 2; while (i+k <= j){ printf("%d %d %d\n",i, j, k); i++; k+=3; } printf("%d-%d-%d\n",i,j,k);

d. int i = 1; int j = 11; while (i < j/2){ printf("i, j\n"); j-=3; }