Icse Computer Programming Question Paper With Solution 2019-2010

Download as pdf or txt
Download as pdf or txt
You are on page 1of 153

icse computer

programming
question paper
with solution
2019-2010
Question 4 2019
DESIGN A CLASS NAME SHOWROOM WITH THE FOLLOWING DESCRIPTION:
INSTANCE VARIABLES/DATA MEMBERS:

STRING NAME: TO STORE THE NAME OF THE CUSTOMER.


LONG MOBNO: TO STORE CUSTOMER’S MOBILE NUMBER.
DOUBLE COST: TO STORE THE COST OF THE ITEM PURCHASED.
DOUBLE DIS: TO STORE THE DISCOUNT AMOUNT.
DOUBLE AMOUNT: TO STORE THE AMOUNT TO BE PAID AFTER DISCOUNT.
METHODS:

SHOWROOM(): DEFAULT CONSTRUCTOR TO INITIALIZE THE DATA MEMBERS

VOID INPUT(): TO INPUT CUSTOMER NAME, MOBILE NUMBER, COST


VOID CALCULATE(): TO CALCULATE THE DISCOUNT ON THE COST OF PURCHASED ITEMS, BASED
ON THE FOLLOWING CRITERIA

COST DISCOUNT(IN PERCENTAGE)


LESS THAN OR EQUAL TO ₹ 10000 5%
MORE THAN ₹ 10000 AND LESS THAN OR EQUAL TO ₹ 20000 10%
MORE THAN ₹ 20000 AND LESS THAN OR EQUAL TO ₹ 35000 15%
MORE THAN ₹ 35000 20%

VOID DISPLAY()- TO DISPLAY CUSTOMER, MOBILE NUMBER, AMOUNT TO BE PAID AFTER


DISCOUNT.
WRITE A MAIN() METHOD TO CREATE AN OBJECT OF THE CLASS AND CALL THE ABOVE
METHODS.
Answer 2019
import java.util.Scanner;
public class ShowRoom
{
String name;
long mobno;
double cost,dis,amount;
/*default constructor*/
public ShowRoom()
{
name="";
mobno=0;
cost=0.0;
dis=0.0;
amount=0.0;
}
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
name=sc.nextLine();
System.out.print("Enter your Mobile Number: ");
mobno=sc.nextLong();
System.out.print("Enter cost of item purchased: ");
cost=sc.nextDouble();
}
public void calculate()
{
if(cost<=10000)
{
dis = cost * 5/100;
amount = cost - dis;
}
else if(cost>10000&&cost<=20000)
{
dis = cost * 10/100;
amount = cost - dis;
}
2019
else if(cost>20000&&cost<=35000)
{
dis = cost * 15/100;
amount = cost - dis;
}

else if(cost>35000)
{
dis = cost * 20/100;
amount = cost - dis;
}
}

public void display()


{
System.out.println("Name of the customer: "+name);
System.out.println("Mobile number : "+mobno);
System.out.println("Amount to be paid after discount: "+amount);
}

public static void main(String[]args)


{
ShowRoom ob1 = new ShowRoom();
ob1.input();
ob1.calculate();
ob1.display();
}
}
Question 5 2019
USE THE SWITCH CASE STATEMENT, WRITE A MENU DRIVEN PROGRAM TO DO THE FOLLOWING:
(A) TO GENERATE AND PRINT LETTERS FROM A TO Z & THEIR UNICODE

LETTER UNICODE
A 65
B 66
C 67
- -
- -
- -
Z 90

(B) DISPLAY THE FOLLOWING PATTERN ITERATION (LOOPING) STATEMENT:

