Você está na página 1de 8

CSE 114 SAMPLE Midterm Exam I

Name:

First

Last

Student ID # _________________________________
Please show your ID when requested.
Please leave at least one seat between yourself and the person next to you.
You are not permitted to take a break during the exam. You must remain in the room until you have
completed the exam.
Please use a pen for all answers.
No books, notes, or electronic equipment can be used during the exam.
Any CHEATING will result in an F as well as being written-up on academic dishonesty. Dont place
anything in a location where it might indicate that you are copying. This is your responsibility.

ATTENTION:

Question #

Points
Available

1
2
3
4
5
6
7
8
9
Total:

15
6
12
6
6
8
6
16
25
100

Points Earned

Make sure your exam has 8 pages and all 9 questions. The copy machine may have
left one out. If you are short a page, please notify your exam proctor immediately
so they may get you the missing page.

CSE 114 Sample Midterm Exam I

Question 1 (15 Points)


Write the code for the binaryToDecimal method below such that it takes a String parameter
representing a positive binary number and returns an int representing its decimal value.
NOTE:

For this question you may use the Math.pow(double a, double b) method,
which is described in the Math API as follows:

static double pow(double a, double b)

Returns of value of the first argument raised to the power of the second argument.
NOTE:

You may also use the charAt(int index) and length() methods, which are
described in the String API as follows:
char charAt(int index)

Returns the character at the specified index.


int length()

Returns the length of this string.


NOTE:

You may not use any methods from any other classes in the JDK.

public static int binaryToDecimal(String num)


{

}
Question 2 (6 Points)
Name 2 possible reasons for the compiler to give you a cannot resolve symbol syntax error.

CSE 114 Sample Midterm Exam I

Question 3 (12 Points)


You may assume the Password and PasswordTester classes below compile and run without any
syntax or runtime errors. Looking at the swap method from the PasswordTester class, you should
notice that it doesnt work properly. As a result, in the main method, the s and p.password values are
not actually swapped.
a) (6) What output results from the execution of the PasswordTester main method?

b) (6) Without changing or adding any variable or parameter declarations, edit the PeopleTester
class such that the s and p.password values are properly swapped by a call to the swap
method. This means you may change the swap and main methods, but you will receive no points
if you change or add any variable or method argument declarations.
public class Password
{
public String password;
public Password(String initPassword)

{ password = initPassword;

}
public class PasswordTester
{
public static void main(String[] args)
{
String s = "TopSecret";
Password p = new Password("NoneOfYourBusiness");
swap(s, p);
System.out.println(s + "\n" + p.password);
}
public static void swap(String a, Password b)
{
String temp = a;
a = b.password;
b.password = temp;
}
}

CSE 114 Sample Midterm Exam I

Question 4 (6 Points)
(3) What is the purpose of overloading a method when defining a class?

(3) What is the reason for enforcing complete information hiding when defining a class?

Question 5: (6 Points)
Rewrite each of the following lines of code without using the ++, --, +=, -=, *=, or /=
operators.
a) x *= 5;
b)

y--;

c)

i /= 10;

Question 6 (8 Points)
You may assume the PasswordMixer class (which uses the Password class defined in Question 3)
below compiles and runs without any syntax or runtime errors. Carefully trace the programs execution,
keeping in mind the reference properties of objects. What output would result from the programs
execution?
public class PasswordMixer
{
public static void main(String[] args)
{
Password email = new Password("MyEmailPwd");
Password sparky = new Password("MySparkyPwd");
Password guess = copy(email);
sparky.password = "Junk";
guess.password = "Junk";
if
if
if
if

(email == guess) System.out.print("A");


(sparky == guess) System.out.print("B");
(email.password == guess.password) System.out.print("C");
(sparky.password == guess.password) System.out.print("D");

}
public static Password copy(Password template)
{
return new Password(template.password);
}
}
CSE 114 Sample Midterm Exam I

Question 7 (6 Points)
Each line of code below contains an assignment statement. To the right of each statement, write what the
value of the assigned variable will be after that statement executes. If a particular statement produces a
syntax error, just write ERROR. Note: Errors in one line should not affect your answers for other lines.
int sumGrades = 5/2;
sumGrades = 5.0/2.0;
sumGrades = (int)(5.0/2.0);
int ratio = 50/100;
double ratio2 = 50/100;
float avg = (long)(5.0/2);

//
//
//
//
//
//

a)
b)
c)
d)
e)
f)

ANSWER:
ANSWER:
ANSWER:
ANSWER:
ANSWER:
ANSWER:

Question 8 (16 Points)


Bill Gates has lost his mind and is using all of his money to build a walking bridge to Pluto, which is
3,670,000,000 miles from earth. The fastest human can run 15 miles per hour. Define the main method
below such that it calculates and prints how many years it will take this insane human to reach Pluto
assuming he/she never slows down until reaching his/her destination. You may not use any primitive
variables. You may only use BigInteger objects for this program. The API for this class includes the
following constructor, divide, and multiply method descriptions respectively:
BigInteger(String val)
Translates the decimal String representation of a BigInteger into a BigInteger.
BigInteger divide(BigInteger val)
Returns a BigInteger whose value is (this / val).
BigInteger multiply(BigInteger val)
Returns a BigInteger whose value is (this * val).
public static void main(String[] args)
{

}
CSE 114 Sample Midterm Exam I

Question 9 (25 Points)


We can use objects to represent the exam grades for an individual student. Define the GradedStudent
class in its entirety such that it has the following characteristics:

(9) Each GradedStudent object HAS-A Person representing the student who took the exams.
In addition, each GradedStudent object has two exam scores representing two equally
weighted exams graded as whole numbers on a scale from 0 to 100.
oProvide complete information hiding for this data such that all of it can be accessed and
such that the exam data can be mutated using legal grade values only.
(6) Provide two different means for constructing a GradedStudent object such that:
oFor each constructor, after it has completed execution all instance variables are assigned
either default values or constructor argument values.
oThere is no repeated/redundant code inside your constructors and instance variables
declarations.
(6) Provide a means for your object to do the following:
oReturn the average of the two exams as a real number
oReturn the higher exam score
(4) Provide a toString() method that returns a String that summarizes the specified object,
including:
oThe name of the GradedStudent object, as taken from the Person object.
oThe exam average for the object.

Printing the String returned by the toString() method might look like:
Name: Joe Shmo
Average: 87.5
NOTE:

You should use the Person class we defined in lecture. The API is listed below:

Person(String initName)
Constructor that initializes this Person's name to the initName value.

String getName()
Returns this Person's name.
int getAge()
Returns this Person's age.
void setAge(int newAge)
Changes this Person's age.

CSE 114 Sample Midterm Exam I

public class GradedStudent


{

CSE 114 Sample Midterm Exam I

}
CSE 114 Sample Midterm Exam I

Você também pode gostar