0% found this document useful (0 votes)
101 views6 pages

5403 Answers

Event driven programming is a paradigm where the flow of a program is determined by events such as user input. It is commonly used in graphical user interfaces and JavaScript applications. There are two types of exceptions in Java - checked exceptions which occur during compilation and unchecked exceptions which occur at runtime. Unchecked exceptions can be created by extending the Exception class. A fixed heap dynamic array allocates storage in the heap rather than the stack, allowing for a flexible size but slower allocation. Pointers store memory addresses and can cause issues if referencing deallocated memory (dangling pointers). Backus-Naur form grammars can recognize context-free languages by using rules to derive strings.

Uploaded by

Pardeep Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views6 pages

5403 Answers

Event driven programming is a paradigm where the flow of a program is determined by events such as user input. It is commonly used in graphical user interfaces and JavaScript applications. There are two types of exceptions in Java - checked exceptions which occur during compilation and unchecked exceptions which occur at runtime. Unchecked exceptions can be created by extending the Exception class. A fixed heap dynamic array allocates storage in the heap rather than the stack, allowing for a flexible size but slower allocation. Pointers store memory addresses and can cause issues if referencing deallocated memory (dangling pointers). Backus-Naur form grammars can recognize context-free languages by using rules to derive strings.

Uploaded by

Pardeep Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. What is Event Driven Programming, where/in-what-area would you typically find it used?

Ans. Event Driven Programming is a programming paradigm in which the flow of the program is
determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages
from other programs/threads which care are broadcast when the event takes place.
Event-driven programming is the dominant paradigm used in graphical user interfaces and other
applications like JavaScript web applications that are centered on performing certain actions in
response to user input.

2. There are two types of exceptions in Java – checked and unchecked. Briefly explain,

(a) What is the difference between these two exception types?

(b) How to create an unchecked exception type.


Ans.
a) The checked exceptions are the exceptions which occur other than run time.
The unchecked exceptions do occur at the runtime.
The checked exception are not preventable.
The unchecked exceptions are completely preventable always and they should not occue.
Many Java statements are likely to cause the unchecked exceptions.
Whereas the checked exceptions are caused by the specific calls.
The checked exceptions occur like when a file is missing which is required by the program, thus this
error can not be sorted and the program needs to be terminated.
The unchecked exceptions are Like if the program calls for a query which far greater than the
actual range. Then the upper limit can be set to upper range, so does the answer of the query, the
preventable.
The example of the checked exceptions are filenotfoundException, IOException etc.
The examples of unchecked exceptions are ArrayIndexOutOfBounds, NullPointerException etc.
b) Unchecked exceptions could be created both explicitly and implicitly. By correcting the system
and the language error, the exception could implemented from the base class Exception. Whereas
in order to handle the specific case. Then it could be caused implicitly. Thus making own exception
throwing and catching it.

3.(a) Define fixed heap-dynamic arrays.

(b) Write a code snippet (in your favourite language) that creates a fixed heap-dynamic array.

Ans. a) in this particular type of array, the range of the subscript and the storage binding both are
fixed after the storage is allocated, the storage is done in the heap not the stack. T
It provides the advantage that the size of the array fits the problem.
It has the disadvantage that the allocation time of heap is greater than the allocation time of the
stack.
b) IN JAVA PROGRAMMING
void create_fixed_heap_dynamic_array{
int arr= new int[10];
}
4. (a)Explain what a pointer type is. Illustrate your answer with examples.

(b)Explain what a dangling pointer is.


Ans. a) pointer type is a data type, in which a variable has in it the range of the values, which are
memory address and the special value called nil. Here the nil tell us that the pointer is not referring
a memory cell at present.
Example: int arr[6];
int *prr=arr;
int first=*(arr); // gives arr[0]

b) a dangling pointer is a pointer which has the address of the heap dynamic variables which are de
allocated in the initial steps. This could lead to the crashing of the program. Also when the dangling
pointer changes these variables then these variables are destroyed.
Example: int * fa=new int;
int *sa= fa;
fa=null;
Here sa is a dangling pointer.

