Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CS

101 Fall, 2013


Computer Programming

Midterm Exam I
School of Engineering



Duration: 110 minutes




Name: ________SOLUTION_____________________



General Instructions:

Take your ID card out and put it on your desk.
DO NOT start until you are told to do so.
Check the available time. STOP writing when time is over.
This is a closed-book exam. You MAY NOT use the textbook or any other supplementary
material during the exam. NO cheat-sheet is allowed.
You MAY NOT have your cell phone with you. Make sure to put it in your bag.
You MAY NOT share anything (notes, pencils, erasers, etc.) with other people.
You MAY NOT leave the exam room in the first 45 minutes; you MAY NOT enter the exam
room after the 45th minute.
You MAY NOT enter the room if you leave (e.g., to take a restroom break).
Read the questions carefully; make sure that you understand the problems well.
Write legibly.







Grading:

1 (10) 2 (8) 3 (8) 4 (8) 5 (6) 6 (18) 7 (25) 8 (17)





Total:


CS 101 Computer Programming - Midterm Exam I P a g e | 2 / 8

1. (3+3+4 points) Write down the output for the following code fragments.

a.
in t n = 1;
wh ile (n < 10) {
print(n);
n += 2;
}

13579


b.
in t x = 1;

in t y = 10;
in t z = 3;

println(z/y == 0); // true


println(x < 0 || y >= 10); // true
println(y%z == 1 && fa lse ); // false

println(y%z < x || (x>0 && y<10) || (y%z > 1 && tr ue ) ); //

false






c.
fo r (in t i = 0; i < 4; i++) {

fo r (in t j = 0; j < 5; j++) {
if ((i+j)%2 == 0) {
print("1");
} el se {
print("0");
}
}

println("");
}

10101
01010
10101
01010

CS 101 Computer Programming - Midterm Exam I P a g e | 3 / 8


2. (4+4 points) Write down the outputs of the following programs.

a.
int i = 0; 1 3 5 7 9 11 13 15 17 19
int j = 1; -9

while (j < 20) {
print(j+" ");
21

j = j+1;
i--;
j++;
}
println();
println(i);

println(j);
b.
for (int i = 4; i <= 12; i++) { 4 is div by 2
if(i%2==0){ 5 is div by 5

println(i+" is divisible by 2");
}else if(i%3==0){
6 is div by 2
println(i+" is divisible by 3"); 8 is div by 2
} 9 is div by 3
if(i%5==0){ 10 is div by 2
println(i+" is divisible by 5"); 10 is div by 5
}
12 is div by 2
}














CS 101 Computer Programming - Midterm Exam I P a g e | 4 / 8

3. (4 + 4 points) Answer the following short questions.


a) The program below is expected to print five Hello on the screen. However, it has several
errors. Correct these errors.

pu bli c vo id run(){
in t i=1
wh ile (tr ue){
println(Hello);

if (i > 5); br eak ;

i+1 = i;

}
}




pu bli c vo id run(){
in t i=1;
wh ile (tr ue){

println(Hello);

if (i >= 5) br eak ;

i = i+1;
}
}



b) What does the following program do? Provide a high-level explanation that states the
purpose of the program. E.g: it reads a number from the user and reports whether this
number is prime or not. Explanations that only recite the code, such as it defines a variable
and then checks if this variable is greater than zero, and then are not what we are looking
for.

pu bli c vo id run(){
fo r(in t n=readInt("n? "); n>0; n=n/10){
print(n%10);
}
}


This program gets a number from the user, then it prints the digits of the number in reverse
order.

CS 101 Computer Programming - Midterm Exam I P a g e | 5 / 8


4. (4 + 4 points) Convert the following for statements to the corresponding while statements.
a)
fo r(in t i=0; i < n; i++){
println(i*i*i);
}


in t i=0;
wh ile (i < n){
println(i*i*i);
i++;
}


b) fo r(in t n=readInt("n? "); n > 0; n=n/10){
println(n%10);

}


in t n=readInt("n? ");
wh ile (n > 0) {
println(n%10);
n=n/10;
}


5. (6 points) The following method min takes two integer numbers and returns the smallest of
these numbers. Using this method, implement another method named min3 that takes three
integer numbers and returns the smallest of these numbers.

pu bli c in t min(in t a, in t b) {
if (a < b) re tur n a;
el se re tur n b;
}


pu bli c i nt min3(in t a, in t b, in t c) {
re tur n min(a, min(b,c));
}



CS 101 Computer Programming - Midterm Exam I P a g e | 6 / 8


6. (18 points) Write a ConsoleProgram that reads an integer number x and a digit d from the user.
Then, it checks if x contains d or not. Sample outputs of the program are shown below. You may
assume the user will always enter a proper digit value for d.

import acm.program.*;

public class DigitChecker extends ConsoleProgram {

public void run(){


int n = readInt("Enter a number? ");
int d = readInt("Enter a digit? ");
for(;n>0;n/=10){
if(n%10==d) break;
}
if(n%10==d){
println("The number contains "+d);
}else{
println("The number does NOT contain "+d);
}
}



CS 101 Computer Programming - Midterm Exam I P a g e | 7 / 8


7. (25 points) Write a ConsoleProgram that reads two integer numbers x and y from the user. Then,
it prints the digits of x that are not included in y. Sample outputs of the program are shown
below.

import acm.program.*;

public class NumberChecker extends ConsoleProgram {


public void run(){
int n1 = readInt("Enter number 1? ");
int n2 = readInt("Enter number 2? ");
for(int i = n1; i > 0; i /= 10) {
int j = n2;
for(; j > 0; j /= 10){
if(j%10 == i%10) break;
}
if(j%10 != i%10){
println(i%10);
}
}
}
}


CS 101 Computer Programming - Midterm Exam I P a g e | 8 / 8

8. (17 points) Write a ConsoleProgram that reads an integer number n and then prints the digits of
n in ascending order. You may NOT use features we havent covered yet, such as strings or
arrays, to solve this problem.
Hint: To solve this problem, you will need to repeatedly find and remove the smallest digit in the
number. This requires finding not only the value of the smallest digit but also its position. For
instance, suppose the number is 381254. Smallest digit is 1, and it is at the 1000s position.
Removing the smallest digit can be done by some division and modular arithmetic, such as:
((381254 / 1000) / 10) * 1000 + 381254 % 1000 = 38254

import acm.program.*;

public class DigitSorter extends ConsoleProgram {


public void run() {
// write your code here
int n = readInt("Enter number: ");

while(n > 0) {
// find the smallest digit and its index (from right)
int smallestDigit = 10;
int positionOfSmallestDigit = 1;
int positionalValue = 1;
int tempN = n;

while(tempN > 0) {
int digit = tempN % 10;
if (digit < smallestDigit) {
smallestDigit = digit;
positionOfSmallestDigit = positionalValue;
}
positionalValue *= 10;
tempN /= 10;
}
print(smallestDigit);

// remove the smallest digit from the number


n = (n / (positionOfSmallestDigit*10)) *
positionOfSmallestDigit + (n % positionOfSmallestDigit);
}

}
}

You might also like