Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key

image

Play button

image

Play button

image

Progress

1/49

Click to flip

49 Cards in this Set

  • Front
  • Back

What is package access, how does it differ to private, protected, and public access ?

Instance variables or methods havingpackage access can be accessed by nameinside the definition of any class in the samepackage




However, neither can be accessed outside thepackage.

List the three scope from which a protected member of a class can be accessed, eg: outside class, inside class etc.

Inside its own class definition




Inside any class derived from it




In the definition of any class in the same package

How does public access differ to package access ?

Public access means anyone from anywhere without restriction can access the member or method. However with package access only things inside the same package has access.

How would you accomplish the following ?




Call the base class version of an overriddenmethod of the base class.

Using super. For example if toString was overriden in class B which inherited from class A. Class B can have a method like :




public String toString(){


return super.toString();


}

True or False :




It is only valid to use super to invoke a methodfrom a direct parent

True.




Repeating super will not invoke a method from some other ancestor class. For example the following is illegal : super.super.toString();




To accomplish this you would have to chain super calls.

Explain the instanceof operator ?

interface Domestic {}


class Animal {}


class Dog extends Animal implements Domestic {}


class Cat extends Animal implements Domestic {}




Imagine a dog object, created with Object dog = new Dog(),


then:


dog instanceof Domestic // true - Dog implements Domestic


dog instanceof Animal // true - Dog extends Animal


dog instanceof Dog // true Dog is Dog


dog instanceof Object // true - Object is the parent type of all objects

Explain the getClass method and how it is used ?

An invocation of getClass() on an objectreturns a representation only of the classthat was used with new to create the object




The results of any two such invocations can be compared with == or != to determine whether ornot they represent the exact same class

What is binding ?

The process of associating a methoddefinition with a method invocation is calledbinding

What is early binding or static binding ?

If the method definition is associated with itsinvocation when the code is compiled, thatis called early binding or static binding

What is dynamic binding ?

If the method definition is associated with itsinvocation when the method is invoked (atrun time), that is called late binding ordynamic binding

What is upcasting ? Give an example ?

Upcasting casting a derived class to its ancestor.


For example, assume B extends A. Then it is okay to do :


A aa = new B();

What is down casting ? Give an example ?

Down casting is when we explicitly cast a class to one of its decendents. For example assume B extends A. then B bb =new B();


A aa = (A) nn; is allowed.

True or false ?




An abstract class can have any numberof abstract and/or fully defined methods

True

What is a class called which fully implements all abstract methods of an abstract class ?

A Concerete class.

Even though one cannot create instances of an abstract class is it okay to reference them by type ?

Yes this is legal.

When should exceptions be used and when should we check for errors ?

Exceptions should be used when something that is not expected occurs. For example when we expect the user to input in a specific format and they do not.

How does one access the message of an exception ?

use the getMessage() method inherited from the exception class.

Why is the following code wrong ?




catch (Exception e){ . . . }


catch (NegativeNumberException e)


{ . . . }

Because NegativeNumberException is a of type Exception and therefore the last catch block will never be executed.

How do you write the method header which thrown two exception: exception1 and exception1

public void method() throws


exception1,exception2{}

What are important point regarding overriding methods which throw exceptions ?

When a method in a derived class isoverridden, it should have the sameexception classes listed in its throwsclause that it had in the base class– Or it should have a subset of them


A derived class may not add anyexceptions to the throws clause– But it can delete some

Exceptions are an example of what kind of programming methodology ?

Exception handling is an example of aprogramming methodology known as eventdrivenprogramming

What does the finally block do ?

The code in the finally block will execute last regardless of whether an exception is thrown and caught or not.

Are runtime exceptions checked exception or unchecked exceptions ?

Runtime exceptions are unchecked exceptions. Though the programmer may choose to catch them.

What is the input stream from the console ?

System.in

What is the output stream from the console ?

System.out

What are some benefits of binary files over text files ?

An advantage of binary files is that they aremore efficient to process than text files





Write an abstract method header.

public abstract void foo();

How would you open a file test.txt and write the line "File" and then close it.

import java.io.PrintWriter;


import java.io.FileOutputStream;


