- Which of one of these context-free grammars is regular? (@
represents the null string)
a. S ::= 'a' T | 'b' U
T ::= 'a' T | @
U ::= U 'b' | 'b'
b. S ::= 'a' A | B
A ::= 'a' A | 'a'
B ::= 'b' B | 'b'
c. S ::= 'a' A 'b'
A ::= 'a' A | 'x'
d. S ::= 'x' S | 'x' A | @
A ::= 'y' A | 'y'
e. S ::= 'a' 'b' S | 'a' 'x'
- Which of the following regular expressions
describes the set of all strings using {a,b,c} such that no
'a' occurs to the right of any 'c'?
- a*b*c*
- (a | b)(b | c)*
- (a | b)* c*
- a* (b | c)*
- (a | b)*(b | c)*
- Given the grammar below, which one of the following is valid (can
be derived from the grammar)?
Note: 5, 8, 6, etc. are all to be treated as instances of the 'int'
terminal for the grammar.
S ::= F S S | 'int'
F ::= '+' | '-' | '*' | '/'
- / + 5 8 - * 6 + - 12 10 2
- / + * 5 8 + 6 12 10 2
- / + * + 5 8 6 12 - 10 2
- / 5 8 6 12 - 10 2 + * +
- / + * + 5 8 6 12 10 - 2
- 5 8 + 6 * 12 + 10 / 2 -
- 5 8 6 + * 12 + 10 2 - / +
- 5 8 + 6 * 12 + 10 2 - /
- 5 8 + 6 * 12 + 10 2 - /
- 5 + 8 6 * 12 + 10 2 - /
- For the grammar below, give a right-most derivation for the
postfix expression:
S ::= S S F | 'int'
F ::= '+' | '-' | '*' | '/'
Give a derivation for 5 8 + 6 * 12 + 10 2 - /
5, 8, etc. all correspond to the 'int' terminal of the grammar.
You can write int5, int8, etc. to indicate
which 'int' is indicated.
- Which two of the following is a corrrect definition of an
ambiguous grammar?
- A grammar is ambiguous if there are two distinct sentences that can be
derived using the grammar.
- A grammar is ambiguous if there are two distinct parse trees that can be
constructed using the grammar.
- A grammar is ambiguous if there is some sentence in the language
of the grammar that has two different derivations.
- A grammar is ambiguous if there is some sentence in the language
of the grammar that has two different parse trees.
- A grammar is ambiguous if there is some sentence in the language
of the grammar that has two different right-most derivations.
- Which two of the following grammars is ambiguous?
a. S ::= 'a' S S | 'x'
b. S ::= S 'a' S | 'x'
c. S ::= S S 'a' | 'x'
d. S ::= 'if' 'cond' 'then' S 'else' S | 'a'
e. S ::= 'a' S | 'b' S | 'x'
- Assume static scope binding using the most-closely-nested rule
and give the output for the following:
int k = 0;
int j = 1;
int a[] = {10,20,30};
void f()
{
a[j]++;
a[k]++;
}
int main()
{
int k = 1;
j++;
f();
print(a[0],a[1],a[2]);
}
- Assume static scope binding and that parameters are passed by
value-result. What is the output of the following:
int k = 0;
int a[] = {5,10,15,20};
void f(int x, int y)
{
k++;
x++;
y++;
a[k] = x + y;
}
main()
{
int k = 1;
f(k,a[k]);
print(k,a[0],a[1],a[2],a[3]);
}
- 2 5 11 15 20
- 1 5 13 15 20
- 2 5 13 15 20
- 2 5 18 16 20
- 3 5 10 15 24
- Repeat the previous problem assuming parameters are passed by reference.
- Repeat the previous problem assuming parameters are passed by name.
- Repeat the previous problem assuming paramters are passed by
macro-expansion.
- Given the two overloaded functions f, whose types are given
below, give an example of a call to f that would be legal if either
version was the only one, but is illegal if both are defined. (Assume
C++ overload resolution rules.)
version 1: void f(int, double);
version 2: void f(char, float);
- Given the two overloaded functions f, whose types are given
below, and the call f('A', 5), which of the descriptions of overload
resolutions is correct? (Assume Java overload resolution rules.)
version 1: void f(int, double);
version 2: void f(char, float);
call: f('A', 5)
- Only the first version is admissible and that is the
version that is called.
- Only the second version is admissible and that is the
version that is called.
- Neither version is admissible and the call is illegal.
- Both versions are admissible and the call is illegal.
- Both versions are admissible but version 2 is called.
- Both versions are admissible but version 1 is called.
-
Which two of the following ML functions is polymorphic?
a. fun f(x) = ( x + 1 );
b. fun f(x,y) =
(
if x = 0 then
y
else
f(x-1, real(x) * y)
);
c. fun f(x,y) =
(
if null(x) then
y
else
f(tl(x), hd(x)::y)
);
d. fun f(x,g) = 2.0 * g(x+1);
e. fun f(g,x,y) =
(
if null(y) then
[x]
else if g(x, hd(y)) then
x::y
else
hd(y)::f(g,x, tl(y))
);
- The c++ code below is an example of which one of these
- a type error detected by the compiler
- a type error detected by the run time environment when the
program runs
- a dangling pointer
- a memory leak
- none of these
int main()
{
int *p, *q;
p = new int(5);
q = p;
delete p;
cout << *q << endl;
}
- Which two of the following is true?
- use more space than an equivalent non-tail-recursive function
- use less space than an equivalent non-tail-recursive function
- use the same space as an equivalent non-tail-recursive function
- use more space than an equivalent function that uses a loop
- use less space than an equivalent function that uses a loop
- use similar space as an equivalent function that uses a loop
- Which two of the following functions is not
tail-recursive?
a. fun f(x,y) =
(
if x = 0 then
y + 1
else
f(x-1, f(x+1, y-1))
);
b. fun f(x,y) =
(
if x = 0 then
y
else
f(x-1, real(x) * y)
);
c. fun f(x,y) =
(
if null(x) then
y
else
f(tl(x), hd(x)::y)
);
d. fun f(x,y) =
(
case y of
nil => x
| z::zs =>
f(x + y, tl(y))
);
e. fun f(g,x,y) =
(
if null(y) then
[x]
else if g(x, hd(y)) then
x::y
else
hd(y)::f(g,x, tl(y))
);
- Write the ML function to remove all integer values from a
list less than 90. That is, write the function remove below that
returns a list like lst, but omitting any value < 90.
fun remove(lst: int list) : int list =
(
);
- How many entries are in the virtual function table for class B?
- 3
- 4
- 5
- 6
- none of these
class A class B : public A
{ {
public: public:
A(); B();
virtual void f(); virtual void h();
virtual void g(); virtual void k();
virtual void h(); void p();
void q(); void q();
};
};
- For each of the calls in method h below in class A, tell whether
the call uses static or dynamic dispatch. (Assume C++ code and rules.)
- a.f() (at 1. in A::h)
- a.g() (at 2. in A::h)
- f() (at 3. in A::h)
- g() (at 4. in A::h)
class A
{
virtual void f();
void g();
void h(A a)
{
a.f(); // 1.
a.g(); // 2.
f(); // 3.
g(); // 4.
}
};
class B : public A { ... };
- Does the call pa->f() in main below, use static
dispatch or dynamic dispatch and which implementation of f is called?
(Refer to the classes A and B from the previous problem.)
int main()
{
A *pa;
pa = new B();
pa->g();
}
- Give the output of the following.
class A
{
public:
virtual void hey()
{ cout << "A::hey "; hi(); }
void hi()
{ cout << "A::hi "; ho(); }
virtual void ho()
{ cout << "A::ho "; }
};
class B: public A
{
virtual void hey ()
{ cout << "B::hey "; hi(); }
void hi() { cout << "B::hi "; ho(); }
virtual void ho()
{ cout << "B::ho "; }
};
void main()
{
A x = B();
A* y = new B();
x.hey();
y->hey();
}
- If a destructor executes for class B which is a subclass of
A, then the destructor for A will execute next. For each of the
following, tell which destructor(s) will execute. Each answer will be
one of these: (i) ~A only, (ii) both ~B and ~A.
- delete p at line 22
- delete q at line 23
1 class A class B : public A
2 { {
3 public: public:
4 A(); B();
5 virtual void f(); virtual void f();
6
7 ~A(); ~B();
8 }; };
9
10 int main()
11 {
12 A *p;
13 A *q;
14
15 p = new A();
16 p->f();
17
18 q = new B();
19 q->f();
20
21
22 delete p;
23 delete q;
24
25 return pa;
26 }
- Repeat the previous problem if the destructor ~A is declared
virtual:
class A
{
public:
A();
virtual void f();
virtual ~A();
}