CS365 Midterm 1

 

1)    This exam is open-note, open book.

2)    You must answer all of the questions.

3)    Answer all the questions on a separate sheet of paper.

4)    You must use Java to implement the coding questions.

5)    Good luck!

 


 

1.     (10 points) Why are virtual methods slower than non-virtual methods? Your answer must be three sentences or less.

 

 

 

 

 

 

 

 

 

2.     (10 points) Why is Java's package mechanism preferable to C++'s friends mechanism for implementing a module? Your answer must be three sentences or less.

 

 

 

 

 

 

 

 

 


3.     (15 points) Write a Java method named quotient that takes a Scanner object as an input, reads the next two integers using the Scanner object, and returns their integer quotient (divide the first integer by the second integer). Do not worry about a divide by zero exception. However, if either of the next two inputs is not an integer, then your method should throw an exception that includes the invalid input. You will need to declare an appropriate exception class and your code should throw an instance of this exception class. Your method should not handle the exception. Instead it should throw the exception to the calling method. Here are a couple Scanner methods that your solution will need to use:

 

boolean hasNextInt(): Returns true if the next token in the input is an integer.

int nextInt(): Scans the next token of the input as an int.

 


4.     (15 points) Write a Java try block that calls quotient and handles both a divide by zero exception (it will be an ArithmeticException that is thrown automatically by Java) and an invalid input exception. The try block should use printf to print the quotient. The exception handler for an ArithmeticException should simply print the string associated with an ArithmeticException. The exception handler for your invalid input exception should print the invalid input.

 

            Scanner input = new Scanner(System.in);

            // Your code goes here

 


Refer to the following grammar for the questions below:

 

E -> number | - E E | + E E

 

5.     (10 points) Is the above grammar LL(1)? Why or why not? Your explanation must be three sentences or less.

 

 

 

 

 

 

 

6.     (10 points) Write a left derivation for the string "- 3 + - 8 6 10".

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7.     (10 points) Draw the parse tree for the above string.


 

8.     (10 points) Suppose you are given the following set of items from the grammar shown on the previous page:

 

E' -> .E   // E'is the special start symbol we introduce for the grammar

E -> .number | . E E | .+ E E

 

Show the sets of items that can be derived from this set.

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

9.     (10 points) Behold the following grammar and its parse table:

 

(1) E -> E + E

(2) E -> E * E

(3) E -> ( E )

(4) E -> -E

(5) E -> id

 

State

+

*

(

)

-

id

$$

E

1

err

err

s3

err

s4

s5

err

s2

2

s6

s7

err

err

err

err

acc

err

3

err

err

s3

err

s4

s5

err

s8

4

err

err

s3

err

s4

s5

err

s9

5

r5(1)

r5(1)

err

r5(1)

err

err

r5(1)

err

6

err

err

s3

err

s4

s5

err

s10

7

err

err

s3

err

s4

s5

err

s11

8

s6

s7

err

s12

err

err

err

err

9

r4(2)

r4(2)

err

r4(2)

err

err

r4(2)

err

10

r1(3)

s7

err

r1(3)

err

err

r1(3)

err

11

r2(3)

r2(3)

err

r2(3)

err

err

r2(3)

err

12

r3(3)

r3(3)

err

r3(3)

err

err

r3(3)

err

 

Execute the parser on the input string -(id):

 

Input                Stack                                        Action

-(id)$$             1