Академический Документы
Профессиональный Документы
Культура Документы
Computer Programming
Midterm Exam I
School of Engineering
Example Solutions
Name: _____________________________________________
General Instructions:
Grading:
Total:
CS 101 Computer Programming - Midterm Exam I Page |2/5
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.
import acm.program.*;
if ( n1 % n2 == 0 ) {
} else {
}
}
}
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.*;
int sum = 0;
while(true) {
if(num == SENTINEL) {
break;
if(num > 0) {
}
}
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.*;
int 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.*;
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.*;
print("X");
} else {
print(" ");
println("");
}
}