Question 2 Isc With Solution
Question 2 Isc With Solution
(ii) How would the complexity change if the second loop went to N instead of M?
MARKING SCHEME
Question 2.
(a) Interface:- Interface defines only abstract methods and final fields with constant value,
ie. defines a protocol of behavior.
Class : A class is a user defined data type and is a collection of methods which are defined
within an encapsulation. Class cannot have multiple inheritance.
(b) (A+B)/C *(D+E) Stack Expression
AB+ /C DE+ A
AB+C/ DE+ (+ AB
AB+C/DE+* ( AB+
AB+C
(° AB+C/
((+ AB+C/DE
EMPTY AB+C/DE+*
(c) Column major address formula: B[pllq] - BA+ W(m (q-L) +(p -La))
W=2. B[pl[q] =?. LË=0, L=0, p=2. q =3, m=7(row), BA = 1046
B (21[3] =1046 + 2 (3*7 +2)
- 1046 + 46 = 1092 Ans.
(d) Exception Handling is a transparent and neat way for handling program errors.
Two types are: checked and unchecked exceptions.
(e) Complexity - (i) ON+M)
(ii) O(N+N) =0 (2N) or O(N) when constant is dropped.
Question 2
(a) How can we override a method in inberitance? [2
(b) A square matrix A[m x m] is stored in the memory with cach element requiring 2 bytes of
storage. If the base address A[1]I] is 1098 and the address at A (4J15] is l144, detemine the
order of the matrix A[m x m] when the matrix is stored Column Major wise.
(c) What is Big O notation? 2
(d) What is an exception? 2
(e) Convert the following infix expression to its postfix form:
a+bc-dle
MARKING SCHEME
Question 2.
Overriding occurs whenever a subclass has a method with same signature as a method in one of its
superclasses. As a result the method in the derived class overrides the method in superclass.
(b) Column major addressformula : A[iJ6]- BA + WG-1) row + (i-)]
BA 1098, I, =1,4=1,W=2, i=4,j-5, A(iJ0]=1144
1144 = 1098 + 2 (S-1)m +41]
1144 = 1098 + &m +6
8 m= 40 ; m= 40/8 ; m=5Ans
(c) Big O Notation is the measurement of the complexity of an algorithm with respect to time factor and
space (memory) factor.
(d) An exception is an unexpected or anomalous situation or error during a program execution.
(e) a+ b c - d/e
a+ be® - de'
abe*+d e /
Question 2
(a) Differentiate between throw and throws with respect to exception handling.
(b Convert the following infix notation to its postfix fom: 21
E* (F/(G- H) * I) +J
(c) Write the algorithm for push operation (to add elements) in an aray based stack. |21
(d) Name the File Stream classes to:
(b An array AR[-4 ....6, -2....12 ], stores clements in Row Major Wise, with [21
the address AR[2][3] as 4142. If cach element requires 2 bytes of storage, find the
Base address.
(c) State the sequence of traversing a binary tree in: [2]
(i) preorder
(i) postorder
(d) Convert the following infix expression into its postfix form: [21
Nc= Uc - Lc + l= 12 -(-2) +l = 15
i=2 j=3
Lr=4 Le=-2
(a) Computational complexity is the growth rate or measureent of an algonthm. The level in difficulty in
solving mathematically posed problems as measured by the time, number of steps or arithmetic operations.
or memory space required (called time complexity. computational complexity. and space complexity.
respectively).
Complexity of the segment = O(n)
(b) =X+(Y-Z)+( (W + E) * F) 1J
(e) 1. It is acyclic
2. No two node can be similar or identical
3. It is recursive
(d) From the class declaration given below, state the nature of the identifies A, B, C [2]
and D:
class A extends B implements C. D
(e) State one advantage and one disadvantage of using recursion over iteration. |2
MARKING SCHEME
Question 2.
(a) Fall Through: Ifa case does not end with a break statement, it will execute the following cases until
and unless it counters a break statement. This condition is called fall through condition.
(b) A+B/C*(D/E*F)
= A+B/C *(DE/F *)
= A+BC/*(DE/F *)
= A+BC/DE /F **
ABC/DE/F**+
D= INTERFACE
(e) Advantage: Recursion makes the complex programs simpler and easy to understand by making the
code reduced than Iteration.
Disadvantage: Recursion occupies more memory and a slow process than iteration.
Question 2
(a) What is an interface? How is it different from a class? [21
(b) Convert the following infix expression to postfix form: I21
P*Q/R+(S+T)
(c) A matrix P[1S][ 10] is stored with cach element requiring 8 bytes of storage. If the base |2]
address at P[O][0] is 1400, determine the address at P[10][7]when the matrix is stored in
Row Major Wise.
(d) (1) What is the worst case complexity of the following code segment: [2]
for (int x= 1;x<-a; x++)
statcmcnts;
statements;
(ü) How would he complexity hange if the three loops went to Ninstead f a,b
and c?
Atheoretical measure of the execution of an algorithm, usually the time or memory needed,
given the problem size n, which is usually the number of items.
Two factors are Time and Space(memory)
(e) Exceptional Handling: The way to handle anomalous situations during run time.
Finally, is a block of code which is executed with or without exceptions.
Question 2.
(a) State the diference betwCen a Bingary Tree structure
and a single Linked List. (2)
A
(b) A mtrix B{10) [20] is stored in he memory with each
element requiring2 bytes ofstorage. Ifthe baso address
at B(23] is 2140, find the address of B[S]14) when the
matrix is stored in Column Mnjor Wise. (2)
(c) Coavert be following infix notation to prefix forn1
(21
K+ Y (Z* W/v)
(a) State the best case and the worst case complexity for
bubble sort algorithm.t :
(e) Wat is the sigaiticanço of the keyword
Menlion the arcas where it is used.
'new' in Java?
(2]
Answer: We reverse the oblained expression to get he final
answer : /4XY"ZWV
Bioary Tree Llbked List
() Worst Case :O (n)
It is a non-linear dala It is a linear daia struc
structure. ture. Best Case: O(a)
(e) The "ncw keyword is imporant because we need
There are threc methods Traversing a linked list iü every time we necd to create an object of a on
to raverse through a tree, is sinple; we start at the
these are: preorder, inor- first Dode, go linearly pimitive data type. It is used to create arays and
der and postorder. and stop when we get create instanccs of a class.,
the last node.
Atree can be Impkment- Alinked list docs not re
ed using arraysas well as quire any other data
Hnked list. structure for its imple
mentation.
(6) Formula to be used: address base +((C- c) rows
+ (P-)) " W
here, rows- 10, columns-20, -2,c, I, W-2, base
- 2140, r= 5, c= 4,
hence we have, address=2140 +((4-1)+ 10+(5-2))
2
- 2140 +(3* 10+3)2
= 2140+ 33+2
2206
Hence the address is 2206
(e) First we reverse the expression: (V/W°Zy(Y+X)
then we convert it to postix form
Ioput Stack Output
VW
VW/
VWIZ
VWIZ
VWIz*
VWIZ*
VWIZ*Y
+ /(+ vWIz
Y
vWIZYX
vWIZYX+
None VWIz"YX+/