



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CMSC 330 Practice Final Examination
Show all work necessary to justify your answers! There are 7 questions.
(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)
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(); } } } }