Intro To Java Practice Midterm
Intro To Java Practice Midterm
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.
For this question you may use the Math.pow(double a, double b) method,
which is described in the Math API as follows:
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)
You may not use any methods from any other classes in the JDK.
}
Question 2 (6 Points)
Name 2 possible reasons for the compiler to give you a cannot resolve symbol syntax error.
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;
}
}
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
}
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:
}
CSE 114 Sample Midterm Exam I
(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