COSC 1337 Chapters 8-11 Midterm

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

Java automatically stores this value in all uninitialized static member variables

0

Look at the following declaration: enum Tree { OAK, MAPLE, PINE } What is the ordinal value of the MAPLE enum constant?

1

What will be the tokens for the following code? String str = "123-456-7890"; String[] tokens = str.split("-");

123 456 7890

The no-arg constructor for a StringBuilder object gives the object enough storage space to hold

16 characters

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

2,3,1,4

In the following code, assume that inputFile references a Scanner object that has been successfully used to open a file: double totalIncome = 0.0; while (inputFile.hasNext()) { try { totalIncome += inputFile.nextDouble(); } catch(InputMismatchException e) { System.out.println("Non-numeric data encountered " + "in the file."); inputFile.nextLine(); } finally { totalIncome = 35.5; } } What will be the value of totalIncome after the following values are read from the file?

35.5

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

456

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

CRC stands for:

Class, Responsibilities, Collaborations

In the following statement, which is the subclass? public class ClassA extends ClassB implements ClassC

ClassA

In the following statement, which is the superclass (ClassA, ClassB, or ClassC)? public class ClassA extends ClassB implements ClassC

ClassB

Given the following code which of the following is TRUE? public class ClassB implements ClassA{} ClassA must override each method in ClassB or ClassB must override each method in ClassA.

ClassB must override each method in ClassA

In the following statement, which is the interface? public class ClassA extends ClassB implements ClassC

ClassC

look at the following declaration: enum Color { RED, ORANGE, GREEN, BLUE } write a statement that defines a variable of this type and initializes it with a BLUE value

Color.Blue

To convert the string, str = "285.74" to a double, use the statement?

Double.toString(str);

If you want to append data to the existing binary file, BinaryFile.dat, write two statements to open the file.

FileOutputStream fstream = new FileOutputStream("BinaryFile.dat", true); DataOutputStream binaryOutputFile = new DataOutputStream(fstream);

Look at the following statement: StringBuilder str = new StringBuilder(25); What will the StringBuilder constructor do?

Give the object, str, 25 bytes of storage and not store anything in them

If a subclass constructor does not explicitly call a superclass constructor

Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes

What would be the results of executing the following code? StringBuilder str = new StringBuilder("Little Jack Horner "); str.append("sat on the "); str.append("corner");

Little Jack Horner sat on the corner

look at the following declaration: enum Color { RED, ORANGE, GREEN, BLUE } what are the enum constants for this type?

RED, ORANGE, GREEN, BLUE

List 5 data types that a method can return

String Int Double Character Array

What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { int init = 10; super(40); } }

The call to the method super must be the first statement in the constructor.

Find the Error int number = 99; String str; str = String.valueOf(number); // change the very first character to 'Z' str.setCharAt(1, 'Z');

The character should be stored at 0 not 1

What will be returned from a method, if the following is the method header? public Rectangle getRectangle()

The values and methods from the Rectangle Class

Each of the numeric wrapper classes has a static ________ method that converts a number to a string

ToString

Assuming that str is declared as follows: String str = "RSTUVWXYZ"; What value will be returned from str.charAt(5)?

W

What is term used for a class that is "wrapped around" a primitive data type and allows you to create objects instead of variables?

Wrapper class

To read data from a binary file you create objects from the following classes: a. FileInputStream and DataInputStream b. File and PrintWriter c. File and Scanner d. BinaryFileReader and BinaryDataReader

a. FileInputStream and DataInputStream

All fields declared in an interface: a. are final and static b. have protected access c. must be initialized in the class implementing the interface d. have private access

a. are final and static

An exception's default error message can be retrieved using this method. a. getMessage() b. GetErrorMessage() c. getDefaultMessage() d. getDefaultErrorMessage()

a. getMessage()

When you write a method that throws a checked exception, you must: a. have a throws clause in the method header b. override the default error method c. use each class only once in a method d. ensure that the error will occur at least once each time the program is executed

a. have a throws clause in the method header

An abstract method has: a. only a header and no body b. only a body and no header c. body ends with a semicolon. d. no header and no body

a. only a header and no body

A subclass may call an overridden superclass method by: a. prefixing its name with the super key word and a dot (.) b. prefixing its name with the name of the superclass in parentheses c. using the extends keyword before the method is called d. calling the superclass method first and then calling the subclass method

a. prefixing its name with the super key word and a dot (.)

What is meant when it is said that an exception is thrown?

an exception object has been created in response to an error that has occurred

All exceptions are instances of classes that extend this class a. RunTimeException b. Throwable c. Error d. Exception

b. Throwable

The IllegalArgumentException class extends the RuntimeException class, and is therefore: a. a checked exception class b. an unchecked exception class c. never used directly d. none of the above

b. an unchecked exception class

A(n) ________ is an object that is generated in memory as the result of an error or an unexpected event. a. exception handler b. exception c. default exception handler d. error message

b. exception

