CS111Final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

If the base case in a recursive method is never reached: A. the method will call itself only once B. the result will always be off by one C. the method will call itself indefinitely D. the method will never call itself

C; By definition

What will the following piece of code output to the screen? String str = "166PennsylvaniaAveDC"; int m = 0; while ( m < 6 ){ if (!Character.isLetter(str.charAt(m))) System.out.print(Character.toUpperCase(str.charAt(m))); m++; } A. PENNSYLVANIAAVEDOC B. PENNSYLVANIAVEDOC C. PennsylvaniaAVEDOC D. 166PENNSYLVANIAAVEDOC E. 166PENNSYLVANIAVEDOC F. 166

F; The method isLetter returns true or false, to indicate if the receiving character is a letter. Notice the !, negation, before Character, in the if clause. The toUpperCase method, if passed a number value, returns the number unchanged.

Describe the following line, taken from a UML diagram: + add(object2:Stock) : Stock

add is a public method that receives as an argument an object of type Stock, given the variable name object2, and the method returns a reference variable to an object of type Stock.

Given the following code, what can you say about all of the methods in ClassA? public class ClassB implements ClassA{}

Because ClassB implements ClassA, then: ClassA is an interface ClassA has all abstract methods ClassA contains only method headers ClassA has all methods that are public.

True / False If you write a toString method to display the contents of an object, object1, for a class, Class1, then the following two statements are equivalent: System.out.println(object1); System.out.println(object1.toString());

False; This is a poorly worded question, because the phrase, "display the contents of" is ambiguous. The toString method of the Object class returns a string consisting of the name of the class of which the object is an instance, the at-sign character "@", and the unsigned hexadecimal representation of the hash code of the object. Only if the toString method explicitly defined in Class1 does the same thing, are the two invocations the same.

Explain each part of the following piece of code: try { (try block statements...) } catch(NumberFormatException | IOException ex) { respondToError(); }

If code within the try block throws an error that is either aNumberFormatException or of type IOException, then that error will be caught and method respondToError() will be invoked.

The following code is syntactically correct True / False public class TestFrame extends Frame { public static void main(){ JLabel label = new JLabel("Test label"); } }

True; Class JFrame extends Frame

True / False A class's static methods do not operate on the fields that belong to any instance of the class.

True; Static methods can rely on ONLY static fields

True / False The String class's valueOf() method accepts a value of any primitive data type as an argument and returns a string representation of the value.

True; The valueOf method is an overloaded method that accepts a value of any primitive data type as an argument and returns a string representation of the value (pg 582).

For the below two Java files, circle any portion that contains a syntax error. Circle those parts of the Java code where there is a mistyped character, missing semicolon, etc. Also, circle those parts of the Java code where there are erroneous or additional letters, code fragments, etc. public class CreditCardException extends Exception{ public CreditCardException(String error){ super("Invalid Credit card : " + error); } } import java.util.Scanner; import java.io.*; public class CreditCardProcessor{ public static void main (String [] args){ File fileToParse = new File(CreditCardNumbers.txt); Scanner inputFile = new Scanner(fileToParse); try { if (inputFile.hasNext()){ String fileLine = inputFile.nextLine(); if (isGood(fileLine)){ System.out.println("Line " + fileLine + " successfully processed"); } } } throws (CreditCardException cce){ System.out.println(cce.getMessage()); } } private static boolean isGood(String accountLine) throw CreditCardException { if (accountLine.substring(0,1) = "2"){ throw CreditCardException("Wrong account"); } return 2; } }

Because File is used, either the class or main should have a throws IOException CreditCardNumbers.txt needs to be in parentheses Instead of trythrows, it should be trycatch The method isGood indicates that the method returns a boolean, but the method returns an integer. In the method isGood, accountLine.substring(0,1)="2" should be accountLine.substring(0,1)=="2" The method isGood throws (and not throw) a CreditCardException Within the method isGood, the statement should be throw new CreditCardException(..), instead of throw CreditCardExcepton(..)

How many lines will be printed by the below code? public class splitExample { public static void main(String args[]) { String str = "Love what you do, " + "and you will never have to work a day in your life."; String [] arrOfStr = str.split("k|fe"); for (String a : arrOfStr) System.out.println(a); } }

3

What will be the value of loc, after the following code is executed? int loc; String str = "The cow jumped over the moon."; loc = str.lastIndexOf("w "); loc *=str.lastIndexOf("moon"); A. 144 B. 111 C. 81 D. 20 E. 0 F. 20 G. 81 H. 111 I. 144

A; First, loc=6. lastIndexOf("moon") returns 24, so loc = 6 * 24 = 144. The lastIndexOf() method returns the position of the last occurrence of a specified value in a string. Note: The string is searched from the end to the beginning, but returns the index starting at the beginning, at position 0. This method returns -1 if the value to search for never occurs. Note: The lastIndexOf() method is case sensitive!

