[go: up one dir, main page]

0% found this document useful (0 votes)
294 views14 pages

Xii Practical Question21

This document contains 19 questions related to Java programming concepts and tasks. The questions cover topics like checking if a number is a Kaprekar number, generating lucky numbers, converting between decimal and hexadecimal, defining the Mobius function, checking for prime adam numbers, generating calendars, calculating date differences, implementing the Caesar cipher, arranging sentences by word count, rearranging words in a sentence by length/alphabetical order, checking for magic squares, finding saddle points in matrices, filling matrices in circular fashion, defining classes to insert/delete array elements, adding times, finding common elements between arrays, and sorting arrays using insertion and selection sort.

Uploaded by

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

Xii Practical Question21

This document contains 19 questions related to Java programming concepts and tasks. The questions cover topics like checking if a number is a Kaprekar number, generating lucky numbers, converting between decimal and hexadecimal, defining the Mobius function, checking for prime adam numbers, generating calendars, calculating date differences, implementing the Caesar cipher, arranging sentences by word count, rearranging words in a sentence by length/alphabetical order, checking for magic squares, finding saddle points in matrices, filling matrices in circular fashion, defining classes to insert/delete array elements, adding times, finding common elements between arrays, and sorting arrays using insertion and selection sort.

Uploaded by

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

Question no 1:

Write a program in java to input a number and check whether it is a kaprekar number or not.
Note: A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two
pieces, a right hand piece that has d digits and a left hand piece that has d or d-1 digits.
If the sum of the two equal pieces is equal to the number,then’n’ is a kaprekar number.
The first few kaprekar numbers are : 9,45,297…

Example 1: 9
9^2 =81,right hand piece of 81 is 1 and left hand piece of 81 is 8
Sum=1+8=9
i.e equal to the number. hence 9 is a kaprekar number.

Question no 2:
Consider a sequence of natural numbers
1,2,3,4,5,6,7,8,9,10…..
Removing every second number we get
1,3,5,7,9,11,13,15
Removing every third number in the process we get
1,3,7,9,13,15,…
And this process continues and we observe that some numbers remain indefinitely in the
sequence,these numbers are called lucky numbers.
Write a program in java to generate and display all lucky numbers upto a given limit
Example 1:
Input n=10
Output
Lucky numbers within 10 are 1,3,7

Question no 3:
Write a program in java to accept a decimal number and convert that to a hexadecimal
number.
Display both the hexadecimal and decimal forms of the number.

Question no 4 :
The Mobius function M(N) for a natural number N is defined as follows:
M(N)=0 if any prime factor of n is contained in n more than once
M(N)=-1p if n is a product of p distinct prime factors.

Examples:
M(78)=-1(for 78 = 2*3*13 and M(78)=-13=-1)
M(34)=1(for 34 =2*17 and M(34)=-12=1).

Design a class MobiusFn to define Mobius function for a natural number.


Question no 5:

A prime adam integer is a positive integer (without leading zeros)


Which is prime as well as adam number.
Prime number: a number which has only two factors i.e, 1 and the number itself
Example:2,3,5,7,11..

Adam number : the square of a number and the square of its reverse are reverse to each
Other .
Example : if n=13 and reverse of ‘n’=31then(13^2 =169 and 31^2=961 then reverse of
169=961)
Accept two positive integers m and n. where m is less than n as user input.
Display all prime adam numbers that are in the range between n and m(both inclusive)
And output them along with the frequency in the format given below:

Test your program with the following data and some random data:
Example 1:
INPUT:
M=5 n=100
Output :
The prime – adam inmtegers aree :
11 13 31
Frequency of Prime adam Integers is:3
Example 2:
Input
M=100 n=200
Output:
The prime adam integers are:101 103 113
Frequency of prime adam integers is :3

Question no 6:
Write a program to accept the year,month and the weekday name of the 1 st day of that month
and generate its calendar.

Example :
Input:
Year:
2016
Month:
February
1 st day of February: Monday

Output:
February 2016
Sun Mon Tue Wed Thu Fri Sat
0 1 2 3 4 5 6
7 8 9 10 11 12 13
….

Question no 7:
Write a program to accept 2 dates in the string format dd/mm/yyyy
And find the difference in days between the 2 dates

Example :
Input:
Date 1:
20/12/2012
Date 2 :
11/02/2013

Output : difference =53 days

The program should include the part for validating the inputs namely the date and the day on
the first January of that year

