Sie sind auf Seite 1von 12

Final Exam

CMSC 331
Principles of Programming Languages

Omar Shehab

Name _________________________

Monday, December 19, 2016


Section - 2 - 6:00-8:00 PM, Janet & Walter Sondheim 105

This exam is a closed-book-open-mind exam. It will be graded on the basis of correctness,


clarity, and neatness. Please show your work for partial credit. Use of electronic device is not
permitted without the permission of the instructor.

Academic Honesty
By enrolling in this course, each student assumes the responsibilities of an active participant in
UMBCs scholarly community in which everyones academic work and behavior are held to the
highest standards of honesty. Cheating, fabrication, plagiarism, and helping others to commit
these acts are all forms of academic dishonesty, and they are wrong. Academic misconduct
could result in disciplinary action that may include, but is not limited to, suspension or dismissal.
2

1. Using the following grammar, show a parse tree and a leftmost derivation for the
statement
A = A * ( B + ( C * A ) ).

Grammar:
<assign> <id> = <expr>
<id> A | B | C
<expr> <id> + <expr> | <id> * <expr> | (<expr>) | <id>

Final Exam CMSC 331 NAME ___________________


3

2. Convert the following BNF grammar to EBNF.

<assign> <id> = <expr>


<id> A | B | C
<expr> <expr> + <expr> | <expr> * <expr> | (<expr>) | <id>

Final Exam CMSC 331 NAME ___________________


4

3. Following is a Haskell code for the number guessing game. There might be a few syntax and
logical errors. Fix as much as you can.

module Main
where

import System.IO
import System.Random

main = do:
hSetBuffering stdin LineBuffering
num = randomRIO (1::Int, 100)
putStrLn "I'm thinking of a number between 1 and 100"
doGuessing num

doGuessing num = do
printf "Enter your guess"
guess = getLine
let guessNum = read guess
if guessNum < num
then do putStrLn "Too low!"
doGuessing num
else if read guess > num
then do putStrLn "Too high!"
doGuessing num
else do putStrLn "You Win!"

Final Exam CMSC 331 NAME ___________________


5

Final Exam CMSC 331 NAME ___________________


6

Final Exam CMSC 331 NAME ___________________


7

4. Following is a Prolog code for the number guessing game. There might be a few syntax and
logical errors. Fix as much as you can.

% guess a number game.


% to play: guess(Minimum, Maximum, []).

% borrow from concatenate


concatenate([], List, List)
concatenate([Head|Tail1], List, [Head|Tail2]) :
concatenate(Tail1, List, Tail2)

% writeList: Write a list of values to screen


writeList([])
writeList([Head|Tail]) :- write(Head), writeList(Tail)

% If A = B, write "Correct! " and A concatenated to the list of GuessesSoFar


equal(A, B, GuessesSoFar) :- A = B, Hint = 'Correct! ', write(Hint),
concatenate([A], GuessesSoFar, Guesses), write(Guesses)
% If A < B, write "Higher. " and prompt for another guess
higher(A, B, GuessesSoFar) : A < B, Hint = 'Higher. ', write(Hint),
concatenate([A], GuessesSoFar, Guesses), guess(Guesses, B)
% If A > B, write "Lower. " and prompt for another guess
lower(A, B, GuessesSoFar) :- A > B, Hint = 'Lower. ', write(Hint),
concatenate([A], GuessesSoFar, Guesses), guess(Guesses, B)

% Prompt for a guess and respond appropriately


guess(Guesses, Number) :-
read(Guess),
(equal(Guess, Number, Guesses), % The guess is a match
higher(Guess, Number, Guesses), % OR the guess is too low
lower(Guess, Number, Guesses)). % OR the guess is too high

% Play the game


guess(Lower, Upper, Guesses) :-
writeList(['Guess a number between ', Lower, ' and ', Upper, '.']),
random(Lower, Upper, Number). % Get the random number
guess(Guesses, Number). % Start guessing

Final Exam CMSC 331 NAME ___________________


8

Final Exam CMSC 331 NAME ___________________


9

Final Exam CMSC 331 NAME ___________________


10

5. Following is a Java code for the number guessing game. There might be a few syntax and
logical errors. Fix as much as you can.

import java.util.Scanner

public class NumberGuessingGame {


public void main() {
int secretNumber
secretNumber = (int) (Math.random() * 999 + 1);
Scanner keyboard = Scanner(System.in);
int guess;
do {
System.out.print("Enter a guess (1-1000): ");
guess = keyboard.nextInt();
if (guess equals secretNumber)
System.out.println("Guess is correct.")
else if (guess less than secretNumber)
System.out.println("Guess is smaller.");
else if (guess greater than secretNumber)
System.out.println("Guess is greater.");
} while (guess != secretNumber);
}

Final Exam CMSC 331 NAME ___________________


11

Final Exam CMSC 331 NAME ___________________


12

Final Exam CMSC 331 NAME ___________________

Das könnte Ihnen auch gefallen