Advanced Programming - CS239

 

Department of Computer Science

Open the home page for this courseWhere to learn moreTools you may find usefulGet helpSend email to the Professor

 

 

 

Preparing for the Final Exam

What to Expect: This exam will be comprehensive. However, you will not be examined on packages, graphics, or animation.

You should expect three types of questions: short answer (e.g., true/false, matching, fill-in, multiple choice), code understanding (e.g., error identification, execution tracing), and code development (e.g., method/class writing, test writing).

What to Study: Obviously, you should study the lectures, labs, and readings, and you should review the programming assignments. However, you should pay particular attention to the labs since there are likely to be questions on the exam like those covered in the labs.

Sample Questions for the Final Exam: The questions below (as well as the sample questions for exam 1, and exam 2 ) are illustrative of the kinds of questions that will be on the exam.

  1. Indicate whether each of the following statements is true or false:

_____

2-dimensional arrays must be rectangular.

_____

The elements in an array must be homogeneous.

_____

Arrays use linked memory.

_____

Elements are added to and removed from a queue in a FIFO manner.

_____

A Vector can not be re-sized.

  1. Illustrate the differences or similarities between a "first-in, first-out" collection and a "last-in, last-out" collection.
  2. Draw a model of how memory is allocated by the following code snippet. Carefully illustrate what happens in statement 1, statement 2, and statements 3 through 6.

4.                         1  Integer[]        values;

5.                          

6.                         2  values = new Integer[5];

7.                          

8.                         3  for (int i=0; i < values.length; i++)

9.                         4  {

10.                     5      values[i] = new Integer(i*10);

11.                     6  }

 

  1. Given the following Printer class:

13.                     import java.util.*;

14.                      

15.                     public class Printer

16.                     {

17.                         private StringTokenizer         tokenizer;

18.                         

19.                      

20.                         public void print(String line)

21.                         {

22.                             tokenizer = new StringTokenizer(line, ",");

23.                      

24.                             if (tokenizer.hasMoreTokens())

25.                             {

26.                                 printTokens(tokenizer);

27.                             }

28.                         }

29.                      

30.                      

31.                         private void printTokens(StringTokenizer tokenizer)

32.                         {

33.                             String      token;

34.                      

35.                             token = tokenizer.nextToken();

36.                             if (tokenizer.hasMoreTokens())

37.                             {

38.                                 printTokens(tokenizer);

39.                             }

40.                      

41.                             System.out.println(token);

42.                         }

43.                      

44.                     }

 

carefully trace the execution of the following:

public class PrinterDriver

{

    public static void main(String[] args)

    {

     Printer      test;

 

     test = new Printer();

     test.print("Adams,Bernstein,Harris");

    }

}

 

  1. In postfix notation, binary arithmetic expressions are written as follows:

operand1 operand2 arithmetic-operator

For example, the following are valid expressions in postfix notation:

5 3 +
2 8 *

Suppose we have a Stack class that holds String objects and a method named evaluate in the Callculator class that is passed the three parts of a postfix expression (as String objects) and returns a String representation of the result. Trace the execution of the following, carefully modeling memory at each step:

  Stack      terms;

 

  terms = new Stack();

 

  terms.push("5");

  terms.push("3");

  terms.push("+");

 

  result = Calculator.evaluate(terms.pop(), terms.pop(), terms.pop());

 

  terms.push(result);

  terms.push("4");

  terms.push("*");

 

  result = evaluate(terms.pop(), terms.pop(), terms.pop());

 

  1. Assuming the following Adder class is compiled properly:

47.                     public class Adder

48.                     {

49.                      

50.                        public static void main(String[] args)

51.                        {

52.                           double        result;

53.                           

54.                           try

55.                           {

56.                              result = add(args);

57.                              System.out.println(args[0]+"+"+args[1]+": "+result);

58.                           }

59.                           catch (ArrayIndexOutOfBoundsException aioobe)

60.                           {

61.                              System.out.println("Too few operands.");

62.                           }

63.                        }

64.                      

65.                      

66.                      

67.                      

68.                        public static double add(String[] terms)

69.                                                          throws ArrayIndexOutOfBoundsException

70.                        {

71.                           double        left, result, right;

72.                           String        operator;

73.                           

74.                           left     = 0.0;

75.                           right    = 0.0;

76.                           

77.                      

78.                           try

79.                           {

80.                              left = Double.parseDouble(terms[0]);

81.                           }

82.                           catch (NumberFormatException nfe)

83.                           {

84.                              left = 0.0;        

85.                           }

86.                           

87.                           try

88.                           {

89.                              right = Double.parseDouble(terms[1]);

90.                           }

91.                           catch (NumberFormatException nfe)

92.                           {

93.                              right = 0.0;        

94.                           }

95.                           

96.                              

97.                           result = right + left;

98.                           

99.                           return result;

100.                    }

101.                 }

carefully trace the execution of each the following:

java Adder 5 4
java Adder 5
java Adder 5 b
java Adder 3 4 5 6

  1. Write a class named QueueOfDoubles that, as its name almost implies, contains a queue of double values (not Double objects).
  2. Modify your SavingsAccount class from Programming Assignment 2 so that it contains a queue of transactions (i.e., deposits and withdrawals). It must use a QueueOfDoubles object. Deposits should be represented in the queue as positive entries and withdrawals should be represented in the queue as negative entries.
  3. Modify your answer to the question above so that it uses a queue of Transaction objects:

105.                   public class Transaction

106.                   {

107.                       public String    type;

108.                       public double    amount, balanceAfterTransaction;

109.                   }

 

That is, each time a deposit or withdrawal is made, a Transaction object should be created and pushed onto the queue. (Note: You may use the Queue class we discussed in lecture.)


Open the syllabus for this courseWhere to learn moreTools you may find usefulGet helpSend email to the Professor

Open the JMU Computer Science Department's page


Copyright ©2004