Icse Computer Programming Question Paper With Solution 2019-2010
Icse Computer Programming Question Paper With Solution 2019-2010
Icse Computer Programming Question Paper With Solution 2019-2010
programming
question paper
with solution
2019-2010
Question 4 2019
DESIGN A CLASS NAME SHOWROOM WITH THE FOLLOWING DESCRIPTION:
INSTANCE VARIABLES/DATA MEMBERS:
else if(cost>35000)
{
dis = cost * 20/100;
amount = cost - dis;
}
}
LETTER UNICODE
A 65
B 66
C 67
- -
- -
- -
Z 90
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
}
}
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.
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[].
}
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;
}
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 BookFair()
{
Bname = "";
price = 0.0;
}
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;
if(wd.equals(reverse)==true)
{
System.out.println(wd+" is a Palindrome 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;
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;
}
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;
/* Input names */
System.out.println("Enter 20 names");
for (i = 0; i < 20; i++)
{
names[i] = sc.nextLine();
}
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;
}
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;
while(temp>0)
{
rem=temp%10;
sum=sum+rem;
product=product*rem;
temp/=10;
}
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;
}
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;
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;
}
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;
}
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;
}
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;
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;
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;
}
System.out.print("Enter STD: ");
for (i = 0; i < 10; i++)
{
std[i] =sc.nextInt();
}
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;
}
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
{
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;
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
}
}
}
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;
}
}
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.
(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));
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];
}
}
}
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;
}
}
}
}