Docsity

C++ Programming Exam: Static Scoping, Arrays, and Bit Vectors, Exams of Programming Languages

Toggle sidebar
bg1
CMSC 330 Practice Final Examination
Show all work necessary to justify your answers! There are 7 questions.
1. (15 points) Assuming that static scoping rules are used, what will the following pro-
gram, in C++ syntax, output if parameters are passed by:
(a) value?
(b) value-result?
(c) reference?
#include <iostream.h>
int k = 5;
void f(int i, int j) {
if(i<4)i=i+1;
else i=i-1;
j=j+2;
k=k-1;
}
main() {
intj=3;
f(j, k);
f(k, j);
cout << j << " " << k << '\n';
}
Should print:
value: 3 3
value-result: 6 6
reference: 6 4
pf3
pf4
pf5

Partial preview of the text

Download C++ Programming Exam: Static Scoping, Arrays, and Bit Vectors and more Exams Programming Languages in PDF only on Docsity!

CMSC 330 Practice Final Examination

Show all work necessary to justify your answers! There are 7 questions.

  1. (15 p oints) Assuming that static scoping rules are used, what will the following pro- gram, in C++ syntax, output if parameters are passed by:

(a) value? (b) value-result? (c) reference?

#include <iostream.h> int k = 5;

void f(int i, int j) { if (i < 4) i = i + 1; else i = i - 1; j = j + 2; k = k - 1; }

main() { int j = 3; f(j, k); f(k, j); cout << j << " " << k << '\n'; }

Should print: value: 3 3 value-result: 6 6 reference: 6 4

Give an unambiguous grammar for the language: L = f w j w 2 f 0 ; 1 g and w contains exactly one o ccurrence of the substring 10 g

A! 1B j 0A B! 1B j 0C j 0 C! 1D j 0C j 0 j 1 D! 1D j 1

Variables of typ e set of Z are usually stored as bit vectors, with 1 bit for each element. For example, if Z is the set, [orange, kiwi, blueberry, grapefruit, pear, banana, straw- berry, plum], then a variable of the typ e set of Z would b e represented as an 8-bit vector. The rst bit represents the element orange, the second bit represents kiwi, etc. If a bit is set (1), then the corresp onding element is a memb er of the set. Otherwise the element is not a memb er of the set.

(a) What is the representation for the set A = [banana, kiwi], and the set B = [straw- berry, kiwi, plum, grapefruit]? A = 01000100 , B = 01010011 (b) What machine instruction could b e used to p erform the op eration C = A [ B , and what is the representation for C? Use C = A OR B, union is bitwise OR, and C = 01010111 (c) What op erations must b e p erformed to test whether the element blueberry is in the set C? Show b oth the set op erations and machine instruction(s) required. Represent blueb erry as A = 00100000, and use A \ C (bitwise AND)

  1. (15 p oints) Pro duce a deterministic nite automaton (DFA) that recognizes the lan- guage: L = f w j w 2 fa; bg and w do esn't end with baba g 5 states, 4 nal, with only the state gotten to by baba non- nal

b

b a bb a

a b

b

a

a

Write a Java class, named Barrier, that p erforms barrier synchronization across N Java threads. The constructor for a Barrier ob ject takes N as an argument, and is called by only one thread (e.g., the main program thread). Barrier should have a synchronized metho d, called bar, that is called by a thread to enter the barrier. The exact syntax of the Java co de is not imp ortant, but it should b e close.

public class Barrier { int num_threads, cur_threads;

public Barrier(int N) { num_threads = N; cur_threads = 0; }

public synchronized void bar() { cur_threads++; if (cur_threads >= N) { notifyAll(); } else { while (cur_threads < N) { wait(); } } } }