5.Describe the operation of a general language recogniser.Explain, how BNF grammar can be used
as a language recogniser for a context-free grammar.
Ans. A general language recognizer is a recognition device that is capable of reading strings of
characters from the alphabet. It would also analyze the given string and it would either accept or
reject the string based from the language given. The general language recogniser, work like if we
have a language l, and the language use an alphabets known as LChars, in order to define the l, with
the help of recognition method, this creating a mechanism called recognitive device, which would
be capable of reading the strings made with help of LChars, thus analysing if it belongs to l or not.
BNF is a Backus Baut form, which is used to describe a syntax. As the BNF and context free grammar
almost similar things, it uses the abstraction for syntactic structures.
<assign> -> <var> = <expression>
LHS refers to the a abstraction and the RHS refers to the definition.

6. Consider the following context-free grammar:

<assign> → <id> = <expr>

<id> → A | B | C

<expr> → (<expr> + <expr>) | <expr> * <expr>| <id>++ | ++<id> | <id>

Provide a leftmost derivation for the following statement:

A = (B + C++) * (++A + B )
ans<assign> → <id> = <expr>
<id> → A | B | C
<expr> → (<expr> + <expr>)
| <expr> * <expr>
| <id> ++
| ++ <id>
| <id>
A=( B+ C++)*(++A + B)

<assign> => <id> = <expr>

=>A=<expr>
=><expr>*<expr>

=> (<expr>+<expr>)*(<expr>+<expr>)

=> (B+ <id>++) *(++<id> +B )

=> (B+ C++) * ( ++A +B)

Hence derived
7. Compute the weakest precondition for each of the following assignment statements and post-
conditions:

(a) b = (c - 25) / 3 {b > 10}

(b) a = (b – 1) * (b – 1) - 9 {a > 0}

Ans. b= (c-25)/3 {b>10}


(c-25)/3 > 10
c-25> 30
c> 55

a>0
(b-1)*(b-1)-9 > 0
(b-1)^ 2> 9
(b-1)> (+)3
b> 4
8. Explain how a functional language implements repetition? Give a pseudocode example of a
function that can be used to compute the product of numbers 1 through 10 and print the result.

Ans. functional language implements the repitition with the help of loops.
The pseudo code is as below:

Function to_give_product (product)


number : int = 1;
for number in 1..10 loop
product := product*number
end loop;
return product.
9. Explain the concept of referential transparency. What are the advantages of writing methods
which are referentially transparent?
Ans. The referential transparency refers to replacing the expression some other value, which was
the result of the expression. Like replacing 2+12 by 14, such that the result is not affected.
Advantages of writing these methods are:
Helps in the better reasoning of the program.
It makes the different sub programs independent of each other
Thus helping in unit testing and refactoring.
10. Define competition synchronisation and cooperation synchronisation.
Ans. Competition synchronisation is required between two tasks when both require the use of some
resource that cannot be simultaneously used. Specifically, if task A needs to access a shared data
location x while task B is accessing x, then task
A must wait for B to complete its processing of x and release the resource first.

Cooperation synchronisation is required between task A and task B when task A must wait for task B
to complete some specific activity before task A can begin or continue its execution.
11. With regard to Java threading, explain the purpose of each of the following common threading
methods:

- sleep(),

- join()

- yield().

Illustrate your answers using code snippets.


Ans. For Sleep ()
Sleep is used to pause the execution of a thread for a specified amount of time. After that time, the
program would continue as usual

public class Example {


public static void main(String ... args) throws InterruptedException {
for (int i = 0; i < 5; i++) {
System.out.println("Hello");
Thread.sleep(2000);
}
}

}
The program would print Hello every 2 seconds

