Practice Midterm 1 Key - Introduction to Programming/Problem Solving | ECS 030, Study notes of Computer Science

Practice Midterm #1 Key Material Type: Notes; Professor: Filkov; Class: Intro Prog&Prob Solving; Subject: Engineering Computer Science; University: University of California - Davis; Term: Spring 2011;

Typology: Study notes

2010/2011

Uploaded on 05/08/2011

evanay-m
evanay-m 🇺🇸

2 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ECS 30
Practice Midterm Key
The questions in this document may have appeared in midterms in previous years when
this class was taught by different instructors.
1. UNIX commands
a. (10 points) You just printed something to printer hexus, and find that it's printing a
bunch of garbage. What are the steps you take to cancel the print job? Use lpq -Phexus
to find the # of my print job and then use lprm -Phexus job# to remove the job from
the queue.
b. (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 -a hw1
c. (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 600 test.c or chmod g-rw o-rw test.c
d. (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/*.h mid1
e. (5 points) You want to view the file test3.c a screen at a time. What UNIX command
would you type? more test3.c
2. (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.
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.
pf3

Partial preview of the text

Download Practice Midterm 1 Key - Introduction to Programming/Problem Solving | ECS 030 and more Study notes Computer Science in PDF only on Docsity!

ECS 30

Practice Midterm Key

The questions in this document may have appeared in midterms in previous years when

this class was taught by different instructors.

  1. UNIX commands

a. (10 points) You just printed something to printer hexus, and find that it's printing a bunch of garbage. What are the steps you take to cancel the print job? Use lpq -Phexus to find the # of my print job and then use lprm -Phexus job# to remove the job from the queue.

b. (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 -a hw

c. (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 600 test.c or chmod g-rw o-rw test.c

d. (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/.h mid**

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

2. (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.

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.

Pts 2 #include <stdio.h>

2 int^ main() { 3 int^ i,^ num,^ num1; 1 do { 2 printf("Please^ enter^ two^ positive^ integers:^ "); 5 scanf("%d%d",^ &num,^ &num1); 3 if(num > 0) { 7 for(i = 1; i <= num; i++) 12 if(num % i == 0 && num1 % i == 0) 3 printf("%d ", i); 3 printf("\n\n"); } } 4 while(num != 0); 2 printf("Done\n"); 1 return 0; }

  1. (15 points) Consider the following recursive function. What does the function do as whole? (Do not describe what each line does.)

void pb(int n) { if (n != 0) { pb(n / 2); putchar('0' + n %2); } } It prints out the binary representation of n starting with the leftmost 1. For example, if n = 43 then pb would print 101011.

  1. (20 points) Assuming that a,b,c, and ans are ints, and a is 2, b is 4, and c is 7 at the beginning of each statement, write on the line the value of ans. There are no syntax errors in these statements.

-2 _ans = 2 * 4 / 7 - 2 + 3 - 4 % 7; 22 _ans = (++a == --b) + 3 * c++; _ 6 _ans = (7 / 4 + 4 / 7) * 3 / 4.0 * 8 ; _ 1 _ans = 7 < 8 && 8 > 6 || 4 == 4 && !(6 < 5); _ 8 _ans = (4 == 4) + ((7 > 7) * 3 || 6 % 3) * 5 + !(6 < 5) * 7;

  1. (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.