+ All Categories
Home > Documents > 440 Sample questions 2009 fall

440 Sample questions 2009 fall

Date post: 03-Jan-2016
Category:
Upload: clarke-soto
View: 22 times
Download: 1 times
Share this document with a friend
Description:
440 Sample questions 2009 fall. Which of the following is not an OO programming language? Java C++ Smalltalk Simula 67 None of the above. Evolution of programming languages. OO. logic. Imperative. functional. 1955 1960 1965 1970 1975 1980 1985 1990 1995 2000 2005 2007. - PowerPoint PPT Presentation
30
1 440 Sample questions 2009 fall
Transcript
Page 1: 440 Sample questions 2009 fall

1

440 Sample questions 2009 fall

Page 2: 440 Sample questions 2009 fall

2

• Which of the following is not an OO programming language?

– Java

– C++

– Smalltalk

– Simula 67

– None of the above.

Page 3: 440 Sample questions 2009 fall

3

Evolution of programming languages

1955

1960

1965

1970

1975

1980

1985

1990

1995

2000

20052007

Fortran IFortran II

Fortran 77

Fortran 90

Lisp

Common Lisp

SchemeML

MercuryDelphi

Haskell

CAML

Goedel

PrologPascal

Ada95

Modula-2

Oberon

Algol 58Algol 60

Algol 68Simula 67

Simula IBasic

C

C++

Java

Ada

Cobol

PL/I

Smalltalk 80

QuickBasicVisual Basic

Fortran IV

Fortran 2003

DelphiAda95

C

C++

Java

Smalltalk 80

C#

mapReduce

Imperative logicfunctionalOO

Page 4: 440 Sample questions 2009 fall

4

• The following figure is used to explain which language:

•– Business process language

– Prism language

– Aspect oriented language

– Persistent programming language

– None of the above.

Page 5: 440 Sample questions 2009 fall

5

• Given the following Hoare triple

{true}

f := 1; n:=10;

while (n != 0) do ( f := n*f; n:=n-1; )

{f=10!}

• Write its loop invariant. You don’t need to write the proof of the program.

Page 6: 440 Sample questions 2009 fall

6

• Solution:

n f

10 1

9 10*1

8 9*10*1

7 8*9*10*1

P is f=10!/n!

Page 7: 440 Sample questions 2009 fall

7

• Which of the following language is not a declarative language? – Java

– Prolog

– SQL

– Scheme

– None of the above.

• Imperative: a programming paradigm that describes computation in terms of a program state and statements that change the program state.

• A program is "declarative" if it describes what something is, rather than how to create it.– Imperative programs make the algorithm explicit and leave the goal implicit;

– Declarative programs make the goal explicit and leave the algorithm implicit.

• Examples of declarative languages: – Functional programming languages, Logic programming languages, SQL.

Page 8: 440 Sample questions 2009 fall

8

• Which of the following is a script language?– Java

– C#

– PHP

– Prolog

– Scheme

– None of the above.

• Scripting: connecting diverse pre-existing components to accomplish a new related task. – Favor rapid development over efficiency of execution;

– Often implemented with interpreters rather than compilers;

– Strong at communication with program components written in other languages.

Page 9: 440 Sample questions 2009 fall

9

• (1 point) BNF was first used to describe the syntax of which language:

– C

– Fortran

– Algol

– COBOL

– LISP

– None of the above

Page 10: 440 Sample questions 2009 fall

10

ALGOL (ALGOrithmic Language)

• de facto standard way to report algorithms in print

• Designed to improve Fortran

• John Backus developed the Backus Normal/Naur Form method of describing programming languages.

• ALGOL 60 inspired many languages that followed it "ALGOL 60 was a great improvement on its successors.“

The full quote is "Here is a language so far ahead of its time, that it was not only an improvement on its predecessors, but also on nearly all its successors" --C. A. R Hoare

procedure Absmax(a) Size:(n, m) Result:(y) Subscripts:(i, k); value n, m; array a; integer n, m, i, k; real y; comment The absolute greatest element of the matrix a, of size n by m is transferred to y, and the subscripts of

this element to i and k; begin integer p, q; y := 0; i := k := 1; for p:=1 step 1 until n do for q:=1 step 1 until m do if abs(a[p, q]) > y then begin y := abs(a[p, q]); i := p; k := q end end Absmax

Page 11: 440 Sample questions 2009 fall

11

• Which of the following is not part of a compiler?

