In this page, only Java program related questions are answered.
Q: State the difference between interface and Class:
Ans: Interface is totally abstract where all the declared data members are final static and all the functions are declared only. Interface can't create any object, class can implements interfaces.
Class is user defined composite type where data members can have any specifiers and functions are defined. Class can create objects.
Q: Convert the infix expression into postfix: (A+B) / C * (D+E)
Ans: AB+C/DE+*
Ans: Exception means run time abnormality. The mechanism to manage run time exception is termed as exception handling.
Two types of exception are checked and unchecked exceptions.
Q: What is the worst time complexity of the following code:
for (int i=0;i< N;i++)
{
statements
}
for (int j=0;j< M;j++)
{
statements
}
What would be the value if we change the second loop to for (int j=0;j< N;j++)?
Ans: In first case complexity is O(N+M) and in the second case the complexity would be O(N) as constant is dropped while calculating complexity.
Q: A character array B[7][6] has a base address 1046 at 0,0. Calculate the address at B[2][3] if the array is stored Column Major wise. Each character requires 2 bytes of storage.
Ans: Address
of a specific location when arranged in column major order is Base Address +
w*(E2*L1+E1)
Where
w represents storage location for each character
E2
is the effective length of 2nd dimension, here 3
L1
is the length of 1st dimension, here 7
E1
is the effective length of 1st dimension, here 2
1046
+ 2 * (3*7 + 2)
1046+46
1092
Question 3.
a) The following function numbers(int) and numbers1(int) are a part of some class. Answer the questions given below showing the dry run/working:
public void numbers(int n)
{
if(n >0)
{
System.out.print(n+” “);
numbers(n-2);
System.out.print(n+” “);
}
}
public String numbers1(int n)
{
If(n<=0)
return “”;
return(numbers(n-1)+n+” “;
}
i) What will be the output of the function numbers(int n) when n=5? [ 2 ]
Ans: This is a recursive function so output will be 531135. The first three digits from the first println () above the recursive call and the last three digits from the println () below the recursive function and the values will be 135 as they were stored in stack.
ii) What will the function numbers1(int n) return when n=6? [ 2 ]
Ans: Output will be 1 2 3 4 5 6
iii) State in one line what is the function numbers1(int) doing apart from recursion? [ 1 ]
Ans: It displays the first 'n' natural numbers
b) The following function is a part of some class. It sorts the array a[] in ascending order using insertion sort technique. There are some places in the code marked by ?1?, ?2?, ?3?, ?4?, ?5? which must be replaced by expression/ statement so that the function works correctly.
void insertsort(int a[])
{
int m= ?1?; int b,I,t;
for(i=?2?;i
{
t=a[i]; b=i-1;
while(?3? >=0 && t
{
a[b+1]= a[b]; ?4?;
}
?5?=t;
}
}
i) What is the expression or statement at ?1? [ 1 ]
ii) What is the expression or statement at ?2? [ 1 ]
iii) What is the expression or statement at ?3? [ 1 ]
iv) What is the expression or statement at ?4? [ 1 ]
v) What is the expression or statement at ?5? [ 1 ]
Ans: (i) a.length
(ii) 0
(iii) b
(iv) b--;
(v) a[b+1]
import java.io.*;
class Point
{
int x,y;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Point()
{
x=0;
y=0;
}
public void readPoint()throws IOException
{
System.out.println("Enter 'x' coordinate:");
x=Integer.parseInt(br.readLine());
System.out.println("Enter 'y' coordinate:");
y=Integer.parseInt(br.readLine());
}
Point midpoint(Point A, Point B)
{
Point obj=new Point();
obj.x= (A.x+B.x)/2;
obj.y= (A.y+B.y)/2;
return obj;
}
void displaypoint()
{
System.out.println("'X' and 'Y' cordinates ="+ x+ ","+y);
}
public static void main(String args[]) throws Exception
{
Point one=new Point();
Point two=new Point();
Point three=new Point();
System.out.println("For first Object\n");
one.readPoint();
System.out.println("For second Object\n");
two.readPoint();
three=three.midpoint(one,two);
System.out.println("For first Object\n");
one.displaypoint();
System.out.println("For second Object\n");
two.displaypoint();
System.out.println("For third Object\n");
three.displaypoint();
}
}
import java.io.*;
class Rearrange
{
String Txt, Cxt;
int len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Rearrange()
{
Txt="";
Cxt="";
len=0;
}
public void readWord()throws IOException
{
System.out.println("Enter the word:");
Txt=br.readLine().toUpperCase();
}
public void convert()
{
int i,x=0;
len=Txt.length();
for(i=0;i< len;i++)
{
switch(Txt.charAt(i))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
x++;
break;
}
if (x>0)
break;
}
if (x==0)// no vowel in the word
Cxt=Txt+"N";
else if(i==0)// vowel at the start
Cxt=Txt+"Y";
else// vowel found anywhere in the middle
{
Cxt=Txt.substring(i);
Cxt=Cxt+Txt.substring(0,i)+"C";
}
}
void display()
{
System.out.println("Original word="+Txt + "\nRearranged word="+Cxt);
}
public static void main(String args[]) throws Exception
{
Rearrange one=new Rearrange();
one.readWord();
one.convert();
one.display();
}
}
import java.io.*;
class Change
{
String str, newstr;
int len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Change()
{
str="";
newstr="";
len=0;
}
public void inputword()throws IOException
{
System.out.println("Enter the word:");
str=br.readLine();
len=str.length();
}
public char caseconvert(char ch)
{
if (Character.isUpperCase(ch))
ch=Character.toLowerCase(ch);
else
ch=Character.toUpperCase(ch);
return ch;
}
void recchange(int x)
{
if(x==len)
return;
newstr=newstr+caseconvert(str.charAt(x));
recchange(++x);
}
void display()
{
System.out.println("Original word="+str + "\nRearranged word="+newstr);
}
public static void main(String args[]) throws Exception
{
Change one=new Change();
one.inputword();
one.recchange(0);
one.display();
}
}
class Worker
{
String name;
double basic;
Worker(String n, double d)
{
name=n;
basic=d;
}
public void display()
{
System.out.println("Name="+name + "\nBasic pay="+basic);
}
}
class Wages extends Worker
{
int hrs;
double rate, wage;
Wages (String n,double d,int h,double r)
{
super(n,d);
hrs=h;
rate=r;
}
private double overtime( )
{
return hrs*rate;
}
public void display()
{
wage=basic+overtime();
super.display();
System.out.println("Wage="+wage);
}
public static void main(String args[]) throws Exception
{
Wages one=new Wages("Sraban",1200.50,2,100);
one.display();
}
}
Ans: FIFO - first in first out
(ii) 0
(iii) b
(iv) b--;
(v) a[b+1]
Section B
Answer any 2 questions.
Each program should be written in such a way that it clearly depicts
the logic of the problem. This can be achieved by using mnemonic names and
comments in the program.
Question 8.
The co-ordinates of
a point P on a two-dimensional plane can be represented by P(x,y) with x as the
x co-ordinate and y as the y co-ordinate. The co-ordinates of midpoint of two
points P1(x1,y1) and P2(x2,y2) can be calculated as P(x,y) where:
x=x1+x2/2, y=y1+y2/2
Design a class Point with the following
details:
Class name
|
:
|
Point
|
Data members
|
||
x
|
:
|
stores the x co-ordinate
|
y
|
:
|
stores the y co-ordinate
|
Member
functions:
|
||
Point()
|
:
|
constructor to initialize x=0 and y=0
|
void
readPoint()
|
:
|
accepts the co-ordinates x and y of a
point
|
Point
midpoint(Point A, Point B):
|
calculates and returns the midpoint of
the two points A and B.
|
|
void
displaypoint()
|
:
|
displays the co-ordinates of a point
|
Specify the class
Point giving details of the constructor( ), member functions void readPoint(),
Point midpoint(Point, Point) and void displaypoint() along with the main
function to create an object and call the functions accordingly to calculate
the midpoint between any two given points.
[ 10 ]
class Point
{
int x,y;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Point()
{
x=0;
y=0;
}
public void readPoint()throws IOException
{
System.out.println("Enter 'x' coordinate:");
x=Integer.parseInt(br.readLine());
System.out.println("Enter 'y' coordinate:");
y=Integer.parseInt(br.readLine());
}
Point midpoint(Point A, Point B)
{
Point obj=new Point();
obj.x= (A.x+B.x)/2;
obj.y= (A.y+B.y)/2;
return obj;
}
void displaypoint()
{
System.out.println("'X' and 'Y' cordinates ="+ x+ ","+y);
}
public static void main(String args[]) throws Exception
{
Point one=new Point();
Point two=new Point();
Point three=new Point();
System.out.println("For first Object\n");
one.readPoint();
System.out.println("For second Object\n");
two.readPoint();
three=three.midpoint(one,two);
System.out.println("For first Object\n");
one.displaypoint();
System.out.println("For second Object\n");
two.displaypoint();
System.out.println("For third Object\n");
three.displaypoint();
}
}
Question 9.
Input a word in
uppercase and check for the position of the first occurring vowel and perform
the following operation.
i)
Words that begin with a vowel are concatenated with “Y” . For example,
EUROPE becomes EUROPEY.
ii)
Words that contain a vowel in between should have the first part from
the position of the vowel till end, followed by the part of the string from
beginning till position of the vowel and is concatenated by “C”. For example
PROJECT becomes OJECTPRC.
iii)
Words which do not contain a vowel are concatenated with “N”. For
example, SKY becomes SKYN.
Design a class
Rearrange using the description of the data members and member functions given
below:
Class
name
|
:
|
Rearrange
|
Data
members
|
||
Txt
|
:
|
to store a word
|
Cxt
|
:
|
to store the rearranged word
|
len
|
:
|
to store the length of the word
|
Member functions
|
||
Rearrange()
|
:
|
constructor to initialize the instance
variables
|
void readword()
|
:
|
to accept the word input in UPPERCASE
|
void convert()
|
:
|
converts the word
into its changed form and stores it in
|
string Txt
|
||
void display()
|
:
|
displays the original and the changed
word
|
Specify the class
Rearrange giving the details of the constructor(), void readword(), void
convert() and void display(). Define a main function to create an object and
call the function accordingly to enable the task.
class Rearrange
{
String Txt, Cxt;
int len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Rearrange()
{
Txt="";
Cxt="";
len=0;
}
public void readWord()throws IOException
{
System.out.println("Enter the word:");
Txt=br.readLine().toUpperCase();
}
public void convert()
{
int i,x=0;
len=Txt.length();
for(i=0;i< len;i++)
{
switch(Txt.charAt(i))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
x++;
break;
}
if (x>0)
break;
}
if (x==0)// no vowel in the word
Cxt=Txt+"N";
else if(i==0)// vowel at the start
Cxt=Txt+"Y";
else// vowel found anywhere in the middle
{
Cxt=Txt.substring(i);
Cxt=Cxt+Txt.substring(0,i)+"C";
}
}
void display()
{
System.out.println("Original word="+Txt + "\nRearranged word="+Cxt);
}
public static void main(String args[]) throws Exception
{
Rearrange one=new Rearrange();
one.readWord();
one.convert();
one.display();
}
}
[ 10 ]
Question 10.
Design a class Change to perform string
related operations. The details of the class are given below:
Class name
|
:
|
Change
|
Data members
|
||
str
|
:
|
stores the word
|
newstr
|
:
|
stores the changed word
|
len
|
:
|
stores the length of the word
|
Member functions
|
||
Change()
|
:
|
default constructor
|
void
inputword()
|
:
|
to accept a word
|
char
caseconvert(char ch)
|
:
|
converts the case of the character and
returns it
|
void
recchange(int)
|
:
|
extracts characters using recursive
technique and
|
changes its case
using caseconvert() and forms a new
|
||
word
|
||
void
display()
|
:
|
displays both the words
|
a) Specify the class
Change, giving details of the constructor(), member functions void inputword(),
char caseconvert(char ch), void recchange(int) and void display(). Define the
main function to create an object and call the functions accordingly to enable
the above change in the given word.
class Change
{
String str, newstr;
int len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Change()
{
str="";
newstr="";
len=0;
}
public void inputword()throws IOException
{
System.out.println("Enter the word:");
str=br.readLine();
len=str.length();
}
public char caseconvert(char ch)
{
if (Character.isUpperCase(ch))
ch=Character.toLowerCase(ch);
else
ch=Character.toUpperCase(ch);
return ch;
}
void recchange(int x)
{
if(x==len)
return;
newstr=newstr+caseconvert(str.charAt(x));
recchange(++x);
}
void display()
{
System.out.println("Original word="+str + "\nRearranged word="+newstr);
}
public static void main(String args[]) throws Exception
{
Change one=new Change();
one.inputword();
one.recchange(0);
one.display();
}
}
b) Differentiate between finite and infinite
recursion. [ 2 ]
Finite recursion is where the base value set in the recursive function stops the recursion after a certain time whereas infinite recursion is when the base value fails to stop the resursion.
Finite recursion is where the base value set in the recursive function stops the recursion after a certain time whereas infinite recursion is when the base value fails to stop the resursion.
Section C Answer any 2 questions.
Each program/algorithm should be written in such a way that it clearly
depicts the logic of the problem step wise. This can also be achieved by using
pseudo codes.
(Flowcharts are not required)
The programs must be written in Java.
The algorithm must be written in general standard
form wherever required.
Question 11.
A super class
Worker has been defined to store the details of a worker. Define a sub class
Wages to compute the monthly wages for the worker. The details of both the
classes are given below:
Class name
|
:
|
Worker
|
Data members
|
:
|
|
Name
|
:
|
to store the name of the worker
|
Basic
|
:
|
to store the basic pay in decimal
|
Member functions
|
||
Worker(….)
|
:
|
parameterized constructor to assign
values to
|
the instance variables
|
||
void display()
|
:
|
display worker details
|
class name
|
:
|
Wages
|
Data members
|
||
hrs
|
:
|
stores the hours worked
|
rate
|
:
|
stores rate per hour
|
wage
|
:
|
stores the overall wage of the worker
|
Member functions
|
||
Wages(….)
|
:
|
parameterized constructor to assign
values to
|
the instance variables of both classes
|
||
double overtime( )
|
:
|
calculates and returns the overtime
amount as
|
(hours * rate )
|
||
void
display()
|
:
|
calculates the wage using the formula
|
wage=overtime amount
+basic pay and displays
|
||
it along with other details
|
Specify the class Worker giving details of the constructor() and void
display(). Using the concept of inheritance, specify the class Wages giving
details of the constructor(), double overtime() and void display(). The main
function need not be written.
[ 10 ]
{
String name;
double basic;
Worker(String n, double d)
{
name=n;
basic=d;
}
public void display()
{
System.out.println("Name="+name + "\nBasic pay="+basic);
}
}
class Wages extends Worker
{
int hrs;
double rate, wage;
Wages (String n,double d,int h,double r)
{
super(n,d);
hrs=h;
rate=r;
}
private double overtime( )
{
return hrs*rate;
}
public void display()
{
wage=basic+overtime();
super.display();
System.out.println("Wage="+wage);
}
public static void main(String args[]) throws Exception
{
Wages one=new Wages("Sraban",1200.50,2,100);
one.display();
}
}
Question 12.
Define a class
Repeat which allows the user to add elements from one end (rear) and remove
elements from one end (rear) and remove elements from the other end (front)
only.
The
following details of the class Repeat are given below:
|
||
Class
name
|
:
|
Repeat
|
Data
members
|
||
st[]
|
:
|
an array to hold a maximum of 100
integer
|
elements
|
||
cap
|
:
|
stores the capacity of the array
|
f
|
:
|
to point to the index of the front
|
r
|
:
|
to point to the index of the rear
|
Member
functions
|
||
Repeat(int
m)
|
:
|
constructor to initialize the data
members
|
cap=m, f=0, r=0 and to create the
integer array
|
||
void
pushvalue(int v)
|
:
|
to add integers from the rear index if
possible
|
else display the message “overflow”
|
||
int
popvalue()
|
:
|
to remove and return
element from the front, if
|
array is empty then return -9999
|
||
void disp()
|
:
|
displays the elements present in the
list
|
a)
|
Specify the class Repeat giving details
of the constructor(int), member function void
|
|
pushvalue(int), int popvalue() and void
disp(). The main function need not be written.
import java.io.*; class Repeat { int st[],f,r,cap; Repeat(int m) { cap=m; st=new int[cap]; r=0;f=0; } public void pushvalue(int v) { if(r==cap) System.out.println("Overflow."); else st[r++]=v; } public int popvalue() { if (f!=0 && f==r) return -9999; else return st[f++]; } void display() { System.out.print("\nThe list is="); for(int i=f;i< r;i++) System.out.print(" "+st[i]); } public static void main(String args[]) throws Exception { Repeat one=new Repeat(100); int v=0; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int i=1; while(i!=0) { System.out.print("\nPress '1' for push, '2' for pop and '0' for quit:"); i=Integer.parseInt(br.readLine()); if (i==1) { System.out.print("\nValue: "); v=Integer.parseInt(br.readLine()); one.pushvalue(v); one.display(); } else if(i==2) { i=one.popvalue(); System.out.print("\nPoped element: "+v); one.display(); } } } } |
[ 8 ]
|
|
b)
|
What is the common name of the entity
described above? Ans: This is Queue
|
[ 1 ]
|
c)
|
On what principle does this entity
work?
|
[ 1 ]
|
Ans: FIFO - first in first out
Question 13.
a)
A linked list is formed from the objects of the class, class ListNodes
{
int item;
ListNodes next;
ListNodes next;
}
Write a method OR an algorithm to compute and return the sum of all
integers items stored in the linked list. The method declaration is specified
below:
int
listsum(ListNodes start);
Method
int listsum(ListNodes start)
{
int sum=0;
while(start!=null)
{
sum=sum+start.item;
start=start.next;
}
return sum;
}
Algorithm
Step 1: stsrt is pointing to the first node
Step 2: Assign 0 to int variable sum
Step 3: Repeat the step no 3 and 4 until start becomes null
Step 4: set sum=sum + start.item
Step 5: Move start to next location
Step 6: Display sum
Step 7: End
Method
int listsum(ListNodes start)
{
int sum=0;
while(start!=null)
{
sum=sum+start.item;
start=start.next;
}
return sum;
}
Algorithm
Step 1: stsrt is pointing to the first node
Step 2: Assign 0 to int variable sum
Step 3: Repeat the step no 3 and 4 until start becomes null
Step 4: set sum=sum + start.item
Step 5: Move start to next location
Step 6: Display sum
Step 7: End
b)
What is Big ‘O’ notation? State its significance.
Ans: Big O notation is used to show the time complexity of a program. Time complexity of program does not represent the time taken by a program for execution but it represents the dominating factor of the program. Time complexity is calculated by counting the critical operations (relational statements) and then the dominating value is find out.
Suppose a loop is like:
for(i=0;i
{
statements
}
Here the critical operation is i
Ans: Big O notation is used to show the time complexity of a program. Time complexity of program does not represent the time taken by a program for execution but it represents the dominating factor of the program. Time complexity is calculated by counting the critical operations (relational statements) and then the dominating value is find out.
Suppose a loop is like:
for(i=0;i
{
statements
}
Here the critical operation is i
c)
Answer the following from the diagram of a Binary tree given below:
Name the parent node of E.
Ans: C |
[ 1 ]
|
|
Write the postorder tree traversal.
Ans: FGDBHECA |
[ 1 ]
|
|
Write the internal nodes of the tree.
Ans: B C D E |
[ 1 ]
|
|
State the level of the root of the
tree.
|
[ 1 ]
|
Ans: 4
No comments:
Post a Comment