Вы находитесь на странице: 1из 5

CS 101 Spring, 2012

Computer Programming
Midterm Exam I
School of Engineering

Duration: 100 minutes

Example Solutions

Name: _____________________________________________

General Instructions:

DO NOT start until you are told to do so.


Check the available time. STOP writing when time is over.
You are not allowed to leave in the first 30 minutes of the exam.
This is an open-book exam. You MAY use the official textbook of the course during the exam.
You MAY NOT use any other supplementary material.
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.
Read the questions carefully; make sure that you understand the problems well.
Write legibly.

Grading:

1 (10) 2 (15) 3 (20) 4 (20) 5 (15) 6 (20)

Total:
CS 101 Computer Programming - Midterm Exam I Page |2/5

1. A Java program is given below.


import acm.program.*;

public class Numbers extends ConsoleProgram {

public void run() {


int z = 0;
int n = 100;
while(n > 0) {
if(n%2 == 1) {
n = n / 2;
}
n = n - 1;
z = z + 1;
print(z + " ");
}
}
}

a. List all the values that n takes.

100, 99, 49, 48, 47, 23, 22, 21, 10, 9, 4, 3, 1, 0

b. What is the output of the program?

12345678

2. Write the rest of the Java program below that takes two integers from the user as an input. Then
the program determines if the first number is a multiple of the second, and prints this
information to the user. Sample outputs can be found below.

Enter the first number: 8 Enter the first number: 20


Enter the second number: 2 Enter the second number: 3
8 is a multiple of 2 20 is not a multiple of 3

import acm.program.*;

public class Numbers extends ConsoleProgram {

public void run() {

int n1 = readInt("Enter the first number: ");


int n2 = readInt("Enter the second number: ");

/* write your code below */

if ( n1 % n2 == 0 ) {

println(n1 + " is a multiple of " + n2);

} else {

println(n1 + " is not a multiple of " + n2);

}
}
}
CS 101 Computer Programming - Midterm Exam I Page |3/5

3. Write the rest of the Java program below that continuously asks for numbers from the user until
the user enters 0. The program adds up the positive numbers and prints out the total sum. A
sample output is shown below.

Enter a number: 8
Enter a number: -3
Enter a number: 2
Enter a number: -1
Enter a number: 0
The sum of positive numbers is 10.

import acm.program.*;

public class SumOfPositives extends ConsoleProgram {

private static final int SENTINEL = 0;

public void run() {

// write your code below

int sum = 0;

while(true) {

int num = readInt("Enter a number: ");

if(num == SENTINEL) {

break;

if(num > 0) {

sum = sum + num;

println("The sum of positive numbers is " + sum);

}
}
CS 101 Computer Programming - Midterm Exam I Page |4/5

4. Write the rest of the Java program below to produce the output shown in the figure. There is a
distance of 10 units between each line-end both on the X and Y axis.

import acm.program.*;
import acm.graphics.*;

public class Lines extends GraphicsProgram {

public void run() {

// write your code below

int n = 10;

while(n <= getWidth()) {

GLine line = new GLine(0, n, n, 0);


add(line);
n = n + 10;

}
}

5. What does the Java program given below print? Fill in the grid that is provided to visualize the
positions of the characters.

import acm.program.*;

public class Grid extends ConsoleProgram {

private static final int N = 7;

public void run() {

for(int i = 0; i < N; i++) {

for(int j = 0; j < N; j++) {

if(i==0 || j==0 || i== N-1 || j==N-1 || i==j) {

print(j);

} else {

print(" ");
0 1 2 3 4 5 6
} 0 1 6
} 0 2 6
0 3 6
println(""); 0 4 6
} 0 5 6
}
0 1 2 3 4 5 6
}
CS 101 Computer Programming - Midterm Exam I Page |5/5

6. Write the rest of the Java program to produce the output shown below.

XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX

import acm.program.*;

public class XGrid extends ConsoleProgram {

private static final int N = 10;

public void run() {

for(int row = 0; row < N; row++) {

for (int col = 0; col < N; col++) {

/* write your code below */

if((row >= N/2 && col < N/2) ||


(row < N/2 && col >= N/2)) {

print("X");

} else {

print(" ");

println("");

}
}

Вам также может понравиться