辅导案例-CS210

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
1

CS210 Midterm Guide - Spring 2020
Date&time: Monday May 11, 2020
• Starts: 12:30 pm
• Submission ends: 6:30 pm
• Start any time between 12:30 pm ~ 4:50 pm (Do not start after 4:50 pm)
• You have 100 minutes to answer, and must submit before 6:30 pm. The exam ends automatically at 6:30.
• Because the exam time is not long (100 minutes), you should manage your time well.
• Only one attempt(submission) allowed.
• Please make sure you have answered all questions prior to submitting.
• You can NOT redo after submission.
• Do not share any file with others.
• Do not copy others.
If you share/copy someone else’s answer, both of you get “F”.
I have already found some students sharing their answers with others in the last assignment and quiz.

[About the Questions]
• 180 points
• Midterm exam is very similar to the previous one (Winter 2020). I will make some modification to the
question.
• The exam consists of 8 questions: 7 questions like part 1, and 1 programming question.
• You are required to explain how it works with the answer. (except programming question)
• A sample question and explanation are in “Midterm exam Sample” (canvas -> Assignments -> Exam)
• You may use eclipse or any IDE, especially for Part 2 (programming question) during the exam.

[FYI: About the Final Exam] – will be updated
• The Final exam will be a common exam for all CS210 sections and it will run at the same time. (Friday,
Jun 19: Final exam (1:30pm))
• The final exam will include all the course material from Chapter 1- 8 and the first two sections of
Chapter 9. Chapter 3G is not included in the final exam.

[Previous Exam]
2

CS210 Midterm Exam Part 1 (Winter 2020, 12:30 Session) Name:
02/06/2020

1. What output is produced by the following loop? (10points)

int number = 12;
for (int count = 0; count < number; count*=2) {
System.out.println((double)number);
if (count number = number / 3;
}
else {
number = number * 3;
}
}

//major topics
for loop
if-else statement
Arithmetic operators (+, -, *, / %)
type casting

//output
12.0
4.0
1.0



2. What output is produced by the following loop? (10points)

for (int i=0; i<=4;i++) {
for (int j=1; j<=i; j++){
if ((i>j) && (j<2)){
System.out.println(i);
}
}
}

//major topics
Nested for loop
Logic operator ( &&, || )

//output
2
3
4


3

3. For each expression, indicate its value. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a
double, Strings in quotes). (4points/each)

String s= "ABCD";
int length=s.length();
System.out.println(s.charAt(length-1)- s.charAt(0) );
System.out.println(s.substring(2));
System.out.printf("output is %5.3f", (double)2/length);

//major topics
String concatenation
Char vs. string
Printf
Type casting

//output
3
CD
output is 0.500


4. What output is produced by the following loop? (12points)

int sum0=0;
String sum1="";
for (int i = 4; i >=1; i--) {
sum0 += i ;
sum1 += sum0;
System.out.println(i + " - " + sum1 + " = " + sum0);
}

//major topics
String concatenation
for loop

//output
4 - 4 = 4
3 - 47 = 7
2 - 479 = 9
1 - 47910 = 10


5. What output is produced by the following code? (16points)

public static int ifElseMystery(int a, int b) {
if (a < b ) {
a = a + 2;
b = b - 3;
System.out.println(a + " " + b);
} else {
a = a - 2;
4

b = b + 3;
System.out.println(a + " " + b);
}

if (a > 0 && b < 0) {
a = a - b;
b = b - a;
System.out.println(a + " " + b);
}

System.out.println(a + " " + b);
return a+b;
}

public static void main(String[] args) {

int a= ifElseMystery(1, 1);
ifElseMystery(1,a);
}

//major topics
if-else statement
Logic operator ( &&, || )
method calling method
parameter passing
method returning value

//output
-1 4
-1 4
3 0
3 0


6. Some errors have occurred while running the following methods. Correct at least five(5) errors to generate
the following output. (10points)

