Practice exam 3
-
Written Portion of the Final Exam - Sample
- (5 points) Choose the best answer to each of the following:
(1) _____
In the Java statement branch = account.charAt(0);
,account
is:- An attribute
- A class
- A method
- An object
- A parameter
(2) _____
In the Java statement branch = account.charAt(0);
,charAt
is:- An attribute
- A class
- A method
- An object
- A parameter
(3) _____
In the Java statement branch = account.charAt(i);
,i
is:- A class
- A method
- A parameter
- All of the above
- None of the above
(4) _____
In the Java statement owes = (balance > 0);
,=
is:- The assignment operator
- A concatenation operator
- A logical operator
- A relational operator
- None of the above
(5) _____
In the Java statement owes = (balance > 0);
,>
is:- The assignment operator
- A concatenation operator
- A logical operator
- A relational operator
- None of the above
(6) _____
In the Java statement owes = (balance > 0);
,owes
must be declared to be a:boolean
char
double
int
- None of the above
(7) _____
In the Java statement currency = new String("Euros");
,"Euros"
is:- An attribute
- An arithmetic operator
- A class
- A parameter
- None of the above
(8) _____
In the Java statement currency = new String("Euros");
,currency
is a variable that holds a:- Fundamental/Primitive/Atomic type
- Reference type
- Value type
- All of the above
- None of the above
(9) _____
In the Java statement january = month[0];
,month
is:- An array
- An element of an array
- A method
- An operator
- A parameter
(10) _____
In the Java statement january = month[0];
,month[0]
is:- An array
- An element of an array
- A method
- An operator
- A parameter
- (10 points) Indicate whether each of the following will generate a compile-time error (C), a run-time error (R), or neither (N).
Using an explicit value constructor of a class that only has a default constructor. Referring to element 10 of an array that has 10 elements. Referring to element 0 of an array that has 2 elements. Using a non-static method as if it is static (i.e., using the class name). Using a static method as if it is non-static (i.e., using a particular object). Passing an array of String
objects (i.e., as the actual parameter) to a method that has a formal parameter that is declared to be aString
object.Passing an array of String
objects (i.e., as the actual parameter) to a method that has a formal parameter that is declared to be an array ofColor
objects. - (5 points) Using the operators and rules defined in Java, evaluate the following expressions or indicate the errors they contain:
"$" + 1.00
(5 == ((5 / 3) * 3))
"A" == "a"
"A".equals("a")
21 >= 18 && <= 65
- (5 points) Answer each of the following questions.
What word is used to describe two methods that have the same name but different parameters (like the valueOf()
methods in theString
class)?What does it mean when one says an object is immutable (like objects in the String
class)?In addition to the fact that it doesn't include the immutable
stereotype in the UML class diagram, how can you tell that objects in theRectangle
class are mutable?In addition to their names and parameters, what else is different about the intValue()
andparseInt()
methods in theInteger
class?Why is it important that the String
,Color
,Point
andRectangle
classes haveequals()
methods? - (10 points) Write a statement that:
Declares m
andn
to beint
variables.Assigns the value -7
tom
.Uses the static abs
method in theMath
class to assign the absolute value ofm
ton
.Declares individuals
to be an array ofPerson
objects.Initializes individuals
so that it can containn
differentPerson
objects.Declares hasCredit
to be an array ofboolean
values.Initializes hasCredit
so that it can contain as many values as there are objects inindividuals
. Note: You may not usem
orn
.Loops over all of the elements in hasCredit
, assigningtrue
to each.Declares current
to be aBillable
object.Instantiates current
using the explicit value constructor in theBillable
class that is passed an array ofPerson
objects and an array ofboolean
values (passing itindividuals
andhasCredit
). - (5 points) Show what will be printed by the following application (assuming it is compiled and executed properly).
public class Loopy { public static void main(String[] args) { int sign, sum; sign = 1; sum = 0; System.out.printf("%5s %5s %5s\n", "i", "sign", "sum"); for (int i=0; i<5; i++) { sum = sum + sign * (i * i); System.out.printf("%5d %5d %5d\n", i, sign, sum); sign = sign * -1; } } }
- (5 points) Given the following
Contact
class:public class Contact { private int extension; private String[] info; public Contact(String[] info, int extension) { this.info = info; this.extension = extension; } public String toString() { String result; result = "Contact:\n"; for (int i=0; i<info.length; i++) { result += info[i] + "\n"; } result += "x" + extension + "\n"; return result; } }
what will be printed by the following code snippet? (Note: Be careful!)
public class Driver { public static void main(String[] args) { Contact[] contacts; String[] text; contacts = new Contact[3]; text = new String[2]; text[0] = new String("Prof. Bernstein"); text[1] = new String("Room 257"); contacts[0] = new Contact(text, 1671); System.out.println(contacts[0].toString()); text[0] = new String("Prof. Mayfield"); text[1] = new String("Room 208"); contacts[1] = new Contact(text, 3314); System.out.println(contacts[1].toString()); text[0] = new String("Prof. Sprague"); text[1] = new String("Room 226"); contacts[2] = new Contact(text, 3312); System.out.println(contacts[2].toString()); System.out.println("Everybody"); for (int i=0; i<contacts.length; i++) { System.out.println(contacts[i].toString()); } } }
- (5 points) Answer one of the following questions. (Note: If you answer both, you will receive the lower of the two grades.)
- Complete the following method.
/** * This method calculates the probability of tossing a * coin n times and and getting n heads. * * The probability of getting a single head is 0.5. The * probability of getting two heads is 0.5*0.5. * The probability of n heads is 0.5*0.5*...*0.5 (where there are * n terms in the product). * * @param n The number of tosses * @return The probability of n heads in n tosses */ public static double probabilityOfHeads(int n) { }
- Complete the following method.
/** * This method finds the number of open parentheses in * a String. * * @param text The String * @return The number of parentheses */ public static int numberOfParentheses(String text) { }
- Complete the following method.
- (5 points) Choose the best answer to each of the following: