0% found this document useful (0 votes)
27 views18 pages

Question 2 Isc With Solution

The document contains a series of questions and answers related to programming concepts, including interfaces, exception handling, computational complexity, and data structures. It covers topics such as converting infix expressions to postfix, calculating memory addresses for arrays, and the differences between classes and interfaces. Additionally, it discusses the advantages and disadvantages of recursion, the nature of identifiers in class declarations, and the definition of Big O notation.

Uploaded by

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

Question 2 Isc With Solution

The document contains a series of questions and answers related to programming concepts, including interfaces, exception handling, computational complexity, and data structures. It covers topics such as converting infix expressions to postfix, calculating memory addresses for arrays, and the differences between classes and interfaces. Additionally, it discusses the advantages and disadvantages of recursion, the nature of identifiers in class declarations, and the definition of Big O notation.

Uploaded by

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

Question 2

(a) State the difference between an Interface and a Class. (21


(b) Convert the following infix notation to postfix notation:
(A+ B)/C* (D+ E)
(c) A character aray B[7) [6] has a base address 1046 at 0,0. Calculate the address at (2]
B[2|[3] if the array is stored Column Major wise. Each character requires two bytes of
storage.
(d) State the use of exceptional handling. Name the two types of exceptions. (21
(e) (i) What is the worst-case complexity of the following code segment:
for (int i=0;i<N;itt)
{sequence of statements

for (int j=0:j<M;jt+)


{sequence of statements

(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:

Write data to a file in binary form.


(ii Read data from a file in text form.
(e) A square matrix M UU of size 10 is stored in the memory with cach element requiring (2]
4 bytes of storage. If the base address at M [O[O] is 1840, detemine the address at M
[4][8] when the matrix is stored in Row Major Wise.
MARKING SCHEME
Question 2.
(a) Throw used to explicitly raise a exception within the pogram, the statement would throw
new exception.
Throws :- this clause is used to indicate the exception that are not handled by the method.
(b) E*(F/(G-H) * I)+J
E*(F/ GH- *I) +J
E*FGH-/|*+ J
EFGH-/| ** J+
(c) Step 1: Start
Step 2: if top >= capacity then OVERFLOW, Exit
Step 3: top = top+l
Step 4: Stack[top] = value
Step 5: Stop
(d) (i) FileOutpu tS tre am / DataOutput Stre am/ FileWriter/ Output Strcam
(ii) FileReader/ DatalnputStream/ InputStream/ FilelnputStream
(e) Row Major address formula: MÜlil = BA + W [(i Ir) * column + (j Ic))
B¢ =1840, Ir =0,lc =0, W=4,rows = 10, column=10,i=4,j=8
MÊ4][8] = 1840 +4[(4-0)x 10+ (8-0)]
1840 + 192
2032
Question 2

(a) State wo advantages of using the concept of inheritance in Java. [21

(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

(A/B+C )* (D/ (E -F))


(e) State the difference between the functions int nextht( )and boolean hasNextlnt( ). 21
MARKING SCHEME
Question 2.
(a () Reusabi lity (ii) Transitive nature.
(b) Address of AR[ij] = B+ W[ Nc ( i - Lr) + (j - Le)]
Address of ARJi.i]=4142
B=?
W=2

Nc= Uc - Lc + l= 12 -(-2) +l = 15
i=2 j=3
Lr=4 Le=-2

4142= B + 2| 15(2-(-4) ) +(3-(-2)) |


4142 =B+ 2[90+ 5]
B=4142- 190 =3952
(c) (1) root - left- right (+LR) (ii) left - right -root (LR+)
(d) (A/B+C)* (D/ (E-F)
= (AB/+C ) * (D/(EF-))
= (AB/C+) * (DEF-)
= AB/C+ DEF-/ *
(e) nextInt() Returns next token as an integer
hasNextInt () Returns true if the input has another token and that token can be interpreted as an
integer.
Question 2
(a) Define computational complexity. Calculate the complexity using Big»o' notation [2]
for the following code segment:
for(int k=0; k<n:k++)
s+=k:
b) Convert the following infix notation into postfix fon: [2]
X +(Y -Z) +( W+E) * F)/J
(c) Differentiate between this keyword and super key word. [2]
(d) The array D[-2...10][3..8] contains double type elements. If the base address is [2]
4110, find the address of D[4]I|S], when the array is stored in Column Major Wise.
(e) State any two characteristics of a Binary tree. [2]
MARKING SCHEME
Question 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

=X+ YZ- + (WE+ * F)/J


=X+ Y Z-+ WE+F* /J

=X+ YZ-+ WE+F* J/


=XYZ- + WE+ F*J/+
(c) this ( ) is used to refer to the instance variables of the class.
super( ) is used to invoke the super class's constructor/ instance variable/methods
(d) The given val ues are:
B=4110, W =8 bytes or 64 bits for double type, I= 4, J= 5, Lr=-2, Lc=3,
M= (10-(-2+1 = 10+2+1 = 13
Address of A[I J[ J ]= B +W * |(I- Lr) + M*(J -Le) ]
= 41 10 +8* [(4-(-2)) + 13 * (5 -3)]
=4110 + 8 * [6 + 13*2]
= 4110 + 8* (6+26]
=4110+8* (32] =4110+ 256 OR = 41 10 + 64 * |32] = 4110 + 2048
= 4366 (Ans) = 6158 (Ans)

(e) 1. It is acyclic
2. No two node can be similar or identical
3. It is recursive

4. There is only one unique path between two nodes.


5. Rooted data structure OR Linear data structure OR Dynamic data structure
6. Every node has a maximum of twosub nodes ( or maximum degree 2)
Question 2
(a) Define the term fall through condition with reference to switch( ) case. |2
(b) Convert the following infix exprssion to postfix form: [2]
A+B/C*(D/E*F)
(c) A matrix A[m]n] is stored with cach element requiring 4 bytes of storage. If the [2|
base address at A[11] is 1500 and the address at A[4|[5] is 1608, determine the
number of rows of the matrix when the matrix is stored in Column Major Wise.

(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**+

(c) Column Major Wise: A[iÚ)=BA+ W[G-1) * rows + (i-1)]


Putting the values: 1608 = 1500 + 4[ (5-1)"rows + (4-1)]
1608 = 1500 + 16 *rows + 12
-16 * rows = -96
Row 6

(d) A= DERIVED CLASS /SUB CLASS


B= SUPER CLASS/BASE CLASS
C= INTERFACE

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;

for (nt y 1;y<<b; yt)

for (int z=l; z <=c; z++)

statements;

(ü) How would he complexity hange if the three loops went to Ninstead f a,b
and c?

(e) Differentiate between a constructor and a method of a class. [21


MARKING SCHEME
Question2
(a) Interface is a non primitive data type which has static and final data members and prototype of
functions ( i.e. functions are not defined
Difference: Interface supports multiple inheritance whercas a Class docs not support multiple
Inheritance

(b) Infix to postfix : P*Q/R +(S+T)


= P* Q/R+ ST+
= P Q* /R+ ST+
= PO*R + ST+
Ans: PO*R/ST++

(c) Row Major Wise: Pii] =BA + W[(i-)* column + G-1]


= 1400 + 8 (10-0 10+(7-0))
= 1400+ 856
P[10||7] = 2256
(c) ) O(a) + O(b x c)
=O{a + bc)
(ii) O(N) + O(N)
= 0(N) taking thce dominant term.
(e) Constructor has the same name of the dass where as a method has a different name. Therei s
no retuming type, not even vod in con structor where as in a method it can return a value.
Question 2
(a) What are Wrapper Classes? Give any two examples. [21
(b) A matrix A[m][m] is stored in the memory with cach element requiring 4 bytes of [2]
storage. If the base address at A [1][1] is 1500 and address of A [4][S] is 1608,
determine the order of matrix when it is stored in Column Major Wise.
(c) Convert the following infix notation to postfix form: [2]
A+ (B-C* (D/E) * F)
(d) Define Big '0' notation. State the two factors which determine the complexity of an [2]
algorithm.
(c) What is exceptional handling? Also, state the purpose offinally block in a try catch [2]
statement.
MARKING SCHEME
Question2
(a) Wrapper Class: A class which defines primitive data type.
A Wrapper class is a class whose object wraps or contains a primitive data types.
Wrapper class in java provides the mechanism to convert primitive into object and
object into prinitive.
Example: Integer, Character, Double etc.
(b) Column Major Wise: PiJ] = BA + W [G -l )* rows + (i-I)
Putting the values: 1608 = 1500 + 4[ (4*m + 3)]
1608 = 1500 + 16m + 12
16m = 1608-1512
m =6

(c) Infix to Postfix: A +(B-C* (D/E) * F)


=A+(B C*DE/* F)
=A+ (B- CDE/* * F)
-A + (BCDE/*F*-)
Ans: ABCDE/*F*.+

(d) Big »0' Notation: The unit of measurement of efficiency of an algorithm.

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+/

You might also like