Question no 8:
Caesar cipher is an encryption technique which is implemented as rot13 (‘rotate by 13
places’0
It is a simple letter substitution cipher that replaces a letter with the letter 13 places after it
in the alphabets, with the other characters remaining unchanged.

Write a program to accept a plain text of length L, where L must be greater than 3 and less
than 100
Encrypt the text if valid as per the Caesar cipher.
Test your program with the sample data and some random data.
Example 1 :
Input:
Hello! How are you?
Output:
The cipher text is :
Uryyb!ubj ne rib

Question no 9:
Accept a paragraph pf text consisting of sentences that are terminated by either a “.”(full
stop),”!”(excalamation mark) or a “?”(question mark).
Assume that there can be maximum 10 sentences in a paragraph.
Write a program in java to arrange the sentences in increasing order of their number of words

Example:
Input:
Hello! how are you? Please come and attend the party

Output:

Hello!=1

How are you?=3

Please come and attend the party.=6

Question no 10:
Write a program in java to accept a sentence which may be terminated by either full stop
question mark or exclamation mark only.
The words are to be separated by a single blank space and are in upper case
Perform the following tasks :
a)check for the validity of the accepted sentence using the terminating character.

b)arrange the words in ascending order of their length. If two or more words have the
same length ,then sort them alphabetically.

c)Display the original sentence along the converted sentence.


Test your program for the following data and some random data:
Example 1:
Input: as you sow so shall you reap
As so sow you you reap shall

Question no 11:
A square matrix is said to be a magic square, if the sum of each row each column and each
diagonal is same .
Write a program to enter an integer number ‘n’. create a magic square of size ‘n*n’. Finally,
print the elements of the matrix as Magic Square.

Note: n<=5

Sample Input: Enter the size of the matrix : 4


16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1

Sample Output:

Question: 12

Write a program to declare a square matrix A[ ] [ ] of order N(N < 20). Allow the user to input
positive integers into this matrix. Perform the following tasks on the matrix.
i. Output the original matrix.
ii. Find the SADDLE POINT for the matrix.

A saddle point is an element of the matrix such that it is minimum element for
the row to which it belongs and the maximum element for the column to which it
belongs. Saddle point for a given matrix is always unique.
Example: In the Matrix 4 5 6
789
513
Saddle point = 7 because it is the minimum element of row 2 and maximum element
of column 1.

Question: 13

Write a Program in Java to fill a square matrix of size ‘n*n” in circular fashion ( clockwise)
With natural numbers from 1 to n*n, taking ‘n’ as input.
For example: if n = 4, then n*n = 16, hence the arrary will be filled as given below.

1 2 3 4

12 13 14 5

11 16 15 6

10 9 8 7

Question: 14

A class Rearrange has been defined to insert an element and to delete an element and to
delete an element from an array. Some specify the class Rearrange giving details of the
functions void enter(), void insert(), void disp10, void disp20 and void remov(). The main
function need not be written.

Class name : Rearrange


Data members / instance variables
A[] : integer type array
N : size of array
Pos1 : position of insertion
Pos2 : position of deletion
Item : item to be inserted
Member functions / methods
Void enter() : to enter size, array elements and to display the entered elements
Void insert() : to accept the element to be inserted , position of insertion.
Void disp1() : to display array after item is inserted
Void disp2() : to display array after item is deleted
Void remov() : to accept the position of deletion and delete element at the position
of deletion
Specify the class rearrange giving details of the functions void enter(), void insert(), void
disp1(), void disp2() and void remov(). The main function need not be written.

Question no 15:
A class ader has been defined to add any two accepted time. Example :
time a - 6 hours 35 minutes
Time b- 7 hours 45 minutes
Their sum is 14 hours 20 minutes (where 60 minutes = 1 hour)
The details of the members are given below :
Class name : adder
Data members / instance variables : a[ ]:
Integer array to hold two elements(hours and minutes)

Member functions/ methods


Adder(): constructor to assign 0 to the array elements void readtime():
To enter the elements of the array
Void addtime(adder x, adder y);adds the time of the two parameterized objects x and y and
stores the sum in the current calling object
void disptime(): displays the array elements with an appropriate message (i.e. hours = and
minutes=)
specify the adder giving the details of the constructor(),void readtime(),void addtime() and
void disptime(). Define the main function to create objects and call the functions accordingly
to enable the task.

Question no 16:
Class collection contains an array of 100 integers. Using the following class descriptions
create an array with some common elements from two integer arrays. Some of the members
of the class are given below :

