CTP 150 final prep 4

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

A method that is defined but not implemented is called an ___ method.

abstract

Which of the following is the correct signature for an equals method for the class named Quiz?

boolean equals(Quiz obj)

What is the value of str after the following code has been executed? String str; String sourceStr = "Hey diddle, diddle, the cat and the fiddle"; str = sourceStr.substring(12,17);

diddl

To convert the string, str = "285.74" to a double and store it in the variable x, use the following statement.

double x = Double.parseDouble(str);

When an exception is thrown, ____.

it must be handled by the program or by the default exception handler

If a class contains an abstract method, ______.

the method will have only a header, but not a body, and end with a semicolon

When you are writing a program with String objects that may have unwanted spaces at the beginning or end of the strings, use this method to delete them.

trim()

When a method throws a user-defined exception,

you have to use throw statement statement explicitly.

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.indexOf("ov");

15 [Note]: look this one up its confusing.

If, within one try statement you want to have catch clauses of the following types, in which order should they appear in your program? (1) Exception (2) IllegalArgumentException (3) RuntimeException (4) Throwable

2314 The catch clauses must be listed from most specific to least specific since the first possible match will be selected.

When a method's return type is a class, what is actually returned to the calling program?

A reference to an object of that class

What will the following code display? String str = "abc456"; int m=0; while (m<6) { if (Character.isLetter(str.charAt(m))) System.out.print(Character.toUpperCase(str.charAt(m))); m++; }

ABC

Assuming that the ABC class defines a copy constructor, give the statement to create a new instance of the ABC class named myObject which is a copy of the instance named oldObject.

ABC myObject = new ABC(oldObject); ABC myObject=new ABC(oldObject);

What is output when the following program runs? public class ExceptionTest { public static void main(String[] args) { String str; int number; try { str = "123"; number = Integer.parseInt(str); System.out.print("A"); } catch(NumberFormatException ex) { System.out.print("B"); } finally { System.out.print("C"); } System.out.print("D"); } }

ACD

How are interfaces like abstract classes?

Both may define abstract methods. Neither can be instantiated.

In the following code segment, assume the variable, custNumber, contains a String value. What does the code do? int i = 0; boolean goodSoFar = true; while (goodSoFar && i < 7) { if (!Character.isDigit(custNumber.charAt(i))) goodSoFar = false; i++; }

Checks if the String contains all digits

Given the following code, which of the following is true? public class ClassB implements ClassA{}

ClassB must override each method in ClassA

In the following statement, which is the superclass? public class ClassX extends ClassY implements ClassZ

ClassY

Match each description with the term.

Correct! Interface : contains only abstract methods and/or constants Concrete class: a class that can be instantiated Sub-class: a class that is derived from another class Abstract class: a partially completed class Super-class :a class that serves as a base class of another class

A(n) ________ is an object that is generated in memory as the result of an error or an unexpected event.

Exception

Quiz 11 This is a section of code that gracefully responds to exceptions when they are thrown.

Exception handler

In an inheritance relationship, the derived class constructor always executes before the superclass constructor.

False

The throws clause causes an exception to be thrown.

False

Quiz 7 The JVM periodically performs this process to remove unreferenced objects from memory.

Garbage collection

The term for the relationship created by object aggregation is

Has a

When you write a method that throws a checked exception, you must ___.

Have a throws clause in the method header

A user-defined exception

Is a checked exception

Look at the following code. What is missing from ClassA? Line 1 public interface MyInterface Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double); Line 5 } Line 6 public class ClassA implements MyInterface Line 7 { Line 8 FIELDA = 60; Line 9 public int methodB(double) { } Line 10 }

It does not override methodA

When the this variable is used to call a constructor,

It must be the first statement in the constructor making the call

Assume that the classes BlankISBN, NegativePrice, and NegativeNumberOrdered are exception classes that inherit from Exception. The following code is a constructor for the Book class. What must be true about any method that instantiates the Book class with this constructor? public Book(String ISBNOfBook, double priceOfBook, int numberOrderedOfBook) throws BlankISBN,NegativePrice,NegativeNumberOrdered { if (ISBNOfBook == "") throw new BlankISBN(); if (priceOfBook < 0) throw new NegativePrice(priceOfBook); if (numberedOrderedOfBook < 0) throw new NegativeNumberOrdered(numberOrderedv); ISBN = ISBNOfBook; price = priceOfBook; numberedOrdered = numberOrderedOfBook; }

It must handle all of the possible exceptions thrown by the constructor or have its own throws clause specifying them

In a catch statement, what does the following code do? System.out.println(e.getMessage());

It prints the error message for an exception.

In the following code, what will the call to super do? public class ClassB extends ClassA { public ClassB() { super(40); System.out.println("Constructor complete!"); } }

It will call the constructor of ClassA that receives an integer as an argument

Polymorphism is accomplished using ___.

Late binding

Look at the following code. Which line has an error? Line 1 public interface Interface1 Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double x){ x = 0.0; } Line 5 }

Line 4 why? The method is an int type but the sole value inside it is a double.