– Scanner;

– Parser;

– Code generator;

– Optimizer;

– Interpreter;

– None of the above.

Page 12: 440 Sample questions 2009 fall

12

Compilation and execution

Output DataInput Data

Target Program

Abstract Program(Optimized)

Parse TreeSymbol Table

Source program

CodeOptimization

SemanticAnalysis

Loader / LinkerCodeGeneration

Computer

Lexical Analysis(scanning)

Syntactic Analysis(parsing)

compiler

Token Sequence

Abstract Program(Intermediate code)

Object Program(Native Code)

Cla

ssifica

tion

by im

ple

men

tatio

n m

eth

od

s

Page 13: 440 Sample questions 2009 fall

13

• (1 point) When you run the following Java program:

public class Hello {

public static void main(String [ ] a){

System.out.println("Hello");

}

}

• How many classes will be loaded into the system?

– One

– Two

– Three

– Dozens

– Hundreds

– None of the above

Page 14: 440 Sample questions 2009 fall

14

Run java -verbose

sol:~/440>java -verbose Hello

[Opened /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Opened /usr/jdk/instances/jdk1.5.0/jre/lib/jsse.jar]

[Opened /usr/jdk/instances/jdk1.5.0/jre/lib/jce.jar]

[Opened /usr/jdk/instances/jdk1.5.0/jre/lib/charsets.jar]