Class name: collection


Data members / instance variables
Arr[ ] : integer array
Len : length of the array
Member functions
Collection() : default constructor
Collection(int): parameterized constructor to assign the length of the array
Void inparr(): to accept the array elements
Collection common(collection): returns a collection containing the common elements of
current collection object and the collection object passed as a parameter
Void arrange(): sort the array element of the object containing common elements in ascending
order using any sorting technique
Void display(): display the array elements

Question no 17:
Write a java program to accept n elements from the user and sort the elements using insertion
sort method

Question no 18:

Write a java program to accept n elements from the user and sort the elements using
selection sort method

Question no 19:

Write a java program to obtain reads through a text file “elt.txt”


The file contains the following text:
The program should be able to display only the phone numbers of all priya’s.
Implement the functionality through following class
Class name: textfile

Methods
Extractphoneno(): receives the names whose phone numbers is to be displayed and the
displays the extracted phone number along with the names and last names .
Assume that the fields are tab separated and the last names width is 7 letter also write
method main()
That obtain the names of the person whose number is to be displayed and invokes the method
accordingly

Question no 20:
Write a java program to accept 10 names and store it in text file named name.txt
Sort names in the file and store it in another text file in the name of order.txt

Question no 21:
A binary file named “ABC.DAT” contains the product code unit price and quantity for the
number of items
Write a method to accept the product code ‘p’ and check the availability of the product and
display with an appropriate message.
The method declaration is as follows:
Void findpro(int p)

Question no 22:
A superclass Perimeter has been defined to calculate the perimeter of a parallelogram.
Define a subclass area to compute the area of the parallelogram by using the required data
members of the super class. The details are given below:
Class name: perimeter
Data members/instance variables:

A: to store the length in decimal.


b: to store the breadth in decimal.
Member functions:
Perimeter(…):parameterized constructor to assign values to data members

Double calculate():calculate and return the perimeter of a parallelogram as 2*(length+breadth)


Void show(): to display the data members along with the perimeter of the parallelogram.
Class name: area

Data members/instance variables:


H: to store the height in decimal.
Area: to store the area of a parallelogram
Member function:
Area(…):parameterized constructor to assign values to data members of both the classes
Void do area(): compute the area as (breadth*height)
Void show(): display the data members of both classes along with the area and perimeter of
the parallelogram.
Specify the class perimeter giving details of the constructor, double calculate() and void
show(). Using the concept of inheritance, specify the class area giving the details of the
constructor, void doArea() and void show().

Question no 23:
A super class detail has been defined to store the details of the customer . Define a subclass
bill to compute the monthly telephone charge of the customer as per the chart given below:

Number of calls
1-100
101-200
201-300
Above 300

Rate
Only rental charge
60 paisa per call + rental charge 80 paisa per call + rental charge 1 rupee per call+ rental
charge
The details of both the classes are given below:
Class Name : Detail

Data members / Instance variables:


name : to store the name of the customer. address : to store the address of the customer.
telno : to store the phone number of the customer. rent : to store the monthly rental charge

Member functions:

Detail(…) : parameterized constructor to assign values to data members.

void show() : to display the detail of the customer.

Class Name : Bill


Data members / Instance variables:

n : to store the number of calls.

amt : to store the amount to be paid by the customer.

Member functions:

Bill(…) : parameterized constructor to assign values to data members of both classes and to
initialize amt = 0.0.

void cal() : calculates the monthly telephone charge as per the charge given above.

void show() : to display the detail of the customer and amount to be paid.

Specify the class Detail giving details of the constructor( ) and void show(). Using the concept
of inheritance, specify the class Bill giving details of the constructor( ), void cal() and void
show().

Specify the class Detail giving details of the constructor( ) and void show(). Using the concept
of inheritance, specify the class Bill giving details of the constructor( ), void cal() and void
show().

Question no 24:

Classes matrix1 and matrix2 are declared to


manage matrix operations using inheritance. The details of the classes are as follows: Class
name:matrix1

Data members:

a[]- double subscripted integer array of size 10*10

b[]-double subscripted integer array of size 10*10

m,n-integers as numbers of rows and columns of a[]

r,c-integers as number of rows and columns of b[]

Member methods:

Matrix1()-constructor to initialize arrays a and b and the other data members with 0. Void
readlimit()-to input values of m,n,r,c as number of rows and columns of a[] and b[].

