CS 111

¡Supera tus tareas y exámenes ahora con Quizwiz!

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");

144. First, loc=6. lastIndexOf("moon") returns 24, so loc = 6 * 24 = 144.

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++; }

166. 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.

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

Which of the following statements is/are false, about exceptions?

A catch clause that uses a parameter variable of the Exception type is capable of catching any exception that extends the Error class. The throw statement informs the compiler that a method throws one or more exception. The throws clause causes an exception to be thrown. 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. The call stack is an internal list of all the methods that are currently executing. Choice D is false because throw, and not throws, is the keyword that manually throws an exception. Choice E is true. In versions of Java prior to Java 7, each catch clause can handle only one type of exception.

What will display when the following code is executed: JLabel label = new JLabel ("It is a beautiful morning."); label.setIcon("SunnyFace.gif");

A label with the text "It is a beautiful day." to the right of the SunnyFace image. The image is placed at the "front" of the label (pg 869).

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.

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 try-throws, it should be try-catch 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(..)

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".

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.

StringBuilder objects are immutable.

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

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.

When the below code is executed, the component x will be editable. ???

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

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.

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 }

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

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

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

For the below UML diagram, which of the lettered choices is/are true? - courseName : String - instructor : Instructor - textBook : TextBook + Course(name : String, instr : Instructor, text : TextBook) + getName() : String + getInstructor() : Instructor + getTextBook() : TextBook + toString() : String

The Course class is an aggregate class. 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);

For the following pseudocode algorithm, what is the base case for the algorithm gcd? Algorithm 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

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.

There are many solutions to this questions. One is shown on page 986 of the textbook. Another is: 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"); }

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.

The following code is syntactically correct. public class TestFrame extends Frame { public static void main(){ JLabel label = new JLabel("Test label"); x.setEditable(false); } }

True. Class JFrame extends Frame.

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.

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).

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.

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.

The try statement may have an optional ____________ clause, which must appear after all of the catch clauses.

finally. By definition.

If the base case in a recursive method is never reached:

the method will call itself indefinitely. By definition.


Conjuntos de estudio relacionados

Ch 10 - Electrical Box Installation

View Set

7.2 - Comparativos (From Spanish 1)

View Set

California: Real Estate Principles - Chapter 8

View Set

Ch 8B Meiosis and Sexual Reproduction Homework

View Set

CA Real Estate Finance Course Exam

View Set

Software Architecture and Design Concepts

View Set

chpt 11 - unemployment & labor force participation

View Set

Chapter 23: Assessment of Respiratory Function

View Set

(PrepU) Chapter 8: Communication

View Set

Ch 9- An introduction to Microbial Genetics

View Set