27. Which of the following statements is/are false, about exceptions? A. A catch clause that uses a parameter variable of the Exception type is capable of catching any exception that extends the Error class. B. The throw statement informs the compiler that a method throws one or more exception. C. The call stack is an internal list of all the methods that are currently executing. D. The throws clause causes an exception to be thrown. E. In versions of Java prior to Java 7, each catch clause can handle only one type of exception.

ABD Choice A is false because objects of typeException are Error have the same superclass(Throwable), so Exception cannot extendError, or vice-versa. Choice B is false because the throws, and the throw, statement informs the compiler that a method throws one or more exceptions. Choice C is true. Choice D is false because throw, and not throws, is the keyword that manually throws an exception. Choice E is true.

For the below UML diagram, which of the lettered choices is/are true? Course - courseName : String - instructor : Instructor - textBook : TextBook ----------------------------------------------------------- + Course(name : String, instr : Instructor, text : TextBook) + getName() : String + getInstructor() : Instructor + getTextBook() : TextBook + toString() : String A. The toString method in the Course class is identical to the toString method of the Object class B. The Course class is an aggregate class. C. At compile time, the Java compiler will create a default constructor, because a no-argument constructor has not been explicitly defined in the class Course. D. An object of type Course cannot be instantiated until at least one object of type TextBook and one object of type Instructor has been created beforehand and is publicly available.

B Course has two fields, instructor and textBook, that are non-native data types, so Course is an aggregate class. Choice A is not necessarily true, because you don't see the contents of the toString method in Course, which might output something different that the toString method in the Object class. Choice C is false, because java will not create a default constructor at compile time because at least one constructor is explicitly declared in the class. Choice D is false, because the Course constructor can be invoked with null as the second and third arguments: Course aCourse = new Course("John", null, null);

The try statement may have an optional ____________ clause, which must appear after all of the catch clauses. A. try-again B. finally C. default D. abort E. inherit F. break

B; By definition

What will display when the following code is executed: JLabel label = new JLabel ("It is a beautiful morning."); label.setIcon("SunnyFace.gif"); A. A label with the text "It is a beautiful day." to the left of the SunnyFace image. B. A label with the text "It is a beautiful day." to the right of the SunnyFace image. C. A label with the text "It is a beautiful day." below the SunnyFace image. D. A label with the text "It is a beautiful day." above the SunnyFace image.

B; The image is placed at the "front" of the label (pg 869)

For the following code, which method1 will be executed when the following statements are executed? ClassA item1 = new ClassB(); item1.method1(); Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB(){} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC(){} Line 14 public void method1(){} Line 15 } A. Line 4 B. Line 9 C. Line 14 D. This is an incorrect use of method1, and the program will crash

B; The method1 on line 9 will be called, because item1 is of type ClassB.

True / False A GUI program automatically stops executing when the end of the main method is reached.

False; A GUI program doesn't stop executing until the application that launches the GUI is closed. As long as a GUI is launched, the action listeners are "listening".

True / False If a method in a subclass has the same signature as a method in the superclass, the subclass method overloads the superclass method.

False; A method in a subclass that has the same signature as a method in the superclass overrides the method in the superclass.

True / False StringBuilder objects are immutable

False; An object of StringBuilder can be modified, unlike String objects.

True / False A catch clause that uses a parameter variable of the Exception type is capable of catching any exception that extends the Error class.

False; The Exception and Error classes are sibling classes (have the same superclass, Throwable), so neither one of them can be used to catch an exception of the other.

True / False When the below code is executed, the component x will be editable. x.setEditable(false);

False; The setEditable method is passed the value false, so the object on which the method is being called, x, will NOT be editable.

What does the following statement do: addButton.addActionListener(new AddButtonListener());

The AddButtonListener inner class is being registered to the object addButton.

For the following pseudocode algorithm, what is the base case for the algorithm gcd? Algorithim gcd(x,y) if (x<y) gcd (y,x) else if(y==0) return x else return gcd(y,x mod y) end gcd

The base case is when y == 0

True / False Recursive algorithms are usually less efficient than iterative algorithms.

True Recursive algorithms must instantiate as many copies of a variable as there are calls to the recursive method (on the stack), which can be significant.

True / False An abstract class is not instantiated, but serves as a superclass for other classes.

True; By definition, an abstract class is not instantiated, but subclassed.

Write a recursive method (larger box) that will print to the screen the message, "Another invocation," and invoke that recursive method (top box), so that the message "Another invocation" is printed 674 times.

printM(674); public static void printM(int n){ if (n == 1) System.out.println("Another invocation"); else { printM(n - 1); System.out.println("Another invocation"); }


Set pelajaran terkait

NSG 106 Chapter 4 Safety/Restraints

View Set

Current Leaders of different positions

View Set

Texas Teachers 158 Practice Exam

View Set

Chapter 10 Cognitive Psychology: Visual Imagery

View Set

HIST-1700-0035 Chapter 02 Diagnostic Quiz tthomas1515

View Set

module 17 quiz: At-Risk and Vulnerable Populations and Related Effects on Health

View Set

chapter 8-uses of life insurance

View Set

Class 4: The importance of Business Ethics

View Set

PHC6118 Pop Health Management Midterm

View Set