Why does the following code cause a compiler error? try { number = Integer.parseInt(str); } catch (IllegalArgumentException e) { System.out.println("Bad number format."); } catch (NumberFormatException e) { System.out.println(str + " is not a number."); }

because NumberFormatException runs after IllegalArgumentException

To write data to a binary file you create objects from the following classes: a. File and PrintWriter b. File and Scanner c. FileOutputStream and DataOutputStream d. BinaryFileWriter and BinaryDataWriter

c. FileOutputStream and DataOutputStream

Like a family tree, a ________ shows the inheritance relationship between classes. a. flowchart b. class map c. class hierarchy d. binary tree

c. class hierarchy

When an exception is thrown: a. it must always be handled by the method that throws it b. the program terminates even if the exception is handled c. it must be handled by the program or by the default exception handler d. it may be ignored

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

The super statement that calls the superclass constructor: a. must be the first statement in the superclass's constructor b. can appear in any method of the subclass c. must be the first statement in the subclass's constructor d. is deprecated and is no longer supported in newer versions of Java

c. must be the first statement in the subclass's constructor

All methods specified by an interface has default a. private access b. protected access c. public access d. packaged access

c. public access

What key word can you use to call a superclass constructor explicitly? a. goto b. this c. super d. extends

c. super

In a try/catch construct, after the catch statement is executed: a. the program returns to the statement following the statement in which the exception occurred b. the program terminates c. the program resumes at the statement that immediately follows the try/catch construct d. the program resumes at the first statement of the try statement

c. the program resumes at the statement that immediately follows the try/catch construct

A protected member of a class may be directly accessed by: a. methods of the same class b. methods of a subclass c. methods in the same package d. All of the above

d. All of the above

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

d. abort

When an "is a" relationship exists between objects, it means that the specialized object has: a. some of the characteristics of the general class, but not all, plus additional characteristics b. some of the characteristics of the general object, but not all c. none of the characteristics of the general object d. all the characteristics of the general object, plus additional characteristics

d. all the characteristics of the general object, plus additional characteristics

If you do not provide an access specifier for a class member, the class member is given ________ by default. a. private access b. public access c. protected access d. package access

d. package access

If your code does not handle and exception when it is thrown, _________ prints an error message and crashes the program

default exception handler

A declaration for an enumerated type begins with this key word.

enum

Find the Error // Superclass public class Vehicle { private double cost; (Other methods ) } // Subclass public class Car extends Vehicle { public Car (double c) { cost =c; } }

expands is not a modifier. can use extends

If str is declared as: String str = "ABCDEFGHI"; What will be returned from Character.toLowerCase(str.charAt(5))?

f

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

garbage collector

The term for the relationship created by object aggregation is:

has a

When the this variable is used to call a constructor

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

Look at the following code. Which line will cause a compiler error? Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public final int method1(int a){ return a;} Line 5 public double method2(int b){ return b;} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b){ return b;} Line 11 public double method2(double c){ return c;} Line 12 }

line 10

Look at the following code. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(){} 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 } Which line(s) method1 will be executed as a result of the following statements? ClassA item1 = new ClassC(); item1.method1();

line 14

The ability to catch multiple types of exceptions with a single catch is known as ________, and was introduced in Java 7.

multi catch

A "has a" relationship can exist between classes. What does that mean?

object aggregation

You cannot use the == operator to compare the contents of _______

objects

what does it mean to catch an exception?

program intercepts the exception and responds to it

write a statement declares Salaried as a subclass of PayType

public class Salaried extends PayType

Find the Error int number = 99; String str; // convert number to a string str.valueOf(number);

should be str = String.valueOf(number);

Which of the following methods of the String class can be used to tokenize a string?

split

Static methods can only operate on _______ fields.

static

look at the following declaration: enum Color { RED, ORANGE, GREEN, BLUE } what is the name of the data type declared by the statement?

static

why are static methods useful in creating utility classes?

static methods can be called at a class level

The term ________ commonly is used to refer to a string that is part of another string

substring

The Character wrapper class provides numerous methods for

testing and converting char variables

Find the Error // Assume inputFile references a Scanner object try { input = inputFile.nextInt(); } finally { inputFile.close(); } catch (InputMismatchException e) { System.out.println (e.getMessage()); }

the finally block should be at the end

Find the Error catch (FileNotFoundException e) { catch (FileNotFoundException e) { System.out.println ("File not found."); } try { File file = new File ("MyFile.txt"); Scanner inputFile = new Scanner (file); }

the try block is first

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

To compare two objects in a class

write an equals method that will make a field by field compare of the two objects


Set pelajaran terkait

Chapter 5: Adaptations to Anaerobic Training Programs

View Set

Life Insurance policies- Provisions, Options and Riders

View Set

IST chpt 8 communications and Networks

View Set

TRUE AND FALSE WORKSHEET (Derived from Courtesy Driving School's Segment I Student Binder)— Michigan

View Set

History- Industrialization of Russia and Japan + Imperialism in China

View Set

Management Information Systems Ch 4 5 6

View Set