CS1043

Exam 3 Review

  1. Show the result of two iterations (one complete step) when using selection sort on the array below. The sort should be in descending order.

25 / 48 / 19 / 75 / 55 / 23 / 14 / 76 / 91 / 28
  1. Give an example of a enhanced for-loop using an array of objects:

Object [] data. = new Object[n];

  1. How many iterations of a binary search would be required to determine that a value is not in an array of size 120?
  1. What does the following program print?

class B

{ public B() {}

public void print(int n) {System.out.println(n*3);}

}

class D extends B

{ public D() {}

public void print(int n)

{ if(n <= 1) super.print(n);

else if (n % 2 == 0) print(n / 2);

else print(3 * n + 1);

}

}

public static void main(String[] args)

{ D d = new D();

d.print(3);

}

  1. Implement a superclass Person that has a name and an age. Make two classes, Student and Instructor, inherit from Person. A Student has a major, and an instructor has a salary. Write the class definitions, a default constructor, one constructor to initialize all the instance variables, and include accessor and mutator methods for all instance variables assigned to each class.
  1. Use nested loops to generate the output for each of the two shapes:

and

&

&

  1. Write a program that opens a file, “review.dat”, and prints the number of lines in the file. Use the try and catch keywords for this example.
  1. Look at the nested loop and determine the number of times myMethod is referenced. Propose a big-O of h function to describe the code fragment if myMethod is O(n).

for ( j=0; j < n; j++ )

{myMethod( C, D, j, 1 );

for ( k=0; k < n; k++ )

myMethod( A, B, j, k );

}

  1. Look at the nested loop and determine the number of times myMethod is referenced and propose a big-O of h function to describe the total count.

for ( j=0; j < n; j++ )

for ( k=0; k <= j; k++ )

myMethod( A, B, j, k );

  1. Determine the result of Help(6,14). Show a trace as you go through this recursive function like we did in class.

public static int Help(int x, int y)

{

if(x > y) return(x + y);

if(x == y) return(Help(x+2,y-1) + Help(x,y-2));

else return(Help(x+1,y-2) + Help(x+2,y-3));

}// end Help

  1. Write a static function complete with a header line that performs a linear search for a value in an array. The value and array should both be integers. The return value should be the index where the value was first located. Return –1 if the value is not found.
  1. What two contexts does a programmer use the super keyword in a Java program?
  1. The Fibonacci series is defined as . Write a static method to recursively compute and return the n-th Fibonacci number given the value n as the input to the method.
  1. The amount of time it takes for a particular algorithm to complete a process is proportional to the expression, , where k is the constant of proportionality. Let T denote time. Then, . If it takes 13 seconds to compute 8192 records, how long does it take to compute 131072 records?
  1. Describe what makes a sorting algorithm stable.
  1. What three criteria are used to determine how to choose an appropriate sorting algorithm?
  1. Explain the difference between the keywords throw and throws.
  1. What is a competent exception handler?
  1. What are the keywords used to create competent exception handlers?
  1. What choices do you have with an exception if the exception is unchecked? What are the choices if the exception is checked?
  1. What happens if a checked exception is ignored by the programmer? What happens if an unchecked exception is ignored by the programmer?
  1. What is the minimum number of catch blocks in a CEH if there is no finally block?
  1. What is the minimum number of catch blocks in a CEH if there is a finally block?
  1. Under what conditions is a finally block required for CEH?
  1. Consider the two methods called in class D as indicated by the comments call 1 and call 2:

public class D extends B

{public void f()

{ this.g(); // call 1

}

public void g()

{ super.g(); // call 2

}

}

Is call 1 early or late binding? Is call 2 early or late binding? Justify your answers.

  1. Which constructor is implicitly called from a sub-class constructor?
  1. What should be added to a derived-class constructor if the base-class has no default constructor?
  1. How does a sub-class constructor determine which super-class constructor to use?
  2. Write a recursive function to compute factorial.
  3. What are the three features required for a recursive algorithm to work properly?
  4. What is a signature in the context of Java programming?
  5. Which types of methods are polymorphic?
  6. Show how to instantiate an ArrayList named myList containing up to 50 BankAccount objects.
  1. What kinds of data can an ArrayList hold? What kinds of data can't be stored in an array list?
  1. Explain how to store an array of data primitives in an ArrayList.
  1. Write a code fragment showing how to determine if two strings have the same data. How do you compare the String references for equality?
  1. Write a code fragment to Box an array of type double into an ArrayList.
  1. What are the wrapper classes?
  1. Show the output for each column on a page:

double x = 3.14159264;

System.out.printf( “x=%7.2f\n”, x );

System.out.printf( “x=%07.3f\n”, x );

String y = “x = “;

System.out.printf( “%s%-4.2f\n”, y, x );

  1. Let xObj, yObj, and zObj be objects that are ordinal and assigned data. Show how to assign the variable zObj to the larger of the data stored in xObj and yObj using the compareTo method.