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

CS 101 Midterm Exam I - Fall 2011 Page 1

gin University
Ozye
CS 101 Fall 2011
Midterm Exam I
Nov. 1st, 2011
Duration: 90 minutes

Name: SOLUTION
DO NOT start until you are told to do so.

STOP writing when time is over.

There are 7 pages in this 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 share anything with other people, including the book, pens, pencils,
erasers, etc.

Read the questions carefully; make sure you understand the problems well.

Write legibly.

Honor code:
I pledge my honor that I have neither given nor received any unauthorized aid on this test.

Read the text above, write the same sentence below, and sign. Tests without
signed honor code will not be accepted.

Signature:

Question: 1 2 3 4 5 6 Total
Points: 0 6 6 12 16 10 50
Score:

Page 1 of 7
Page 2 CS 101 Midterm Exam I - Fall 2011

1. [0 points] What is the answer to the ultimate question of life, the universe, and every-
thing?

42

Page 2 of 7
CS 101 Midterm Exam I - Fall 2011 Page 3

2. [6 points] Write a ConsoleProgram that asks the user to enter the radius of a circle
and the central angle of a sector (in degrees, always between 0 and 360).

The program then calculates and prints the area of the sector based on the following
formula:

r2
360
You must also define PI as a constant set to 3.14, and use this constant in your calcu-
lation. Sample output and a code template are given below.

import acm.program.*;

public class CircleSectorArea extends ConsoleProgram {

private static final double PI = 3.14;

public void run() {


int r = readInt("Enter radius: ");
int angle = readInt("Enter central angle: ");

double area = angle/360.0 * PI * r * r;

println("Sector area is " + area);


}
}

Page 3 of 7
Page 4 CS 101 Midterm Exam I - Fall 2011

3. [6 points] Write a ConsoleProgram that asks the user to enter the duration of an
event in seconds and then converts that value to a combination of hours, minutes and
seconds. Sample output and a code template are given below. You may assume that the
user input is always a non-negative integer.

import acm.program.*;

public class SecondsToHours extends ConsoleProgram {


public void run() {
int fullSeconds = readInt("Enter seconds: ");

int secs = fullSeconds % 60;


int mins = (fullSeconds / 60) % 60;
int hours = fullSeconds / 60 / 60;

print(fullSeconds + " seconds is ");


print(hours + " hours, ");
println(mins + " minutes and " + secs + " seconds.");
}
}

Page 4 of 7
CS 101 Midterm Exam I - Fall 2011 Page 5

4. [12 points] Write a ConsoleProgram that asks the user to enter two integers, x and n,
then calculates xn and prints the result. Sample output and a code template are given
below. You may assume that n is always a positive integer. Using a library function is
not allowed.

import acm.program.*;

public class Exponent extends ConsoleProgram {


public void run() {
int x = readInt("Enter x: ");
int n = readInt("Enter n: ");

int result = 1;
for (int i = 0; i < n; i++) {
result *= x;
}
println(x +" to the power "+ n +" is " + result);

}
}

Page 5 of 7
Page 6 CS 101 Midterm Exam I - Fall 2011

5. [16 points] Write a ConsoleProgram that reads an integer from the user, and then
prints the largest digit in that number. Sample output and a code template are given
below.

import acm.program.*;

public class LargestDigit extends ConsoleProgram {


public void run() {
int number = readInt("Enter a number: ");

int largestDigit = 0;
while(number > 0) {
int digit = number % 10;
if(digit > largestDigit) {
largestDigit = digit;
}
number /= 10;
}
println("Largest digit is " + largestDigit);

}
}

Page 6 of 7
CS 101 Midterm Exam I - Fall 2011 Page 7

6. [10 points] Below is a program that uses nested loops to print a pattern of characters.

import acm.program.*;

public class NestedLoop extends ConsoleProgram {


public void run() {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (((i + j) == 11) || i == 1 || j == 10) {
print("x");
} else {
print(" ");
}
}
println("");
}
}
}

What does the program above print? Provide your answer below, where each cell repre-
sents the position of a character. There may be more spaces than you need.

x x x x x x x x x x
x x
x x
x x
x x
x x
x x
x x
x x
x x

Page 7 of 7

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