[Loaded java.lang.Object from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.io.Serializable from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.Comparable from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.CharSequence from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.String from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.reflect.GenericDeclaration from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.reflect.Type from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.reflect.AnnotatedElement from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.Class from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.Cloneable from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.ClassLoader from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.System from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

… (hundreds of related classes)

[Loaded Hello from file:/global/fac2/jlu/440/]

Hello

[Loaded java.lang.Shutdown from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

[Loaded java.lang.Shutdown$Lock from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar]

public class Hello {public static void main(String [] a){ System.out.println("Hello");}}

Page 15: 440 Sample questions 2009 fall

15

• A program written in an interpreted language is usually

– slow to run;

– slow to develop;

– slow to compile;

– None of the above.

Page 16: 440 Sample questions 2009 fall

16

Interpreted language

• Programs are executed from source form, by an interpreter.

– many languages have both compilers and interpreters, including Lisp, BASIC, and Python.

• Disadvantages:– Much slower

– Real time translation;– Initially, interpreted languages were compiled line-by-line; each

line was compiled as it was about to be executed, and if a loop or subroutine caused certain lines to be executed multiple times, they would be recompiled every time.

– Require more space.– Source code, symbol table, …

• Advantage of interpreted languages– Easy implementation of source-level debugging operations,

because run-time errors can refer to source-level units– E.g., if an array index is out of range, the error message can easily

indicate the source line and the name of the array.

– It can take less time to interpret it than the total time required to compile and run it. This is especially important when prototyping and testing code when an edit-interpret-debug cycle can often be much shorter than an edit-compile-run-debug cycle. (e.g., csh)

Cla

ssifica

tion

by im

ple

men

tatio

n m

eth

od

s

Page 17: 440 Sample questions 2009 fall

17

• Compared with declarative languages, which of the following is not true for imperative languages:

– Can have side effects;

– More efficient to run;

– Closer to computer;

– Harder to implement;

– None of the above.

• True or false: Declarative languages are more difficult to implement than imperative languages.

Page 18: 440 Sample questions 2009 fall

18

Why is it difficult to implement declarative languages?

• By definition, it specifies what the task is, not the way how to solve it.

• Consider the following query:– customers(id, name, phone)– Orders(o-id, c-id, product, price, date)

SELECT product, price, dateFROM customers, ordersWHERE customers.id = orders.c-id AND customers.name=“john”

• It declares what we want. Does not specify how to implement it.– e.g. which condition to run first?

• There are many different ways to implement. – A naïve one would be very expensive (construct the Cartesian product

of the two tables, join two ids first) would be very expensive;• Query engine (compiler) will take care of these implementation

issue.

• Conclusions:– Declarative programming focus on higher level of abstraction;– It is more difficult to implement.

Page 19: 440 Sample questions 2009 fall

19

• True or false: Imperative languages are very high level programming languages.

thought

Languagesmachine

High Level Language

Assembly Language

Machine Language

Very high level Language

Closer to humans

Page 20: 440 Sample questions 2009 fall

20

• Which of the following is the rule for if statement?

– None of above

}{}{

}{}{

QSthenBifP

QSP

}{}{

}{}{

QSthenBifP

QSBP

}{}{

}{}{

QSthenBifP

QBQSP

}{}{

}{}{

QSthenBifP

QBQSP

Page 21: 440 Sample questions 2009 fall

21

Which of the following is the rule for while statement?

}{}{

}{}{

QSdoBwhileP

QSP}{}{

}{}{

QSdoBwhileP

QPPSP

}{}{

}{}{

QSdoBwhileP

QBPPSBP }{}{

}{}{

BPSdoBwhileP

PSP

Page 22: 440 Sample questions 2009 fall

22

• True or false: A compiler can translate source code of one high level language to another high level language.

• Compilation: translating high-level program (source language) into machine code (machine language)

– Slow translation, fast execution

• High level to high level language translation: programming language transformation

Page 23: 440 Sample questions 2009 fall

23

True or false: Modern high level programming languages removed GOTO statement, hence they can’t describe some computational tasks that could have been described using GOTO statement.

Page 24: 440 Sample questions 2009 fall

24

Structured programming

• Any program can be goto-free (1966, Böhm and Jacopini, CACM)

– any program with gotos could be transformed into a goto-free form involving only

– Sequential composition

– choice (IF THEN ELSE) and

– loops (WHILE condition DO xxx),

– possibly with duplicated code and/or the addition of Boolean variables (true/false flags).

C

S

Y NS2

C

S1

Y N

S1 S2

Cla

ssifica

tion

by p

rog

ram

min

g p

ara

dig

ms

Page 25: 440 Sample questions 2009 fall

25

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.

• {P} S {Q} means “if we assume that P holds before S starts executing, then Q holds at the end of the execution of S”

– I.e., if we assume P before execution of S, Q is guaranteed after execution of S

• To prove total correctness we also have to prove that the loop terminates

Page 26: 440 Sample questions 2009 fall

26

• Given the following Hoare triple

{true}

x := 0; f := 1;

while ( x < n ) do (x := x + 1; f := f * x;)

{f=n!}

• Write its loop invariant.

f=x!

∧ x<=n

Page 27: 440 Sample questions 2009 fall

27

• List four different language paradigms and give at least one example language for each paradigm.

• Aspect-oriented programming addresses the problem of crosscutting concerns. What are crosscutting concerns and what are the two main problems of crosscutting concerns?

Page 28: 440 Sample questions 2009 fall

28

Given the following partial program, fill in the missing part so that it will print the following:

Hello Harry, having fun?

public class MessageCommunicator {

public static void deliver(String person, String message) {

System.out.print(person + ", " + message);

}

}

public class Test {

public static void main(String[] args) {

MessageCommunicator.deliver("Harry", "having fun?");

}

}

public aspect HindiSalutationAspect {

pointcut sayToPerson(String person) :

call(void MessageCommunicator.deliver(____________ ))

&& args(____________________________);

void around(String person) : sayToPerson(person) {

proceed(______________);

}

}

Page 29: 440 Sample questions 2009 fall

29

For our Account example in this course, suppose the main method will run the following two statements in sequel. The initial account balance is zero.

account.credit(100);

account.debit(20);

• What will be the account balance for the following advice?

void around(): call(* *.credit(..)){ proceed();}

• What will be the balance if we run the following advice instead?

void around(float amount): call(* *.credit(float)) && args(amount) {

proceed(amount+1000);

}

• What will be the balance for the following advice without including the previous two advices:

– void around(): call(* *.debit(..)){ }

Page 30: 440 Sample questions 2009 fall

30

• Prove that the following Hoare triple is true. You should write down the details of derivation. In each step please write the rule name that is used. {x=2 y=0} x:=y; y:=x; {x=0 y=0}

Solution:

{x=0 y=0 [x/y]} y:=x; {x=0 y=0}, by assignment Axiom

{x=0 x=0} y:=x; {x=0 y=0}, by simplification

a) {x=0} y:=x; {x=0 y=0}, by simplification

{x=0 [y/x]} x:=y; {x=0}, by assignment axiom

b) {y=0} x:=y; {x=0}, by simplification

By sequential composition rule and a), b),

c) {y=0} x:=y; y:=x; {x=0 y=0}

d) x=2 y=0 y=0 , by logic

by Consequence rule and c), d), we can get

d) {x=2 y=0} x:=y; y:=x; {x=0 y=0}


Recommended