Practice exam 3
- 
Written Portion of the Final Exam - Sample
Answer all of the following questions. This is a "closed book" examination and you must work entirely on your own. You may use the following reference card (written in UML).
- (5 points) Choose the best answer to each of the following:
(1) _____In the Java statement branch = account.charAt(0);,accountis:- An attribute
 - A class
 - A method
 - An object
 - A parameter
 
(2) _____In the Java statement branch = account.charAt(0);,charAtis:- An attribute
 - A class
 - A method
 - An object
 - A parameter
 
(3) _____In the Java statement branch = account.charAt(i);,iis:- 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);,owesmust be declared to be a:booleanchardoubleint- 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");,currencyis 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];,monthis:- 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 Stringobjects (i.e., as the actual parameter) to a method that has a formal parameter that is declared to be aStringobject.Passing an array of Stringobjects (i.e., as the actual parameter) to a method that has a formal parameter that is declared to be an array ofColorobjects. - (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 theStringclass)?What does it mean when one says an object is immutable (like objects in the Stringclass)?In addition to the fact that it doesn't include the immutablestereotype in the UML class diagram, how can you tell that objects in theRectangleclass are mutable?In addition to their names and parameters, what else is different about the intValue()andparseInt()methods in theIntegerclass?Why is it important that the String,Color,PointandRectangleclasses haveequals()methods? - (10 points) Write a statement that:
Declares mandnto beintvariables.Assigns the value -7tom.Uses the static absmethod in theMathclass to assign the absolute value ofmton.Declares individualsto be an array ofPersonobjects.Initializes individualsso that it can containndifferentPersonobjects.Declares hasCreditto be an array ofbooleanvalues.Initializes hasCreditso that it can contain as many values as there are objects inindividuals. Note: You may not usemorn.Loops over all of the elements in hasCredit, assigningtrueto each.Declares currentto be aBillableobject.Instantiates currentusing the explicit value constructor in theBillableclass that is passed an array ofPersonobjects and an array ofbooleanvalues (passing itindividualsandhasCredit). - (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 
Contactclass: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: