ECS 30B

Sample Midterm Questions

 

These are  just samples of questions.  may not cover all topics---will include materials covered all the way thru monday, and material

in the book all the way from Chap 2 thru 5.4, and other assigned reading (e.g., Unix tutorial)

 

1.      (10 points) You just printed something, and realize that it is a binary file that will print a bunch of garbage.  What are the steps you take to cancel the print job?


  First do lpq to find the job, and then do lprm to remove the job.

 

2.      (5 points) What UNIX command would you type to see a list of all of the files (including the hidden files) in directory hw1?


ls -la hw1

 

3.      (5 points) You just wrote and saved the file test2.c.  Now you want to make sure that only you can read and write it.  What UNIX command would you type to make test.c only readable and writeable by you?


chmod og-rw test.c

 

4.      (5 points) You want to move all of your C source files (ending in .c) and header files (ending in .h) from your hw1 subdirectory to your mid1 subdirectory.  What UNIX command should you type?


mv hw1/*\.c hw1

 

5.      (5 points)  You want to view the file test3.c a screen at a time.  What UNIX command would you type?

 
more test3.c

 

6.      (25 points)  Given the following series of if statements, provide the outputs for each X.  Note that more than one printf can be executed for each X.


The best way to understand this is to make a program out of this--read X, and then execute the test. Then look at the output,
and try to explain it. I don't think it would be helpful for me to just give the answer here.

 

if (X > 20)

printf(“First ”);

else

if(X < 5)

   printf(“Second “);

    else

      printf(“Third “);

  if(X == 20)

    printf(“Fourth “);

  else

    if(X < 19 || X > 25)

      printf(“Fifth “);

  if(X > 5 && X < 22)

    printf(“Sixth ”);

 

a.  (5 points) X = 0    ________________________________________________________________________

 

b.  (5 points) X = 5    ________________________________________________________________________

 

c. (5 points) X = 17  ________________________________________________________________________

 

d. (5 points) X = 20  ________________________________________________________________________

 

e. (5 points) X = 28  ________________________________________________________________________

 


7.       (20 points)Assuming that x is 11, y is 6, and z is 1 at the beginning of each statement, what is the value of w:


I'm assuming statements are independent, and not executed in order. To do this, remember some simple precedence rules; Do paranthesized expressions first. Then do unary operators (prefix +, -, --, !, ++) then do the binary multiplies/divides, and then the binary additions/subtrations. All of these associate left to right (so that 3-2-1 is actually (3-2)-1, and not 3-(2-1)). Also as mentioned in class, boolean conditions evalute to 1 if true, and 0 if false. C is weird that way.  Operator precedence and associativity is described in the book, but also in wikipedia and here


a.)    w = (x != y) -2 + 7                  ___1-2+7, which is  = 6________________________________

 

b.)    w = x-- +  y-- * ++z                _____11+6*2, which is = 23 (note * precedes +)___________

 

c.)    w = x == y || x != y && z > x _______0 || 1 && 0, which is  = 0 (&& precdes ||)___________

 

d.)    w = !!(x * 4) + x % y                  ____(note that !0 = 1, and !1 = 0, and !x is 0 if x is not zero) so 1 + 11%6  which is  =  6

 

e.)    w = 7  * --y + !(y ==5)           ________7*5+0  which is = 35__________________________


8.         (50 points) Write a complete, warning-free, C program that lists all of the common factors of two positive integers.  The program will prompt the user for the two numbers, and then list on one line all the factors that the numbers have in common.  A factor of a number divides into that number with a remainder of zero.  The program will continue to ask the user for more pairs of numbers until the first number entered is zero.  Your prompts and formats should match that shown.  User input is in bold.


Here's the solution. Note the nested loop (while loop within while loop). READ carefully.

 

Please enter two positive integers: 20 12

1 2 4

 

Please enter two positive integers: 60 120

1 2 3 4 5 6 10 12 15 20 30 60

 

Please enter two positive integers: 8 9

1

 

Please enter two positive integers: 0 230

Done.