Void input_a()-to input matrix a[] of order m*n Void input_b()-to input matrix b[] of order m*n
Void shoowmats()-to print matrix a[][]and matrix b[][].
Class name: matrix2(derived class of matrix 1)

Data member:

C[][]- double subscripted integer array of size 10*10

Member methods:

Matrix 2()-a constructor to initialize matrix

C[][] with 0

Void findproduct()-to find the product of matrix c[][],b[][] and store resultant matrix in c[][].The
matrix can be multiplied if n=r.

Void showmatrix()-to print matrix c[][] if multiplication is done ,otherwise output the message
“Matrix a and b cannot be multiplied.

Question no 25:

A class Iscscores defines the scores of a candidate in six subjects and


another class bestfour defines the best four subjects. The details of both the classes are
given below:
class name: Iscscores

data member : int number [6][2] to contain marks for six subjects and subject code (numeric)

member function:

iscscores(): constructor to accept the marks

int point(): to return the point in each subject according to the following:
marks>=90 1 point
80-89- 2 point
70-79- 3 point
60-69- 4 point
50-59- 5 point
40-49- 6 point
- - - - - - - - - - - - accordingly

Class name : best four

Member functions/methods :

Void bestsubjects(): to display the total points and best four subject codes using the concept
of inheritance
Specify the details of both the classes using the concept of inheritance.

Specify the details of the constructor Iscscores(), and functions int point(), void
bestsubjects().

Other functions are written for you and you need not write the main function.

Question no 26:

A class Admission contain the admission numbers of 100 students.


Some of the data members/ member functions are given below:
Class name: Admission
Data member/instance variable:
Adno[ ]: Integer array to store admission numbers
Member functions/methods:
Admission(): constructur to initialize the array elements
void fillArray(): to accept the element of the array in ascending order
int binSearch(int I, int u, int v): to search for a particular admission number(v) using binary
search and recursive technique and return 1

Specify the class Admission giving details of


the constructor, void fillArrray() and int binSearch(int, int, int). Define the main() function to
Create an object and call the functions accordingly to enable task.

Question:27

Java program to explain the importance of exception handling in java.create user defined
exception.

Question no 28:
Write a java program to exhibit the importance of exception handling in java(explicit
handling).

Question no 29:
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. top: to point the index of the top end.
Member functions:

Register(int max): constructor to initialize the data members 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

Question no 30:

Write a java program to accept an expression from the user and the validity of the given
expression using stack data structure

Question no 31:
Queue is a linear data structure which enables the user to add elements from the rear end and
remove elements from the front end only, using the concept of FIFO.

Note:
Since insertion and deletion both takes place from different ends, We have to keep record of
the index of both the front as well as the rear end. Consider a q[] is implemented using an
array q[].index of the front element is stored in the variable ’front ’and index of the last
element is store in the variable rear.

Question:32

Write a program to create a binary tree and perform the inorder traversal

Question:33

Write a program to create a binary tree and perform the


preorder traversal

Question no 34:
Write a program to create a binary tree and perform the postorder traversal

Question no 35:
A class called Evenseries has been definedto find the smallest value of the integer ‘n’ such
that , 2 + 4/2! + 8/3 + 6/4! + ……. + 2n/n!>=s where 2.0<s<7.0
Some of the members of the class evenseries are as follows

Class name : EvenSeries

Data members/instance variables:

N: long integer type to store number of terms

S: float variable where 2.0<s<7.0

K: float variable to store the value of series evaluated

Member Functions/methods:
EvenSeries(): constructor to initialize data members to 0.

Void accept(): to accept the value of the data members

Long fact(long x): to compute and return factorial of x

Void disp(): calculates and displays the least value of n

Specify the class EvenSeries giving the details of the constructor and the functions void
accept(),long fact(long x), and void disp().
The main function need not to be written.

Question no 36:
The lcm of two or more non zero integers is the least positive integer divisible by all of them.
Denoted as lcm(a,b)

The gcd of two integers is the largest integer that divides them both . This is usually denoted
as gcd(a,b). There is a relationship between lcm and gcd of two numbers say a and b that
lcm(a,b)*gcd(a,b)=ab

That is the product of the lcm and the gcd is equal to the product itself . write a class
numbers that contains these methods :

i).lcm that receives two integers and returns the lcm of the two integers

ii).gcd that receives two integers and returns the gcd of the two integers

iii). Relation that receives two integers and displays the relationship between lcm and gcd of
these two numbers

iv).main() that reads two numbers from the user and analyzes relationship of these two
numbers.

You might also like