0% found this document useful (0 votes)
15 views

Program Questions

Uploaded by

ziii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Program Questions

Uploaded by

ziii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MOCK PROGRAMS TEST SET-2 FM:-(15X4=60M) Comment Lines and Variable Description should be written.

Q.1. A public library issues books, which a student can use for 7 days without any fine, while returning the book a fine
will be charged only for excess days, if returned after specified days. Design a class Library with the following
specifications:
Class name : Library
Member variables / Data members:
bname - to store the name of the book issued.
days - to store the number of days taken to return the book.
extra - to store the extra days after the specified days.
fine_amount - to store the amount to be paid as fine.
Member methods:
void input() - to input the name of the book and the number of days taken to return the book.
void computeFine() - to compute the extra days taken to return the book and calculate the fine amount only for extra
days as per the given slabs:
Extra days Charge per day
Upto 5 days ₹5
6 to 15 days ₹10
Above 15 days ₹20
void display() - to display the name of the book, extra days taken to return
and the fine amount.
Now define a main() method to create an object and invoke the above member methods for obtaining the desired
output.
import java.util.*;
class Library{String bname;int days,extra,fine_amount;
void input(){Scanner br=new Scanner(System.in);
System.out.println(“Enter book name and number of days taken to return the book”);
bname=br.nextLine();days=br.nextInt();}
void computeFine(){if(days>7)extra=days-7;
if((extra>=1)&&(extra<=5)) fine_amount=extra*5;
else if((extra>=6)&&(extra<=15))
fine_amount=(5*5)+(extra-5)*10;
else
fine_amount=(5*5)+(10*10)+(extra-15)*20;}
void display(){System.out.println(bname+” “+days+” “+extra+” “+fine_amount);}
public static void main(){Library ob=new Library(); ob.input();ob.computeFine();ob.display();}}

Q.2. Design a class to overload a method show() as follows:


void show() : To print the following pattern using nested loop.
12345
2345
345
45
5
void show(double r) : To find and print the rounded off value of the parameter r without
using any mathematical library method and conditional statement.
class p{
void show(){int i,j;
for(i=1;i<=5;i+++)
{for(j=i;j<=5;j++)
{System.out.print(j);}System.out.println();}}
void show(double r)
{ System.out.print((r-(int)r>0.5)?((int)(r+0.5)):((int)r));}}}}

Q.3.Define a class to declare a double dimensional array of 5 rows and 5 columns, store 1 in the left diagonal and 0 in
the right diagonal and 2 in the rest cells. Print the table.
class p{public static void main(){int i,j; int a[][]=new int[5][5];
for(i=0;i<5;i++)
{for(j=0;j<5;j++)
{if(i==j)
a[i][j]=1;
else if(i+j==4)
a[i][j]=0;
else
a[i][j]=2;
System.out.print(a[i][j]);}
System.out.println();}}}
Q.4.Define a class to accept two strings a sentence and a word , convert them to lowercase, check if the word is part of
sentence. If yes display the position at which the string is present, if not present “Strings are not compatible”.
import java.util.*;
class rou{public static void main(){Scanner br=new Scanner(System.in);
String s,w,e;int i,p=0,c=0;char t;
System.out.println("Enter A sentence and word");
s=br.nextLine();s=s.trim();s=s+" ";w=br.next();s=s.toLowerCase();w=w.toLowerCase();
for(i=0;i<s.length();i++)
{t=s.charAt(i);
if(t==' ')
{e=s.substring(p,i);
p=i+1;
if(e.equals(w)==true)
c++;
}
}
if(c>0)
System.out.println(s+" "+w+"Are compatible");
}}
MOCK PROGRAMS TEST SET-1 FM:-(15X4=60M) Comment Lines and Variable Description should be written.
Q.1.Define a class called SECURITY with the following description:
Instance variables/Data members:
String name-to store name of the security personnel
int hour-to store number of hours for which wages are to be paid
double wages-to calculate and store the wages
Member methods-
void accept()-to input and store the name of the customer,hour and rate
void calculate()-to calculate the wages of security personnel.
Hours Charges/hour
Up to 40 hours 5/hour
For next 20 hours 6.5/hour
For next 20 hours 8/hour
void display()-to display the details of security personnel including wages
Write a main function to create the object of the class. Call the member methods by using the object.
import java.util.*;
class SECURITY{String name;int hour;double wages=0.0d;
void accept(){Scanner br=new Scanner(System.in);
System.out.println(“Enter name and hours”);
name=br.nextLine();hour=br.nextInt();}
void calculate(){
if((hour>=1)&&(hour<=40)) wages=hour*5;
else if((hour>=41)&&(hour<=60))
wages=(40*5)+(hour-40)*6.5;
else
wages=(40*5)+(20*6.5)+(hour-60)*8;}
void display(){System.out.println(name+” “+hour+” “+wages);}
public static void main(){SECURITY ob=new SECURITY(); ob.accept();ob.calculate();ob.display();}}

