CSC220 Review for Final Exam Spring 2005 P. Van Hise Part 1 Given the nested if-else structure below, answer questions 1-2. if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; 1) If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed? a) 0 b) 2 c) 3 d) 4 e) 5 2) If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed? a) 0 b) 2 c) 3 d) 4 e) 5 3) The break statement does which of the following? a) ends the program b) transfers control out of the current control structure such as a loop or switch statement c) ends the current line of output, returning the cursor d) denotes the ending of a switch statement e) indicates the end of line when using System.out.print 4) If x is an int where x = 0, what will x be after the following loop terminates? while (x < 100) x *= 2; a) 2 a) 64 b) 100 c) 128 d) None of the above, this is an infinite loop 5) How many times will the following loop iterate? int x = 10; while (x > 0) { System.out.println(x); x--; } a) 0 times b) 1 time 6) 7) 8) c) 9 times d) 10 times e) 11 times Given that s is a String, what does the following loop do? for (int j = s.length( ); j > 0; j--) System.out.print(s.charAt(j-1)); a) it prints s out backwards b) it prints s out forwards c) it prints s out backwards after skipping the last character d) it prints s out backwards but does not print the 0 th character e) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0 The following nested loop structure will execute the inner most statement (x++) how many times? for (int j = 0; j < 100; j++) for (int k = 100; k > 0; k--) x++; a) 100 b) 200 c) 10,000 d) 20,000 e) 1,000,000 In order to create a constant, you would use which of the following Java reserved words? a) private b) static c) int d) final e) class Part 2 1) An exception can produce a “call stack trace” which lists a) the active methods in the order that they were invoked b) the active methods in the opposite order that they were invoked c) the values of all instance data of the object where the exception was raised d) the values of all instance data of the object where the exception was raised and all local variables and parameters of the method where the exception was raised e) the name of the exception thrown 2) A finally clause will execute a) only if the try statement that precedes it does not throw an exception b) only if the try statement that precedes it throws an exception that is caught c) only if the try statement that precedes it throws an exception that is not caught d) only if the try statement that precedes it throws an exception, whether it is caught or not e) in any circumstance 3) Which of the following messages passed to the String str could throw a StringIndexOutOfBoundsException? a) str.length( ) b) str.charAt(2); c) str.replace(‘a’, ‘A’); d) str.equals(str); e) any of the above could throw a StringIndexOutOfBoundsException 4) Character streams manage a) byte-sized data b) binary data c) Unicode characters d) ASCII characters e) compressed data 5) The Scanner class provides an abstraction for input operations by a) using try and catch statements to catch any IOException instead of throwing the Exception elsewhere b) parsing input lines into individual tokens c) performing conversion operations from String to the appropriate type as specified in the Scanner message d) inputting from the standard input stream if create is called using System.in e) all of the above 6) The difference between a checked and an unchecked exception is: a) checked exceptions need not be listed in a throws clause b) unchecked exceptions must be listed in a throws clause c) neither kind of exception follows the rules of exception propagation d) an unchecked exception requires no throws clause e) a checked exception always must be caught by a try block; an unchecked exception does not public int question(int x, int y) { if (x == y) return 0; else return question(x-1, y) + 1; } 7) If the method is called as question(8, 3), what is returned? a) 11 b) 8 c) 5 d) 3 e) 24 8) Calling this method will result in infinite recursion if which condition below is initially true? a) (x = = y) b) (x != y) c) (x > y) d) (x < y) e) (x = = 0 && y != 0) 9) The following method should: return true if the int parameter is even and either positive or 0, and false otherwise. Which set of code should you use to replace … so that the method works appropriately? a) b) c) d) e) public boolean question(int x) { … } if (x = = 0) return true;else if (x < 0) return false;else return question(x – 1); if (x = = 0) return false;else if (x < 0) return true;else return question(x – 1); if (x = = 0) return true;else if (x < 0) return false;else return question(x – 2); if (x = = 0) return false;else if (x < 0) return true;else return question(x – 2); return(x = = 0); 10) What does the following method compute? Assume the method is called initially with i = 0 public int question(String a, char b, int i) { if (i = = a.length( )) return 0; else if (b = = a.charAt(i)) return question(a, b, i+1) + 1; else return question(a, b, i+1); } a) b) c) d) e) The length of String a The length of String a concatenated with char b The number of times char b appears in String a Returns 1 if char b appears in String a at least once, and 0 otherwise The char which appears at location i in String a Assume that int[ ] a = {6, 2, 4, 6, 2, 1, 6, 2, 5} and consider the two recursive methods below, foo and bar. public int foo(int[ ] a, int b, int j) { if (j < a.length) if (a[j] != b) return foo (a, b, j+1); else return foo (a, b, j+1) + 1; else return 0; } public int bar(int[ ] a, int j) { if (j < a.length) return a[ j ] + bar(a, j+1); else return 0; } 11) What is the result of calling foo(a, 2, 0);? a) 0 b) 1 c) 2 d) 3 e) 4 12) What is the result of calling bar(a, 0);? a) b) c) d) e) 0 5 6 12 34 13) What is the result of bar(a, 8);? a) 0 b) 5 c) 6 d) 12 e) 34 14) What does the following recursive method determine? public boolean question(int[ ]a, int[ ] b, int j) { if (j = = a.length) return false; else if (j = = b.length) return true; else return question(a, b, j+1); } a) Returns true if a and b are equal in size, false otherwise b) Returns true if a is larger than b, false otherwise c) Returns true if b is larger than a, false otherwise d) Returns true if a and b have no elements e) Returns the length of array a + length of array b Part 3 For questions 1 – 4, use the following partial class definitions: public class A1 { public int x; private int y; protected int z; … } public class A2 extends A1 { protected int a; private int b; … } public class A3 extends A2 { private int q; … } 1) Which of the following is true with respect to A1, A2 and A3? a) A1 is a subclass of A2 and A2 is a subclass of A3 b) A3 is a subclass of A2 and A2 is a subclass of A1 c) A1 and A2 are both subclasses of A3 d) A2 and A3 are both subclasses of A1 e) A1, A2 and A3 are all subclasses of the class A 2) Which of the following lists of instance data are accessible in class A2? a) x, y, z, a, b b) x, y, z, a c) x, z, a, b d) z, a, b e) a, b 3) Which of the following lists of instance data are accessible in A3? a) x, y, z, a, b, q b) a, b, q c) a, q d) x, z, a, q e) x, a, q 4) Which of the following is true regarding the use of instance data y of class A1? a) it is accessible in A1, A2 and A3 b) it is accessible in A1 and A2 c) it is accessible only in A1 d) it is accessible only in A3 e) it is not accessible to any of the three classes 5) The instruction super( ); does which of the following? a) calls the method super as defined in the current class b) calls the method super as defined in the current class’ parent class c) calls the method super as defined in java.lang d) calls the constructor as defined in the current class e) calls the constructor as defined in the current class’ parent class 6) Inheritance through an extended (derived) class supports which of the following concepts? a) interfaces b) modulary c) information hiding d) code reuse e) correctness 7) Aside from permitting inheritance, the visibility modifier protected is also used to a) permit access to the protected item by any class defined in the same package b) permit access to the protected item by any static class c) permit access to the protected item by any parent class d) ensure that the class can not throw a NullPointerException e) define abstract elements of an interface 8) Which of the following is an example of multiple inheritance? a) a computer can be a mainframe or a PC b) a PC can be a desktop or a laptop c) a laptop is both a PC and a portable device d) a portable device is a lightweight device e) Macintosh and IBM PC are both types of PCs 9) Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by a) importing classes b) implementing interfaces c) overriding parent class methods d) creating aliases e) using public rather than protected or private modifiers 10) If a programmer writes a class wanting it to be extended by another programmer, then this programmer must a) change private methods and instance data to be protected b) change public methods and instance data to be protected c) change all methods to be protected d) change the class to be protected e) none of the above, the programmer does not have to change anything 11) All classes in Java are directly or indirectly subclasses of the _______ class. a) Wrapper b) String c) Reference d) this e) Object 12) Which of the following is not a method of the Object class? a) clone b) compareTo c) equals d) toString 13) Which of the following is true regarding Java classes? a) All classes must have 1 parent but may have any number of children (derived or extended) classes. b) All classes must have 1 child (derived or extended) class but may have any number of parent classes. c) All classes must have 1 parent class and may have a single child (derived or extended) class. d) All classes can have any number (0 or more) of parent classes and any number of children (derived or extended) classes. e) All classes can have either 0 or 1 parent class and any number of children (derived or extended) classes. 14) In order to determine the type that a polymorphic variable refers to, the decision is made a) by the programmer at the time the program is written b) by the compiler at compile time c) by the operating system when the program is loaded into memory d) by the Java run-time environment at run time e) by the user at run time For questions 15-17, assume that Student, Employee and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney. Consider the following code where … are the required parameters for the constructors: Person p = new Person(…); int m1 = p.getMoney( ); // assignment 1 p = new Student(…); int m2 = p.getMoney( ); // assignment 2 if (m2 < 100000) p = new Employee(…); else if (m1 > 50000) p = new Retired(…); int m3 = p.getMoney( ); // assignment 3 15) The reference to getMoney( ) in assignment 1 is to the class a) Person b) Student c) Employee d) Retired e) none of the above, this cannot be determined by examining the code 16) The reference to getMoney( ) in assignment 2 is to the class a) Person b) Student c) Employee d) Retired e) none of the above, this cannot be determined by examining the code 17) The reference to getMoney( ) in assignment 3 is to the class a) Person b) Student c) Employee d) Retired e) none of the above, this cannot be determined by examining the code 18) Which of the following methods is a static method? The class in which the method is defined is given in parentheses following the method name. a) equals (String) b) toUpperCase (String) c) sqrt (Math) d) format (DecimalFormat) e) paint (Applet) 19) An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship? a) inner b) i c) private d) this e) static 20) Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by a) importing classes b) implementing interfaces c) overriding parent class methods d) creating aliases e) using public rather than protected or private modifiers 21) In which phase of program development would you expect the programmer(s) to create the pseudocode? a) Software requirements b) Software design c) Software implementation d) Software testing e) Could occur in any of the above Part 4 For questions 1-4, assume values is an int array that is currently filled to capacity, with the following values: 9 4 12 1) What is returned by values[3]? a) 9 b) 12 c) 2 d) 6 e) 3 2) What is the value of values.length? a) 0 b) 5 c) 6 d) 7 e) 18 3) 2 6 8 18 What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0. for (j=0; j < list.length; j++) if (list[j] < temp) c++; a) b) c) d) e) It finds the smallest value and stores it in temp It finds the largest value and stores it in temp It counts the number of elements equal to the smallest value in list It counts the number of elements in list that are less than temp It sorts the values in list to be in ascending order An int array stores the following values. Use the array to answer questions 4 – 7 9 4 12 2 6 8 18 4) Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm? a) 9, 4, 12, 2, 6, 8, 18 b) 4, 9, 12, 2, 6, 8, 18 c) 2, 4, 12, 9, 6, 8, 18 d) 2, 4, 6, 8, 9, 12, 18 e) 2, 4, 9, 12, 6, 8, 18 5) Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm? a) 9, 4, 12, 2, 6, 8, 18 b) 2, 4, 9, 6, 12, 8, 18 c) 2, 4, 12, 9, 6, 8, 18 d) 2, 4, 6, 8, 9, 12, 18 e) 2, 4, 12, 6, 8, 9, 18 6) How many passes will it take in all for Selection Sort to sort this array? a) 2 b) 4 c) 5 d) 6 e) 7 7) What are the main programming mechanisms that constitute object-oriented programming? a) Encapsulation, inheritance, polymorphism b) Encapsulation, abstraction, inheritance c) Inheritance, polymorphism, recursion d) Polymorphism, recursion, abstraction e) None of the above 8) What is printed by the following code? Consider the polymorphic invocation. public class Inherit { class Figure { void display( ) { System.out.println("Figure"); } } class Rectangle extends Figure { void display( ) { System.out.println("Rectangle"); } } class Box extends Figure { void display( ) { System.out.println("Box"); } } Inherit( ) { Figure f = new Figure( ); Rectangle r = new Rectangle( ); Box b = new Box( ); f.display( ); f = r; f.display( ); f = b; f.display( ); } public static void main(String[ ] args) { new Inherit( ); } } a) Figure Rectangle Box b) Rectangle Box c) Figure Figure Figure d) Syntax error. This code won't compile or execute e) None of the above 9) One way polymorphism is achieved is by a) extending b) overriding c) embedding d) abstraction e) encapsulation 10) Which of these methods will sort an array of floats into ascending order? a) void arrange(double[ ] ary) { for (int n=0; n<ary.length; n++) for (int k=n; k<ary.length; k++) if (ary[n] > ary[k]) { double x = ary[n]; ary[n] = ary[k]; ary[k] = x; } } b) void arrange(double [ ] ary) { for (int n=0; n<ary.length; n++) for (int k=n; k<ary.length; k++) if (ary[n] < ary[k]) { double x = ary[n]; ary[n] = ary[k]; ary[k] = x; } } c) void arrange(double [ ] ary) { for (int n=1; n<=ary.length; n++) for (int k=n; k<ary.length; k++) if (ary[n] > ary[k]) { double x = ary[n]; ary[n] = ary[k]; ary[k] = x; } } d) void arrange(double [ ] ary) { for (int n=0; n<ary.length; n++) for (int k=n; k<ary.length; k++) if (ary[n] > ary[k]) double x = ary[n]; ary[n] = ary[k]; ary[k] = x; } e) None of the above ANSWERS: Part 1 1.C 2B 3B 4D 5D 6A 7C 8D Part 2 1. B Explanation: The call stack trace provides the names of the methods as stored on the run-time stack. The method names are removed from the stack in the opposite order that they were placed, that is, the earliest method was placed there first, the next method second, and so forth so that the most recently invoked method is the last item on the stack, so it is the first one removed. The stack trace then displays all active methods in the opposite order that they were called (most recent first). 2E 3B 4.C Explanation: Character streams are used to manage 16-bit Unicode characters. This differs from a byte stream that is used to manage any kind of byte-sized data, including ASCII characters and binary data of other types. 5 E Explanation: Input into a Java program is difficult because it requires a lot of overhead. The Scanner class implements all of that overhead so that the programmer does not have to see it. Thus, Scanner is an abstraction for performing input operations without the details. These details include importing java.io classes, handling IOExceptions in some way, inputting from the standard input stream, dividing the input into individual tokens and converting each token as needed into the requesting form. 6 D Explanation: A checked exception must either be caught or it must be listed in a throws clause. An unchecked exception requires no throws clause. Both kinds of exceptions follow the rules of exception propagation. 7C 8D 9C 10 C 11 D 12 E 2C 3D 4C 5E 6D 13 B 14 B Part 3 1B 7 A Explanation: The visibility modifier protected is used to control access to the item in a protected (guarded) manner. The protection is that access is restricted to the current class (like private items), classes in the same package, or extended classes of this class. 8 C Explanation: Multiple inheritance means that a given class inherits from more than one parent class. Of those listed above, a laptop computer inherits properties from both PCs and portable devices. The answers in a, b and e are all examples of single inheritance where a class has at least two children (in a, computer has children mainframe and PC, in b, PC has children desktop and laptop, in e, PC has children Macintosh and IBM PC). Answer d denotes a property of a class. 9B 10 A 11 E 12 B 13 A 14 D 15 A Explanation: At this point of the program, p is a Person, and so getMoney is a reference to Person’s getMoney method. 16 B Explanation: At this point of the program, p is a Student, and so getMoney is a reference to Student’s getMoney method. 17 E 18 C 19 D 20 B 21 B Answer: Pseudocode is a description of an algorithm written in an English-like way rather than in a specific programming language. This is part of the program’s design. In the implementation phase, the programmer(s) translates the pseudocode into the programming language being used. Part 4 1C 2D 3D 4C 5C 6D 7A 8A 9B 10 A