Review

Stack

  • Understand the concept of stack in terms of:

a)The nature of stack – last in first out (LIFO)

b)Be able to give real life applications where stack is applicable

c)Stack operation – push, pop, peek, empty, (and to a lesser extent search)

d)Verify if expression has balanced parentheses

  • Collections interface

a)Know the principal methods in the collection interface

b)Know the major interfaces that are subclass of Collections

c)Distinguish between those concrete class that are derived from Set and those from List

I gave a handout on set operations, take a look at it and run the program. That should help a great deal. I

  • File and Streams

a)Be able to differentiate between byte streams and character streams

b)Know at least two concrete classes for reading and two for writing in both categories

c)Know the purpose of the class File and the more frequently used methods

d)Be aware that most of these I/O classes through exception, and the possible type of exception to be thrown

  • Recursion

a)Understand the principle of recursion

b)Be able to write simple recursive definitions

  • Write short pieces of code to bear out any of the points above.

Questions

Q1. Exceptions identify errors that arise in your program. Name the two categories of exceptions that are derived directly from Throwable and differentiate between them.

Q2 True or false. The classes InputStream and OutputStream are used to read and write byte data.

Q.4 The classes Reader and Writer are used to read and write character data. True or false?

Q5 Write down the output as it would appear on the screen.

class Exceptions

{

public static void main(String[] args)

{

int x[] = {2, 4, 6};

method(0, x);

method(x[2], x);

method(2, x);

}

static void method(int i, int arr[])

{

String str = "Yes its true";

try

{

System.out.println("Programming II");

System.out.println("This first value is " + arr[i]);

System.out.println("This second value is " + arr[i]/(arr[i] - 6));

System.out.println("This line will be printed");

}

catch(ArithmeticException e)

{

System.out.println("ArithemticException caught");

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println("ArrayIndexOutOfBoundsException caught");

}

finally

{

System.out.println(str);

}

}

}

Q16 Given the following incomplete class definition, write appropriate Java code that will use the string strto create a file object. [1 mark]

class myFile

{

File f;

myFile(String str)

{

}

}

Q7 Given the following method declaration, write appropriate code that will retrieve the number of bytes in the file.

int getBytes(File f)

{

}

Q8 Given the following incomplete method, write appropriate Java code to that will use the string variable str to:

(i)Create a BufferedReader object.

(ii)Use the BufferedReader object created in part (i) to read the file until it is empty.

void readFile()

{

}

Question 6 Write down a possible output from the following program as it would appear on the screen.

[4 marks]

import java.util.*;

public class SetExample

{

public static void main(String args[])

{

Set set = new HashSet();

set.add("Bernadine");

set.add("Elizabeth");

set.add("Gene");

set.add("Elizabeth");

set.add("Clara");

System.out.println("Result: " + set);

Set sortedSet = new TreeSet(set);

System.out.println("Sorted result: " + sortedSet);

}

}

1