Q.2. A tech number has even number of digits .If the number is broken into two equal halves, then the square of sum of
these halves is equal to the number itself. WAP to input a number, check and print whether it is a tech number or
not.Example 3025 30+25=55->552=3025
import java.util.*;
class num
{
public static void main()
{Scanner br=new Scanner(System.in);
int n,n1,d=0,c=0,f=0,l=0,s=0;
System.out.println("Enter a number");
n=br.nextInt();n1=n;
while(n!=0)
{d=n%10;c++;n=n/10;}
if(c%2==0)
{f=n1%(int)Math.pow(10,c/2);
l=n1/(int)Math.pow(10,c/2);
s=f+l;
s=s*s;
if(s==n1)
System.out.println(n1+"Tech Number");
else
System.out.println(n1+"Not Tech Number");
}
else
System.out.println(n1+"Not Tech Number");
}}

Q.3. WAP to input a sentence and display the word of the sentence that contains maximum number of vowels.
import java.util.*;
class rou{public static void main(){Scanner br=new Scanner(System.in);
String s,w="",e;int i,p=0,c=0,max=0;char t,q;
System.out.println("Enter A sentence ");
s=br.nextLine();s=s.trim();s=s+" ";s=s.toLowerCase();
for(i=0;i<s.length();i++)
{t=s.charAt(i);
if(t==' ')
{e=s.substring(p,i);
p=i+1;
for(int j=0;j<e.length();j++)
{q=e.charAt(j);
if((q=='a')||(q=='e')||(q=='i')||(q=='o')||(q=='u'))
c++;}
if(c>max){max=c;w=e;}c=0;}}
System.out.println(max+" "+w);
}}