import java.io.FileNotFoundException;




try{


PrintWriter writer = new PrintWriter( new FileOutputStream(test.txt);


writer.println("File");


writer.close();


}catch(Exception e){


}

What does the import java.io.FileNotFoundException exception mean in the context of opening a file for writing ?

It means that the file could not be created or opened for some reason.

What is the behavior of FileOutputStream when there is no textfile with the specified name to open ?

A new textfile with that name is created.

What is the behavior of FileOutputStream when there is a textfile with the specified name to open ?

The textfile is opened and writes to the file will clear the current file and write the data.

What java classes are required to read from a text file ?

java.util.Scanner


java.io.FileInputStream


java.io.FileNotFoundException

Write code to open a text file for reading ?

import java.util.Scanner;


import java.io.FileInputStream;


import java.io.FileNotFoundException;


Scanner s = null;


try{


s = new Scanner( new FileInputStream("filename"));


}catch(FileNotFoundException e){


}




Remember that the Scanner class MUST be declared outside the try block !!!

Once a file has been opened successfully how would one start reading from it ?

Using the normal scanner methods :


Scanner.nextLine();


Scanner.nextInt();


Scanner.nextDouble();


etc.

What will a Scanner Object do if nextLine() is invoked when the file has nothing left in it ?

It will raise an exception.

How can we check whether there are things that can be read in the file ?

Use hasNextInt()


hasNextLine()




etc.

Write code which will open and read a file until there are no more lines remaining without raising an exception. You can assume imports

Scanner s = null;


try{


s = new Scanner(new FileInputStream("filename"))


}catch(FileNotFoundException e){


System.exit(0);


}




while(s.hasNextLine()){


System.out.println(s.nextLine());


}


s.close();



How would you use BufferedReader to open a file for reading ?

Using the BufferedReader and FileReader classes like so :




BufferedReader reader = null


try{


reader = new BufferedReader( new FileReader("fileName"))


}catch(Exception e){


System.exit(0);


}



What methods of the BufferedReader class can be used to read a file which is opened ?




What do they normally return and what do they return when there is nothing to read.

readLine and read.




readLine returns the line when there is a line to read and returns null otherwise.




read returns the integer value of the next char if present otherwise returns -1

What are the objects which represent the System input stream, System output stream, and the System error stream ?

System.in

System.out


System.err





How would you redirect the system error stream to a file ?

import java.io.PrintStream;




PrintStream s = null;


try{


s = new PrintStream(new FileOutputStream("Filename"));


System.setErr(s);


}catch(Exception e){


System.exit(0);


}



How would you open a binary file for output and then write an object to the file ?

import java.io.ObjectOutputStream;


import java.io.FileOutputStream;


import java.io.FileNotFoundException;




ObjectOutputStream out = null;


try{


s = new ObjectOutputStream( new FileOutputStream("Filename"));


s.writeObject(myObject);


}catch(Exception e){
System.exit(0);


}




Remember that the writeObject method may throw an io exception.

What methods can be used with ObjectOutputStream to write Objects to file ? And what must each object implement ?

All objects must implement java.io.Serializable.




methods include :




writeInteger(Integer);


writeObject(Object);


etc.

How would you read an object from a binary file ?

import java.io.ObjectInputStream;


import java.io.FileInputStream;


import java.io.FileNotFoundException;




ObjectInputStream s = null;


myObject ob = null;


try{


s = new ObjectInputStream( new FileInputStream("FileName"));


ob = (myObject) s.readObject();


}catch(exception e){


System.exit(0);


}



What modifier must all instance variables and methods of an interface have ?

All methods must be public.


All variables must be public final constant.

Can interfaces inherit from classes or abstract classes ?

No. Interfaces can only inherit from other interfaces.

How would one implement an interface ?

Use the syntax implements interface xx. And then fully define all methods in the interface.

How would you implement the Comparable interface. Where does the Comparable interface live ?

Comparable resides in the java.lang and therefore doesn't have to be imported.



Comparable has one require method




public int compareTo(Object)



What do the returns of Comparables compareTo method mean ?

-1 this precedes other


0 equals other


+1 procedes other