1
12
123
1234
12345
Answer 2019
import java.util.Scanner;
class program5
{
public static void main(String[]args)
{
int choice=0,i=0;
Scanner sc=new Scanner(System.in);
System.out.println("Press 1 for Unicode of A to Z ");
System.out.println("Press 2 for pattern");
System.out.print("Enter Your Choice:");
choice = sc.nextInt();
switch(choice)
{
case 1:

System.out.println("Letters : Unicode");
for(i=65;i<=90;i++)
{
System.out.println((char)i+" : "+i);
}
break;
case 2:
int k=0,j=0;
for(i=1;i<=5;i++)
{
k=1;
for(j=1;j<=5;j++)
{
if(j<=i)
{
System.out.print(k);
k++;
}
else
System.out.print(" ");
}
System.out.println();
}
break;
default:
System.out.println("Invalid choice");
}
}
}
Question 6 2019
WRITE A PROGRAM TO INPUT 15 INTEGER ELEMENTS IN AN ARRAY
AND SORT THEM IN ASCENDING ORDER USING THE BUBBLE SORT
TECHNIQUE.
Answer 2019
import java.util.Scanner;
public class program6
{
public static void main(String[]args)
{
int i=0,len=0,temp=0,j=0;
Scanner sc = new Scanner(System.in);
int arr []=new int[15];
System.out.println("Enter 15 elements in an array ");
for(i=0;i<15;i++)
{
arr[i]=sc.nextInt();
}

len=arr.length;
for(i=0;i< len-1;i++)
{
for(j=0;j< len-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(i=0;i< len;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Question 7 2019
DESIGN A CLASS TO OVERLOAD A FUNCTION SERIES() AS FOLLOWS:
(I) VOID SERIES(INT X, INT N) – TO DISPLAY THE SUM OF THE
SERIES GIVEN BELOW:
X¹+X²+X³+. . . . .X^N TERMS
(II)VOID SERIES(INT P) – TO DISPLAY THE FOLLOWING SERIES
0, 7, 26, 63. . . . .P TERMS
(III) VOID SERIES() – TO DISPLAY THE SUM OF THE SERIES GIVEN
BELOW:
1/2+1/3+1/4+. . . . . .1/10
Answer 2019
public class program7
{
public void series(int x,int n)
{
int sum=0,i=0;
for(i=1;i<=n;i++)
{
sum= sum + (int)Math.pow(x,i);
}
System.out.println(sum);
}
public void series(int p)
{
int i=0;
for(i=1;i<=p;i++)
{
System.out.print((i*i*i-1)+",");

}
}
public void series()
{
double sum=0,i=0;
for(i=2;i<=10;i++)
{
sum = sum + 1/i;
}
System.out.println(sum);

}
}
Question 8 2019
WRITE PROGRAM TO INPUT A SENTENCE AND CONVERT IT INTO
UPPERCASE AND COUNT AND DISPLAY THE TOTAL NO. OF WORDS
STARTING WITH A LETTER ‘A’.

EXAMPLE:
SAMPLE INPUT: ADVANCEMENT AND APPLICATION OF INFORMATION
TECHNOLOGY ARE EVER CHANGING
SAMPLE OUTPUT: TOTAL NUMBER OF WORD STARTING WITH LETTER
‘A’= 4
Answer 2019
import java.util.Scanner;
public class program8
{
public static void main(String[]args)
{
String sen="";
int i=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
sen=sc.nextLine();
sen=sen.toUpperCase();
int count=0;
for(i=0;i< sen.length();i++)
{
if(i==0 && sen.charAt(i)=='A')
{
count++;
}
else if(i>0)
{
if((sen.charAt(i - 1) == ' ') && (sen.charAt(i)=='A'))
{
count++;
}
}

}
System.out.println("Total number of words Starting with letter 'A'= "+count);
}
}
Question 9 2019
A TECH NUMBER HAS A EVEN NUMBER OF DIGITS IF THE NUMBER IS
SPLIT IN TWO EQUAL HALVES , THEN THE SQUARE OF SUM OF THESE
TWO HALVES IS EQUAL TO THE NUMBER ITSELF. WRITE A PROGRAM
TO GENERATE AND PRINT ALL 4 DIGIT TECH NUMBERS:
EXAMPLE:
CONSIDER THE NUMBER 3025
SQUARE OF SUM OF THE HALVES OF 3025
= (30 + 25)²
= (55)²
3025 IS A TECH NUMBER
Answer 2019
public class program9
{
public static void main(String[]args)
{

int firstHalf=0,secondHalf=0,sumOfSquare=0,i=0;
for(i=1000;i<=9999;i++)
{
/* dividing number into two half*/
firstHalf=i/100;
secondHalf=i%100;
sumOfSquare=(int)Math.pow((firstHalf+secondHalf),2);
if(sumOfSquare==i)
{
System.out.print(i+" ");
}
}
}
}
Question 4 2018
DESIGN A CLASS RAILWAYTICKET WITH THE FOLLOWING
DESCRIPTION:
INSTANCE VARIABLES/DATA MEMBERS:
STRING NAME: TO STORE THE NAME OF THE CUSTOMER.
STRING COACH: TO STORE THE TYPE OF COACH CUSTOMER WANTS
TO TRAVEL.
LONG MOBNO: TO STORE CUSTOMER’S MOBILE NUMBER.
INT AMT: TO STORE BASIC AMOUNT OF TICKET.
INT TOTALAMT: TO STORE THE AMOUNT TO BE PAID AFTER
UPDATING THE ORIGINAL AMOUNT.
METHODS:
VOID ACCEPT(): TO TAKE INPUT FOR NAME, COACH, MOBILE
NUMBER AND AMOUNT.
VOID UPDATE(): TO UPDATE THE AMOUNT AS PER THE COACH
SELECTED. EXTRA AMOUNT TO BE ADDED IN THE AMOUNT AS
FOLLOWS:
TYPE OF COACHES AMOUNT
FIRST_AC 700
SECOND_AC 500
THIRD_AC 250
SLEEPER NONE

VOID DISPLAY(): TO DISPLAY ALL DETAILS OF A CUSTOMER SUCH AS


NAME, COACH, TOTAL AMOUNT AND MOBILE NUMBER.
WRITE A MAIN() METHOD TO CREATE AN OBJECT OF THE CLASS AND
CALL THE ABOVE METHODS.
Answer 2018
import java.util.Scanner;

public class RailwayTicket


{
String name;
String coach;
long mobno;
int amt;
int totalamt;
public RailwayTicket()
{
name="";
coach="";
mobno=0;
amt=0;
totalamt=0;
}
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name of passenger: ");
name = sc.nextLine();
System.out.print("Enter coach:(Type 1A for first class, 2A for second class,
3A for third class & SL for Sleeper class) ");
coach = sc.nextLine();
System.out.print("Enter mobno of passenger: ");
mobno = sc.nextLong();
System.out.print("Enter amount: ");
amt = sc.nextInt();
}
2018
public void update()
{
if (coach.equalsIgnoreCase("1A"))
{
totalamt = amt + 700;
}
else if (coach.equalsIgnoreCase("2A"))
{
totalamt = amt + 500;
}
else if (coach.equalsIgnoreCase("3A"))
{
totalamt = amt + 250;
}
else if (coach.equalsIgnoreCase("SL"))
{
totalamt = amt;
}
else
{
System.out.println("Invalid coach number");
}
}

public void display()


{
System.out.println("Name: " + name);
System.out.println("Coach: " + coach);
System.out.println("Mobile Number: " + mobno);
System.out.println("Amount: " + amt);
System.out.println("Total Amount: " + totalamt);
}
2018
public static void main(String args[])
{
RailwayTicket ob1 = new RailwayTicket();
ob1.accept();
ob1.update();
ob1.display();
}
}
Question 5 2018
WRITE A PROGRAM TO INPUT A NUMBER AND CHECK AND PRINT
WHETHER IT IS A PRONIC NUMBER OR NOT. THE PRONIC NUMBER IS
THE NUMBER THAT IS THE PRODUCT OF TWO CONSECUTIVE
INTEGERS.
EXAMPLES:
12 = 3 × 4
20 = 4 × 5
42 = 6 × 7
Answer 2018
import java.util.Scanner;
public class PronicNumbers
{
public static void main(String[] args)
{
int num=0, i=0;
boolean flag=false;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
num=sc.nextInt();
for(i=0;i<=num;i++)
{
if((i*(i+1))==num)
{
flag=true;
break;
}
}
if(flag==true)
{
System.out.print(num + " is a pronic number ");
}
else
{
System.out.print(num + " is not a pronic number ");
}

}
}
Question 6 2018
WRITE A PROGRAM IN JAVA TO ACCEPT A STRING IN LOWERCASE
AND CHANGE THE FIRST LETTER OF EVERY WORD TO UPPERCASE.
DISPLAY THE NEW STRING.

SAMPLE INPUT: WE ARE IN CYBER WORLD


SAMPLE OUTPUT: WE ARE IN CYBER WORLD
Answer 2018
import java.util.Scanner;
public class program6
{
public static void main(String[] args)
{
int i=0;
char ch=' ';
String sen="",output="";
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
sen = sc.nextLine();
for (i = 0; i< sen.length(); i++)
{
ch = sen.charAt(i);
if (i == 0 || sen.charAt(i - 1) == ' ')
{
output = output + Character.toUpperCase(ch);
}
else
{
output = output + ch;
}
}
System.out.println("Output: " + output);
}
}
Question 7 2018
DESIGN A CLASS TO OVERLOAD A FUNCTION VOLUME() AS FOLLOWS:

(I) DOUBLE VOLUME(DOUBLE R) – WITH RADIUS ‘R’ AS AN


ARGUMENT, RETURNS THE VOLUME OF SPHERE USING THE
FORMULA:
V = 4 / 3 × 22 / 7 × R3

(II)DOUBLE VOLUME(DOUBLE H, DOUBLE R) – WITH HEIGHT ‘H’ AND


RADIUS ‘R’ AS THE ARGUMENTS, RETURNS THE VOLUME OF A
CYLINDER USING THE FORMULA:
V = 22 / 7 × R2 × H

(III) DOUBLE VOLUME(DOUBLE L, DOUBLE B, DOUBLE H) – WITH


LENGTH ‘L’, BREADTH ‘B’ AND HEIGHT ‘H’ AS THE ARGUMENTS,
RETURNS THE VOLUME OF A CUBOID USING THE FORMULA:
V=L×B×H
Answer 2018
public class program7
{

double volume(double r)
{
double vol = 4.0 / 3 * 22 / 7 * Math.pow(r, 3);
return vol;
}
double volume(double h, double r)
{
double vol = 22.0 / 7 * Math.pow(r, 2) * h;
return vol;
}
double volume(double l, double b, double h)
{
double vol = l * b * h;
return vol;
}
}
Question 8 2018
WRITE A MENU-DRIVEN PROGRAM TO DISPLAY THE PATTERN AS PER
USER’S CHOICE:
PATTERN 1
ABCDE
ABCD
ABC
AB
A
PATTERN 2
B
LL
UUU
EEE
FOR AN INCORRECT OPTION, AN APPROPRIATE ERROR MESSAGE
SHOULD BE DISPLAYED.
Answer 2018
import java.util.Scanner;
public class program8
{
public static void main(String[]args)
{
int choice=0,i=0,j=0,num=0;
char k=' ',ch=' ';
String wd="";
Scanner sc=new Scanner(System.in);
System.out.println("Press 1 or 2 to choose a pattern: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter number of lines: ");
num = sc.nextInt();
for(i=1;i<=num;i++)
{
k='A';
for(j=1;j<=num;j++)
{
if(j<=num+1-i)
{
System.out.print(k);
k++;
}
else
{
System.out.print(" ");
}
}
System.out.println();
2018
}
break;
case 2:
System.out.print("Enter a word: ");
wd=sc.next();
for(i=0;i< wd.length();i++)
{
ch=wd.charAt(i);
for(j=0;j< wd.length();j++)
{
if(j<=i)
{
System.out.print(ch);
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
break;
default:
System.out.println("Invalid coice");
}
}
}
Question 9 2018
WRITE A PROGRAM TO ACCEPT NAME AND TOTAL MARKS OF N
NUMBER OF STUDENTS IN TWO SINGLE SUBSCRIPTS ARRAY NAME[]
AND TOTALMARKS[].

CALCULATE AND PRINT:


(I) THE AVERAGE OF THE TOTAL MARKS OBTAINED BY N NUMBER OF
STUDENTS.
[AVERAGE = (SUM OF TOTAL MARKS OF ALL THE STUDENTS) / N]
(II) DEVIATION OF EACH STUDENT’S TOTAL MARKS WITH THE
AVERAGE.
[DEVIATION = TOTAL MARKS OF A STUDENT – AVERAGE]
Answer 2018
import java.util.Scanner;
public class program9
{
public static void main(String[] args)
{
int sum=0,n=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
n = sc.nextInt();
String[] name = new String[n];
int[] totalmarks = new int[n];
for (int i = 0; i< n; i++)
{
System.out.println("Student " + (i + 1));
System.out.print("Enter name of student: ");
name[i] = sc.next();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
}

for (int i = 0; i< n; i++)


{
sum = sum + totalmarks[i];
}
double average = (double) sum / n;
System.out.println("Average of all marks " + average);
for (int i = 0; i< n; i++)
{
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
}
}
}
Question 4 2017
DEFINE A CLASS ELECTRIC BILL WITH THE FOLLOWING
SPECIFICATIONS:
CLASS: ELECTRICBILL
INSTANCE VARIABLE/ DATA MEMBER:
STRING N – TO STORE THE NAME OF THE CUSTOMER
INT UNITS – TO STORE THE NUMBER OF UNITS CONSUMED
DOUBLE BILL – TO STORE THE AMOUNT TO PAID
MEMBER METHODS:
VOID ACCEPT() – TO ACCEPT THE NAME OF THE CUSTOMER AND
NUMBER OF UNITS CONSUMED
VOID CALCULATE() – TO CALCULATE THE BILL AS PER THE
FOLLOWING TARIFF :
NUMBER OF UNITS RATE PER UNIT
FIRST 100 UNITS RS.2.00
NEXT 200 UNITS RS.3.00
ABOVE 300 UNITS RS.5.00
A SURCHARGE OF 2.5% CHARGED IF THE NUMBER OF UNITS
CONSUMED IS ABOVE 300 UNITS.

VOID PRINT() – TO PRINT THE DETAILS AS FOLLOWS :


NAME OF THE CUSTOMER ………
NUMBER OF UNITS CONSUMED ……
BILL AMOUNT …….
WRITE A MAIN METHOD TO CREATE AN OBJECT OF THE CLASS AND
CALL THE ABOVE MEMBER METHODS.
Answer 2017
import java.util.Scanner;

public class ElectricBill


{
String n;
int units;
double bill;
public ElectricBill()
{
n="";
units=0;
bill=0;

}
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of customer: ");
n = sc.next();
System.out.print("Enter units: ");
units = sc.nextInt();
}
public void calculate()
{
if (units<= 100)
{
bill = units * 2;
}
else if (units>100 && units<=300)
{
bill =100*2+ (units-100) * 3;
}
2017
else
{
bill = 100*2 + 200*3+(units-300) * 5;
double charge = bill * 2.5 / 100;
bill = bill + charge;
}
}
public void print()
{
System.out.println("Name of the customer: " +n);
System.out.println("Number of units consumed: " +units);
System.out.println("Bill amount: " +bill);
}
public static void main(String[] args)
{
ElectricBill ob1 = new ElectricBill();
ob1.accept();
ob1.calculate();
ob1.print();
}
}
Question 5 2017
WRITE A PROGRAM TO ACCEPT A NUMBER AND CHECK AND DISPLAY
WHETHER IT IS A SPY NUMBER OR NOT.
(A NUMBER IS SPY IF THE SUM ITS DIGITS EQUALS THE PRODUCT OF
THE DIGITS.)
EXAMPLE: CONSIDER THE NUMBER 1124 , SUM OF THE DIGITS = 1 +
1 + 2 + 4 = 8 PRODUCT OF THE DIGITS = 1 X 1 X 2 X 4=8
Answer 2017
import java.util.Scanner;

public class program5


{
public static void main(String[] args)
{
int num=0,sum=0,product=1,rem=0,temp=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
temp=num;
while (temp>0)
{
rem = temp % 10;
sum = sum + rem;
product = product * rem;
temp = temp / 10;
}
if (sum == product)
{
System.out.println(num+" is a Spy number");
}
else
{
System.out.println(num+" is Not a spy number");
}
}
}
Question 6 2017
USING SWITCH STATEMENT, WRITE A MENU DRIVEN PROGRAM FOR
THE FOLLOWING:
I) TO FIND AND DISPLAY THE SUM OF THE SERIES GIVEN BELOW:
S = X1 – X2 + X3 – X4 + X5 … – X20
(WHERE X=2)
II) TO DISPLAY THE FOLLOWING SERIES:
1 11 111 1111 11111
FOR AN INCORRECT OPTION, AN APPROPRIATE ERROR MESSAGE
SHOULD BE DISPLAYED.
Answer 2017
import java.util.Scanner;

public class program6


{
public static void main(String[] args)
{
int choice=0,i=0,j=0;
double sum=0;
System.out.println("Press 1 for Sum of series");
System.out.println("Press 2 to Display Series");
System.out.print("Enter your choice: ");
Scanner sc = new Scanner(System.in);
choice = sc.nextInt();
switch (choice)
{
case 1:
sum = 0;
for (i = 1; i <= 20; i++)
{
if (i % 2 == 0)
{
sum = sum - Math.pow(2, i);
}
else
{
sum = sum +Math.pow(2, i);
}
}
System.out.println("Sum = " + sum);
break;
2017
case 2:
int temp =0;
for (i = 1; i<= 5; i++)
{
temp=temp*10+1;
System.out.print(temp+" ");

}
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
Question 7 2017
WRITE A PROGRAM TO INPUT INTEGER ELEMENTS INTO AN ARRAY
OF SIZE 20 AND PERFORM THE FOLLOWING OPERATIONS:
I) DISPLAY LARGEST NUMBER FROM THE ARRAY
II) DISPLAY SMALLEST NUMBER FROM THE ARRAY
III) DISPLAY SUM OF ALL THE ELEMENTS OF THE ARRAY
Answer 2017
import java.util.Scanner;

public class program7


{
public static void main(String[] args)
{
int i=0,smallest=0,largest=0,sum=0;
Scanner sc = new Scanner(System.in);
int[] arr = new int[20];
System.out.print("Enter 20 numbers in an array: ");
for (i = 0; i < 20; i++)
{
arr[i] = sc.nextInt();
}
smallest = arr[0];
largest = arr[0];

for (i = 0; i < 20; i++)


{
if (arr[i] < smallest)
{
smallest = arr[i];
}
if (arr[i] > largest)
{
largest = arr[i];
}
sum = sum + arr[i];
}
2017
System.out.println(smallest+" is the Smallest number in the
array");
System.out.println(largest+" is the Largest number in the
array ");
System.out.println(sum+" is the Sum of the given array");
}
}
Question 8 2017
DESIGN A CLASS TO OVERLOAD A FUNCTION CHECK() AS FOLLOWS:
I) VOID CHECK(STRING STR, CHAR CH) – TO FIND AND PRINT THE
FREQUENCY OF A CHARACTER
IN A STRING.
EXAMPLE :
INPUT — OUTPUT
STR = “SUCCESS” NUMBER OF S PRESENT IS=3
CH = ‘S’
II) VOID CHECK (STRING S1) – TO DISPLAY ONLY THE VOWELS FROM
STRING S1 , AFTER CONVERTING IT TO LOWER CASE.
EXAMPLE :
INPUT: S1= “COMPUTER”
OUTPUT: O U E
Answer 2017
public class program8
{
public void check(String str, char ch)
{
int result = 0,i=0;
char ch1=' ';
for (i = 0; i < str.length(); i++)
{
ch1= str.charAt(i);
if (ch == ch1)
{
result++;
}
}
System.out.println("number of "+ch+" present is = " + result);

public void check(String s1)


{
int i=0;
char ch=' ';
s1 = s1.toLowerCase();
for (i = 0; i < s1.length(); i++)
{
ch = s1.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' ||ch == 'o'||ch == 'u')
{
System.out.print(ch + " " );
}
}
}
}
Question 9 2017
WRITE A PROGRAM TO INPUT FORTY WORDS IN AN ARRAY. ARRANGE
THESE WORDS IN DESCENDING ORDER OF ALPHABETS, USING
SELECTION SORT TECHNIQUE. PRINT THE SORTED ARRAY.
Answer 2017
import java.util.Scanner;
public class program9
{
public static void main(String[] args)
{
int i=0,j=0,largestWordPos=0;
String temp="";
Scanner sc = new Scanner(System.in);
String[] wd = new String[40];
System.out.println("Enter 40 words: ");
for (i = 0; i < 40; i++)
{
wd[i] = sc.next();
}

for (i = 0; i < wd.length - 1; i++)


{
largestWordPos = i;
for (j = i + 1; j < wd.length; j++)
{
if (wd[j].compareTo(wd[largestWordPos]) > 0)
{
largestWordPos = j;
}
}
temp = wd[largestWordPos];
wd[largestWordPos] = wd[i];
wd[i] = temp;
}
2017
/* Print the sorted array*/
System.out.println("Word Array After Sorting");
for (i = 0; i < 40; i++)
{
System.out.println(wd[i]);
}
}
}
Question 4 2016
DEFINE A CLASS NAMED BOOKFAIR WITH THE FOLLOWING
DESCRIPTION:
INSTANCE VARIABLES/DATA MEMBERS:
STRING BNAME – STORES THE NAME OF THE BOOK.
DOUBLE PRICE – STORES THE PRICE OF THE BOOK.
MEMBER METHODS:
(I) BOOKFAIR() – DEFAULT CONSTRUCTOR TO INITIALIZE DATA
MEMBERS.
(II) VOID INPUT() – TO INPUT AND STORE THE NAME AND THE PRICE
OF THE BOOK.
(III) VOID CALCULATE() – TO CALCULATE THE PRICE AFTER
DISCOUNT. DISCOUNT IS CALCULATED BASED ON THE FOLLOWING
CRITERIA.
PRICE DISCOUNT
LESS THAN OR EQUAL TO RS 1000: 2% OF PRICE
MORE THAN RS 1000 AND LESS THAN OR EQUAL TO RS 3000: 10%
OF PRICE
MORE THAN RS 3000: 15% OF PRICE

(IV) VOID DISPLAY() – TO DISPLAY THE NAME AND PRICE OF THE


BOOK AFTER DISCOUNT.
WRITE A MAIN METHOD TO CREATE AN OBJECT OF THE CLASS AND
CALL THE ABOVE MEMBER METHODS.
Answer 2016
import java.util.Scanner;

public class BookFair


{
String Bname;
double price;

public BookFair()
{
Bname = "";
price = 0.0;
}

public void Input()


{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the book name: ");
Bname = sc.nextLine();
System.out.print("Enter the price: ");
price = sc.nextDouble();
}

public void calculate()


{
double discountPer = 0;
if (price <= 1000)
{
discountPer = 2;
}
else if (price > 1000 && price <= 3000)
{
discountPer = 10;
}
2016
else if (price > 3000)
{
discountPer = 15;
}
price = price - (price * discountPer / 100);
}

public void display()


{
System.out.println("Name: " + Bname);
System.out.println("Price after discount: " + price);
}
public static void main(String[] args)
{
BookFair ob1 = new BookFair();
ob1.Input();
ob1.calculate();
ob1.display();
}
}
Question 5 2016
USING THE SWITCH STATEMENT, WRITE A MENU DRIVEN PROGRAM
FOR THE FOLLOWING:
(I) TO PRINT THE FLOYD’S TRIANGLE [GIVEN BELOW]
1
23
456
7 8 9 10
11 12 13 14 15
(II) TO DISPLAY THE FOLLOWING PATTERN
I
IC
ICS
ICSE
FOR AN INCORRECT OPTION, AN APPROPRIATE ERROR MESSAGE
SHOULD BE DISPLAYED.
Answer 2016
import java.util.Scanner;

public class program5


{
public static void main(String[] args)
{
int choice=0,i=0,j=0,n=0;
Scanner sc = new Scanner(System.in);
System.out.println("Press 1 for Floyd's triangle");
System.out.println("Press 2 to print ICSE Pattern");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter number of lines: ");
n = sc.nextInt();
int number = 1;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
System.out.print(number + " ");
number++;
}
System.out.println();
}
break;
2016
case 2:
System.out.print("Enter aword: ");
String word = sc.next();
for (i = 0; i < word.length(); i++)
{
for (j = 0; j <= i; j++)
{
System.out.print(word.charAt(j) + " ");
}
System.out.println();
}
break;

default:
System.out.println("Invalid choice");
break;
}
}
}
Question 6 2016
SPECIAL WORDS ARE THOSE WORDS WHICH STARTS AND ENDS WITH
THE SAME LETTER.
EXAMPLES: EXISTENCE ,COMIC, WINDOW
PALINDROME WORDS ARE THOSE WORDS WHICH READ THE SAME
FROM LEFT TO RIGHT AND VICE-VERSA.
EXAMPLE: MALAYALAM MADAM LEVEL ROTATOR CIVIC
ALL PALINDROMES ARE SPECIAL WORDS, BUT ALL SPECIAL WORDS
ARE NOT PALINDROMES. WRITE A PROGRAM TO ACCEPT A WORD
CHECK AND PRINT WHETHER THE WORD IS A PALINDROME OR ONLY
SPECIAL WORD.
Answer 2016
import java.util.Scanner;

public class program6


{
public static void main(String[] args)
{
int i=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the word: ");
String wd = sc.next();
/* Check whether the word given is palindrome or not */
String reverse = "";
for (i = wd.length() - 1; i >= 0; i--)
{
reverse = reverse + wd.charAt(i);
}

if(wd.equals(reverse)==true)
{
System.out.println(wd+" is a Palindrome word");
}

// Check whether the given word is a special word

if (wd.charAt(0) == wd.charAt(wd.length() - 1))


{
System.out.println(wd+" is a Special word");
}

}
}
Question 7 2016
DESIGN A CLASS TO OVERLOAD A FUNCTION SUMSERIES() AS
FOLLOWS:
(I) VOID SUMSERIES(INT N, DOUBLE X) – WITH ONE INTEGER
ARGUMENT AND ONE DOUBLE ARGUMENT TO FIND AND DISPLAY THE
SUM OF THE SERIES GIVEN BELOW:
S = (X/1) – (X/2) + (X/3) – (X/4) + (X/5) … TO N TERMS
(II) VOID SUMSERIES() – TO FIND AND DISPLAY THE SUM OF THE
FOLLOWING SERIES:
S = 1 + (1 X 2) + (1 X 2 X 3) + … + (1 X 2 X 3 X 4 X … 20)
Answer 2016
public class program7
{
public void SumSeries(int n, double x)
{
int i=0;
double sum = 0;
for (i = 1; i <= n; i++)
{
if (i % 2 == 1)
{
sum = sum + (x / i);
}
else
{
sum = sum - (x / i);
}
}
System.out.println("Sum = " + sum);
}
public void SumSeries()
{
int sum = 0,i=0,product=1,j=0;
for (i = 1; i <= 20; i++)
{
product = 1;
for (j = 1; j <= i; j++)
{
product = product * j;
}
sum = sum + product;
}
System.out.println("Sum = " + sum);
2016
}
}
Question 8 2016
WRITE A PROGRAM TO ACCEPT A NUMBER AND CHECK AND DISPLAY
WHETHER IT IS A NIVEN NUMBER OF NOT.
(NIVEN NUMBER IS THAT NUMBER WHICH IS DIVISIBLE BY ITS SUM
OF DIGITS).
EXAMPLE: CONSIDER THE NUMBER 126. SUM OF ITS DIGITS IS 1 + 2
+ 6 = 9 AND 126 IS DIVISIBLE BY 9.
Answer 2016
import java.util.Scanner;

public class program8


{
public static void main(String[] args)
{
int num=0,rem=0,temp=0,sumOfDigits=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
sumOfDigits = 0;
temp = num;
while (temp > 0)
{
rem = temp % 10;
temp = temp / 10;
sumOfDigits = sumOfDigits + rem;
}
if (num % sumOfDigits == 0)
{
System.out.println(num+" is a Niven number");
}
else
{
System.out.println(num +" is Not a niven number");
}
}
}
Question 9 2016
WRITE A PROGRAM TO INITIALIZE THE SEVEN WONDERS OF THE
WORLD ALONG WITH THEIR LOCATIONS IN TWO DIFFERENT ARRAYS.
SEARCH FOR A NAME OF THE COUNTRY INPUT BY THE USER. IF
FOUND, DISPLAY THE NAME OF THE COUNTRY ALONG WITH ITS
WONDER, OTHERWISE DISPLAY “SORRY NOT FOUND!”
SEVEN WONDERS – CHICHEN ITZA, CHRIST THE REDEEMER,
TAJMAHAL, GREAT WALL OF CHINA, MACHU PICCHU, PETRA,
COLOSSEUM
LOCATIONS – MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
EXAMPLE – COUNTRY NAME: INDIA OUTPUT: INDIA – TAJMAHAL
COUNTRY NAME: USA
OUTPUT: SORRY NOT FOUND!
Answer 2016
import java.util.Scanner;

public class program9


{
public static void main(String[] args)
{
int locationPosition=0,i=0;
String country="";
String[] wonders = { "CHICHEN ITZA", "CHRIST THE
REDEEMER", "TAJMAHAL", "GREAT WALL OF CHINA","MACHU PICCHU",
"PETRA","COLOSSEUM" };
String[] locations = { "MEXICO", "BRAZIL", "INDIA",
"CHINA", "PERU", "JORDAN", "ITALY" };
Scanner sc = new Scanner(System.in);
System.out.print("Enter the country to be searched: ");
country = sc.next();

/* Search country in the array using linear search*/

locationPosition = -1;
for (i = 0; i < locations.length; i++)
{
if (locations[i].equals(country)==true)
{
locationPosition = i;
break;
}
}
if (locationPosition != -1)
{
System.out.println(locations[locationPosition] + " -
" + wonders[locationPosition]);
2016

}
else
{
System.out.println("Sorry location Not Found!");
}
}
}
Question 4 2015
DEFINE A CLASS PARKINGLOT WITH THE FOLLOWING DESCRIPTION:
INSTANCE VARIABLES/DATA MEMBERS:
INT VNO – TO STORE THE VEHICLE NUMBER
INT HOURS – TO STORE THE NUMBER OF HOURS THE VEHICLE IS
PARKED IN THE PARKING LOT
DOUBLE BILL – TO STORE THE BILL AMOUNT
MEMBER METHODS:
VOID INPUT() – TO INPUT AND STORE VNO AND HOURS
VOID CALCULATE() – TO COMPUTE THE PARKING CHARGE AT THE
RATE OF RS.3 FOR THE FIRST HOUR OR PART THEREOF, AND RS.1.50
FOR EACH ADDITIONAL HOUR OR PART THEREOF.
VOID DISPLAY() – TO DISPLAY THE DETAIL
WRITE A MAIN METHOD TO CREATE AN OBJECT OF THE CLASS AND
CALL THE ABOVE METHODS
Answer 2015
import java.util.Scanner;
public class ParkingLot
{
int vno;
int hours;
double bill;
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter vehicle number: ");
vno = sc.nextInt();
System.out.print("Enter hours: ");
hours = sc.nextInt();
}
public void calculate()
{
bill = 3 + (hours - 1) * 1.50;
}
public void display()
{
System.out.println("Vehicle number: " + vno);
System.out.println("Hours: " + hours);
System.out.println("Bill: Rs. " + bill);
}
public static void main(String[] args)
{
ParkingLot ob1 = new ParkingLot();
ob1.input();
ob1.calculate();
ob1.display();
}
}
Question 5 2015
WRITE TWO SEPARATE PROGRAMS TO GENERATE THE FOLLOWING
PATTERNS USING ITERATION(LOOP) STATEMENTS:
(A)
*
*#
*#*
*#*#
*#*#*
(B)
54321
5432
543
54
5
Answer 2015
import java.util.Scanner;

public class Program5A


{
public static void main(String[] args)
{
int no=0,i=0,j=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
no = sc.nextInt();
for (i = 1; i <= no; i++)
{
for (j = 1; j <= i; j++)
{
/* if j is even then print '#' else print '*' */
if (j % 2 == 0)
{
System.out.print("# ");
}
else
{
System.out.print("* ");
}
}
System.out.println();
}
}
}
2015
import java.util.Scanner;

public class Program5b


{
public static void main(String[] args)
{
int no=0,temp=0,i=0,j=0;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number: ");
no = scanner.nextInt();
for (i = no; i >= 1; i--)
{
temp = no;
for (j = 1; j <= i; j++)
{
System.out.print(temp + " ");
temp--;
}
System.out.println();
}
}
}
Question 6 2015
WRITE A PROGRAM IN TO INPUT AND STORE ALL ROLL NUMBERS,
NAMES AND MARKS IN 3 SUBJECTS OF N NUMBER OF STUDENTS IN
FIVE SINGLE DIMENSIONAL ARRAYS AND DISPLAY THE REMARK
BASED ON AVERAGE MARKS AS GIVEN BELOW:
AVERAGE MARKS = TOTAL MARKS/3
AVERAGE MARKS REMARK
85 – 100: EXCELLENT
75 – 84 : DISTINCTION
60 – 74 : FIRST CLASS
40 – 59 : PASS
LESS THAN 40: POOR
Answer 2015
import java.util.Scanner;

public class program6


{
public static void main(String[] args)
{
int i=0;
double average=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
int[] rollNos = new int[n];
String[] names = new String[n];
int[] subject1 = new int[n];
int[] subject2 = new int[n];
int[] subject3 = new int[n];

for (i = 0; i < n; i++)


{
System.out.println("Student " + (i + 1));
System.out.print("Enter roll number: ");
rollNos[i] = sc.nextInt();
System.out.print("Enter name: ");
sc.nextLine();
names[i] = sc.nextLine();
System.out.print("Enter marks in subject 1: ");
subject1[i] = sc.nextInt();
System.out.print("Enter marks in subject 2: ");
subject2[i] = sc.nextInt();
System.out.print("Enter marks in subject 3: ");
subject3[i] = sc.nextInt();
}
2015
for(i = 0; i < n; i++)
{
System.out.println( "Name : " + names[i]);
System.out.println( "Roll Number : " + rollNos[i]);
average= (subject1[i] + subject2[i] + subject3[i]) /
3;
if (average >= 85 && average <= 100)
{
System.out.println("EXCELLENT");
}
else if(average >= 75 && average <= 84)
{
System.out.println("DISTINCTION");
}
else if (average >= 60 && average <= 74)
{
System.out.println("FIRST CLASS");
}
else if (average >= 40 && average <= 59)
{
System.out.println("PASS");
}
else if (average < 40)
{
System.out.println("POOR");
}
}
}
}
Question 7 2015
DESIGN A CLASS TO OVERLOAD A FUNCTION JOYSTRING() AS
FOLLOWS:
(I) VOID JOYSTRING(STRING S, CHAR CH1, CHAR CH2) WITH ONE
STRING AND TWO CHARACTER ARGUMENTS THAT REPLACES THE
CHARACTER ARGUMENT CH1 WITH THE CHARACTER ARGUMENT CH2
IN THE GIVEN STRING S AND PRINTS THE NEW STRING
EXAMPLE:
INPUT VALUE OF S = “TECHNALAGY”
CH1 = ‘A’
CH2 = ‘O’
OUTPUT : “TECHNOLOGY”
(II) VOID JOYSTRING(STRING S) WITH ONE STRING ARGUMENT THAT
PRINTS THE POSITION OF THE FIRST SPACE AND THE LAST SPACE
OF THE GIVEN STRING S.
EXAMPLE:
INPUT VALUE OF = “CLOUD COMPUTING MEANS INTERNET BASED
COMPUTING”
FIRST INDEX : 5
LAST INDEX : 36
(III) VOID JOYSTRING(STRING S1, STRING S2) WITH TWO STRING
ARGUMENTS THAT COMBINES THE TWO STRINGS WITH A SPACE
BETWEEN THEM AND PRINTS THE RESULTANT STRING
EXAMPLE :
INPUT VALUE OF S1 = “COMMON WEALTH”
S2 = “GAMES”
OUTPUT : “COMMON WEALTH GAMES”
(USE LIBRARY FUNCTIONS)
Answer 2015
public class program7
{
public void Joystring(String s, char ch1, char ch2)
{
String output = s.replace(ch1, ch2);
System.out.println("Output = " + output);
}
public void Joystring(String s)
{
int firstIndexOfSpace = s.indexOf(' ');
int lastIndexOfSpace = s.lastIndexOf(' ');
System.out.println("First index of space = " +
firstIndexOfSpace);
System.out.println("Last index of space = " +
lastIndexOfSpace);
}
public void Joystring(String s1, String s2)
{
String output = s1.concat(" ").concat(s2);
System.out.println("Output = " + output);
}

}
Question 8 2015
WRITE A PROGRAM TO INPUT TWENTY NAMES IN AN ARRAY.
ARRANGE THESE NAMES IN DESCENDING ORDER OF ALPHABETS ,
USING THE BUBBLE SORT TECHNIQUE.
Answer 2015
import java.util.Scanner;

public class program8


{
public static void main(String[] args)
{
int i=0,j=0;
String temp="";
Scanner sc = new Scanner(System.in);
String[] names = new String[20];

/* Input names */
System.out.println("Enter 20 names");
for (i = 0; i < 20; i++)
{
names[i] = sc.nextLine();
}

/* Sort names using bubble sort technique */


for (i = 0; i < names.length-1; i++)
{
for (j = 0; j < (names.length - i-1); j++)
{
if (names[j].compareTo(names[j+1]) < 0)
{
temp = names[j];
names[j] = names[j+1];
names[j+1] = temp;
}
}
}
2015

/* Print sorted names */


System.out.println("Sorted names: ");
for (i = 0; i < names.length; i++)
{
System.out.print(names[i]+"");
}
}
}
Question 9 2015
Use switch statement,write a menu driven program to:
(i) To find and display all the factors of a number input by the user
(including 1 and excluding number itself.)
Example :
Sample Input : n = 15.
Sample Output : 1, 3, 5
(ii) To find and display the factorial of a number input by the user. The
factorial of a non-negative integer n ,denoted by n!,is the product of all
integers less than or equal to n.
Example :
Sample Input : n = 5
Sample Output : 120.
For an incorrect choice, an appropriate error message should be
displayed.
Answer 2015
import java.util.Scanner;

public class program9


{
public static void main(String[] args)
{
int i=0,n=0,factorial=0;
Scanner sc = new Scanner(System.in);
System.out.println("press 1 for Factors");
System.out.println("press 2 for Factorial");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter a number: ");
n = sc.nextInt();
for (i = 1; i < n; i++)
{
if (n % i == 0)
{
System.out.println(i);
}
}
break;
2015

case 2:
System.out.print("Enter a number: ");
n = sc.nextInt();
factorial = 1;
for (i = 1; i <= n; i++)
{
factorial = factorial * i;
}
System.out.println("Factorial: " +
factorial);
break;

default:
System.out.println("Invalid choice");
break;
}
}
}
Question 4 2014
Define a class named movieMagic with the following description:
Instance variables/data members:
int year – to store the year of release of a movie
String title – to store the title of the movie.
float rating – to store the popularity rating of the movie. (minimum
rating = 0.0 and maximum rating = 5.0)
Member Methods:
(i) movieMagic() Default constructor to initialize numeric data
members to 0 and String data member to “”.
(ii) void accept() To input and store year, title and rating.
(iii) void display() To display the title of a movie and a message based
on the rating as per the table below. RATING MESSAGE TO BE
DISPLAYED
0.0 to 2.0: Flop
2.1 to 3.4: Semi-hit
3.5 to 4.5 :Hit
4.6 to 5.0: Super Hit
Write a main method to create an object of the class and call the
above member methods.
Answer 2014
import java.util.Scanner;

