Section – C

### ISC Computer Science 2018 Class-12 Previous Year Question Paper Solved

• Each program should be written in such a way that it clearly depicts the logic of the problem stepwise.
• This can be achieved by using comments in the program and mnemonic names or pseudo-codes for
algorithms.
• The programs must be written in Java and the algorithms must be written in general standard form, wherever required/specified.
• Flowcharts are not required.

Question 10.
A superclass Number is defined to calculate the factorial of a number. Define a subclass Series to find the sum of the series S = 1! + 2! + 3! + 4! + ………. + n! [5] The details of the members of both classes are given below:
Class name: Number
Data member/instance variable:
n: to store an integer number
Member functions/methods:
Number(int nn): parameterized constructor to initialize the data member n=nn
int factorial(int a): returns the factorial of a number
(factorial of n = 1 × 2 × 3 × …… × n)
void display()
Class name: Series
Data member/instance variable:
sum: to store the sum of the series
Member functions/methods:
Series(…) : parameterized constructor to initialize the data members of both the classes
void calsum(): calculates the sum of the given series
void display(): displays the data members of both the classes
Assume that the superclass Number has been defined. Using the concept of inheritance, specify the class Series giving the details of the constructor(…), void calsum() and void display().
The superclass, main function and algorithm need NOT be written.

```import java.io.*;
import java.util. Scanner;
class Number{
int n;
public Number(int nn) {
n = nn;
}
public int factorial(int a) {
if(a <= 1)
return 1;
return a * factorial(--a);
}
public void display() {
System.out.println(“Number: ” + n);
}
}
class Series extends Number {
int sum;
public Series(int n) {
super(n);
sum = 0;
}
public void calcSum() {
for(int i = 1; i <= n; i++) {
sum += super.factorial(i);
}
}
public void display() {
super, display();
System.out.println(“Series sum: ” + sum);
}
}
class Factorial{
public static void main(String args[ ]) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the number: ”);
int niun = sc.nextInt();
Series obj = new Series(num);
obj.calcSum();
obj.display();
}
}```

Question 11.
A register is an entity which can hold a maximum of 100 names. The register enables the user to add and remove names from the topmost end only.
Define a class Register with the following details:
Class name: Register
Data members/instance variables:
stud[]: array to store the names of the students
cap: stores the maximum capacity of the array to point the index of the top end
top: to point the index of the top end
Member functions:
Register (int max) : constructor to initialize the data member cap = max, top = -1 and create the string array
void push(String n): to add names in the register at the top location if possible, otherwise display the message “OVERFLOW” String pop(): removes and returns the names from the topmost location of the register if any, else returns “\$\$”
void display (): displays all the names in the register
(a) Specify the class Register giving details of the functions void push(String) and String pop().
Assume that the other functions have been defined. [4] The main function and algorithm need NOT be written.
(b) Name the entity used in the above data structure arrangement. [1] Answer 11:

```(a) import java.io.*;
import java.util.Scanner;
class Register{
private String stud[];
private int cap;
private int top;
public Register(int max) {
top = -1;
cap = max;
if(cap > 100)
cap = 100;
stud = new String[cap];
}
public void push(String n) {
if(top + 1 < cap)
stud[++top] = n;
else
System.out.println(“OVERFLOW”);
}
public String pop() {
if(top == -1)
return “\$\$”;
else
return stud[top--];
}
public void display() {
if(top== -1)
System.out.println(“Register empty.”);
else
{
System.out.println(“StudentList:”);
for(int i = 0; i <= top; i++)
System.out.println(stud[i]);
}
}
public static void main(String args[ ]) throws IOException {
Scanner sc = new Scanner(System.in);
System. out.print(“Maximum size: ”);
int max = sc.nextlnt();
Register obj = new Register(max);
while(true) {
System.out.println(“1. Push”);
System.out. println(“2. Pop”);
System.out.println(“3. Display”);
int choice = sc.nextlnt();
switch(choice) {
case 1:
System.out.print(“Student Name: ”);
String n = sc.next();
obj.push(n);
break;
case 2:
n = obj.pop();
if(n.equals(“\$\$”))
System.out.println(“UNDERFLOW!”);
else
System.out.println(n + “popped.”);
break;
case 3:
obj.display();
break;
default:
System.out.println(“Exiting...”);
return;
}
}
}
}```

(b) The entity used in the above data structure is stack. Stack works upon the principle of LIFO i. e., Last In First Out.

Question 12.
(a) A linked list is formed from the objects of the class Node. The class structure of the Node is given below: [2]

```class Node
{
int n;
}```

Write an Algorithm OR a Method to search for a number from an existing linked list.
The method declaration is as follows:
void FindNode(Node str, int b)
(b) Answer the following questions from the diagram of a Binary Tree given below:

(i) Write the inorder traversal of the above tree structure. [1] (ii) State the height of the tree, if the root is at level 0 (zero). [1] (iii) List the leaf nodes of the tree. [1] Answer 12:

```(a) public void FindNode(Node str, int d) {
Node str = start;
intpos= 1;
while(str != null) {
if(str.data == d) {
System.out.println(d + “found at position” + pos);
break;
}
else{
str = str.next;
pos++;
}
}
if(str==null)
}```

(b) (i) Inorder Traversal = E A B
= G E C A B D
= G E C H A B D F
(ii) Height of Tree = 4
(iii) Leaf nodes of the tree are those nodes that do not have any child. Therefore, leaf nodes are H and F.

-: End of ISC Computer Science 2018 Class-12 Solved Paper :-

Thanks