440 Sample Questions Dec
440 Sample Questions Dec
Questions are mostly from midterm and final exams in 2006. Some outdated questions are removed. Sample
answers are in red font. They are provided by Wenyi Zhou.
Answer: e
2. Which one of the following is not a type of polymorphism
Answer: c
3. What formal system provides the semantic foundation for Prolog?
Answer: a
4. Which of the following is the advantage of declarative languages over imperative languages?
(a) Can use abstract data type; (d) Can be implemented by an interpreter or com-
(b) Easy to verify the properties of the program; piler;
(c) Is more efficient; (e) Can be strong-typed.
Answer: b
5. Given the expression ((λx.x)λx.x)a in lambda calculus. Derive the expression as far as possible using β −
reduction. The final result will be:
Answer: d
6. Which of the following captures all the pointcuts of calling private methods that take int as its only argument?
1
(a) call(private int *()) (d) call(* private *(*))
(b) call(private * *(int))
(c) call (private *(int)) (e) None of above
Answer: b
7. Which one of the following is not a language construct in AspectJ?
Answer: b
8. In Scheme language, which of the following is not a higher-order function?
Answer: b
9. What is the value of (map (lambda (x) (* 2 x)) ’(1 2 3)) ?
Answer: d
10. Given the following Java code: a = 2 * 3.14. It is using which of the following polymorphism?
Answer: b
11. Given the following class definition. Which of the following methods could be legally placed after the comment
with the commented word ”//Here”?
(a) public void amethod(String s, int i) (c) public void amethod(int i, String mystring)
(b) public int amethod(int i, String s) (d) public void Amethod(int i, String s)
If there are more than one correct answers, select all of them.
Answer: a and d (overloaded methods need to have different method signature)
2
2 Select true or false for the following statements
1. In hybrid implementation, the source code is translated into intermediate code, then the intermediate code is
interpreted. Answer: True.
2. In an abstract data type, the data representation of the type is the focus. Answer: False. In ADT, a type is
defined in terms of operations.
3. One programming language can support multiple programming paradigms. Answer: True.
4. If we can prove the Hoare triple {P} S {Q}, we can say that program S is totally correct for the pre-condition
P and pos-condition Q. Answer: False.
5. Java supports higher order function. Answer: False
3 Provide short answers for the following questions, each in a few sen-
tences
1. What is static binding? Give a short answer with one sentence. Answer: The binding such as the association
of a method name to the method definition is static, i.e., at compile time.
2. Java 5 supports parametric polymorphism (generics). Write the name of another language that supports
parametric polymorphism. Answer: C++ (Template).
3. Java language support automated garbage collection. Give the name of another language that supports auto-
mated garbage collection in its typical implementation. Answer: C++ or C# for instance
4. (Code scattering problem can be solved by which programming method? Answer: Aspect-Oriented Program-
ming
5. Explain strong typing in one sentence. Answer: The compiler tries to find as many potential errors as possible.
6. Give an example in Java that violates strong typing. You need to write a fragment of the code and explain
why it is not strong typing.
7. List the principal phases of compilation in the correct order. Answer: Lexical analysis, Syntax analysis,
Semantics analysis, Code generation.
8. In Java, give an example of an attribute of a variable that has a static binding time. Give another example of
an attribute that has a dynamic binding time.
9. Give one advantage of compilation over interpretation. Answer: For instance, fast execution
10. Give one advantage of interpretation over compilation. Answer: For instance, source-level debugging, because
run-time errors can refer to source-level units. It can take less time to interpret it than the total time required
to compile and run it.
11. Give one advantage of hybrid implementation. Answer: Faster than pure interpretation since the source is
translated only once.
12. Give the following Java program. If there are compilation errors, point out the errors and change the program
into a correct one. If it is correct, give the print out result.
3
1 public class Test {
2 static int count(Vector<Object> ns) {
3 int i=0;
4 for (Object n: ns) i++;
5 return i;
6 }
7 public static void main(String[] a) {
8 Vector<Integer> v = new Vector<Integer>();
9 v.add(214);
10 v.add(440);
11 System.out.println(count(v));
12 }
13 }
as
By modifying lines 4 and 5 only, make the program correct if possible. If you cannot make it correct, explain
the reasons.
Answer: We cant make it correct by changing the part in italics only. Both interfaces A and B need to be
implemented. m2 in two classes A and B has the same signature. Therefore they can not be implemented in
the same class.
14. List four different language paradigms that are mentioned in this course and give one example language for
each category.
• Paradigm: AOP, representative language: AspectJ
• Paradigm: OOP, representative language: Java
• Paradigm: Functional Programming, representative language: Scheme
• Paradigm: LogicProgramming, representative language: Prolog
15. Among the following list of programming languages and develop environment:
Algol, C, C++, Java, C#, Fortran, LISP, Prolog, SIMULA, Scheme, Basic, COBOL, Eclipse, Perl, Ruby.
4
4 Other questions
1. Given the following definitions:
findnth([H|T], 1, H).
findnth([H|T], N, X):- N1 is N-1, findnth(T,N1,X).
subset([ ],Y).
subset([A|X],Y) :- member(A,Y), subset(X,Y).
4. Given the following Java classes and aspect, fill in the missing parts so that it can print out the following:
1 public class M e s s a g e C o m m u n i c a t o r {
2 public static void deliver ( String message ) {
3 System . out . println ( message );
4 }
5 public static void deliver ( String person , String message ) {
6 System . out . print ( person + " ," + message );
7 }
8 }
9
10 public aspect H i n d i S a l u t a t i o n A s p e c t {
11 pointcut sayToPerson ( String person )
12 : call ( void M e s s a g e C o m m u n i c a t o r . deliver (String,String)) && arg (person,String);
13
14 void around ( String person ) : sayToPerson ( person ) {
15 proceed (person + "-ji");
16 }
17 }
18 public class Test {
19 public static void main ( String [] args ) {
20 M e s s a g e C o m m u n i c a t o r . deliver ( " Wanna learn AspectJ ? " );
21 M e s s a g e C o m m u n i c a t o r . deliver ( " Harry " , " having fun ? " );
22 }
23 }
5
5. Given the following XML document as an input for XSLT programs, answer the following questions:
< source >
< employee >
< firstName > Joe </ firstName >
< surName > Smith </ surName >
</ employee >
Quention 1: Write the output of the following XSLT program. You dont need to write the exact spaces and
carriage returns.
< xsl:sty lesheet xmlns:xsl = " http: // www . w3 . org /1999/ XSL / Transform " version = " 1.0 " >
< xsl:template match = " source / employee " >
</ xsl:template >
< xsl:template match = " employee / supervisor / employee " >
first name is < xsl:value-of select = " firstName " / >
</ xsl:template >
</ xsl:st yleshee t >
Answer: Empty
Question 2: Write an XSLT program to extract a list of the first names of all the employees under supervisor
elements, separated by coma. For this sample data, the output should be “Steve, fn2”.
A sample solution is
<xsl:template match="source/employee">
<xsl:apply-templates select="supervisor"/>
</xsl:template>
<xsl:template match="supervisor">
<xsl:apply-templates select="employee"/>
</xsl:template>
<xsl:template match="supervisor/employee[1]">
<xsl:value-of select="firstName"/>
</xsl:template>
<xsl:template match="supervisor/employee[position()>1]">
,<xsl:value-of select="firstName"/>
</xsl:template>
</xsl:stylesheet>
6
6. Prove the following Hoare triple is true.
{true}
f := 1; n:=10;
while (n != 0) do ( f := n*f; n:=n-1; )
{f=10!}
Sample answer:
f n
1 10
10*1 9
9*10*1 8
10!
B is n 6= 0, ¬B is n = 0, so P is f = n! .
10! 10!
f= n := n − 1 f = (1, by assignment axiom)
(n − 1)! n!
10! 10!
n×f = f := n ∗ f f = (2, by assignment axiom)
(n − 1)! (n − 1)!
10! 10!
f= f := n ∗ f f = (3, by arithmetic from 2)
n! (n − 1)!
10! 10!
f= f := n ∗ f ; n := n − 1 f = (4, by sequential composition on 3 and 1)
n! n!
10! 10!
n 6= 0 ∧ f = ⇒f = (5, by arithmetic)
n! n!
10! 10!
n 6= 0 ∧ f = f := n ∗ f ; n := n − 1 f = (6, by consequence rule on 5 and 4)
n! n!
10! 10!
f= while(n 6= 0)do(f := n ∗ f ; n := n − 1) n = 0 ∧ f = (7, by while rule on 6)
n! n!
10!
n=0∧f = ⇒ 10! (8, by arithmetic)
n!
10! n o
f= while(n 6= 0)do(f := n ∗ f ; n := n − 1) f = 10! (9, by consequence rule on 8 and 7)
n!
10! 10!
f= n := 10; f = (10, by assignment axiom)
10! n!
{1 = 1} f := 1; {f = 1} (11, by assignment axiom)
10!
{true} f := 1; n := 10; f = (12, by sequential composition on 10 and 11)
n!
10!
{true} f := 1; n := 10; W HILE f = (13, by sequential composition on 12 and 9)
n!