Look at the following code. The method in line ________ will override the method in line ________. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a){} Line 5 public int method2(int b){} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b){} Line 11 public int method2(double c){} Line 12 }

Lines: 10, 4

Look at the following code. The method in line ________ will override the method in line ________. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a){} Line 5 public double method2(int b){} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b, int c){} Line 11 public double method2(double c){} Line 12 }

No methods are overridden in the code given

When a field is declared static, there will be

Only one copy of the field in memory

Quiz 9 When declaring class data members, it is best to declare them as

Private members

Which of the following is true about protected access?

Protected members are actually named constants Protected members cannot be accessed by methods in any other classes [Correct Answer] Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package You Answered Protected members may be accessed by methods in the same package or in a subclass, but only if the subclass is in the same package

If ClassA is derived from ClassB, then

Public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA

If you have defined a class SavingsAccount with a public static method getNumberOfAccounts() which of the following can be used in another class to call the getNumberOfAccounts()method? (Assume that an instance of the class has not been created.)

SavingsAccount.getNumberOfAccounts();

To convert the int variable, number to a string, use the following statement.

String str = Integer.toString(number);

Which of the following statements will print the maximum value an int variable may have?

System.out.println(Integer.MAX_VALUE);

Which of the following will call the toString method of the object myObject?

System.out.println(myObject.toString()); [true] All samples given will call the toString method. System.out.println(myObject); System.out.println("Hello " + myObject);

When an exception is thrown by code in the try block, the JVM begins searching the try statement for a catch clause that can handle it and passes control of the program to ___.

The first catch clause that can handle the exception

If the program does not handle an unchecked exception,

The program is halted and the default exception handler handles the exception [Note] Only a checked exception will cause a compilation error. When a method throws an unchecked exception and the caller is not prepared to handle the exception (does not have a try/catch), the exception is returned to the JVM which handles the exception by aborting the program. A good example is Input_Mismatch_Exception thrown by the Scanner when a character is given in response to nextInt method call. The code will compile without a try/catch around the nextInt call. However, the program will abort when the exception occurs.

What does the following statement do? Float number = new Float(8.8);

The statement creates a Float object, initializes the object to 8.8 and stores the object's address in the variable number.

The only limitation that static methods have is

They cannot refer to non-static members of the class

All exceptions are instances of classes that extend this class.

Throwable

A checked exception must either be handled via try-catch constructs, or the throwing method must specify that the appropriate exception type may be thrown by appending a throws clause to the method's definition.

True

Every class has a toString method and an equal's method inherited from the Object class.

True

Every class is either directly or indirectly derived from the Object class.

True

It is not possible for a superclass to call a subclass's method.

True

Quiz 8 True or False. Wrapper classes are used to store primitive values as an object.

True

The key word this is the name of a reference variable that an object can use to refer to itself.

True

True or False. A class may implement multiple interfaces.

True

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

True

True or False. An interface defines behaviors that a class must implement.

True

When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.

True

When an object reference is passed to a method, the method may change the values in the object.

True

Two ways of concatenating two Strings are ____.

Use the concat() method or use the + between the two Strings

If object1 and object2 are objects of the same class, to make object2 a copy of object1

Write a copy method that will make a field by field copy of object1 data members into object2 data members

If the following is from the method section of a UML diagram, which of the following statements is true? + add(object2:Stock): Stock

[ True] This is a public method named add that accepts and returns references to objects in the Stock class. [ Not True] This is a public method named Stock that adds two objects. [ Not True] This is a private method named Stock that adds two objects. [ Not true ] This is a private method named add that accepts and returns objects of the Stock class.

If the following is from the method section of a UML diagram, which of the following statements is true? + equals(object2:Stock) : boolean

[Not True] This is a private method that returns a boolean value [True] This is a public method that accepts a Stock object as its argument and returns a boolean value [Not True] This is a public method that returns a Stock object [Not True] This is a private method that receives two objects from the Stock class and returns a boolean value

Which of the following is not true about static methods?

[True]They are often used to create utility classes that perform operations on data, but have no need to collect and store data. [True]They are created by placing the key word static after the access specifier in the method header. [True]It is not necessary for an instance of the class to be created to execute the method. [Not True]They must be called from an instance of the class.

Which of the following is required for polymorphism?

[all are correct]All of the items listed are required Classes must be in the same class hierarchy A subclass must override a superclass method The subclass object instance must be referenced using a superclass reference

Which of the following is true about the statement below? Double myDouble = 5.5;

[not True] The statement performs unwrapping. Correct! [True] The statement performs autoboxing. [Not True] It results in an error because you cannot assign a wrapper class object to a primitive variable. [Not True]The statement performs unboxing.


Conjuntos de estudio relacionados

Chapter 25: The Rise of the Warrior Class in Japan

View Set

Meteorology and Climatology Ch 3

View Set

Basic Accounting Principles Review

View Set

Chapter 8 Searching and Sorting Arrays

View Set

AP Gov - Civil Rights and Civil Liberties

View Set

AUD SU 3.4: Understanding the entity and its environment

View Set

Week 5: Module 2, Section A, Ch 1, 2, 3

View Set