Q.4.An array is referred to as a ‘Special array’ if it has equal number of 0s and 1s in it. Write a program to accept
some positive integers in a DDA (double dimensional array) of size 4x4 and check whether it is a special
array or not. Display an error message if the array does not contain any ‘zero’ and ‘one’. [15]
Example:
n[ ][ ] = { { 1,3,1,0 }, { 2,4,5,6 }, { 2,0,8,0 }. { 1,7,5,9 } } ;
Total number of 0s = 3
Total number of 1s = 3
Hence, the array n[ ][ ] is a Special array.
import java.util.*;
class p{public static void main(){Scanner br=new Scanner(System.in);int i,j,o=0,z=0; int a[][]=new int[4][4];
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
{a[i][j]=br.nextInt();
if(a[i][j]==0)z++;
else if(a[i][j]==1)o++;
}}
if((z!=0)&&(o!=0)&&(z==o))
System.out.println(“It is a Special Array”);
else if((z!=0)&&(o!=0)&&(z!=o))
System.out.println(“It is not a Special Array”);
else if((z==0)&&(o==0))
System.out.println(“ERROR”);}}
MOCK PROGRAMS TEST SET-3 FM:-(15X4=60M) Comment Lines and Variable Description should be written.
Q.1.Define a class PhoneBill that calculates the telephone bill of a customer with the following description:-
Data members/Instance variables:
String name :Name of the customer
long phone:phone number of the customer
int bill-bill number
int call-number of calls made
double amount-bill amount to be paid by the person
member functions:-
i) PhoneBill()-A constructor to initialize data members with their default values.
ii) PhoneBill()-A parameterized constructor to accept the billno,name,phone no. and no.of calls
iii) void calculate()-to calculate the amount to be paid.
No.of calls Rate
First 100 calls 0.60 per call
Next 100 calls 0.80 per call
Next 100 calls 1.20 per call
Above 300 calls 1.50 per call
iv) void display()-To display the values of all data members with the final billing amount where a fixed monthly
rental is applicable to all consumers is Rs.125.
Write a main method to invoke all the above functions to perform the desired task.
import java.util.*;
class PhoneBill{String name;long phone;int bill,call;double amount;
PhoneBill(){name=” “;phone=0l;bill=0;call=0;amount=0.0d;}
PhoneBill(int b,String n,int c,long p)
{name=n;bill=b;call=c;phone=p;}
void calculate()
{if((call>=1)&&(call<=100))
amount=call*0.60;
else if((call>100)&&(call<=200))
amount=(100*0.60)+(call-100)*0.80;
else if((call>200)&&(call<=300))
amount=(100*0.60)+(100*0.80)+(call-200)*1.20;
else
amount=(100*0.60)+(100*0.80)+(100*1.20)+(call-300)*1.50;}
void display(){System.out.println(bill+” “+name+” “+” “+phone+” “+(amount+125));}
public static void main(){PhoneBill ob=new PhoneBill ();
PhoneBill ob=new PhoneBill (890,”pop”,356,9937265780);
ob.calculate();ob.display();}}

Q.2.Design a class to overload the function count to do the following-


void count(char,char)-A method to accept two different characters and display the sum and difference of their ASCII
values.
class p{
void count(String)-to count and print all the alphabets excluding the vowels present in the string.
class pop{
void count(char a,char b)
{if(a==b)
System.out.println("Equal");
else if(a!=b)
System.out.println((a+b)+" "+(a-b));}
}
void count(String n)
{char p;n=n.toLowerCase();int i;
for(i=0;i<n.length();i++)
{p=n.charAt(i);
if(Character.isLetter(p)==true)
{if((p=='a')||(p=='e')||(p==i)||(p=='o')||(p=='u'))
continue;
else System.out.print(p);}
}}}

Q.3. WAP to declare a double dimensional table of 3 rows and 3 columns, input values into it and print the table in
matrix form, also display sum of elements in left diagonal and product of elements in right diagonal
import java.util.*;
class p{public static void main(){Scanner br=new Scanner(System.in);int i,j,s=0,p=1; int a[][]=new int[3][3];
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{a[i][j]=br.nextInt();
if(i==j)s=s+a[i][j];
else if(i+j==2)p=p*a[i][j];
}}
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{System.out.print(a[i][j]);}
System.out.println();}
System.out.print(s+” “+p);}}
Q.4. WAP to input a sentence and convert it into uppercase and display reverse of each word in a separate line.
For eg-India is my country
Output:-AIDNI
SI
YM
YRTNUOC

