i. How can we reduce the complicated Boolean expressions? [1]
ii. How and when is the constructor of a class called? [1]
iii. What type of string can be held in in the StringBuffer class? [1]
iv. What do you understand by the term Big-O Notation? [1]
v. What is scheduling? [1]
vi. What do you mean by “heap memory”? [1]
vii. Is linked list a linear or non-linear data structure? [1]
viii. When does an infinite recursion occur? [1]
ix. Write the prefix notation for the following statement:
A + (B * C) [1]
x. What do you mean by recursive definition? Also, give an example. [1]
i: The complicated Boolean expression can be reduced into a simpler one by using Boolean laws, which were introduced by George Boole.
ii: The constructor is called with new operator at the time of creating an object of the class.
iii: String that can be changed or modified (mutable) can be stored in the StringBuffer class.
iv. The Big-O notation is used to depict time taken by an algorithm when its input size grows.
v. Scheduling refers to deciding how a shared resource is to be allocated to all the applications that want to use it.
vi. It is a memory pool area managed by Java to create objects dynamically.
vii. According to access strategies, a linked list is a linear one. According to storage, a linked list is a non-linear one.
viii. An infinite recursion occurs when a recursive function calls itself again and endlessly. This is done when either the base case is missing or it is bypassed while executing the program.
ix. Prefix of A + (B * C) is:
= A+ (*BC)
= +A*BC
x. A recursive definition is defined in terms of its smaller versions.
Example:
Xn = X*X*X*X……….*X
The same statement can be written in a recursive definition as:
Xn = X*(Xn-1) for n>1
i. Identify what type of gate is represented by each of the following phrases: [2]
(a) Any high input guarantees a low output.
(b) Any low input guarantees a high output.
(c) Any low input guarantees a low output.
ii. Name the classes used in (a) text files (b) binary files for IO operation. [2]
iii. Write the code to push an element in a linked stack. [3]
(a) Any high input guarantees a low output: NOR gate.
(b) Any low input guarantees a high output: NAND gate.
(c) Any low input guarantees a low output: AND gate.
iii:
//Code to push an element in a linked stack
public void push(int item)
{
Node temp = new Node();
if(temp==null)
{
System.out.println("Overflow");
}
else
{
System.out.println("Element "+item+" has been pushed in the stack.");
temp.data=item;
temp.link=top;
top = temp;
}
}
The complexity of each of the three sets of statements given below is as follows:
Set1: O(n2)
Set2: O(n)
Set3: O(n3)
What will be the complexity if these three sets are executed in a sequence? [3]
The complexity of statements which are executed in a sequence can be calculated by adding the complexity of each statement.
Thus, the complexity of the three sets of statements together will be: O(n2) + O(n) + O(n3)
= O(n2 +n + n3)
= O(n3) (consider the dominant term)
Write the code to illustrate the concept of early binding. [10]
In early binding, data and method are bind with the object before the execution of the program.
class Animal
{
public String type = "mammal";
public void show()
{
System.out.println("The animal is a Dog");
}
public static void main(String[] args)
{
Animal doggie = new Animal();
// Early binding
doggie.show();
System.out.println("The type is: " + doggie.type);
}
}
As shown in the program, the type of animal which is to be displayed is known to the compiler before execution.
Write the Java code to implement a stack as an array. [10]
public class Stack
{
String st[];
int size, top, ctr;
public Stack()
{
size=0;
}
public Stack(int sz)
{
size=sz;
top = -1;
st = new String[size];
}
void pushName(String n)
{
if(top==size-1)
System.out.println("Stack Overflow");
else
{
st[++top]=n;
}
}
void popName()
{
if(top==-1)
{
System.out.println("Stack Underflow");
}
else
{
System.out.println(" " " +st[top] + " " has been poped from the stack");
top--;
}
}
void display()
{
System.out.println("nThe Stack is ");
for(ctr=top;ctr>=0;ctr--)
System.out.println(st[ctr]);
}
public static void main(String args[])
{
Stack stk = new Stack(5);
stk.pushName("Kiran");
stk.pushName("Sonia");
stk.pushName("Amit");
stk.pushName("Prashant");
stk.display();
stk.popName();
stk.display();
}
}
OUTPUT:
To illustrate polymorphism, write a Java program to search for the following birds for a firm:
Birds | Name | Sounds |
Sparrow | Chinni | |
Peacock | Cutie | Scream |
Use a Bird array [] and an abstract method birdSound(). [10]
// Class Bird
abstract class Bird
{
String Name;
String Sound;
public void display()
{
System.out.println("Name = " +Name);
System.out.println("Sound = " +Sound);
}
abstract public void birdSound();
}
// Class Sparrow
public class Sparrow extends Bird
{
public Sparrow()
{
Name="Chinni";
}
public void birdSound()
{
Sound="Twitter";
}
}
// Class Peacock
public class Peacock extends Bird
{
public Peacock()
{
Name="Cutie";
}
public void birdSound()
{
Sound="Scream";
}
}
//Class Firm to achieve runtime polymorphism
public class Farm
{
public static void main()
{
Bird br[]=new Bird[2];
Sparrow s=new Sparrow();
s.birdSound();
Peacock p=new Peacock ();
p.birdSound();
br[0]=s;
br[1]=p;
System.out.println("Bird = Sparrow");
br[0].display();
System.out.println("Bird = Peacock");
br[1].display();
}
}
Output:
i. Represent the Boolean Expression X’Y + Y’Z with the help of NAND gates only. [5]
ii. Write down a Java program that throws run-time exception because array element beyond its range is accessed in the program. [5]
i.
ii.
public class Demo
{
public static void main()
{
int a[] = {1,2,3,4,5};
for(int i = 0; i <= 5; i++)
{
System.out.println("array element "+i+" is: "+a[i]);
}
}
}
Output:
Define the following terms in detail.
1. Stack
2. Queue [10]
Stack: It is a data structure in which data is added and removed from only one end, called the top.
A stack is generally implemented using only two principle operations (apart from constructor and destructor methods):
push | Adds an item to stack |
pop | Extracts most recently pushed item from stack |
A common model of stack is a plate or coin stacker. Plates are "pushed" onto the top and "popped" off from the top.
Queue: It is a data structure that allows the items to be inserted at one end and removed from the other end, i.e., FIRST IN FIRST OUT.
Applications of Queues
1. For implementing any natural FIFO service like telephone enquiry, reservation requests, traffic flow etc.
2. For handling scheduling of processes in multitasking operating system, e.g., FCFS scheduling, Round-robin scheduling etc.Write the code to illustrate the concept of early binding. [10]
In early binding, data and method are bind with the object before the execution of the program.
class Animal
{
public String type = "mammal";
public void show()
{
System.out.println("The animal is a Dog");
}
public static void main(String[] args)
{
Animal doggie = new Animal();
// Early binding
doggie.show();
System.out.println("The type is: " + doggie.type);
}
}
As shown in the program, the type of animal which is to be displayed is known to the compiler before execution.
Write a class for Employees of an organisation with necessary attributes and methods. [10]
class Employee
{
int EmpId;
String Name;
String department;
double salary;
int attendance;
int submitAttend(boolean a)
{
if(a)
attendance++;
return attendance;
}
void promotion(int increment)
{
salary = salary + increment;
System.out.println(“Salary of ”+Name+“ being increased by ”+increment+“ with promotion”);
}
}
Write a recursive program to read a character one by one till the user does not enter dot (.) and then display the series of entered characters in reverse order. [10]
Output:
Write the Java code to implement a stack as an array. [10]
public class Stack
{
String st[];
int size, top, ctr;
public Stack()
{
size=0;
}
public Stack(int sz)
{
size=sz;
top = -1;
st = new String[size];
}
void pushName(String n)
{
if(top==size-1)
System.out.println("Stack Overflow");
else
{
st[++top]=n;
}
}
void popName()
{
if(top==-1)
{
System.out.println("Stack Underflow");
}
else
{
System.out.println(" " " +st[top] + " " has been poped from the stack");
top--;
}
}
void display()
{
System.out.println("nThe Stack is ");
for(ctr=top;ctr>=0;ctr--)
System.out.println(st[ctr]);
}
public static void main(String args[])
{
Stack stk = new Stack(5);
stk.pushName("Kiran");
stk.pushName("Sonia");
stk.pushName("Amit");
stk.pushName("Prashant");
stk.display();
stk.popName();
stk.display();
}
}
OUTPUT:
Write a program in Java to show how an error can occur in the program. [10]
Correct the spelling of void and run the program after compilation to get the correct output.
Output:
Write a recursive program to generate the Fibonacci series.
[Hint: 0, 1, 1, 2, 3, 5, 8, 13, 21…..] [10]
Output
To illustrate polymorphism, write a Java program to search for the following birds for a firm:
Birds | Name | Sounds |
Sparrow | Chinni | |
Peacock | Cutie | Scream |
Use a Bird array [] and an abstract method birdSound(). [10]
// Class Bird
abstract class Bird
{
String Name;
String Sound;
public void display()
{
System.out.println("Name = " +Name);
System.out.println("Sound = " +Sound);
}
abstract public void birdSound();
}
// Class Sparrow
public class Sparrow extends Bird
{
public Sparrow()
{
Name="Chinni";
}
public void birdSound()
{
Sound="Twitter";
}
}
// Class Peacock
public class Peacock extends Bird
{
public Peacock()
{
Name="Cutie";
}
public void birdSound()
{
Sound="Scream";
}
}
//Class Firm to achieve runtime polymorphism
public class Farm
{
public static void main()
{
Bird br[]=new Bird[2];
Sparrow s=new Sparrow();
s.birdSound();
Peacock p=new Peacock ();
p.birdSound();
br[0]=s;
br[1]=p;
System.out.println("Bird = Sparrow");
br[0].display();
System.out.println("Bird = Peacock");
br[1].display();
}
}
Output:
Take your CBSE board preparation to another level with AI based and rich media animation on Extramarks - The Learning App.
Features of Learning App