Question: 1. A method must do either of two things with a checked exception. What are those two things? Ignore the exception, or (2) check the

1. A method must do either of two things with a checked exception. What are those two things?

Ignore the exception, or (2) check the exception.

Handle the exception in a catch{} block, or (2) return the exception to the sender.

Handle the exception in a catch{} block, or (2) throw the exception to the method that called this method.

Handle the exception in a catch{} block, or (2) handle the exception in a try{} block,

2.What method of an Exception object returns a message string?

getError()

getMessage()

printMessage()

traceMessage()

3.Can a text file written by a Java program be read andopened by anyone?

No---Java uses its own text file format.

No---the ends of lines will be completely different.

Yes---usually there will be no problem.

Does not exist

4.Can data flow through a given stream in both directions?

No; a stream has one direction only, input or output.

No; streams only work for output.

Yes; only one stream is needed to read and write file.

Yes; but only one direction at a time.

5.What happens during execution if an negative value is used for an array index?

An IndexOutOfBoundsException is thrown.

The program outputs to the console

The program reads from the console

The program goes into a loop

6.What does the following constructor do: FileWriter fw = new FileWriter( "myFile.txt" );

Reads from the console

Write to the close

Creates a loop array

Any previous "myFile.txt" in the current directory will be deleted and a new one created.

7.Predict the output of following Java Program

// filename Main.java

classGrandparent {

publicvoidPrint() {

System.out.println("Grandparent's Print()");

}

}

classParentextendsGrandparent {

publicvoidPrint() {

System.out.println("Parent's Print()");

}

}

classChildextendsParent {

publicvoidPrint() {

super.super.Print();

System.out.println("Child's Print()");

}

}

publicclassMain {

publicstaticvoidmain(String[] args) {

Child c =newChild();

c.Print();

}

}

Compiler Error in super.super.Print()

Grandparent's Print()

Parent's Print()

Child's Print()

Runtime Error

Write toa file

8.What is an advantage of polymorphism?

The same program logic can be used with objects of several related types.

Variables can be re-used in order to save memory.

Constructing new objects from old objects of a similar type saves time.

Polymorphism is a dangerous aspect of inheritance and should be avoided.

9.What ispolymorphismin Java?

It is when a single variable is used with several different types of related objects at different places in a program.

It is when a program uses several different types of objects, each with its own variable.

It is when a single parent class has many child classes.

It is when a class has several methods with the same name but different parameter types.

10:

finalclassComplex {

privatefinaldoublere;

privatefinaldoubleim;

publicComplex(doublere,doubleim) {

this.re = re;

this.im = im;

}

publicString toString() {

return"("+ re +" + "+ im +"i)";

}

}

classMain {

publicstaticvoidmain(String args[])

{

Complex c =newComplex(10.1, 15.1);

System.out.println("Complex number is "+ c);

}

}

Complex number is (10.1 + 15.1)

Compiler Error

Complex number is SOME_GARBAGE

Complex number is C..x@8e2fb5

11.Say that a method catches an IOException in a catch{} block. Is it possible for that block to do some processing and then throw the same exception to the caller?

No---when an exception is caught the Exception object is destroyed.

No---an exception may be caught only once.

Yes---as long as the method also has a throws clause for that exception.

Yes---but the catch{} block must construct a new Exception object.

12.What does closing a file do

Flushes pending output and finishes processing the file.

Finishes processing the file, only.

Flushes pending output, but the file may still be written to.

Makes the file read-only.

13:

Predict the output of following program. Note that fun() is public in base and private in derived.

classBase {

publicvoidfoo() { System.out.println("Base"); }

}

classDerived extends Base {

privatevoidfoo() { System.out.println("Derived"); }

}

publicclassMain {

publicstaticvoidmain(String args[]) {

Base b =newDerived();

b.foo();

}

}

Base

Derived

Compiler Error

Wait fortyping fromkeyboard

14.What happens if a FileWriter constructor is given an illegal file name?

The program bombs immediately.

The users disk is corrupted.

The constructor returns a null value.

An IOException is thrown.

15.Can an object of a child type be assigned to a variable of the parent type? For example,

Card crd; BirthDay bd = new BirthDay("Lucinda", 42); crd = bd; // is this correct? 

No--there must always be an exact match between the variable and the object types.

No--but a object of parent type can be assigned to a variable of child type.

Yes--an object can be assigned to a reference variable of the parent type.

Yes--any object can be assgned to any reference variable.

16.When a file is opened for appending where does write() put new text

At the beginning of the file.

At the end of the file.

Where ever the user specifies.

Files cannot be appended to.

17.Which of the following is a correct outline of a method that throws an exception when it finds a problem?

public void someMethod ()

{ ...

if ( problem )

throw new Exception("Useful Message");

...

}

public voidsomeMethod()throws Exception

{ ...

if ( problem )

Exception("Useful Message");

...

}

public voidsomeMethod()throws Exception

{ ...

if ( problem )

throw (new Exception("Useful Message"));

...

}

public voidsomeMethod()throws Exception

{ ...

if ( problem )

throw new Exception("Useful Message");

...

}

18.What is the last method to be called after calling theoutputstreamor inputstream

close()

stop()

start()

finish()

19.What determines what method is run in the following:

Card crd = new BirthDay("Lucinda", 42); crd.greeting(); 

The type of the object.

The type of the reference variable.

Thetime the object wascreated.

Theageoftheobject

20.In order for the following code to be correct, what must be the type of the reference variablecard?

_________ card; card = new Valentine( "Joe", 14 ) ; card.greeting(); card = new Holiday( "Bob" ) ; card.greeting(); card = new Birthday( "Emily", 12 ) ; card.greeting(); 

Valentine

Holiday

Birthday

Card

21.The input/output package usually used with Java is:

java.input

java.io

java.inout

java.file

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!