5403 Answers
5403 Answers
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,
(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) 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.
<id> → A | B | C
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)
=>A=<expr>
=><expr>*<expr>
=> (<expr>+<expr>)*(<expr>+<expr>)
Hence derived
7. Compute the weakest precondition for each of the following assignment statements and post-
conditions:
(b) a = (b – 1) * (b – 1) - 9 {a > 0}
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:
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().
}
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);
}
}
}
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
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:
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),
(a) Describe, what will the output be if we typed the following query:
matrix_sum(X).