import java.util.*;
class rou{public static void main(){Scanner br=new Scanner(System.in);
String s,w="",e;int i,j,p=0;char t,q;
System.out.println("Enter A sentence ");
s=br.nextLine();s=s.trim();s=s+" ";s=s.toUpperCase();
for(i=0;i<s.length();i++)
{t=s.charAt(i);
if(t==' ')
{e=s.substring(p,i);
p=i+1;
for( j=e.length()-1;j>=0;j--)
{q=e.charAt(j);
System.out.print(q);}}
System.out.println();}
}}
MOCK PROGRAMS TEST SET-4 FM:-(15X4=60M) Comment Lines and Variable Description should be written.
Q.1. WAP to design a class employee with the data members
Employee code(ec), Employee name(en), Annual Grade Pay(gp) of an employee, Annual Tax Liability(tax)
And Member methods:-
employee()-to store default values
void accept()- To accept Employee code, Employee name, Annual Grade Pay of an employee. void cal()-To determine the
Annual Tax Liability (it is the amount an employee has to pay as tax to the government) as follows:
Annual Grade Up to Up to 350,000 Up to 5 lakhs Above 5 lakhs
Pay 250,000
Tax Rate (%) Nil 10 20 30
If the age of the employee is above 65 years, additional concession of 5% is provided on the tax liability. If Annual Gross Pay
is over 5 lakhs, an additional 2% Educational cess has to be levied /charged on the tax amount payable.
void display()- To print all the details of an employee.
void main()-To create an object and call the above methods.
import java.util.*;
class employee{int ec;String en;int gp,age;double tax;
employee(){ec=0;en=” “;gp=0;age=0;tax=0.0d;}
void accept(){Scanner br=new Scanner(System.in);System.out.println(“Enter employee code,employee name and
annual grade pay”);
ec=br.nextInt();en=br.nextLine();gp=br.nextInt();age=br.nextInt();}
void cal(){if((gp>=1)&&(gp<=250000))
tax=0.0d;
else if((gp>250000)&&(gp<=350000))
tax=(250000*0.0)+(gp-250000)*0.10;
else if((gp>350000)&&(gp<500000))
tax=(250000*0.0)+(100000*0.10)+(gp-350000)*0.20;
else
{tax=(250000*0.0)+(100000*0.10)+(150000*0.20)+(gp-500000)*0.30;tax=tax+(tax*0.02);}
if(age>65)
{tax=tax-(tax*0.05;}}
void display(){System.out.println(ec+” “+en+” “+gp+” “+age+” “+tax);}
public static void main()
{employee ob=new employee();
ob.accept();ob.cal();ob.display();}}

Q.2.Design a class to overload a function compute() as follows:-


i)void compute(String str,int p)-With one string argument and one integer argument. It displays the word in uppercase if
p is 1 otherwise, displays the word in lowercase.
ii)void compute(char ch,int p)-with one character argument and one integer argument. It checks and displays whether a
character is in uppercase or not if p is 1 otherwise, checks and displays the character is in lowercase or not.
iii)void compute(String str,char ch)-with one string argument and one character argument. It displays the first three
characters of a String if chr is ‘f’ otherwise, displays the last three characters of the string.
class num
{
void compute(String str,int p)
{if(p==1)
System.out.println(str.toUpperCase());
else
System.out.println(str.toLowerCase());
}
void compute(char ch,int p)
{if(p==1)
{if(Character.isUpperCase(ch))
System.out.println(ch+"UPPERCASE");
}
else
if(Character.isLowerCase(ch))
System.out.println(ch+"LOWERCASE");
}
void compute(String str,char ch)
{if(ch=='f')
System.out.println(str.substring(0,3));
else
{int p=str.length();System.out.println(str.substring(p-3,p));}}}
Q.3.WAP to input a sentence and convert it into uppercase and display each word in a separate line.
For eg-India is my country
Output:-INDIA
IS
MY
COUNTRY
import java.util.*;
class rou{public static void main(){Scanner br=new Scanner(System.in);
String s,w="",e;int i,p=0;char t;
System.out.println("Enter A sentence ");
s=br.nextLine();s=s.trim();s=s+" ";s=s.toUpperCase();
for(i=0;i<s.length();i++)
{t=s.charAt(i);
if(t==' ')
{e=s.substring(p,i);
p=i+1;
System.out.println(e);}}
}}

Q.4.WAP to declare an integer array of size 10, input values into it, sort the array in descending order using selection
sort technique and print it.
import java.util.*;
class selection1
{public static void main()
{int no[]=new int[10];
Scanner br=new Scanner(System.in);
for(int i=0;i<10;i++)
{System.out.println("Enter no");
no[i]=br.nextInt();}
int m,p;
for(int i=0;i<10;i++)
{m=no[i];p=i;
for(int j=i+1;j<10;j++)
{if(no[j]>m)
{m=no[j];p=j;}}
no[p]=no[i];no[i]=m;}
System.out.println("The descending order is:-");
for(int j=0;j<10j++)
{System.out.println(no[j]);}}}

You might also like