public static void sum(int a, int b) {
return a+b;
}

public static void main(String[] args) {

double a=1;
double b=2;

for (int i=1; i<=3; i--) {
result=sum(a++,b++);
System.out.println(result);
}
}
5


//major topics
Method returning value
Variable declaration and data type
For loop

//output
3
5
7


7. As we discussed in the class, fill in the table (total 14 blanks) to find ??? of the method that generates the following
stars (14point)

***
*****
*******
*********
for (int i=1; i<= 4; i++){
for (int j = 1; j <= ??? ; j++){
System.out.print("*");
}
System.out.println();
}

i No of Stars (2 *i) (2 *i)+1
1 3 2 3
2 5 4 5
3 7 6 7
4 9 8 9

See slide (04-17-2020) for detail

8. What output is produced by the following code? (16 points)

public static int doThis(int a, int b) {
if (a <= b || b <= 10) {
a = a + 1;
b = b + 3;
} else {
a = a - 1;
}

while (a a=a+b;
while(a>4) {
a--;
b--;
}
6

}

System.out.println(a + " " + b);
return a;
}

public static void main(String[] args) {

int a=0;
int b=0;
for (int i=4; i>0; i/=2) {
a=i;
b = doThis(b, a);
System.out.println(a + " " + b);
}
}

//major topics
While loop
Method calling method
Method returning value
If-else statement
Arithmetic operators (+, -, *, / %)


//output
4 3
4 4
5 5
2 5
6 4
1 6

[ No guideline is provided for Part2]
CS210 Midterm Exam (Winter 2020) Part 2. Name:
02/07/2020

1. Write a Java program using while loop, random class and scanner that determine whether random number is an even
number or odd number. (total 30points)

1) Your program will generate a random number from 5 to 30 (10points)
2) Your program will prompt the user to input a type of the random number(“even” or “odd”, ignore case) (5points).
3) If the user’s input is the same as the type of the random number (either “even” or “odd”), your program will
terminate with “Correct!” and “Good bye!” messages (10points),
4) otherwise, print “Wrong!”, then ask user to input type of the number again (5points).

Sample log of execution: [This is not a desired output!! Just a SAMPLE]
Assume: random number is 13
7

Number is 13
Type? even
Wrong!
Type? odd
Correct!
Good bye!


2. Write a Java program that calculates sales price using while loop. Your program will prompt the user for information
about two parameters: membership ID(data type: String and uses only upper-case letter) and input double number of
price. (total 50points)

1) In your main method, ask user to type membership ID, then send to a method to determine “valid” or “invalid”.
2) One method should receive membership ID from main method, then return either true (valid) or false (invalid) to the
main method (10points)
3) User ID must be 5 digits-length and the first char must be the same as the last char. (5points)
4) If user typed wrong ID, ask user to input ID again. (5points)
5) If the user input 00000 for the ID, then your program will terminate with “Good Bye!” message. (5points)
6) Otherwise your program does run repeatedly. (5points)
7) If the membership ID is valid, ask user to input price, then send to a method membership ID and price to calculate
payment.
8) One method should receive the parameters (method membership ID and price) and return how much money to pay
to buy the good (10points).
9) The customer should receive discount of 10% from the original price if membership ID starts with ‘A’ (5points).
For example, if a method received (“A301A”, 100.0), the method should return 90.0. (eg: 100.0 - (100.0* 0.1) = 90.0)
10) When you print out sales price in main method, use ONLY ONE decimal point (eg: 90.0) and $ sign. (5points)

[Important!] You must create one method to check membership ID and another method to calculate payment. Those
method must return its result to main method.

Sample log of execution: [This is not a desired output!! Just a SAMPLE]
ID? A301
Wrong ID, please input ID again.
ID? A301B
Wrong ID, please input ID again.
ID? A301A
Price? 100.0
You pay : $90.0

ID? C500C
Price? 100.0
You pay : $100.0

ID? 00000
Good Bye!



51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468