public class movieMagic


{
int year;
String title;
float rating;
public movieMagic()
{
year = 0;
title = "";
rating = 0;
}
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter title: ");
title = sc.nextLine();
System.out.print("Enter year: ");
year = sc.nextInt();
System.out.print("Enter rating: ");
rating = sc.nextFloat();
}
public void display()
{
System.out.println(title);
if(rating >= 0 && rating <= 2.0)
{
System.out.println("Flop");
}
else if(rating >= 2.1 && rating <= 3.4)
{
System.out.println("Semi-hit");
2014

}
else if (rating >= 3.5 && rating <= 4.5)
{
System.out.println("Hit");
}
else if (rating >= 4.6 && rating <= 5.0)
{
System.out.println("Super Hit");
}
else
{
System.out.println("Invalid Rating Entered");
}
}
public static void main(String[] args)
{
movieMagic ob1 = new movieMagic();
ob1.accept();
ob1.display();
}
}
Question 5 2014
A special two-digit number is such that when the sum of its digits is
added to the product of its digits, the result is equal to the original
two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits= 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits
to the product of its digits. If the value is equal to the number input,
output the message “Special 2-digit number” otherwise, output the
message “Not a Special 2-digit number”.
Answer 2014
import java.util.Scanner;

public class program5


{
public static void main(String[] args)
{
int temp=0,sum=0,product=1,num=0,rem=0,finalSum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
num = sc.nextInt();
temp=num;

while(temp>0)
{
rem=temp%10;
sum=sum+rem;
product=product*rem;
temp/=10;
}

finalSum = sum + product;

if (finalSum == num)
{
System.out.println(num+" is Special 2-digit
number");
}
else
{
System.out.println(num+" is Not a Special 2-digit
number");
}
2014
}
}
Question 6 2014
Write a program to assign a full path and file name as given below.
Using library functions, extract and output the file path, file name and
file extension separately as shown.
Input C:\Users\admin\Pictures\flower.jpg
Output Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg
Answer 2014
import java.util.Scanner;

public class program6


{
public static void main(String[] args)
{
int LastBackslashPos=0,DotPos=0;
String Path="",file="",extension="",fullPath="";
Scanner sc = new Scanner(System.in);
System.out.print("Enter file name and path: ");
fullPath = sc.nextLine();
LastBackslashPos = fullPath.lastIndexOf('\\');
DotPos = fullPath.lastIndexOf('.');
Path = fullPath.substring(0, LastBackslashPos + 1);
file = fullPath.substring(LastBackslashPos + 1, DotPos);
extension = fullPath.substring(DotPos + 1);
System.out.println("Path: " + Path);
System.out.println("File Name: " + file);
System.out.println("Extension: " + extension);
}

}
Question 7 2014
Design a class to overload a function area() as follows:
(i) double area(double a. double b, double e) with three double
arguments, returns the area of a scalene triangle.
area = √(s(s – a)(s – b)(s – c))
where s = (a + b + c) / 2.
(ii) double area(int a, int b, int height) with three integer arguments,
returns the area of a trapezium
area = 1/2 × height × (a + b)
(iii) double area(double diagonal1, double diagonal2) with two double
arguments, returns the area of a rhombus
area = 1/2 × (diagonal1 × diagonal2)
Answer 2014
public class Area
{
public double area(double a, double b, double c)
{
double s = (a + b + c) / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
public double area(int a, int b, int height)
{
double area = 0.5 * height * (a + b);
return area;
}
public double area(double diagonal1, double diagonal2)
{
double area = 0.5 * (diagonal1 * diagonal2);
return area;
}
}
Question 8 2014
Using the switch statement. write a menu driven program to calculate
the maturity amount of a Bank Deposit.
The user is given the following options:
(i) Term Deposit
(ii) Recurring Deposit
For option (i) accept principal(P), rate of interest(r) and time period in
years(n). Calculate and output the maturity amount(A) receivable
using the Formula:
P(1+r/100)n
For option (ii) accept Monthly Installment (P), rate of interest(r) and
time period in months (n). Calculate and output the maturity
amount(A) receivable using the formula
P*n+(P*(n*(n+1))/2)*(r/100)*(1/12)
For an incorrect option, an appropriate error message should be
displayed.
Answer 2014
import java.util.Scanner;

public class program8


{
public static void main(String[] args)
{
int choice=0;
double maturityAmount=0;
Scanner sc = new Scanner(System.in);
System.out.println("Press 1 for Term Deposit");
System.out.println("Press 2 for Recurring Deposit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter principal: ");
double P1 = sc.nextDouble();
System.out.print("Enter rate of interest: ");
double r1 = sc.nextDouble();
System.out.print("Enter period in years: ");
double n1 = sc.nextDouble();
/* maturity amount = p*((1+r1)/100)^n1 */
maturityAmount = P1 * Math.pow(1 + r1 / 100.0, n1);
System.out.println("Maturity Amount is " + maturityAmount);
break;
case 2:
System.out.print("Enter monthly installment: ");
double P2 = sc.nextDouble();
System.out.print("Enter rate of interest: ");
double r2 = sc.nextDouble();
System.out.print("Enter period in months: ");
double n2 = sc.nextDouble();
2014

/* Maturity amount=p2 *n2 +p2*(n2*(n2+1)/2)*(r2/100)*(1/12) */


maturityAmount = P2 * n2 + P2 * (n2 * (n2 + 1) / 2.0) * (r2 / 100.0) * (1.0
/ 12);
System.out.println("Maturity Amount is " + maturityAmount);
break;
default:
System.out.println("Invalid choice");
}
}
}
Question 9 2014
Write a program to accept the year of graduation from school as an
integer value from the user. Using the Binary Search technique on the
sorted array of integers given below, output the message ‘Record
exists’ if the value input is located in the array. If not, output the
message Record does not exist”.
(1982, 1987, 1993. 1996, 1999, 2003, 2006, 2007, 2009, 2010)
Answer 2014
import java.util.Scanner;

public class program9


{
public static void main(String[] args)
{
int left=0,right=0,mid=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter year of graduation: ");
int graduation = sc.nextInt();
int[] years = {1982, 1987, 1993, 1996, 1999, 2003, 2006,
2007, 2009, 2010};
boolean found = false;
left = 0;
right = years.length - 1;
while (left <= right)
{
mid = (left + right) / 2;
if (years[mid] == graduation)
{
found = true;
break;
}
else if(years[mid] < graduation)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
2014

if (found==true)
{
System.out.println(graduation+" Record exists");
}
else
{
System.out.println(graduation+" Record does not
exist");
}
}
}
Question 4 2013
Define a class called FruitJuice with the following description:
Instance variables/data members:
int product_code – stores the product code number
String flavour – stores the flavour of the juice.(orange, apple, etc)
String pack_type – stores the type of packaging (tetra-pack, bottle etc)
int pack_size – stores package size (200ml, 400ml etc)
int product_price – stores the price of the product
Member Methods:
FruitJuice() – default constructor to initialize integer data members
to zero and string data members to “”.
void input() – to input and store the product code, flavor, pack type,
pack size and product price.
void discount() – to reduce the product price by 10.
void display() – to display the product code, flavor, pack type,
pack size and product price.
Answer 2013
import java.util.Scanner;

public class FruitJuice


{
int product_code;
String flavour;
String pack_type;
int pack_size;
int product_price;
public FruitJuice()
{
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter product code: ");
product_code = sc.nextInt();
System.out.println("Enter flavour: ");
flavour = sc.next();
System.out.println("Enter pack type: ");
pack_type = sc.next();
System.out.println("Enter pack size: ");
pack_size = sc.nextInt();
System.out.println("Enter product price: ");
product_price = sc.nextInt();
}
2013

public void discount()


{
product_price = product_price-10;
}
public void display()
{
System.out.println("Product Code: " + product_code);
System.out.println("Flavour: " + flavour);
System.out.println("Pack Type: " + pack_type);
System.out.println("Pack Size: " + pack_size);
System.out.println("Product Price: " + product_price);
}
}
Question 5 2013
The International Standard Book Number (ISBN) is a unique numeric
book identifier which is printed on every book. The ISBN is based upon
a 10-digit code. The ISBN is legal if:
1xdigit1 + 2xdigit2 + 3xdigit3 + 4xdigit4 + 5xdigit5 + 6xdigit6 +
7xdigit7 + 8xdigit8 + 9xdigit9 + 10xdigit10 is divisible by 11.
Example: For an ISBN 1401601499
Sum=1×1 + 2×4 + 3×0 + 4×1 + 5×6 + 6×0 + 7×1 + 8×4 + 9×9 +
10×9 = 253 which is divisible by 11.
Write a program to:
(i) input the ISBN code as a 10-digit integer.
(ii) If the ISBN is not a 10-digit integer, output the message “Illegal
ISBN” and terminate the program.
(iii) If the number is 10-digit, extract the digits of the number and
compute the sum as explained above.
If the sum is divisible by 11, output the message, “Legal ISBN”. If the
sum is not divisible by 11, output the message, “Illegal ISBN”.
Answer 2013
import java.util.Scanner;

public class program5


{
public static void main(String[] args)
{
int i=0,sum=0,digit=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter ISBN code: ");
long isbnLong = sc.nextLong();
String isbn = Long.toString(isbnLong);
if (isbn.length() != 10)
{
System.out.println("Ilegal ISBN");
}
else
{
sum = 0;
for (i = 0; i < 10; i++)
{
digit =
Integer.parseInt(Character.toString(isbn.charAt(i)));
sum = sum + (digit * (i + 1));
}
if (sum % 11 == 0)
{
System.out.println(isbn+" is a Legal
ISBN");
}
2013
else
{
System.out.println(isbn+" is not a legal
ISBN");
}
}
}
}
Question 6 2013
Write a program that encodes a word into Piglatin. To translate word
into Piglatin word, convert the word into uppercase and then place the
first vowel of the original word as the start of the new word along with
the remaining alphabets. The alphabets present before the vowel being
shifted towards the end followed by “AY”.
Sample Input(1): London Sample Output(1): ONDONLAY
Sample Input(2): Olympics Sample Output(2): OLYMPICSAY
Answer 2013
import java.util.Scanner;

public class program6


{
public static void main(String[] args)
{
int i=0;
char ch=' ';
String wd="",piglatin="";
boolean vowelFound=false;

Scanner sc = new Scanner(System.in);


System.out.print("Enter a String: ");
wd = sc.next();
wd = wd.toUpperCase();
vowelFound = false;
for (i = 0; i < wd.length(); i++)
{
ch = wd.charAt(i);
if ((ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'))
{
vowelFound=true;
piglatin =wd.substring(i);
piglatin=piglatin+wd.substring(0,i);
break;
}

}
if(vowelFound==false)
{
piglatin=wd;
}
piglatin = piglatin + "AY";
System.out.println("Piglatin word is " + piglatin);
}
}
Question 7 2013
Write a program to input 10 integer elements in an array and sort them
In descending order using bubble sort technique.
Answer 2013
import java.util.Scanner;
public class program7
{
public static void main(String[] args)
{
int i=0,j=0,temp=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter ten numbers:");
int[] numbers = new int[10];
for (i = 0; i < 10; i++)
{
numbers[i] = sc.nextInt();
}
for (i = 0; i < 10-1; i++)
{
for (j = 0; j < 10 - i - 1; j++)
{
if (numbers[j] < numbers[j + 1])
{
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
System.out.println("Sorted Numbers:");
for (i = 0; i < 10; i++)
{
System.out.print(numbers[i]+" ");
}
}
}
Question 8 2013
Design a class to overload a function series() as follows:
(i) double series(double n) with one double argument and returns the
sum of the series.
sum = 1/1 + 1/2 + 1/3 + ….. 1/n
(ii) double series(double a, double n) with two double arguments and
returns the sum of the series.
sum = 1/a2 + 4/a5 + 7/a8 + 10/a11 ….. to n terms
Answer 2013
public class Overload
{
public double series(double n)
{
double s= 0;
for (int i = 1; i <= n; i++)
{
s = s + (1.0 / i);
}
return s;
}

public double series(double a, double n)


{
double s = 0;
for (int i = 0; i < n; i++)
{
s = s + ((3 * i + 1.0) / Math.pow(a, 3 * i + 2));
}
return s;
}
}
Question 9 2013
Using the switch statement, write a menu driven program:
(i) To check and display whether a number input by the user is
a composite number or not (A number is said to be a composite, if it
has one or more then one factors excluding 1 and the number itself).
Example: 4, 6, 8, 9…
(ii) To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be
displayed.
Answer 2013
import java.util.Scanner;

public class program9


{

public static void main(String[] args)


{
int number=0;
Scanner sc = new Scanner(System.in);

System.out.println("Press 1 toCheck composite number");


System.out.println("Press 2 toFind smallest digit of a number");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter a number: ");
number = sc.nextInt();
int i=0;
boolean IsComposite=false;
for (i = 2; i < number; i++)
{
if (number % i == 0)
{

IsComposite=true;
break;
}
}

if(IsComposite==true)
{
System.out.println("It is a composite number");
}
2013

else
{
System.out.println("It is not a composite number");
}
break;
case 2:
System.out.print("Enter a number: ");
number = sc.nextInt();
int smallest = 9,rem=0;
while (number > 0)
{
rem = number % 10;
if (rem < smallest)
{
smallest = rem;
}
number = number / 10;
}

System.out.println("Smallest digit is " + smallest);


break;
default:
System.out.println("Incorrect choice");
break;
}
}
}
Question 4 2012
Define a class called Library with the following description:
Instance variables/data members:
Int acc_num – stores the accession number of the book
String title – stores the title of the book
String author – stores the name of the author
Member Methods:
(i) void input() – To input and store the accession number, title and
author.
(ii)void compute() – To accept the number of days late, calculate and
display the fine charged at the rate of Rs.2 per day.
(iii) void display() To display the details in the following format:
Accession Number Title Author.
Write a main method to create an object of the class and call the above
member methods.
Answer 2012
import java.util.Scanner;

public class Library


{
int acc_num;
String title;
String author;
Scanner sc=new Scanner(System.in);
public Library()
{
acc_num=0;
title="";
author="";
}

public void input()


{
System.out.print("Enter accession number: ");
acc_num =sc.nextInt();
sc.nextLine();
System.out.print("Enter title: ");
title = sc.nextLine();
System.out.print("Enter author: ");
author = sc.nextLine();

public void compute()


{
System.out.print("Enter number of days late: ");
int late = sc.nextInt();
int fine = 2 * late;
System.out.println("Fine is Rs " + fine);
}
2012

public static void main(String[] args)


{
Library ob1 = new Library();
ob1.input();
ob1.compute();
ob1.display();
}
}
Question 5 2012
Given below is a hypothetical table showing rates of Income Tax for
male citizens below the age of 65 years:
Taxable Income (TI) Income Tax
< 1,60,000 Nil
>1,60,000 and <= 5,00,000 ( TI – 1,60,000 ) * 10%
> 5,00,000 and <= 8,00,000 [(TI – 5,00,000 ) *20%] + 34,000
> 8,00,000 [(TI – 8,00,000 ) *30% ] + 94,000

Write a program to input the age, gender (male or female) and Taxable
Income of a person.If the age is more than 65 years or the gender is
female, display “wrong category". If the age is less than or equal to 65
years and the gender is male, compute and display the Income Tax
payable as per the table given above.
Answer 2012
import java.util.Scanner;

public class program5


{
public static void main(String[] args)
{
int age=0;
double tax=0,income=0;
String gender="";
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
age = sc.nextInt();
System.out.print("Enter gender: ");
gender = sc.next();
System.out.print("Enter taxable income: ");
income = sc.nextDouble();
if (age > 65 || gender.equals("female"))
{
System.out.println("Wrong category");
}
else
{

if (income <= 160000)


{
tax = 0;
}
else if(income > 160000 && income <= 500000)
{
tax = (income - 160000) * 10 / 100;
}
else if(income > 500000 && income <= 800000)
{
tax = (income - 500000) * 20 / 100 + 34000;
}
else
{
tax = (income - 800000) * 30 / 100 + 94000;
}
2012
System.out.println("Income tax is " + tax);
}
}
}
Question 6 2012
Write a program to accept a string. Convert the string to uppercase.
Count and output the number of double letter sequences that exist in
the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN
APPLE"
Sample Output: 4
Answer 2012
import java.util.Scanner;
public class program6
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String: ");
String sen = sc.nextLine();
sen = sen.toUpperCase();
int count = 0;
for (int i = 1; i < sen.length(); i++)
{
if (sen.charAt(i) == sen.charAt(i - 1))
{
count++;
}
}
System.out.println(count);
}
}
Question 7 2012
Design a class to overload a function polygon() as follows:
(i) void polygon(int n, char ch) : with one integer argument and one
character type argument that draws a filled square of side n using the
character stored in ch.
(ii) void polygon(int x, int y) : with two integer arguments that draws a
filled rectangle of length x and breadth y, using the symbol ‘@’
(iii)void polygon( ) : with no argument that draws a filled triangle
shown below.

Example:
(i) Input value of n=2, ch=’O’
Output:
OO
OO
(ii) Input value of x=2, y=5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
***
Answer 2012
public class program7
{
public void polygon(int n, char ch)
{
int i=0,j=0;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
System.out.print(ch);
}
System.out.println();
}
}
public void polygon(int x, int y)
{
int i=0,j=0;
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
System.out.print("@");
}
System.out.println();
}
}
public void polygon()
{
int i=0,j=0;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Question 8 2012
Using the switch statement, write a menu driven program to:
(i) Generate and display the first 10 terms of the Fibonacci series
0,1,1,2,3,5….The first two Fibonacci numbers are 0 and 1, and each
subsequent number is the sum of the previous two.
(ii)Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits=18
For an incorrect choice, an appropriate error message should be
displayed
Answer 2012
import java.util.Scanner;

public class program8


{
public static void main(String[] args)
{
int choice=0;
Scanner sc = new Scanner(System.in);

System.out.println("press 1 for Fibonacci Sequence");


System.out.println("press 2 for Sum of Digits");
System.out.print("Enter choice: ");
choice =sc.nextInt();
switch (choice)
{
case 1:
int a = 0;
int b = 1;
System.out.print("0 1 ");
for (int i = 3; i <= 10; i++)
{
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
break;
case 2:
System.out.print("Enter a number: ");
int num = sc.nextInt();
int sum = 0;
while (num > 0)
{
int rem = num % 10;
sum = sum + rem;
num = num / 10;
}
System.out.println("Sum of digits is " + sum);
break;
2012

default:
System.out.println("Invalid Choice");
}
}
}
Question 9 2012
Write a program to accept the names of 10 cities in a single dimension
string array and their STD (Subscribers Trunk Dialing) codes in another
single dimension integer array. Search for a name of a city input by the
user in the list. If found, display “Search Successful” and print the
name of the city along with its STD code, or else display the message
“Search Unsuccessful, No such city in the list’.
Answer 2012
import java.util.Scanner;

public class program9


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] city = new String[10];
int[] std = new int[10];
int i=0;
System.out.print("Enter city: ");

for ( i = 0; i < 10; i++)


{
city[i] =sc.next();

}
System.out.print("Enter STD: ");
for (i = 0; i < 10; i++)
{

std[i] =sc.nextInt();

System.out.print("Enter city name to search: ");


String target = sc.next();
boolean flag = false;
for (i = 0; i < 10; i++)
{
if (city[i].equals(target)==true)
{
System.out.println("Search successful");
System.out.println("City : " + city[i]);
System.out.println("STD code : " + std[i]);
flag = true;
break;
}
}
2012
if(flag==false)
{
System.out.println("Search Unsuccessful, No such city in the
list");
}
}
}
Question 4 2011
Define a class called mobike with the following description:
Instance variables/data members:
int bno – to store the bike’s number
int phno – to store the phone number of the customer
String name – to store the name of the customer
int days – to store the number of days the bike is taken on rent
int charge – to calculate and store the rental charge
Member methods:
void input( ) – to input and store the detail of the customer.
void compute( ) – to compute the rental charge
The rent for a mobike is charged on the following basis.
First five days :Rs 500 per day
Next five days: Rs 400 per day
Rest of the days: Rs 200 per day
void display ( ) – to display the details in the following format:
Bike No.| PhoneNo.| No. of days |Charge
Answer 2011
import java.util.Scanner;
public class mobike
{
int bno;
int phno;
String name;
int days;
int charge;

public void input()


{
Scanner sc = new Scanner(System.in);
System.out.print("Enter bike number: ");
bno = sc.nextInt();
System.out.print("Enter phone number: ");
phno = sc.nextInt();
System.out.print("Enter your name: ");
name = sc.next();
System.out.print("Enter number of days: ");
days = sc.nextInt();
}

public void compute()


{
if (days <= 5)
{
charge = 500 * days;
}
else if (days >5 && days <=10)
{
charge = 5 * 500 + (days - 5) * 400;
}
else
{
charge = 5 * 500 + 5 * 400 + (days - 10) * 200;
}
}
2011

public void display()


{
System.out.println("Bike No. \tPhone No. \t No. of Days \t Charge");
System.out.println(bno + "\t" + phno + "\t" + days + "\t" + charge);
}
}
Question 5 2011
Write a program to input and sort the weight of ten people. Sort and
display them in descending order using the selection sort technique
Answer 2011
import java.util.Scanner;

public class program5


{
public static void main(String[]args)
{
int i=0,j=0,temp=0;
Scanner sc = new Scanner(System.in);
int[] weights = new int[10];
System.out.println("Enter weights: ");
for (i = 0; i < 10; i++)
{
weights[i] = sc.nextInt();
}
for (i = 0; i < 10; i++)
{

for (j = i+1; j < 10; j++)


{
if (weights[i] < weights[j])
{
temp = weights[i];
weights[i] = weights[j];
weights[j] = temp;
}
}

}
System.out.println("Sorted weights:");
for (i = 0; i < 10; i++)
{
System.out.println(weights[i]);
}
}
}
Question 6 2011
Write a program to input a number and print whether the number is a
special number or not. (A number is said to be a special number, if the
sum of the factorial of the digits of the number is same as the original
number).
Answer 2011
import java.util.Scanner;
public class program6
{
public static void main(String args[])
{
int num=0, temp=0, digit=0, sum = 0 , fact=0,i=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be checked.");
num = sc.nextInt();
temp = num;

while(temp!=0)
{
digit = temp%10;
fact=1;
for(i = 1; i<=digit; i++)
{
fact=fact*i;
}
sum = sum + fact;
temp = temp/10;
}
if(sum==num)
{
System.out.println(num+" is a Special Number.");
}
else
{
System.out.println(num+" is not a Special Number.");
}
}
}
Question 7 2011
Write a program to accept a word and convert it into lowercase if it is
in uppercase, and display the new word by replacing only the vowels
with the character following it.
Example:
Sample Input : computer
Sample Output : cpmpvtfr
Answer 2011
import java.util.Scanner;

public class program7


{
public static void main(String[]args)
{
int i=0,len=0;
char ch=' ';
String wd="";
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String: ");
wd = sc.next();
wd= wd.toLowerCase();
String wd1 = "";
len=wd.length();
for (i = 0; i < len; i++)
{
ch = wd.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
wd1 =wd1 + (char) (ch + 1);
}
else
{
wd1 =wd1 + ch ;
}
}
System.out.println(wd1);
}

}
Question 8 2011
Design a class to overload a function compare ( ) as follows:
(a) void compare (int, int) – to compare two integer values and print
the greater of the two integers.
(b) void compare (char, char) – to compare the numeric value of two
character with higher numeric value
(c) void compare (String, String) – to compare the length of the two
strings and print the longer of the two.
Answer 2011
public class program8
{

public void compare(int a, int b)


{
int max = Math.max(a, b);
System.out.println(max);
}

public void compare(char a, char b)


{
char max = (char) Math.max(a, b);
System.out.println(max);
}

public void compare(String a, String b)


{
int max=Math.max(a.length(),b.length());

if (max == a.length())
{
System.out.println(a+" string has greater length than "+b);
}
else
{
System.out.println(b+" string has greater length than "+a);
}

}
}
Question 9 2011
Write a menu driven program to perform the following . (Use switch-
case statement)
(a) To print the series 0, 3, 8, 15, 24 ……. n terms (value of ‘n’ is to be
an input by the user).
(b) To find the sum of the series given below:
S = 1/2+ 3/4 + 5/6 + 7/8 … 19/20
Answer 2011
import java.util.Scanner;

public class program9


{
public static void main(String[] args)
{
int choice=0,n=0,i=0,term=0;
double sum=0,d=2;
Scanner sc = new Scanner(System.in);
System.out.println("enter 1 To Print 0, 3, 8, 15, 24... n tersm");
System.out.println("enter 2 to print Sum of series 1/4 + 3/4 + 7/8 + ... n terms");
System.out.print("Enter your choice: ");
choice = sc.nextInt();

switch(choice)
{
case 1:
System.out.print("Enter no. of terms: ");
n = sc.nextInt();
for (i = 1; i <= n; i++)
{
term = i * i - 1;
System.out.print(term + " ");
}
break;

case 2:
d=2;
for (i = 1; i <= 19; i=i+2)
{
sum = sum +i /d;
d=d+2;
}
System.out.println(sum);
break;
default:
System.out.println("Invalid choice");
}
}
}
Question 4 2010
Write a program to perform binary search on a list of integers given
below, to search for an element input by the user. If it is found display
the element along with its position, otherwise display the message
“Search element not found”.
5,7,9,11,15,20,30,45,89,97
Answer 2010
import java.util.Scanner;
public class BinarySearch
{
public static void main(String[]args)
{
int size=0,lower=0,upper=0,M=0;
boolean flag=false;
Scanner sc=new Scanner(System.in);
int arr[]={5,7,9,11,15,20,30,45,89,97};
size=arr.length;
System.out.println("Elements given array:");
for(int i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
upper=arr.length-1;
System.out.println("Enter the elements to be searched");
int s=sc.nextInt();
while(lower<=upper)
{
M=(lower+upper)/2;
if(s==arr[M])
{
System.out.println("Element is available at index "+(M+1));
flag=true;
break;
}
else if(s>arr[M])
{
lower=M+1;
}
else
{
upper=M-1;
}
}
if(flag==false)
{
2010

System.out.println("Search element not found");

}
}
}
Question 5 2010
Define a class Student described as below:
Data members/instance variables : name,age,m1,m2,m3 (marks in 3
subjects), maximum, average
Member methods :
(i) A parameterized constructor to initialize the data members.
(ii) To accept the details of a student.
(iii) To compute the average and the maximum out of three marks.
(iv) To display the name, age, marks in three subjects, maximum and
average.
Write a main method to create an object of a class and call the above
member methods.
Answer 2010
import java.util.Scanner;
public class Student
{
String name;
int m1, m2, m3,age,maximum;
double average;

public Student(String n, int a, int marks1, int marks2, int marks3, int max, double avg)
{
name = n;
age = a;
m1 = marks1;
m2 = marks2;
m3 = marks3;
maximum = max;
average = avg;
}

/* accept() method to accept name,age and marks*/


public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
name = sc.nextLine();
System.out.print("Enter age: ");
age =sc.nextInt();
System.out.print("Enter marks of 1st subject: ");
m1 = sc.nextInt();
System.out.print("Enter marks of 2nd subject: ");
m2 = sc.nextInt();
System.out.print("Enter marks of 3rd subject: ");
m3 = sc.nextInt();
}

public void calculate()


{
average = (m1 + m2 + m3) / 3.0;
maximum = Math.max(m1, (Math.max(m2, m3)));
}
2010
public void display()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks1 " + m1);
System.out.println("Marks2 " + m2);
System.out.println("Marks3 " + m3);
System.out.println("Maximum: " + maximum);
System.out.println("Average: " + average);
}

public static void main(String[] args)


{
Student ob1 = new Student("",0,0,0,0,0,0);
ob1.accept();
ob1.calculate();
ob1.display();
}

}
Question 6 2010
Shasha Travels Pvt. Ltd. gives the following discount to its customers:
Ticket amount Discount
Above Rs 70000 – 18%
Rs 55001 to Rs 70000 – 16%
Rs 35001 to Rs 55000 – 12%
Rs 25001 to Rs 35000 – 10%
less than Rs 25001 – 2%
Write a program to input the name and ticket amount for the customer
and calculate the discount amount and net amount to be paid. Display
the output in the following format for each customer :
(Assume that there are 15 customers, first customer is given the serial
no (SlNo.) 1, next customer 2 … and so on.

SL.NO. Name Ticket charges Discount Net amount 1


- - - - -

(Assume that there are 15 customers, first customer is given the serial
no (SlNo.) 1, next customer 2 … and so on.
Answer 2010
import java.util.Scanner;
public class Program6
{
public static void main(String[] args)
{

String name[];
double amount[];
double discount=0,net=0;
int i=0;
Scanner sc = new Scanner(System.in);
name=new String[15];
amount=new double[15];
for(i=0;i<15;i++)
{
System.out.println("Customer:"+(i+1));

System.out.print("Enter name: ");


name[i] = sc.nextLine();
System.out.print("Enter ticket amount: ");
amount[i] = sc.nextDouble();
sc.nextLine();
}

System.out.println("Sl. No.\t Name \t Charges \t Discount \t Net Amount");

for(i=0;i<15;i++)
{
if(amount[i] > 70000)
{
discount = 0.18*amount[i];
}
else if(amount[i] >= 55001 && amount[i] <= 70000)
{
discount = 0.16*amount[i];

}
2010
else if(amount[i] >= 35001 && amount[i] <= 55000)
{
discount = 0.12*amount[i];
}
else if (amount[i] >= 25001 && amount[i] <= 35000)
{
discount = 0.10*amount[i];
}
else if (amount[i] <= 25000)
{
discount = 0.02*amount[i];
}

net = amount[i] - discount;


System.out.println((i+1) + "\t" + name[i] +"\t" + amount[i] + "\t" + discount + "\t" + net);
}

}
}
Question 7 2010
Write a menu driven program to accept a number and check and
display whether it is a Prime Number or not OR an Automorphic
Number or not. (Use switch-case statement).
(a) Prime number : A number is said to be a prime number if it is
divisible only by 1 and itself and not by any other number. Example :
3,5,7,11,13 etc.
(b) Automorphic number : An automorphic number is the number
which is contained in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is
present as the last two digits.
Answer 2010
import java.util.Scanner;
public class Program7
{
public static void main(String[]args)
{
int choice,n,i=0,count=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for Prime number");
System.out.println("Enter 2 for Automorphic number");
System.out.print("Enter your choice: ");
choice = sc.nextInt();

switch(choice)
{
case 1:
System.out.print("Enter a no. ");
n = sc.nextInt();

for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count == 2)
{
System.out.println(n+" is a Prime no.");
}
else
{
System.out.println(n+" is not a Prime no.");
}
break;
2010
case 2:
System.out.print("Enter a no. ");
n = sc.nextInt();
int temp,rem;
temp = n;
/*counting digits in number 'n'*/
while(temp>0)
{
temp/=10;
count++;
}
rem = (n*n) % (int)Math.pow(10,count);
if(rem==n)
{
System.out.println(n+" is an Automorphic no. ");
}
else
{
System.out.println(n+ " is Not an Automorphic no. ");
}
break;
default:
System.out.println("Invalid Choice");
}
}
}
Question 8 2010
Write a program to store 6 element in an array P, and 4 elements in an
array Q and produce a third array R, containing all elements of array P
and Q. Display the resultant array .
Answer 2010
import java.util.Scanner;
public class program8
{
public static void main(String[] args)
{
int i=0;
Scanner sc = new Scanner(System.in);
int[] p = new int[6];
int[] q = new int[4];
int[] r = new int[10];
System.out.print("Enter 6 elements in array P: ");
for (i = 0; i < 6; i++)
{
p[i] = sc.nextInt();
}
System.out.print("Enter 4 elements in array Q: ");
for (i = 0; i < 4; i++)
{
q[i] = sc.nextInt();
}
for (i = 0; i < 6; i++)
{
r[i] = p[i];
}
for (i = 0; i < 4; i++)
{
r[i + 6] = q[i];
}
System.out.print("Elements of array R: ");
for (i = 0; i < 10; i++)
{
System.out.println(r[i]);
}
}
}
Question 9 2010
Write a program to input a string in uppercase and print the frequency
of each character.
Answer 2010
import java.util.Scanner;
public class program9
{
public static void main(String[]args)
{
int len=0,count=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string ");
String sen=sc.nextLine();
sen=sen.toUpperCase();
len=sen.length();
System.out.println("Characters:Frequency");
for(char j='A';j<='Z';j++)
{
for(int i=0;i< len;i++)
{
if(sen.charAt(i)==j)
{
count++;
}
}
if(count>0)
{
System.out.println(j+" : "+count);
count=0;
}
}
}
}

You might also like