For Join()
join is used by a thread to wait for another thread to have finished it's execution, and then this
thread would finish
class ExThread extends Thread throws InterruptedException
{
public void run()
{
for (int i = 0; i < 2; i++)
{
Thread.sleep(1000);
System.out.println("Current Thread is " + Thread.currentThread().getName());
System.out.println(i);
}
}
}

public class Example {


public static void main(String ... args) throws InterruptedException {
for (int i = 0; i < 5; i++) {
Thread t1 = new ExThread();
Thread t2 = new ExThread();
t1.start();
t1.join();
t2.start();
}
}
}
Here, 2nd thread would be stared after t1 has completely finished.

For yield()
yeild() function is attached to a thread so that any other thread with high priority could consume its
CPU to which the previous thread has been allocated, and after completion of high priority thread,
the older thread would continue as normal

public class Example {


public static void main(String ... args) throws InterruptedException {
for (int i = 0; i < 5; i++) {
Thread t1 = new ExThread();
Thread t2 = new ExThread();

t1.start();
t1.yield();
t2.start();
}
}
}
Here, t2 will finish itself first, and then t1 in case of a single-core CPU

12. Explain what a closure is and what features of closures make them useful?
Ans. Closures are the functions in the class which are having the bounded variables. They are passed
to the another function in the form of the parameter. With the closure outer function can be
accessed through the inner function only.
These are useful
Data privacy.
Currying
They are mostly used in Java script than Java. Closures are referred as powerful tools by JavaScript
because of number of benefits, like calculating how many times the user clicked on the window.
Features making it useful are as follow:
1. Function combined with the reference to lexical environment, forms the closure. Every time a
function is being formed, the closure is also created.
2. The private methods can be emulated with the help of the closures.
13. In logic programming, we can often replace two propositions with a single proposition via the
process of resolution. Explain the general process of resolution and give an example of it in action.
Ans. In logic programming, 2 propositions are replaced with the single proposition. The resolution
can be used to join the two propositions and derive the new proposition, with the help of some
clues.
For example
Parent(bill, Jake)
Parent(bill, Shelley)
Thus the siblings(Jake, Shelley) is derived.
Only by the following expression,
Siblings(X,Y) :- parents(P,X), parents(P,Y) X\==Y
This will return two different siblings, otherwise it could return X in both the conditions.
14. Explain the closed-world assumption used by Prolog. Why is this a limitation?
Ans. The Prolog doesn't have knowledge about the world around it. Only the facts stated in its
database are considered true to Prolog. Any query that doesn't have enough information in
database to prove it as true is considered to be false. This is called as closed-world problem.
The main limitation of the assumption is that, the system can prove the given query as true but the
system cant prove the query to be false. Generally, Prolog is true/fail system rather than a
true/false system.
15. Write the following English conditional statements as Prolog headed Horn clauses:

a. If John is the father of Bill, then John is an ancestor of Bill.

b. If Andrew is the father of John, and Andrew is the father of Mary, then Mary is the sister of John.
Ans. a) ancestor(John, Bill) :- father(John, Bill)
b) sister(Mary, John) :- father(Andrew, John), father(Andrew, Mary)
16. Consider the following Prolog program:

num(1).

num(2).

num(3).

num(4).

matrix(A11,A12,A21,A22):-

num(A11),num(A12),num(A21),num(A22),

A11 =\= A12,A11 =\= A21,A11 =\= A22,

A12 =\= A21,A12 =\= A22,A21 =\= A22.

matrix_sum(X):- matrix(A11,A12,A21,A22), X is A11+A12+A21+A22.

(a) Describe, what will the output be if we typed the following query:

matrix_sum(X).

(b) Explain your answer


Ans. a) The output will be in the form of rows and columns. The output is just like a matrix. In this
matrix sum would be sum of all the values in matrix.
b) the explanation should be straight forward, matrix sum is supposed to be sum of all rows, and
sum of all columns in the matrix, the result be a matrix of dimension 1x2, where first value is sum of
rows, then 2nd is the sum of columns

You might also like