You are on page 1of 171

Computer Applications with Java & BlueJ Class X

2005 – Question 4. [15 Marks]


Write a class with name Employee and basic as its data member, to find the gross pay of an employee for the
following allowances and deduction. Use meaningful variables.
Dearness Allowance = 25% of Basic Pay
House Rent Allowance = 15% of Basic Pay
Provident Fund = 8.33% of Basic Pay
Net pay = Basic Pay + Dearness Allowance + House Rent Allowance
Gross Pay = Net Pay – Provident Fund.

// Program to find the gross pay of an employee...

import java.util.*;

class Employee
{
static double basic;

public static void main( String args[ ] )


{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter Basic Pay ");


basic = sc.nextDouble( );

double DA = basic * 0.25;


double HRA = basic * 0.15;
double PF = basic * 0.0833;
double Net_Pay = basic + DA + HRA;
double Gross_Pay = Net_Pay - PF;

System.out.println("Gross Pay : "+Gross_Pay);


}
}

OUTPUT 1:
Enter Basic Pay
10000
Gross Pay : 13167.0

OUTPUT 2:
Enter Basic Pay
15000
Gross Pay : 19750.5

PUPIL TREE SCHOOL, BALLARI. Page 1 2023-2024


Computer Applications with Java & BlueJ Class X

2005 – Question 5. (Solution 1) [15 Marks]


Write a program to input any given string to calculate the total number of characters and vowels present in the
string and also reverse the string.
Example: INPUT:
Enter string : SNOWY
OUTPUT:
Total number of characters : 05
Number of Vowels : 01
Reverse string : YWONS
/* Program to input any given string to calculate the total number of
characters and vowels present in the string and also reverse the string. */
import java.util.*;

class MyString
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a String ");


String s = sc.nextLine( );

int count = s.length( );


int vcount = 0;

String rev = "";


s = s.toUpperCase( );

for (int i=count-1; i>=0; i--)


{
char ch = s.charAt(i);

if ( ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')


vcount++;

rev = rev + ch;


}
System.out.println("The total number of characters : "+count);
System.out.println("The total number of vowels : "+vcount);
System.out.println("The reversed string : "+rev);
}
}
OUTPUT 1: OUTPUT 2:
Enter a String Enter a String
SNOWY Rainbow
The total number of characters : 5 The total number of characters : 7
The total number of vowels : 1 The total number of vowels : 3
The reversed string : YWONS The reversed string : WOBNIAR

PUPIL TREE SCHOOL, BALLARI. Page 2 2023-2024


Computer Applications with Java & BlueJ Class X

2005 – Question 5. (Solution 2) [15 Marks]

// Program to input any given string to calculate the total number of


// characters and vowels present in the string and also reverse the string.

import java.util.*;

class MyString2
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a String ");


String s = sc.nextLine( );

int count = s.length( );


int vcount = 0;

String rev = "";

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


{
char ch = s.charAt(i);
rev = ch + rev;

ch = Character.toLowerCase(ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


vcount++;
}

System.out.println("The total number of characters : "+count);


System.out.println("The total number of vowels : "+vcount);
System.out.println("The reversed string : "+rev);
}
}

OUTPUT:
Enter a String
TigEr
The total number of characters : 5
The total number of vowels : 2
The reversed string : rEgiT

PUPIL TREE SCHOOL, BALLARI. Page 3 2023-2024


Computer Applications with Java & BlueJ Class X

2005 – Question 6. (Solution 1) [15 Marks]


Write a program using a function called area( ) to compute the area of a:
(i) circle (𝜋 ∗ 𝑟 2 )where π = 3.14
(ii) square (side * side)
(iii) rectangle (length * breadth)
Display the menu to output the area as per User’s choice.

// Program to compute the area of circle, square and rectangle


// using function area( )

import java.util.*;

class FindArea
{

double area(double radius)


{ OR double area(double radius)
double PI = 3.14; {
double area = PI*radius*radius; return Math.PI*Math.pow(radius,2);
return area; }
}

int area(int side)


{
int area = side * side;
return area;
}

double area(double length, double breadth)


{
double area = length * breadth;
return area;
}

public static void main(S tring args[ ] )


{
Scanner sc = new Scanner(System.in);
FindArea obj = new FindArea( );
int choice;
do
{
System.out.println(" My Menu ");
System.out.println(" 1.Circle ");
System.out.println(" 2.Square ");
System.out.println(" 3.Rectangle ");

System.out.println(" Enter your choice to find area : ");


choice = sc.nextInt( );

PUPIL TREE SCHOOL, BALLARI. Page 4 2023-2024


Computer Applications with Java & BlueJ Class X

switch(choice)
{
case 1 : System.out.println(" Enter Radius ");
double r = sc.nextDouble( );
System.out.println(" Area of Circle = " + obj.area(r) );
break;

case 2 : System.out.println(" Enter Side ");


int s = sc.nextInt( );
System.out.println(" Area of Side = " + obj.area(s) );
break;

case 3 : System.out.println(" Enter Length and Breadth ");


double l = sc.nextDouble( );
double b = sc.nextDouble( );
System.out.println(" Area of Rectangle = " + obj.area(l,b) );
break;

default : System.out.println(" Wrong choice ! ");


}
}
while(choice < 4);
}}
OUTPUT:
My Menu
1.Circle
2.Square
3.Rectangle
Enter your choice to find area : 1
Enter Radius 5
Area of Circle = 78.5

My Menu
1.Circle
2.Square
3.Rectangle
Enter your choice to find area : 0
Wrong choice !

My Menu
1.Circle
2.Square
3.Rectangle
Enter your choice to find area : 3
Enter Length and Breadth
12
8
Area of Rectangle = 96.0

PUPIL TREE SCHOOL, BALLARI. Page 5 2023-2024


Computer Applications with Java & BlueJ Class X

2005 – Question 6. (Solution 2) [15 Marks]


// Program to compute the area of circle, square and rectangle
import java.util.*;
class FindArea2
{ public static double area(double r)
{
return 3.14*r*r;
}

public static int area(int s)


{
return s*s;
}

public static double area(double l, double b)


{
return l*b;
}

public static void main(String args[ ])


{
Scanner sc = new Scanner(System.in);
System.out.println(" My Menu ");
System.out.println(" 1.Circle ");
System.out.println(" 2.Square ");
System.out.println(" 3.Rectangle ");
System.out.println(" Enter your choice to find area : ");
int choice = sc.nextInt( );

switch(choice)
{
case 1 : System.out.println(" Enter Radius ");
double r = sc.nextDouble( );
System.out.println(" Area of Circle = " + area(r) );
break;

case 2 : System.out.println(" Enter Side ");


int s = sc.nextInt( );
System.out.println(" Area of Side = " + area(s) );
break;

case 3 : System.out.println(" Enter Length and Breadth ");


double l = sc.nextDouble( );
double b = sc.nextDouble( );
System.out.println(" Area of Breadth = " + area(l,b) );
}
}
}

PUPIL TREE SCHOOL, BALLARI. Page 6 2023-2024


Computer Applications with Java & BlueJ Class X

2005 – Question 7. [15 Marks]

Write a program to bubble sort the following set of values in ascending order.
5, 3, 8, 4, 9, 2, 1, 12, 98, 16
Output: 1, 2, 3, 4, 5, 8, 9, 12, 16, 98

// Program of bubble sort to set the values in ascending order...

class BubbleSort
{
public static void main( String args[ ] )
{
int A[ ] = {5,3,8,4,9,2,1,12,98,16};

int n = A.length;
int temp;

for (int i=1; i<n; i++) for (int i=0; i<n; i++)
{ {
for (int j=0; j<n-i; j++) for (int j=0; j<n-i-1; j++)
if ( A[ j ] > A[ j+1] ) OR if ( A[ j ] > A[ j+1] )
{ {
temp = A[ j ]; temp = A[ j ];
A[ j ] = A[ j+1 ]; A[ j ] = A[ j+1 ];
A[ j+1 ] = temp; A[ j+1 ] = temp;
} }
} }

System.out.println(" Sorted values are below ");


for (int i=0; i<n; i++)
System.out.println(A [i] );
}
}

OUTPUT:
Sorted values are below
1
2
3
4
5
8
9
12
16
98

PUPIL TREE SCHOOL, BALLARI. Page 7 2023-2024


Computer Applications with Java & BlueJ Class X

2005 – Question 8. (Solution 1) [15 Marks]


Write a program to print the sum of negative numbers, sum of positive even numbers and sum of positive odd
numbers from a list of numbers(N) entered by the User. The list terminates when the User enters a zero.

// Program to print sum of negative, positive even and odd numbers...

import java.util.*;

class Summation
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
int n,sumNeg=0,sumPosEven=0,sumPosOdd=0;

do
{
System.out.print(" Enter a number : ");
n = sc.nextInt();

if ( n < 0 )
sumNeg += n;
if (n>0 && n%2== 0)
sumPosEven += n;
if (n>0 && n%2== 1)
sumPosOdd += n;
}
while(n !=0 );

System.out.println(" Sum of Negative Number : "+sumNeg);


System.out.println(" Sum of Positive Even Number : "+sumPosEven);
System.out.println(" Sum of Positive Odd Number : "+sumPosOdd);
}
}

OUTPUT:
Enter a number : -9
Enter a number : 6
Enter a number : -4
Enter a number : 7
Enter a number : 4
Enter a number : 11
Enter a number : 0
Sum of Negative Number : -13
Sum of Positive Even Number : 10
Sum of Positive Odd Number : 18

PUPIL TREE SCHOOL, BALLARI. Page 8 2023-2024


Computer Applications with Java & BlueJ Class X

2005 – Question 8. (Solution 2) [15 Marks]

// Program to print sum of negative, positive even and odd numbers...

import java.util.*;

class Summation2
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
int n=1, sumNeg=0,sumPosEven=0,sumPosOdd=0;

while(n != 0)
{
System.out.print(" Enter a number : ");
n = sc.nextInt( );

if (n < 0)
sumNeg += n;
else if (n%2==0)
sumPosEven += n;
else
sumPosOdd += n;
}

System.out.println(" Sum of Negative Number : "+sumNeg);


System.out.println(" Sum of Positive Even Number : "+sumPosEven);
System.out.println(" Sum of Positive Odd Number : "+sumPosOdd);
}
}

OUTPUT:
Enter a number : -22
Enter a number : 10
Enter a number : -7
Enter a number : 35
Enter a number : 22
Enter a number : 89
Enter a number : -46
Enter a number : 0
Sum of Negative Number : -75
Sum of Positive Even Number : 32
Sum of Positive Odd Number : 124

PUPIL TREE SCHOOL, BALLARI. Page 9 2023-2024


Computer Applications with Java & BlueJ Class X

2005 – Question 9. [15 Marks]


Write a program to initialize an array of 5 names and initialize another array with their respective telephone
numbers. Search for a name input by the User, in the list. If found, display “Search Successful” and print the name
along with the telephone number, otherwise display “Search unsuccessful. Name not enlisted”.

// Program to search a name, if found display their respective


// telephone number...

import java.util.*;

class Search
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

String names[ ] = {"Subhash","Tilak","Bhagat","Mohan","Vallabh"};


String telephone[ ] = {"1897","1856","1907","1869","1875"};

System.out.println(" Enter a name ");


String name = sc.nextLine();

int pos = -1;

for(int i=0; i<names.length; i++)


{
if ( name.equalsIgnoreCase(names[i]) )
{
pos = i;
break;
}
}

if (pos >= 0)
{
System.out.println("Search Successful");
System.out.println( names[pos]+" telephone number is "+telephone[pos] );
}
else
System.out.println("Search unsuccessful. Name not enlisted");
}
}

OUTPUT:
Enter a name
bhagat
Search Successful
Bhagat telephone number is 1907

PUPIL TREE SCHOOL, BALLARI. Page 10 2023-2024


Computer Applications with Java & BlueJ Class X

2006 – Question 4. [15 Marks]


Write a program to calculate and print the sum of odd numbers and the sum of even numbers for the first n natural
numbers.
The integer n is to be entered by the user.

/* Program print the sum of odd numbers and the sum of even numbers
for the first n natural numbers. */

import java.util.*;

class Summation
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter value of N : ");


int n = sc.nextInt( );

int evenSum=0, oddSum=0;

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


{
if ( i%2 == 0 )
evenSum += i;
else
oddSum += i;
}

System.out.println(" Sum of even numbers = " + evenSum);


System.out.println(" Sum of odd numbers = " + oddSum);
}
}

OUTPUT:
Enter value of N :
10
Sum of even numbers = 30
Sum of odd numbers = 25

Alternate Logic:

if ( i%2 == 0 ) if ( i%2 != 1 ) if ( i%2 == 1 ) if ( i%2 != 0 )


evenSum += i; evenSum += i; oddSum += i; oddSum += i;
else else else else
oddSum += i; oddSum += i; evenSum += i; evenSum += i;

PUPIL TREE SCHOOL, BALLARI. Page 11 2023-2024


Computer Applications with Java & BlueJ Class X

2006 – Question 5. [15 Marks]


A cloth showroom has announced the following festival discounts on the purchase of items, based on the total
cost of the items purchased.

Total Cost Discount(in Percentage)

Less than or equal to Rs. 2000 5%

Rs. 2001 to Rs. 5000 25%

Rs. 5001 to Rs. 10000 35%

Above Rs. 10000 50%

Write a program to input the total cost and to compute and display the amount to be paid by the customer after
availing the discount.

// Program to compute festival discounts of a cloth showroom...


import java.util.*;

class ShowRoom
{ public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter total cost : ");


double cost = sc.nextDouble( );

double discount, rate;

if (cost <= 2000)


rate = 5;
else if (cost <= 5000)
rate = 25;
else if (cost <= 10000)
rate = 35;
else
rate = 50;

discount = cost * rate / 100;

System.out.println(" The amount to be paid after discount = " + (cost - discount) );


}
}

OUTPUT:
Enter total cost : 9540
The amount to be paid after discount = 6201.0

PUPIL TREE SCHOOL, BALLARI. Page 12 2023-2024


Computer Applications with Java & BlueJ Class X

2006 – Question 6. (Solution 1) [15 Marks]


Consider the following statement:
“January 26 is celebrated as the Republic Day of India”.
Write a program to change 26 to 15, January to August, Republic to Independence and finally print “August 15 is
celebrated as the Independence Day of India”.

// Program to find and replace a word in a sentence...


import java.util.*;

class MyString
{
String s1;

MyString(String x)
{
s1 = x ;
}

void display( )
{
System.out.println(s1);
}

void replace(String fword, String rword)


{
s1 = s1 + " ";
String s2 = "", eword = "";

for(int i=0; i<s1.length(); i++)


{
char ch = s1.charAt(i);

if ( ch == ' ' )
{
if ( eword.equalsIgnoreCase(fword))
s2 = s2 + " " + rword;
else
s2 = s2 + " " + eword;
eword = "";
}
else
eword = eword + ch;
}
s1 = s2.trim( );
}

public static void main( String args[ ] )


{ Scanner sc = new Scanner(System.in);
MyString s1 = new MyString("January 26 is celebrated as the Republic Day of India");
s1.display( );
s1.replace("26","15");
s1.replace("January","August");
s1.replace("republic","Independence");
s1.display( );
}}

PUPIL TREE SCHOOL, BALLARI. Page 13 2023-2024


Computer Applications with Java & BlueJ Class X

2006 – Question 6. (Solution 2) [15 Marks]

// Program to find and replace a word in a sentence…


import java.util.*;

class FindReplace
{
public static void main(String args[ ])
{
String s1="January 26 is celebrated as the Republic Day of India";

s1 = s1 + " ";
String s2 = "";
String w = "";

for (int i=0; i<s1.length( ); i++)


{
char ch = s1.charAt(i);

if ( ch != ' ' )
w += ch;
else
{
if ( w.equals("26") )
w = "15";
else if ( w.equals("January") )
w = "August";
else if ( w.equals("Republic") )
w = "Independence";

s2 = s2 + " " + w;
w = "";
}
}
s2 = s2.substring(1);
System.out.println( s2 );
}
}

OUTPUT:
August 15 is celebrated as the Independence Day of India

Note: s1 = string1, s2 = string2, w = word and s2.substring(1) is used to copy all characters of the string except
first character because it contains space.

PUPIL TREE SCHOOL, BALLARI. Page 14 2023-2024


Computer Applications with Java & BlueJ Class X

2006 – Question 7. [15 Marks]


Write a program that outputs the results of the following evaluations based on the number entered by the user.
(i) Natural logarithm of the number
(ii) Absolute value of the number
(iii) Square root of the number
(iv) Random numbers between 0 and 1.

// Program to perform evaluations based on user choice...


import java.util.*;

class Evaluation
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter the value of x : ");


double x = sc.nextDouble( );

System.out.println(" 1. Natural logarithm of the number ");


System.out.println(" 2. Absolute value of the number ");
System.out.println(" 3. Square root of the number ");
System.out.println(" 4. Random numbers between 0 and 1 ");
System.out.print(" Enter your choice : ");
int choice = sc.nextInt( );

switch(choice)
{ case 1: System.out.println(" Math.log(x) = "+Math.log(x));
break;
case 2: System.out.println(" Math.abs(x) = "+Math.abs(x));
break;
case 3: System.out.println(" Math.sqrt(x) = "+Math.sqrt(x));
break;
case 4: System.out.println(" Math.random() = "+Math.random( ) );
}
}
}

OUTPUT:
Enter the value of x : 36
1. Natural logarithm of the number
2. Absolute value of the number
3. Square root of the number
4. Random numbers between 0 and 1

Enter your choice : 1


Math.log(x) = 3.58351893845611

PUPIL TREE SCHOOL, BALLARI. Page 15 2023-2024


Computer Applications with Java & BlueJ Class X

2006 – Question 8. [15 Marks]


The marks obtained by 50 students in a subject are tabulated as follows:
Name Marks

. .
. .
. .

Write a program to input the names and marks of the students in the subject.
Calculate and display:
𝑠𝑢𝑏𝑗𝑒𝑐𝑡𝑡𝑜𝑡𝑎𝑙
(i) The subject average marks (𝑠𝑢𝑏𝑗𝑒𝑐𝑡𝑎𝑣𝑒𝑟𝑎𝑔𝑒𝑚𝑎𝑟𝑘𝑠 = 50
)
(ii) The highest mark in the subject and the name of the student.
(The maximum marks in the subject are 100)

// Program to process student marks…


import java.util.*;

class Student
{ public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);
int n = 5;

String names[ ] = new String[n];


int marks[ ] = new int[n];

int total=0;

int max=0;
String name="";

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


{
System.out.print(" Enter name of student"+(i+1)+" : ");
names[i] = sc.next( );
System.out.print(" Enter "+ names[i] +" marks : ");
marks[i] = sc.nextInt( );

total += marks[i];

if (max < marks[i])


{
max = marks[i];
name = names[i];
}
}

PUPIL TREE SCHOOL, BALLARI. Page 16 2023-2024


Computer Applications with Java & BlueJ Class X

double avg = (double) total / n ;

System.out.println(" Subject total : "+total);


System.out.println(" Subject average marks : " + avg);
System.out.println(" Highest mark in the subject : " + max);
System.out.println(" Name of the student : " + name);

}
}

OUTPUT:
Enter name of student1 : Dharsheel
Enter Dharsheel marks : 97

Enter name of student2 : Lokjeeth


Enter Lokjeeth marks : 98

Enter name of student3 : Mokshagna


Enter Mokshagna marks : 99

Enter name of student4 : Pavan


Enter Pavan marks : 96

Enter name of student5 : Rakshith


Enter Rakshith marks : 97

Subject total : 487


Subject average marks : 97.4

Highest mark in the subject : 99


Name of the student : Mokshagna

Note: In the above program change the value of n to 50(int n = 50), as per question asked in the exam.

PUPIL TREE SCHOOL, BALLARI. Page 17 2023-2024


Computer Applications with Java & BlueJ Class X

2006 – Question 9. [15 Marks]


Write a program to accept 15 integers from the keyboard, assuming that no integer entered is a zero. Perform
selection sort on the integers and then print them in ascending order.

// Program to sort the integers in ascending order using selection sort…


import java.util.*;

class SelectionSort
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

int n = 15;
int A[ ] = new int[n];

System.out.println(" Enter "+n+" numbers ");

int i=0;

while (i<n)
{
int x = sc.nextInt( );

if (x==0)
System.out.println(" Don't enter zero ! ");
else
A[i++] = x;
}

int pos, temp;

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


{
pos = i;
for (int j=i+1; j<n; j++)
if (A[pos] > A[j])
pos = j;

temp = A[i];
A[i] = A[pos];
A[pos] = temp;
}

System.out.println(" Sorted array is below ");


for(i=0; i<n; i++)
System.out.println(A[i]);
}
}

PUPIL TREE SCHOOL, BALLARI. Page 18 2023-2024


Computer Applications with Java & BlueJ Class X

OUTPUT:
Enter 15 numbers
97
32
51
0
Don't enter zero !
64
-69
512
-10
48
1000
123
899
-1024
786
10
203

Sorted array is below


-1024
-69
-10
10
32
48
51
64
97
123
203
512
786
899
1000

PUPIL TREE SCHOOL, BALLARI. Page 19 2023-2024


Computer Applications with Java & BlueJ Class X

2007 – Question 4. [15 Marks]


Define a class Salary described as below:
Data members:
Name, Address, Phone, Subject Specialization, Monthly Salary, Income Tax.

Member methods:
(a) To accept the details of a teacher including the monthly salary.
(b) To display the details of the teacher.
(c) To compute the annual Income Tax as 5% of the annual salary above ₹ 1,75,000/-.

Write a main method to create object of a class and call the above member method.
// Program to calculate teachers income tax based on monthly salary...
import java.util.*;

class Salary
{
String name,addr,ph,sub;
double sal,total,IT;

void accept( )
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter Name,Address,Phone,Subject & Monthly salary");
name = sc.nextLine( );
addr = sc.nextLine( );
ph = sc.nextLine( );
sub = sc.nextLine( );
sal = sc.nextDouble( );
}

void compute( )
{
total = sal * 12;

if ( total > 175000 )


IT = (total - 175000) * 5/100;
}

void display( )
{
System.out.println(" Teacher Details ");
System.out.println(" Name : " + name );
System.out.println(" Address : " + addr );
System.out.println(" Phone No : " + ph );
System.out.println(" Subject : " + sub );
System.out.println(" Annual Salary : " + total );
System.out.println(" Income Tax : " + IT );
}

PUPIL TREE SCHOOL, BALLARI. Page 20 2023-2024


Computer Applications with Java & BlueJ Class X

public static void main(String args[ ])


{
Salary obj = new Salary( );
obj.accept( );
obj.compute( );
obj.display( );
}
}

OUTPUT 1:
Enter Name,Address,Phone,Subject & Monthly salary
Suresh
Nehru Circle
273649
Physics
20000

Teacher Details
Name : Suresh
Address : Nehru Circle
Phone No : 273649
Subject : Physics
Annual Salary : 240000.0
Income Tax : 3250.0

OUTPUT 2:
Enter Name,Address,Phone,Subject & Monthly salary
Ramesh
Gandhi Nagar
278125
Social
12000

Teacher Details
Name : Ramesh
Address : Gandhi Nagar
Phone No : 278125
Subject : Social
Annual Salary : 144000.0
Income Tax : 0.0

PUPIL TREE SCHOOL, BALLARI. Page 21 2023-2024


Computer Applications with Java & BlueJ Class X

2007 – Question 5. (Solution 1 With Nested Loop) [15 Marks]


Write a program to compute and display the sum of the following series.
(1 + 2) (1 + 2 + 3) (1 + 2 + 3 + 4) (1 + 2 + 3 + 4+. . . 𝑛)
+ + +. . . . . . +
(1𝑋2) (1𝑋2𝑋3) (1𝑋2𝑋3𝑋4) (1𝑋2𝑋3𝑋4. . . 𝑛)

import java.util.*;

class Series1
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

double total=0, sum, product;

System.out.println(" Enter n value ");


int n = sc.nextInt( );

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


{
sum = 0;
product = 1;

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


{
sum = sum + j;
product = product * j;
}
total = total + sum / product;
}
System.out.println(" Total = " + total );
}
}

OUTPUT:
Enter n value
5
Total = 3.04166

PUPIL TREE SCHOOL, BALLARI. Page 22 2023-2024


Computer Applications with Java & BlueJ Class X

2007 – Question 5. (Solution 2 Without Nested Loop) [15 Marks]

import java.util.*;

class Series2
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

double total=0, sum=1, product=1;

System.out.println(" Enter n value ");


int n = sc.nextInt( );

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


{
product = product * i ;
sum = sum + i;

total = total + (sum/product);


}

System.out.println(" Total = " + total);


}
}

OUTPUT 1:
Enter n value
4
Total = 2.9166

OUTPUT 2:
Enter n value
5
Total = 3.04166

PUPIL TREE SCHOOL, BALLARI. Page 23 2023-2024


Computer Applications with Java & BlueJ Class X

2007 – Question 6. [15 Marks]


Write a program to initialize the given data in an array and find the minimum and maximum values along with
the sum of the given elements.
Number: 2 5 4 1 3

Output:
Minimum value: 1
Maximum value: 5
Sum of the elements: 15

// Program to find largest, smallest and sum of all elements of the array...

import java.util.*;

class Array
{
public static void main(String args[ ])
{
int A[ ] = {2,5,4,1,3};

int max = A[0];


int min = A[0];
int sum = A[0];

for (int i=1; i<A.length; i++)


{
if ( max < A[i] )
max = A[i];
else if ( min > A[i] )
min = A[i];

sum = sum + A[i];


}

System.out.println(" Minimum value : "+min);


System.out.println(" Maximum value : "+max);
System.out.println(" Sum of the elements : "+sum);
}
}

OUTPUT:
Minimum value : 1
Maximum value : 5
Sum of the elements : 15

PUPIL TREE SCHOOL, BALLARI. Page 24 2023-2024


Computer Applications with Java & BlueJ Class X

2007 – Question 7. (Solution 1) [15 Marks]


Write a program to enter a sentence from the keyboard and count the number of times a particular word
occurs in it. Display the frequency of the search word.
Example:
Input:
Enter a sentence: the quick brown fox jumps over the lazy dog.
Enter a word to be searched: the
Output:
Search word occurs: 2 times

// Program to count the number of times a word occurs in a sentence…


import java.util.*;

class SearchWord1
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a sentence : ");


String sent = sc.nextLine( );

System.out.println(" Enter a word to be searched : ");


String sword = sc.nextLine( );

sent = sent + " " ;


String eword = "" ;
int count = 0;

for (int i=0; i<sent.length( ); i++)


{
char ch = sent.charAt(i);

if ( ch == ' ' )
{
if ( sword.equalsIgnoreCase(eword) )
count++;

eword = "" ;
}

if ( Character.isLetter(ch) )
eword = eword + ch;
}
System.out.println(" Search word occurs : "+ count + " times ");
}
}

OUTPUT:
Enter a sentence :
the? quick brown fox jumps over The lazy dog.
Enter a word to be searched :
the
Search word occurs : 2 times

PUPIL TREE SCHOOL, BALLARI. Page 25 2023-2024


Computer Applications with Java & BlueJ Class X

2007 – Question 7. (Solution 2) [15 Marks]

// Program to count the number of times a word occurs in a sentence...


import java.util.*;

class SearchWord2
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a sentence : ");


String s = sc.nextLine( );

System.out.println(" Enter a word to be searched : ");


String w = sc.nextLine( );

int count = 0 ;
int sl = s.length( );
int wl = w.length( );

for (int i=0; i <= sl-wl; i++)


{
int j=0;
while( j<wl && w.charAt(j) == s.charAt(i+j) )
j++;

if ( j == wl )
count++;
}
System.out.println(" Search word occurs : "+ count + " times ");
}
}

OUTPUT 1: OUTPUT 2:
Enter a sentence : Enter a sentence :
TOMATOMATOMATOMATOMATO MATHEMATICS
Enter a word to be searched : Enter a word to be searched :
TOMATO MAT
Search word occurs : 5 times Search word occurs : 2 times

Note: s = sentence, w = word, sl = sentence length, wl = word length

PUPIL TREE SCHOOL, BALLARI. Page 26 2023-2024


Computer Applications with Java & BlueJ Class X

2007 – Question 8. [15 Marks]


Using a switch statement, write a menu driven program to convert a given temperature from Fahrenheit to
Celsius and vice versa. For an incorrect choice, an appropriate error message should be displayed.
Hint: C = 5/9 X ( F – 32 ) and F = 1.8 X C + 32

// Program to convert celsius to fahrenheit and vice-versa...


import java.util.*;

class Temperature
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
double C,F;

System.out.println(" 1. Celsius to Fahrenheit ");


System.out.println(" 2. Fahrenheit to Celsius ");

System.out.println(" Enter your choice ");


int choice = sc.nextInt( );

switch (choice)
{
case 1 : System.out.println(" Enter Celsius ");
C = sc.nextDouble( );
F = (C*9/5) + 32;
System.out.println(" Fahrenheit = " + F );
break;

case 2 : System.out.println(" Enter Fahrenheit ");


F = sc.nextDouble( );
C = (F-32) * 5/9;
System.out.println(" Celsius = " + C );
break;

default : System.out.println(" Wrong choice ");


}
}
}
OUTPUT:
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice
2
Enter Fahrenheit
113
Celsius = 45.0

PUPIL TREE SCHOOL, BALLARI. Page 27 2023-2024


Computer Applications with Java & BlueJ Class X

2007 – Question 9. (Solution 1) [15 Marks]


Write a program using a method Palin( ), to check whether a string is a palindrome or not palindrome is a
string that reads the same from left to right and vice versa.
E.g. MADAM, ARORA, ABBA, etc.

// Program to check a word is palindrome or not…


import java.util.*;

class Palindrome1
{
public static boolean Palin(String word)
{
word = word.toUpperCase( );
boolean flag = true;
int size = word.length()-1;

for (int i=0, j=size; i<=size/2; i++, j--)


{
if ( word.charAt( i ) != word.charAt( j ) )
{
flag = false;
break;
}
}
return flag;
}

public static void main(String args[ ])


{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a word ");


String word = sc.nextLine( );

if ( Palin(word) )
System.out.println(word + " is Palindrome ");
else
System.out.println(word + " is Not palindrome ");
}
}

OUTPUT 1: OUTPUT 2: OUTPUT 3: OUTPUT 4:


Enter a word Enter a word Enter a word Enter a word
RaceCar OPPO G VAIBHAV
RaceCar is Palindrome OPPO is Palindrome G is Palindrome VAIBHAV is Not palindrome

PUPIL TREE SCHOOL, BALLARI. Page 28 2023-2024


Computer Applications with Java & BlueJ Class X

2007 – Question 9. (Solution 2) [15 Marks]

// Program to check a word is palindrome or not…

import java.util.*;

class Palindrome2
{
public static boolean Palin( String w )
{
String rev = "" ;

for (int i=0; i<w.length( ); i++)


rev = w.charAt(i) + rev;

System.out.println(" Reverse = " + rev);

return w.equalsIgnoreCase(rev);
}

public static void main(String args[ ])


{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a word ");


String word = sc.nextLine( );

if ( Palin(word) )
System.out.println(word + " is Palindrome ");
else
System.out.println(word + " is Not palindrome ");
}
}

OUTPUT 1: OUTPUT 2: OUTPUT 3: OUTPUT 4:


Enter a word Enter a word Enter a word Enter a word
Rat Arora ma'am 343
Reverse = taR Reverse = arorA Reverse = ma'am Reverse = 343
Rat is Not palindrome Arora is Palindrome
ma'am is Palindrome 343 is Palindrome

PUPIL TREE SCHOOL, BALLARI. Page 29 2023-2024


Computer Applications with Java & BlueJ Class X

2008 – Question 4. [15 Marks]


Define a class Employee having the following description:
Data members (Inputs):
int PAN To store Permanent Account Number
String name To store Name
double taxIncome To store Annual Taxable Income
double tax To store Tax that is calculated

Member functions:
input( ) Store the PAN number, name, taxableincome
calc( ) Calculate tax for an employee
display( ) Output details of an employee

Write a program to compute the tax according to the given conditions and display the output as per given
format.
Total Annual Taxable Income Tax Rate
Up to ₹ 1,00,000 No Tax
From ₹ 1,00,001 to 10% of the income exceeding
₹ 1,50,000 ₹ 1,00,000
From ₹ 1,50,001 to ₹ 5000 + 20% of the income
₹ 2,50,000 exceeding ₹ 1,50,000
Above ₹ 2,50,000 ₹ 25,000 + 30% of the income
exceeding ₹ 2,50,000

Output:
PAN Number Name Tax-Income Tax
_ _ _ _
_ _ _ _
_ _ _ _
_ _ _ _

// Program to compute the income-tax of employee...


import java.util.*;

class Employee
{
int PAN;
String name;
double taxIncome, tax;

void input( )
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter Employee Name, PAN number and Taxable Income");
name = sc.nextLine( );
PAN = sc.nextInt( );
taxIncome = sc.nextDouble( );
}

PUPIL TREE SCHOOL, BALLARI. Page 30 2023-2024


Computer Applications with Java & BlueJ Class X

void calc( )
{
if ( taxIncome <= 100000 )
tax = 0 ;
else if ( taxIncome <= 150000 )
tax = ( taxIncome - 100000 ) * 0.1;
else if ( taxIncome <= 250000 )
tax = ( taxIncome - 150000 ) * 0.2 + 5000;
else
tax = (taxIncome - 250000 ) * 0.3 + 25000;
}

void display( )
{
System.out.println(" PAN No. \t Name \t Tax-Income \t Tax ");
System.out.println(PAN + "\t" + name + "\t" + taxIncome + "\t" + tax);
}

public static void main( String args[ ] )


{
Employee obj = new Employee( );
obj.input( );
obj.calc( );
obj.display( );
}
}

OUTPUT:
Enter Employee Name, PAN number and Taxable Income
Neha 1234 100000
Brahmini 3456 135000
Hithashri 5678 196000
Hunar 7890 384500

PAN No. Name Tax-Income Tax


1234 Neha 100000.0 0.0
3456 Brahmini 135000.0 3500.0
5678 Hithashri 196000.0 14200.0
7890 Hunar 384500.0 65350.0

PUPIL TREE SCHOOL, BALLARI. Page 31 2023-2024


Computer Applications with Java & BlueJ Class X

2008 – Question 5. (Solution 1) [15 Marks]


Write a program to input a string and print out the text with the uppercase and lowercase letters reversed, but all
other characters should remain the same as before.
Example:
INPUT: WelComE To scHool
OUTPUT: wELcOMe tO SchOOL

// Program to convert upper case letters to lower case and vice-versa…

import java.util.*;

class Convert1
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a string ");


String s1 = sc.nextLine( );

String s2 = "" ;

for (int i=0; i<s1.length( ); i++)


{
char ch = s1.charAt(i);

if (ch>=65 && ch<=90)


s2 = s2 + (char) (ch+32);
else if (ch>=97 && ch<=122)
s2 = s2 + (char) (ch-32);
else
s2 = s2 + ch;
}

System.out.println( s2 );
}
}

OUTPUT:
Enter a string
aCTIVATE 5g aIRtEL *123#
Activate 5G AirTel *123#

Note: In the above program we used ASCII values, A=65, Z=90, a=97 and z=122

PUPIL TREE SCHOOL, BALLARI. Page 32 2023-2024


Computer Applications with Java & BlueJ Class X

2008 – Question 5. (Solution 2 & Solution 3) [15 Marks]


class Convert2
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a string ");


String s1 = sc.nextLine( );

String s2 = "" ;

for (int i=0; i<s1.length( ); i++)


{
char ch = s1.charAt(i); Solution 2

if (ch>='A' && ch<='Z')


s2 += (char) (ch + ' ');
else if (ch>='a' && ch<='z')
s2 += (char) (ch - ' ');
else
s2 += ch;
}
System.out.println( s2 );
}
}

class Convert3
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a string ");


String s1 = sc.nextLine( );

String s2 = "";

for (int i=0; i<s1.length(); i++)


{
char ch = s1.charAt(i); Solution 3

if ( Character.isUpperCase(ch) )
s2 += Character.toLowerCase(ch);
else if ( Character.isLowerCase(ch) )
s2 += Character.toUpperCase(ch);
else
s2 += ch;
}
System.out.println( s2 );
}
}

PUPIL TREE SCHOOL, BALLARI. Page 33 2023-2024


Computer Applications with Java & BlueJ Class X

2008 – Question 6. [15 Marks]


Define a class and store the given city names in a single dimensional array. Sort these names in alphabetical order
using the Bubble Sort technique only.
Input: Delhi, Bangalore, Agra, Mumbai, Calcutta
Output: Agra, Bangalore, Calcutta, Delhi, Mumbai

/* Program to sort the city names in alphabetical order


using Bubble sort technique(algorithm)... */

import java.util.*;

class BubbleSort
{
public static void main(String args[ ])
{
String A[ ] = {"Delhi","Hyderabad","Bengaluru","Agra","Mumbai","Calcutta"};

String temp;

int i, j;

for ( i=1; i<A.length; i++ )


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

for (i=0; i<A.length; i++)


System.out.println( A[i] );
}
}

OUTPUT:
Agra
Bengaluru
Calcutta
Delhi
Hyderabad
Mumbai

PUPIL TREE SCHOOL, BALLARI. Page 34 2023-2024


Computer Applications with Java & BlueJ Class X

2008 – Question 7. (Solution 1) [15 Marks]


Write a menu-driven class to accept a number from the user and check whether it is a Palindrome or a Perfect
number.
(a) Palindrome number – (a number is a palindrome which when read in reserve order is same as read in the right
order)
Example: 11, 101, 151, etc.
(b) Perfect number – (a number is called perfect if it is equal to the sum of its factors other than the number itself.)
Example: 6 = 1 + 2 + 3

// Program to check number is perfect or palindrome based on user choice...

import java.util.*;

class PerfectPalindrome1
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a number ");


int n = sc.nextInt( );

System.out.println(" 1.Check Perfect or Not ");


System.out.println(" 2.Check Palindrome or Not ");

System.out.println(" Enter your choice ");


int choice = sc.nextInt( );

switch(choice)
{
case 1 : perfect(n); break;

case 2 : palindrome(n); break;

default : System.out.println(" Wrong choice ");


}
}

public static void perfect( int n )


{
int sum = 0;

for (int i=1; i<=n/2; i++)


{
if (n%i == 0)
sum = sum + i;
}

if ( sum == n )
System.out.println(" It is Perfect number ");
else
System.out.println(" Not Perfect number ");
}

PUPIL TREE SCHOOL, BALLARI. Page 35 2023-2024


Computer Applications with Java & BlueJ Class X

public static void palindrome( int n )


{
int rev=0, x=n, d;

while (n>=1)
{
d = n % 10;
rev = rev * 10 + d;
n = n / 10;
}

if ( rev == x )
System.out.println(" It is Palindrome ");
else
System.out.println(" Not Palindrome ");
}
}

OUTPUT 1:
Enter a number
28
1.Check Perfect or Not
2.Check Palindrome or Not
Enter your choice
1
It is Perfect number

OUTPUT 2:
Enter a number
2882
1.Check Perfect or Not
2.Check Palindrome or Not
Enter your choice
2
It is Palindrome

OUTPUT 3:
Enter a number
6
1.Check Perfect or Not
2.Check Palindrome or Not
Enter your choice
-1
Wrong choice

PUPIL TREE SCHOOL, BALLARI. Page 36 2023-2024


Computer Applications with Java & BlueJ Class X

2008 – Question 7. (Solution 2) [15 Marks]

// Program to check number is perfect or palindrome based on user choice...


import java.util.*;

class PerfectPalindrome2
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a number ");


int n = sc.nextInt( );

System.out.println(" 1.Check Perfect or Not ");


System.out.println(" 2.Check Palindrome or Not ");

System.out.println(" Enter your choice ");


int choice = sc.nextInt( );

switch(choice)
{
case 1 : if ( IsPerfect(n) )
System.out.println(" It is Perfect number ");
else
System.out.println(" Not Perfect number ");
break;

case 2 : if ( IsPalindrome(n) )
System.out.println("It is Palindrome ");
else
System.out.println("Not Palindrome ");
break;

default : System.out.println(" Wrong choice ");


}
}

// Function 1 ( Method 1 ) // Function 2 ( Method 2 )


public static boolean IsPerfect( int n ) public static boolean IsPalindrome( int n )
{ {
int sum = 0; int rev=0,x=n,d;

for (int i=1; i<=n/2; i++) while (n>=1)


{ {
if (n%i == 0) d = n%10;
sum = sum + i; rev = rev*10+d;
} n = n/10;
}
return sum == n;
return rev == x;
} }

} // End of class

PUPIL TREE SCHOOL, BALLARI. Page 37 2023-2024


Computer Applications with Java & BlueJ Class X

2008 – Question 8. (Solution 1) [15 Marks]


Write a class with the name Volume using function overloading that computes the volume of a cube, a sphere and
a cuboid.
Formula:
volume of a cube (vc) = s*s*s
volume of sphere (vs) = 4/3*π*r*r*r (where π = 3.142 or 22/7)
volume of a cuboid (vcd) = l*b*h

// Program to compute the volume of a cube, a sphere and a cuboid…

import java.util.*;

class VOLUME
{

public static void volume(double s)


{
System.out.println(" Volume of a cube (vc) = " + (s * s * s) );
}

public static void volume(double PI, double r)


{
System.out.println(" Volume of a sphere (vs) = " + ( 4.0/3 * PI * r * r * r) );
}

public static void volume(double l, double b, double h)


{
System.out.println(" Volume of a cuboid (vcd) = " + ( l * b * h ) );
}

public static void main(String args[ ])


{
volume ( 6, 8, 9 );
volume ( Math.PI, 10 );
volume ( 12 );
}

OUTPUT:
Volume of a cuboid (vcd) = 432.0
Volume of a sphere (vs) = 4188.7902
Volume of a cube (vc) = 1728.0

PUPIL TREE SCHOOL, BALLARI. Page 38 2023-2024


Computer Applications with Java & BlueJ Class X

2008 – Question 8. (Solution 2) [15 Marks]

// Program to compute the volume of a cube, a sphere and a cuboid...

import java.util.*;

class VOLUME
{

public static int volume( int s )


{
return s * s * s;
}

public static double volume( double r )


{
return 4/3.0 * Math.PI * Math.pow( r,3 );
}

public static double volume( double l, double b, double h )


{
return l * b * h;
}

public static void main( String args[ ] )


{
System.out.println(" Volume of a cuboid = " + volume(6,8,9) );
System.out.println(" Volume of a sphere = " + volume(10.0) );
System.out.println(" Volume of a cube = " + volume(12) );
}

OUTPUT:
Volume of a cuboid = 432.0
Volume of a sphere = 4188.7902
Volume of a cube = 1728

PUPIL TREE SCHOOL, BALLARI. Page 39 2023-2024


Computer Applications with Java & BlueJ Class X

2008 – Question 9. [15 Marks]

Write a program to calculate and print the sum of each of the following series:
(a) Sum (S) = 2 - 4 + 6 – 8 + … -20
𝑥 𝑥 𝑥 𝑥 𝑥
(b) Sum (S) = 2 + 5 + 8 + 11 +. . . . . . + 20
(Value of x is to be input by the user)

// Program to find the sum of the series...

import java.util.*;

class Series
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner( System.in );

double sum1=0, sum2=0;

for (int i=2; i<=20; i+=2)


{
if ( i%4 == 0 )
sum1 -= i;
else
sum1 += i;
}
System.out.println(" Sum of Series1 = " + sum1 );

System.out.print(" Enter x value : ");


double x = sc.nextDouble( );

for (int i=2; i<=20; i+=3)


sum2 += x/i;

System.out.println(" Sum of Series2 = " + sum2 );

}
}

OUTPUT:
Sum of Series1 = -10.0
Enter x value : 12
Sum of Series2 = 13.153934300993125

PUPIL TREE SCHOOL, BALLARI. Page 40 2023-2024


Computer Applications with Java & BlueJ Class X

2009 – Question 4. [15 Marks]


An electronics shop has announced the following seasonal discounts on the purchase of certain items.

Purchase Amount Discount on Discount on


in Rs. Laptop Desktop PC

0 – 25,000 0.0% 5.0%

25,001 – 57,000 5.0% 7.5%

57,001 – 1,00,000 7.5% 10.0%

More than 1,00,000 10.0% 15.0%

Write a program based on the above criteria, to input name, address, amount of purchase and the type of
purchase (L for Laptop and D for Desktop) by a customer. Compute and print the net amount to be paid by a
customer along with his name and address.
(Hint: discount = ( discount rate / 100 ) * amount of purchase
Net amount = amount of purchase – discount)

// Program to compute the discount on electronic products…


import java.util.*;

class ElectronicShop
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter Customer Name, Address ");


String name = sc.nextLine( );
String addr = sc.nextLine( );

System.out.println(" Enter Amount of Purchase ");


double amt = sc.nextDouble( );

System.out.println(" Enter Type of Purchase L for Laptop and D for Desktop ");
char ch = Character.toUpperCase( sc.next( ).charAt(0) );

double r = 0;

switch ( ch )
{
case 'L' : if ( amt <=25000 )
r=0;
else if ( amt <= 57000 )
r=5;
else if ( amt <= 100000 )
r = 7.5 ;
else
r = 10 ;
break ;

PUPIL TREE SCHOOL, BALLARI. Page 41 2023-2024


Computer Applications with Java & BlueJ Class X

case 'D' : if ( amt <= 25000 )


r=5;
else if ( amt <= 57000 )
r = 7.5 ;
else if ( amt <= 100000 )
r = 10 ;
else
r = 15 ;
}

double dis = r / 100 * amt;


double net = amt - dis;

System.out.println(" Customer Name : " + name);


System.out.println(" Address : " + addr);
System.out.println(" Net amount to be paid : " + net);
}
}

OUTPUT:
Enter Customer Name, Address
Mayukh
Srinagar

Enter Amount of Purchase


50000

Enter Type of Purchase L for Laptop and D for Desktop


desktop

Customer Name : Mayukh


Address : Srinagar
Net amount to be paid : 46250.0

Note: where amt = amount, ch = choice, r = rate, dis = discount & net = net amount

PUPIL TREE SCHOOL, BALLARI. Page 42 2023-2024


Computer Applications with Java & BlueJ Class X

2009 – Question 5. [15 Marks]


Write a program to generate a triangle or an inverted triangle till n terms based upon the user’s choice of triangle
to be displayed.
Example 1 Example 2
Input: Type 1 for a Triangle and Input: Type 1 for a Triangle and
Type 2 for an Inverted triangle Type 2 for an Inverted triangle
1 2
Enter the number of terms Enter the number of terms
5 6
Output: Output:
1 666666
22 55555
333 4444
44444 333
555555 22
1

// Program to display the numbers in triangle patterns…

import java.util.*;

class Triangles
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

System.out.println(" 1. for a Triangle ");


System.out.println(" 2. for an Inverted triangle ");

System.out.print(" Enter your choice : ");


int choice = sc.nextInt( );

System.out.print(" Enter the number of terms : ");


int n = sc.nextInt( );

int i, j;

switch ( choice )
{
case 1 : for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
System.out.print( i );

System.out.println( );
}
break;

PUPIL TREE SCHOOL, BALLARI. Page 43 2023-2024


Computer Applications with Java & BlueJ Class X

case 2 : for (i=n; i>=1; i--)


{
for (j=1; j<=i; j++)
System.out.print( i );

System.out.print("\n");
}
break;

default : System.out.println(" Wrong choice ");


}
}
}

OUTPUT 1:
1. for a Triangle
2. for an Inverted triangle
Enter your choice : 2

Enter the number of terms : 9


999999999
88888888
7777777
666666
55555
4444
333
22
1

OUTPUT 2:
1. for a Triangle
2. for an Inverted triangle
Enter your choice : 1

Enter the number of terms : 6


1
22
333
4444
55555
666666

PUPIL TREE SCHOOL, BALLARI. Page 44 2023-2024


Computer Applications with Java & BlueJ Class X

2009 – Question 6. [15 Marks]


Write a program to input a sentence and print the number of characters found in the longest word of the
given sentence.
For example, if S = “India is my country” then the output should be 7.

// Program to display longest word and its length of a sentence…

import java.util.*;

class LongestWord
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a Sentence ");


String s = sc.nextLine( );

s += " ";

int big=0;
String lword="", eword="";

for (int i=0; i<s.length( ); i++)


{
char ch = s.charAt( i );

if ( ch == ' ' )
{
if ( eword.length( ) > big )
{
big = eword.length( );
lword = eword;
}
eword = "";
}

if ( Character.isLetter(ch) )
eword += ch;
}

System.out.println(" Longest word is " + lword);


System.out.println(" Its Length is "+ big);
}
}

OUTPUT:
Enter a Sentence
India is my country, My motherland is great!!!!!!!!!!
Longest word is motherland
Its Length is 10

Note: where s = sentence, lword = longest word, eword = extracted word

PUPIL TREE SCHOOL, BALLARI. Page 45 2023-2024


Computer Applications with Java & BlueJ Class X

2009 – Question 7. [15 Marks]


Design a class to overload a function num_calc( ) as follows:

(a)void num_calc(int num, char ch) with one integer argument and one character argument, computes the square
of integer argument if choice ch is ‘s’ otherwise finds its cube.

(b) void num_calc(int a, int b, char ch) with two integer arguments and one character argument. It computes the
product of integer arguments if ch is ‘p’ else adds the integers.

(c) void num_calc(String s1, String s2) with two string arguments, which prints whether the strings are equal or
not.

// Program to overload a function num_calc( ) as follows…


import java.util.*;

class Overload
{
public static void num_calc(int num, char ch)
{
if (ch == 's' || ch == 'S')
System.out.println(" The Square = " + (num * num) );
else
System.out.println(" The Cube = " + (num * num * num) );
}

public static void num_calc(int a, int b, char ch)


{
if (ch == 'p' || ch == 'P')
System.out.println(" The Product = " + (a*b) );
else
System.out.println(" The Addition = " + (a+b) );
}

public static void num_calc(String s1, String s2)


{
if (s1.equalsIgnoreCase(s2))
System.out.println(" Strings are equal ");
else
System.out.println(" Strings are different ");
}

public static void main(String args[ ])


{
num_calc(7,'S');
num_calc(93,8,'g');
num_calc("APPLE","apple");
}
}
OUTPUT:
The Square = 49
The Addition = 101
Strings are equal

PUPIL TREE SCHOOL, BALLARI. Page 46 2023-2024


Computer Applications with Java & BlueJ Class X

2009 – Question 8. [15 Marks]


Write a menu driven program to accept a number from the user and check whether it is a “BUZZ” number or
to accept any two numbers and print the ‘GCD’ of them.
(a) A BUZZ number is the number which either ends with 7 or divisible by 7.
(b) GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide the
larger number by the smaller, the remainder then divides the previous divisor. The process is repeated till the
remainder is zero. The divisor the results the GCD.
/* Menu driven program to check number is Buzz number or not
and also find HCF of 2 numbers... */
import java.util.*;

class Menu
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Press 1 to Check Buzz number or not ");


System.out.println(" Press 2 to Find GCD of two numbers ");

System.out.println(" Which is your choice? ");


int choice = sc.nextInt( );

switch (choice)
{
case 1 : System.out.println(" Enter a number ");
int n = sc.nextInt( );
if ( n%7==0 || n%10==7 )
System.out.println(" It is Buzz number ");
else
System.out.println(" Not a Buzz number ");
break;

case 2 : System.out.println(" Enter any 2 integers ");


int a = sc.nextInt( );
int b = sc.nextInt( );
int dividend = a, divisor = b, remainder;
while (divisor != 0)
{
remainder = dividend % divisor;
dividend = divisor;
divisor = remainder;
}
int gcd = dividend;
int lcm = a*b / gcd;
System.out.println(" GCD = " + gcd );
System.out.println(" LCM = " + lcm );
}
}
}

PUPIL TREE SCHOOL, BALLARI. Page 47 2023-2024


Computer Applications with Java & BlueJ Class X

OUTPUT 1: OUTPUT 2:
Press 1 to Check Buzz number or not Press 1 to Check Buzz number or not
Press 2 to Find GCD of two numbers Press 2 to Find GCD of two numbers

Which is your choice? 1 Which is your choice? 2

Enter a number Enter any 2 integers


91 89
It is Buzz number 41

GCD = 1
LCM = 3649

OUTPUT 3: OUTPUT 4:
Press 1 to Check Buzz number or not Press 1 to Check Buzz number or not
Press 2 to Find GCD of two numbers Press 2 to Find GCD of two numbers

Which is your choice? 2 Which is your choice? 1

Enter any 2 integers Enter a number


456 1947
360 It is Buzz number

GCD = 24
LCM = 6840

OUTPUT 5: OUTPUT 6:
Press 1 to Check Buzz number or not Press 1 to Check Buzz number or not
Press 2 to Find GCD of two numbers Press 2 to Find GCD of two numbers

Which is your choice? 1 Which is your choice? 2

Enter a number Enter any 2 integers


71 10
Not a Buzz number 10

GCD = 10
LCM = 10

PUPIL TREE SCHOOL, BALLARI. Page 48 2023-2024


Computer Applications with Java & BlueJ Class X

2009 – Question 9. (Solution 1 Using Arrays) [15 Marks]


The annual examination results of 50 students in a class are tabulated as follows.
Roll no. Subject A Subject B Subject C
………. ………….. ………….. …………..
Write a program to read the data, calculate and display the following:
(a) Average marks obtained by each student.
(b) Print the roll number and average marks of the students whose average mark is above 80.
(c) Print the roll number and average marks of the students whose average mark is below 40.

// Program to display annual examination results of 50 students...


import java.util.*;

class Examination
{ public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

int rollno[ ] = new int[50];


int sub1[ ] = new int[50];
int sub2[ ] = new int[50];
int sub3[ ] = new int[50];
double avg[ ] = new double[50];
int i;

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


{
System.out.println(" Enter the Roll Number ");
rollno[i] = sc.nextInt( );
System.out.println(" Enter the Marks for Subject 1 : ");
sub1[i] = sc.nextInt( );
System.out.println(" Enter the Marks for Subject 2 : ");
sub2[i] = sc.nextInt( );
System.out.println(" Enter the Marks for Subject 3 : ");
sub3[i] = sc.nextInt( );

avg[i] = (sub1[i]+sub2[i]+sub3[i]) / 3.0;


}
System.out.println(" Average Marks obtained by each student ");
System.out.println(" Roll No. \t Average ");
for(i=0; i<50; i++)
System.out.println(rollno[i]+"\t"+avg[i]);

System.out.println(" Average Marks obtained by student whose average is above 80 ");


System.out.println(" Roll No. \t Average ");
for(i=0; i<50; i++)
if (avg[i]>80)
System.out.println(rollno[i]+"\t"+avg[i]);

System.out.println(" Average Marks obtained by student whose average is below 40 ");


System.out.println(" Roll No. \t Average ");
for(i=0; i<50; i++)
if (avg[i]<40)
System.out.println(rollno[i]+"\t"+avg[i]);
}
}

PUPIL TREE SCHOOL, BALLARI. Page 49 2023-2024


Computer Applications with Java & BlueJ Class X

2009 – Question 9. (Solution 2 Using Array of Objects) [15 Marks]

// Program to display annual examination results of students…

import java.util.*;

class Student
{
String name;
int rollno;
int sub1,sub2,sub3,total;
double avg;

static Scanner sc = new Scanner(System.in);

void input( )
{
System.out.println(" Enter Student name, rollno and 3 subjects marks ");
name = sc.next( );
rollno = sc.nextInt( );
sub1 = sc.nextInt( );
sub2 = sc.nextInt( );
sub3 = sc.nextInt( );
}

void compute( )
{
total = sub1 + sub2 + sub3;
avg = total / 3.0;
}

void output( )
{
System.out.println(rollno+"\t"+name+"\t"+sub1+"\t"+sub2+"\t"+sub3+"\t"+total+"\t"+avg);
}

public static void main( String args[ ] )


{
System.out.print(" Enter Students strength : ");
int n = sc.nextInt( );

Student s[ ] = new Student[n];


int i;

for (i=0; i<s.length; i++)


{
s[i] = new Student( );
s[i].input( );
s[i].compute( );
}

PUPIL TREE SCHOOL, BALLARI. Page 50 2023-2024


Computer Applications with Java & BlueJ Class X

System.out.println(" All Students Information is Below ");


for (i=0; i<n; i++)
s[i].output( );

System.out.println(" All Students whose average marks is above 80 ");


for (i=0; i<n; i++)
{
if (s[i].avg > 80)
s[i].output( );
}

System.out.println(" All Students whose average marks is below 40 ");


for (i=0; i<n; i++)
{
if (s[i].avg < 40)
s[i].output( );
}
}
}

OUTPUT:
Enter Students strength : 5
Enter Student name, rollno and 3 subjects marks
Tharun 101 64 71 55
Enter Student name, rollno and 3 subjects marks
Jack 102 81 79 83
Enter Student name, rollno and 3 subjects marks
Peter 103 38 25 19
Enter Student name, rollno and 3 subjects marks
Zafreen 104 99 87 92
Enter Student name, rollno and 3 subjects marks
Rakesh 105 41 40 37

All Students Information is Below


Roll Name sub1 sub2 sub3 total avg
101 Tharun 64 71 55 190 63.33
102 Jack 81 79 83 243 81.0
103 Peter 38 25 19 82 27.33
104 Zafreen 99 87 92 278 92.66
105 Rakesh 41 40 37 118 39.33

All Students whose average marks is above 80


Roll Name sub1 sub2 sub3 total avg
102 Jack 81 79 83 243 81.0
104 Zafreen 99 87 92 278 92.66

All Students whose average marks is below 40


Roll Name sub1 sub2 sub3 total avg
103 Peter 38 25 19 82 27.33
105 Rakesh 41 40 37 118 39.33

PUPIL TREE SCHOOL, BALLARI. Page 51 2023-2024


Computer Applications with Java & BlueJ Class X

2010 – Question 4. [15 Marks]


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,100

// Program to search an element in the array using binary search


// technique…

import java.util.*;

class BinarySearch
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner( System.in );

int A[ ] = {5,7,9,11,15,20,30,45,89,97,100};

System.out.print(" Enter an element to search : ");


int x = sc.nextInt( );

int start=0, end=A.length-1;


int mid, pos=-1;

while ( start <= end )


{
mid = ( start + end ) / 2;

if ( x == A[mid] )
{
pos = mid;
break;
}

if ( x > A[mid] )
start = mid + 1;
else
end = mid - 1;
}

if ( pos >= 0 )
System.out.println(" Element found at position : "+(pos+1) );
else
System.out.println(" Search element not found ");
}
}

OUTPUT:
Enter an element to search : 20
Element found at position : 6

PUPIL TREE SCHOOL, BALLARI. Page 52 2023-2024


Computer Applications with Java & BlueJ Class X

2010 – Question 5. [15 Marks]


Define a class Student described as below:
Data members/Instance variables:
name, age, m1, m2, m3 (marks in 3 subjects), maximum, average

Member methods:
(a) A parameterized constructor to initialize the data members.
(b) To accept the details of a student.
(c) To compute the average and the maximum out of three marks.
(d) 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.

// Program to compute student marks…

import java.util.*;

class Student
{
String name;
int age,m1,m2,m3,max;
double avg;

Student( ) { }

Student(String n, int age, int m1, int m2, int m3)


{
name = n;
this.age = age;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
}

void accept( )
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter Student name, age and 3 subject marks ");
name = sc.nextLine( );
age = sc.nextInt( );
m1 = sc.nextInt( );
m2 = sc.nextInt( );
m3 = sc.nextInt( );
}

void compute( )
{
avg = (m1+m2+m3) / 3.0;

if (m1>m2 && m1>m3)


max = m1;
else if (m2>m3)
max = m2;
else
max = m3;
}

PUPIL TREE SCHOOL, BALLARI. Page 53 2023-2024


Computer Applications with Java & BlueJ Class X

void display( )
{
System.out.println(name+"\t"+age+"\t"+m1+"\t"+m2+"\t"+m3+"\t"+max+"\t"+avg);
}

public static void main( String args[ ] )


{
Student a = new Student("Vishal",15,67,94,50);
a.compute( );
a.display( );

Student b = new Student( );


b.accept( );
b.compute( );
b.display( );
}
}

OUTPUT:
name age m1 m2 m3 max avg
Vishal 15 67 94 50 94 70.33

Enter Student name, age and 3 subject marks


Rishan
14
97
98
99

name age m1 m2 m3 max avg


Rishan 14 97 98 99 99 98.0

PUPIL TREE SCHOOL, BALLARI. Page 54 2023-2024


Computer Applications with Java & BlueJ Class X

2010 – Question 6. [15 Marks]


Shasha Travels Pvt Ltd. Gives the following discount to its customers:

Ticket amount Discount

Above Rs. 70,000 18%

Rs. 55,001 to Rs. 70,000 16%

Rs. 35,001 to Rs. 55,000 12%

Rs. 25,001 to Rs. 35,000 10%

Less than Rs. 25,001 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:
Sl.No. Name Ticket Charge Discount Net Amount
1 ----------- ------------------- ---------- -----------------
2 ----------- ------------------- ---------- -----------------
( Assume that there are 15 customers )

// Program to compute the discount on ticket amount…


import java.util.*;

class ShashaTravels
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

int n=5, r ;

String name[ ] = new String[n];


double tc[ ] = new double[n];
double dis[ ] = new double[n];
double net[ ] = new double[n];

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


{
System.out.print(" Enter name of the customer " + (i+1) + " : ");
name[i] = sc.nextLine( );
System.out.print(" Enter ticket charge : ");
tc[i] = Double.parseDouble(sc.nextLine( ));

if (tc[i] > 70000)


r = 18;
else if (tc[i] > 55000)
r = 16;
else if (tc[i] > 35000)
r = 12;
else if (tc[i] > 25000)
r = 10;
else
r = 2;

PUPIL TREE SCHOOL, BALLARI. Page 55 2023-2024


Computer Applications with Java & BlueJ Class X

dis[i] = tc[i] * r/100;


net[i] = tc[i] - dis[i];
}

System.out.println(" Shasha Travels Ticket Details ");


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

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


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

OUTPUT:
Enter name of the customer 1 : Jagadish
Enter ticket charge : 54000

Enter name of the customer 2 : Parinitha


Enter ticket charge : 86000

Enter name of the customer 3 : Ramesh


Enter ticket charge : 25000

Enter name of the customer 4 : Vaishnavi


Enter ticket charge : 55001

Enter name of the customer 5 : Sangeetha


Enter ticket charge : 35000

Shasha Travels Ticket Details


Sl.No Name Ticket charge Discount Net Amount
1 Jagadish 54000.0 6480.0 47520.0
2 Parinitha 86000.0 15480.0 70520.0
3 Ramesh 25000.0 500.0 24500.0
4 Vaishnavi 55001.0 8800.16 46200.84
5 Sangeetha 35000.0 3500.0 31500.0

PUPIL TREE SCHOOL, BALLARI. Page 56 2023-2024


Computer Applications with Java & BlueJ Class X

2010 – Question 7. (Solution 1) [15 Marks]


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 prime number if it is divisible only


by 1 and itself and not by any other number.
Example: 2, 3, 5, 7, 11, 13 etc.

(b) Automorphic number: An Automorphic number is a number which is


contained in the last digit(s) of its square.
Example: 76 is an automorphic number as its square is 5776 and 76 is
present as the last two digits.

/* Menu-driven program to check a number is prime or not


and automorphic or not… */

import java.util.*;

class AutoPrime
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a number ");


int num = sc.nextInt( );

boolean flag = true;

System.out.println(" Press 1 to check Prime or not ");


System.out.println(" Press 2 to check Automorphic or not ");

System.out.println(" Which is your choice? ");


int choice = sc.nextInt( );

switch ( choice )
{
case 1 : for (int i=2; i<=Math.sqrt(num); i++)
{
if (num%i == 0)
{
flag = false;
break;
}
}

if ( flag )
System.out.println(num + " is Prime ");
else
System.out.println(num + " is Composite ");
break;

PUPIL TREE SCHOOL, BALLARI. Page 57 2023-2024


Computer Applications with Java & BlueJ Class X

case 2 : int sqr = num * num;


while (num >= 1)
{
if (num%10 != sqr%10)
{
flag=false;
break;
}
num = num/10;
sqr = sqr/10;
}

if ( flag )
System.out.println(" Automorphic ");
else
System.out.println(" Not automorphic ");
break;

default : System.out.println(" Invalid choice ");


}
}
}

OUTPUT 1:
Enter a number
215
Press 1 to check Prime or not
Press 2 to check Automorphic or not
Which is your choice?
2
Not automorphic

OUTPUT 2:
Enter a number
49
Press 1 to check Prime or not
Press 2 to check Automorphic or not
Which is your choice?
1
49 is Composite

PUPIL TREE SCHOOL, BALLARI. Page 58 2023-2024


Computer Applications with Java & BlueJ Class X

2010 – Question 7. (Solution 2) [15 Marks]


/* Menu-driven program to check a number is prime or not and automorphic or not… */
import java.util.*;

class AutoPrime2
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter a number ");


int n = sc.nextInt( );

boolean flag = true;

System.out.println(" Press 1 to check Prime or not ");


System.out.println(" Press 2 to check Automorphic or not ");

System.out.print(" Which is your choice? ");


int choice = sc.nextInt( );

switch( choice )
{
case 1 : int factors=0;
for(int i=1; i<=n; i++)
{
if (n%i == 0)
factors++;
}
if (factors > 2)
System.out.println(n+" is Composite ");
else
System.out.println(n+" is Prime ");
break;

case 2 : int x=n, d=1;


while( n>=1 )
{
d = d*10;
n = n/10;
}
if ( (x*x)%d == x)
System.out.println(x+" is Automorphic ");
else
System.out.println(x+" is not automorphic ");
}
}
}
OUTPUT:
Enter a number 376
Press 1 to check Prime or not
Press 2 to check Automorphic or not

Which is your choice? 2


376 is Automorphic

PUPIL TREE SCHOOL, BALLARI. Page 59 2023-2024


Computer Applications with Java & BlueJ Class X

2010 – Question 8. [15 Marks]


Write a program to store 6 elements in an array P, and 4 elements in an array Q and produce a third array R,
containing all the elements of array P and Q. Display the resultant array.

Example:

P[]= 4 6 1 2 3 10
Input
Q[ ] = 19 23 7 8

Output R[ ] = 4 6 1 2 3 10 19 23 7 8

// Program to merge(join) two arrays…

import java.util.*;

class Merge
{
public static void main(String args[ ])
{
int P[ ] = {4,6,1,2,3,10};
int Q[ ] = {19,23,7,8};

int R[ ] = new int[P.length+Q.length];

int i, j;

for(i=0; i<P.length; i++)


R[i] = P[i];

for(j=0; j<Q.length; j++)


R[i+j] = Q[j];

for(i=0; i<R.length; i++)


System.out.print(R[i] + "\t");
}
}

OUTPUT:
4 6 1 2 3 10 19 23 7 8

Alternate Logic:

for (j=0; j<Q.length; j++) for (j=0; j<Q.length; j++,i++) for (j=0; j<Q.length; j++)
R[i+j] = Q[j]; R[i] = Q[j]; R[i++] = Q[j];

PUPIL TREE SCHOOL, BALLARI. Page 60 2023-2024


Computer Applications with Java & BlueJ Class X

2010 – Question 9. (Solution 1) [15 Marks]


Write a program to input a string in uppercase and print the frequency of each character.
Example:
Input : COMPUTER HARDWARE
Output:

Characters: A C D E H M O P R T U W

Frequency: 2 1 1 2 1 1 1 1 3 1 1 1

// Program to print frequency of each character of the string...


import java.util.*;

class Frequency
{
public static void main( String args[ ] )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a string ");


String s = sc.nextLine( ).toUpperCase( );

for (char ch='A'; ch<='Z'; ch++)


{
int count=0;

for(int i=0; i<s.length( ); i++)


{
if ( ch == s.charAt(i) )
count++;
}

if (count > 0)
System.out.println(ch + "\t" + count);
}
}
}

OUTPUT:
Enter a string
Computer Hardware
A 2
C 1
D 1
E 2
H 1
M 1
O 1
P 1
R 3
T 1
U 1
W 1

PUPIL TREE SCHOOL, BALLARI. Page 61 2023-2024


Computer Applications with Java & BlueJ Class X

2010 – Question 9. (Solution 2) [15 Marks]

// Program to print frequency of each character of the string...


import java.util.*;

class Frequency2
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a string ");


String s = sc.nextLine( ).toUpperCase( );

int alpha[ ] = new int[26];

for(int i=0; i<s.length(); i++)


{
char ch = s.charAt(i);

if ( Character.isLetter(ch) )
alpha[ch - 'A'] ++;
}

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


{
if ( alpha[i] != 0 )
System.out.println( (char)('A'+i) + "\t" + alpha[i] );
}
}
}

OUTPUT:
Enter a string
an apple a day keeps the doctor away
A 6
C 1
D 2
E 4
H 1
K 1
L 1
N 1
O 2
P 3
R 1
S 1
T 2
W 1
Y 2

PUPIL TREE SCHOOL, BALLARI. Page 62 2023-2024


Computer Applications with Java & BlueJ Class X

2011 – Question 4. [15 Marks]


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/functions:
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. Phone No. Name No. Of days Charge
---------- ------------ ------- --------------- ---------

// Program to compute the rental charge of bike...


import java.util.*;

class Mobike
{
String name;
int bno,phno,days,charge;

void input( )
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter Bike number : ");


bno = sc.nextInt( );
System.out.print(" Enter Customer name and phone number : ");
name = sc.nextLine( );
phno = sc.nextInt( );
System.out.print(" Enter number of days bike is taken on rent : ");
days = sc.nextInt( );
}

void compute( )
{
if ( days <= 5 )
charge = days * 500;
else if ( days <= 10 )
charge = 2500 + (days-5)*400;
else
charge = 2500 + 2000 + (days-10)*200;
}

PUPIL TREE SCHOOL, BALLARI. Page 63 2023-2024


Computer Applications with Java & BlueJ Class X

void display( )
{
System.out.println(" Bike No. \t Phone No. \t Name \t No.of days \t Charge ");
System.out.println(bno+"\t"+phno+"\t"+name+"\t"+days+"\t"+charge);
}

public static void main(String args[ ])


{
Mobike obj = new Mobike( );

obj.input( );
obj.compute( );
obj.display( );
}
}

OUTPUT 1:
Enter Bike number : 1234
Enter Customer name and phone number : Chavi
9876543210
Enter number of days bike is taken on rent : 9
Bike No. Phone No. Name No.of days Charge
1234 9876543210 Chavi 9 4100

OUTPUT 2:
Enter Bike number : 5678
Enter Customer name and phone number : Gouri
9876543210
Enter number of days bike is taken on rent : 14
Bike No. Phone No. Name No.of days Charge
5678 9876543210 Gouri 14 5300

PUPIL TREE SCHOOL, BALLARI. Page 64 2023-2024


Computer Applications with Java & BlueJ Class X

2011 – Question 5. [15 Marks]


Write a program to input and store the weight of ten people. Sort and display them in descending order using the
selection sort technique.
// Program to sort the weight of ten people in descending order
// using the selection sort technique…

import java.util.*;

class SelectionSort
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" How many people are there? ");


int n = sc.nextInt( );

int A[ ] = new int [n];

System.out.println(" Enter weight of "+n+" people ");


for(int i=0; i<A.length; i++)
A[i] = sc.nextInt( );

int pos, temp;

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


{
pos = i;
for(int j=i+1; j<n; j++)
{
if ( A[pos] < A[j] )
pos = j;
}
temp = A[i];
A[i] = A[pos];
A[pos] = temp;
}

System.out.println(" Weight of people in descending order ");


for(int i=0; i<=A.length-1; i++)
System.out.println(A[i]);
}
}

OUTPUT:
How many people are there? 5
Enter weight of 5 people
19 34 82 100 67
Weight of people in descending order
100 82 67 34 19

PUPIL TREE SCHOOL, BALLARI. Page 65 2023-2024


Computer Applications with Java & BlueJ Class X

2011 – Question 6. [15 Marks]


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.)

Example:
145 is a special number, because 1! + 4! + 5! = 1 + 24 + 120 = 145
(Where ! stands for factorial of the number and the factorial value of a number is the product of all integers
from 1 to that number, example 5! = 1*2*3*4*5 = 120).

// Program to check the number is special or not...


import java.util.*;

class Special
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter the number ");


int n = sc.nextInt( );

int sum=0, fact, d, x=n;

while( n>=1 )
{
d = n%10;
n = n/10;

fact = 1;
for(int i=1; i<=d; i++)
fact = fact * i;

sum = sum + fact;


}

System.out.println(" Sum = "+sum);

if ( sum == x )
System.out.println(" Special number ");
else
System.out.println(" Not special number ");
}
}

OUTPUT 1: OUTPUT 2: OUTPUT 3:


Enter the number Enter the number Enter the number
3054 40585 6
Sum = 151 Sum = 40585 Sum = 720
Not special number Special number Not special number

Note: 0 factorial is equal to 1

PUPIL TREE SCHOOL, BALLARI. Page 66 2023-2024


Computer Applications with Java & BlueJ Class X

2011 – Question 7. (Solution 1) [15 Marks]


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 character following it.
Example:
Input: computer
Output: cpmpvtfr

// Program to replace vowels with the character following it…


import java.util.*;

class ReplaceVowels
{
public static void main( String args[] )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a word ");


String s = sc.nextLine( ).toLowerCase( );

for (int i=0; i<s.length( ); i++)


{
char ch = s.charAt(i);

if ( ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' )


System.out.print( ++ch );
else
System.out.print( ch );
}
}
}

OUTPUT:
Enter a word
Covid19
cpvjd19

PUPIL TREE SCHOOL, BALLARI. Page 67 2023-2024


Computer Applications with Java & BlueJ Class X

2011 – Question 7. (Solution 2) [15 Marks]

// Program to replace vowels with the character following it...


import java.util.*;

class ReplaceVowels2
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a word ");


String s = sc.nextLine().toLowerCase();

String nw = "";

for(int i=0; i<s.length(); i++)


{
char ch = s.charAt(i);

if ( ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')


ch++;

nw += ch;
}

System.out.println("Encoded word is: " + nw);


}
}

OUTPUT 1:
Enter a word
computer
Encoded word is: cpmpvtfr

OUTPUT 2:
Enter a word
emu
Encoded word is: fmv

Note:
where nw = new word

PUPIL TREE SCHOOL, BALLARI. Page 68 2023-2024


Computer Applications with Java & BlueJ Class X

2011 – Question 8. (Solution 1) [15 Marks]


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(ASCII) values of two
characters and print the 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.

// Program to overload compare function(method)…

import java.util.*;

class Overload
{
public static void compare(int x, int y)
{
if (x>y)
System.out.println(x);
else
System.out.println(y);
}

public static void compare(char ch1, char ch2)


{
if (ch1>ch2)
System.out.println(ch1);
else
System.out.println(ch2);
}

public static void compare(String s1, String s2)


{
if ( s1.length() > s2.length() )
System.out.println(s1);
else
System.out.println(s2);
}

public static void main(String args[ ])


{
compare(45,54);
compare('a','A');
compare("Visvesvaraya", "Vishweshwaraiah");
}
}

OUTPUT:
54
a
Vishweshwaraiah

PUPIL TREE SCHOOL, BALLARI. Page 69 2023-2024


Computer Applications with Java & BlueJ Class X

2011 – Question 8. (Solution 2 & 3) [15 Marks]

Solution 2
import java.util.*;

class Overload
{
public static void compare(int x, int y)
{
int big = (x>y)? x : y ;
System.out.println(big);
}

public static void compare(char ch1, char ch2)


{
char big = (ch1>ch2)? ch1 : ch2 ;
System.out.println(big);
}

public static void compare(String s1, String s2)


{
String big = ( s1.length() > s2.length() )? s1: s2 ;
System.out.println(big);
}

Solution 3
import java.util.*;

class Overload
{
public static void compare(int x, int y)
{
System.out.println( Math.max(x,y) );
}

public static void compare(char ch1, char ch2)


{
System.out.println( (char) Math.max(ch1,ch2) );
}

public static void compare(String s1, String s2)


{
System.out.println( (s1.length() > s2.length())? s1: s2);
}

PUPIL TREE SCHOOL, BALLARI. Page 70 2023-2024


Computer Applications with Java & BlueJ Class X

2011 – Question 9. [15 Marks]


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:
Sum = 1/2 + 3/4 + 5/6 + 7/8... 19/20.

// Program to compute the series based on user choice…


import java.util.*;

class Series
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Press 1 to print 0,3,8,15,24...n terms ");


System.out.println(" Press 2 to find sum of 1/2+3/4+...+19/20 ");

System.out.print(" Enter your choice : ");


int choice = sc.nextInt( );

switch(choice)
{
case 1 : System.out.print(" Enter value of n : ");
int n = sc.nextInt( );
for(int i=1; i<=n; i++)
System.out.println( i*i-1 );
break;

case 2 : double sum=0;


for(int i=2; i<=20; i=i+2)
sum = sum + (i-1) / (double) i;
System.out.println(sum);
break;

default : System.out.println(" Invalid choice ");


}
}
}

OUTPUT 1: OUTPUT 2:
Press 1 to print 0,3,8,15,24...n terms Press 1 to print 0,3,8,15,24...n terms
Press 2 to find sum of 1/2+3/4+...+19/20 Press 2 to find sum of 1/2+3/4+...+19/20
Enter your choice : 1 Enter your choice : 2
Enter value of n : 10 8.5355
0 3 8 15 24 35 48 63 80 99

PUPIL TREE SCHOOL, BALLARI. Page 71 2023-2024


Computer Applications with Java & BlueJ Class X

2012 – Question 4. [15 Marks]


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:
void input( ) To input and store the accession number, title and author.
void compute( ) To accept the number of days late, calculate and display
the fine charged at the rate of Rs. 2 per day.
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.
// Program to calculate fine charged on the book…
import java.util.*;

class Library
{ int acc_num, days;
String title, author;
Scanner sc = new Scanner(System.in);

void input( )
{
System.out.println(" Enter accession number, title and author of the book");
acc_num = Integer.parseInt(sc.nextLine( ));
title = sc.nextLine( );
author = sc.nextLine( );
}

void compute( )
{
System.out.print(" Enter number of days late : ");
days = sc.nextInt( );
System.out.println(" Fine amount = "+ days*2);
}

void display( )
{
System.out.println(" Accession number \t Title \t Author ");
System.out.println(acc_num + "\t" + title + "\t" + author );
}
OUTPUT
public static void main(String args[ ]) Enter accession number, title and author
{ 2001
Library obj = new Library( ); Harry Potter
J K Rowling
obj.input( );
obj.compute( ); Enter number of days late : 12
obj.display( ); Fine amount = 24
}
} Accession number Title Author
2001 Harry Potter J K Rowling

PUPIL TREE SCHOOL, BALLARI. Page 72 2023-2024


Computer Applications with Java & BlueJ Class X

2012 – Question 5. [15 Marks]


Given below is a hypothetical(assumption) table showing rates of Income Tax for male citizens below the age
of 65 years:
Taxable Income(TI) in Rs. Income Tax in Rs.

Does not exceed ₹ 1,60,000 Nil

Is greater than ₹ 1,60,000 & less than or (TI – 1,60,000) x 10%


equal to ₹ 5,00,000

Is greater than ₹ 5,00,000 & less than or [(TI - 5,00,000) x 20%] + 34,000
equal to ₹ 8,00,000

Is greater than ₹ 8,00,000 [(TI - 8,00,000) x 30%] + 94,000


Write a program to input the age, gender(male or female) and Taxable Income of a person.If the person 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 then compute and display the Income Tax payable as per the table given above.
// Program to compute income tax for male citizens below age of 65 years...
import java.util.*;

class IncomeTax
{ public static void main(String args[ ])
{ Scanner sc = new Scanner(System.in);

int age;
char gender;
double TI, tax;

System.out.print(" Enter the age : ");


age = sc.nextInt( );

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


gender = Character.toUpperCase(sc.next().charAt(0));

if ( age>65 || gender=='F' )
{
System.out.print(" Wrong category ");
System.exit(0);
}

System.out.print(" Enter taxable income : ");


TI = sc.nextDouble( );

if (TI <= 160000)


tax = 0; OUTPUT
else if (TI <= 500000) Enter the age : 31
tax = (TI-160000) * 0.1; Enter gender : male
else if (TI <= 800000) Enter taxable income : 750000
tax = (TI – 500000) * 0.2 + 34000; Income Tax Payable : 84000.0
else
tax = (TI – 800000) * 0.3 + 94000;

System.out.print(" Income Tax Payable : "+tax);


}}

PUPIL TREE SCHOOL, BALLARI. Page 73 2023-2024


Computer Applications with Java & BlueJ Class X

2012 – Question 6. [15 Marks]


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.
Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Output: 4

// Program to count the number of double letter sequences in the string

import java.util.*;

class DoubleLetter
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a string ");


String s = sc.nextLine().toUpperCase( );

System.out.println(s);

int count=0;

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


{
char x = s.charAt(i);
char y = s.charAt(i+1);

if (x == y)
{
count++;
System.out.println(x+""+y);
}
}
System.out.println(" Total double letter sequences are = "+count);
}
}

OUTPUT 1:
Enter a string
she was feeding the little rabbit with an apple
SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
EE
TT
BB
PP
Total double letter sequences are = 4

OUTPUT 2:
Enter a string
Google pizza
GOOGLE PIZZA
OO
ZZ
Total double letter sequences are = 2

PUPIL TREE SCHOOL, BALLARI. Page 74 2023-2024


Computer Applications with Java & BlueJ Class X

2012 – Question 7. [15 Marks]


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 arguments that draws a filled triangle shown below.

Example:
(i) Input value of n=2, ch=’O’ (ii) Input value of x=2, y=5 (iii) No Input Value
Output: Output: Outptut:
OO @@@@@ *
OO @@@@@ **
***

/* Program to overload a function polygon… */


import java.util.*; public static void main( String args[ ] )
{
class Overload polygon( );
{ polygon(4,8);
public static void polygon(int n, char ch) polygon(5,'$');
{ }
for(int i=1; i<=n; i++) }
{
for(int j=1; j<=n; j++)
System.out.print(ch); OUTPUT:
System.out.println( ); *
} **
} ***

@@@@@@@@
public static void polygon(int x, int y) @@@@@@@@
{ @@@@@@@@
for(int i=1; i<=x; i++) @@@@@@@@
{
for(int j=1; j<=y; j++)
System.out.print("@"); $$$$$
System.out.println( ); $$$$$
} $$$$$
} $$$$$
$$$$$
public static void polygon( )
{
for(int i=1; i<=3; i++)
{
for(int j=1; j<=i; j++)
System.out.print("*");
System.out.println( );
}
}

PUPIL TREE SCHOOL, BALLARI. Page 75 2023-2024


Computer Applications with Java & BlueJ Class X

2012 – Question 8. [15 Marks]


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, 8, …
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.

// Program to generate fibonacci series upto 10 and to find sum of digits...


import java.util.*;

class MenuDriven
{ public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println(" 1.Fibonacci series upto 10 ");


System.out.println(" 2.Sum of the digits ");

System.out.print(" Enter your choice : ");


int choice = sc.nextInt( );

switch(choice)
{
case 1 : int c,a=0,b=1;
System.out.println(" Fibonacci series upto 10 ");
System.out.print(a + ",");
System.out.print(b + ",");
for(int i=3; i<=10; i++) OUTPUT:
{ 1.Fibonacci series upto 10
c = a + b; 2.Sum of the digits
System.out.print(c+",");
a = b; Enter your choice : 1
b = c; Fibonacci series upto 10
} 0,1,1,2,3,5,8,13,21,34,
break;

case 2 : int n,d,sum=0;


System.out.print(" Enter a number : ");
n = sc.nextInt( );
while( n != 0 )
{
d = n % 10;
sum = sum + d;
n = n/10;
}
System.out.println(" Sum of the digits = "+ sum);
break;
default : System.out.println(" Wrong Choice ");
}
} }

PUPIL TREE SCHOOL, BALLARI. Page 76 2023-2024


Computer Applications with Java & BlueJ Class X

2012 – Question 9. [15 Marks]


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”.

// Program to search a city in the array and display its STD code…

import java.util.*;

class StdCode
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

int n=10;
String city[ ] = new String[n];
int std[ ] = new int[n];

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


{
System.out.println(" Enter name of city "+(i+1));
city[i] = sc.nextLine( );

System.out.println(" Enter "+ city[i] +" STD code ");


std[i] = Integer.parseInt(sc.nextLine( ));
}

System.out.println(" Enter a city name to search ");


String x = sc.nextLine( );

int pos = -1;

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


{
if ( x.equalsIgnoreCase(city[i]) )
{
pos = i;
break;
}
}

if (pos == -1)
System.out.println("Search Unsuccessful, No such city in the list");
else
{
System.out.println("Search Successful");
System.out.println(city[pos]+" STD Code is "+std[pos]);
}
}
}

PUPIL TREE SCHOOL, BALLARI. Page 77 2023-2024


Computer Applications with Java & BlueJ Class X

OUTPUT:
Enter name of city 1
Tawang
Enter Tawang STD code
03794

Enter name of city 2


Varanasi
Enter Varanasi STD code
0542

Enter name of city 3


Gulmarg
Enter Gulmarg STD code
01953

Enter name of city 4


Agartala
Enter Agartala STD code
0381

Enter name of city 5


Rameswaram
Enter Rameswaram STD code
04573

Enter name of city 6


Porbandar
Enter Porbandar STD code
0286

Enter name of city 7


Jabalpur
Enter Jabalpur STD code
0761

Enter name of city 8


Kavaratti
Enter Kavaratti STD code
04896

Enter name of city 9


Ludhiana
Enter Ludhiana STD code
0161

Enter name of city 10


Port Blair
Enter Port Blair STD code
23406

Enter a city name to search


Jabalpur
Search Successful
Jabalpur STD Code is 0761

PUPIL TREE SCHOOL, BALLARI. Page 78 2023-2024


Computer Applications with Java & BlueJ Class X

2013 – Question 4. [15 Marks]


Define a class named FruitJuice with the following description:

Instance variables/Data members:


int product_code stores the product code number
String flavor stores the flavor of the juice. (E.g. orange, apple, etc.)
String pack_type stores the type of packaging (E.g. tetra-pack, PET bottle, etc.)
int pack_size stores package size (E.g. 200 ml, 400 ml, etc.)
int product_price stores the price of the product

Member methods:
(i) FruitJuice( ) - Default constructor to initialize integer data members to 0 and string
data members to “”(NULL)
(ii) void input( ) - To input and store the product code, flavor, pack type, pack size and
product price.
(iii) void discount( ) - To reduce the product price by 10
(iv) void display( ) - To display the product code, flavor, pack type, pack size and product price.

// Program to input fruit juice price and reduce the price by 10 Rs.

import java.util.*;

class FruitJuice
{
int product_code;
String flavour;
String pack_type;
int pack_size;
int product_price;

FruitJuice( )
{
product_code = pack_size = product_price = 0;
flavour = pack_type = "";
}

void input( )
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter product code : ");


product_code = Integer.parseInt(sc.nextLine( ));

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


flavour = sc.nextLine( );

System.out.print(" Enter pack type : ");


pack_type = sc.nextLine( );

System.out.print(" Enter pack size : ");


pack_size = sc.nextInt( );

System.out.print(" Enter product price : ");


product_price = sc.nextInt( );
}

PUPIL TREE SCHOOL, BALLARI. Page 79 2023-2024


Computer Applications with Java & BlueJ Class X

void discount( )
{
product_price -= 10;
}

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);
}

public static void main(String args[ ])


{
FruitJuice obj = new FruitJuice( );
obj.input( );
obj.discount( );
obj.display( );
}
}

OUTPUT 1:
Enter product code : 101
Enter flavour : Strawberry milkshake
Enter pack type : Tetra pack
Enter pack size : 250
Enter product price : 50

Product Code : 101


Flavour : Strawberry milkshake
Pack Type : Tetra pack
Pack Size : 250
Product Price: 40

OUTPUT 2:
Enter product code : 102
Enter flavour : Pine Apple
Enter pack type : PET bottle
Enter pack size : 500
Enter product price : 90

Product Code : 102


Flavour : Pine Apple
Pack Type : PET bottle
Pack Size : 500
Product Price: 80

PUPIL TREE SCHOOL, BALLARI. Page 80 2023-2024


Computer Applications with Java & BlueJ Class X

2013 – Question 5. (Solution 1) [15 Marks]


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 8750942123


Sum = 1X8 + 2X7 + 3X5 + 4X0 + 5X9 + 6X4 + 7X2 + 8X1 + 9X2 + 10X3 = 176
Sum 176 which is divisible by 11.

Write a program to:


(a) Input the ISBN code as 10-digit integer.
(b) If the ISBN is not a 10-digit integer, output the message, “Illegal ISBN” and terminate the program.
(c) 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”.

// Program to check given ISBN is legal or not...


import java.util.*;

class ISBN
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter an ISBN code : ");


long isbn = sc.nextLong( );

long n = isbn;
int count=0;

while( n>0 )
{
count++;
n=n/10;
}

if ( count != 10 )
{
System.out.println(" ISBN is wrong! ");
return;
}

n = isbn;
long d, sum=0;

for(int i=10; i>=1; i--)


{
d = n % 10;
sum = sum + d * i;
n = n/10;
}

PUPIL TREE SCHOOL, BALLARI. Page 81 2023-2024


Computer Applications with Java & BlueJ Class X

System.out.println(" sum = "+sum);

if ( sum%11 == 0 )
System.out.println(" Legal ISBN ");
else
System.out.println(" Illegal ISBN ");
}
}

OUTPUT 1: OUTPUT 2: OUTPUT 3: OUTPUT 4:


Enter an ISBN code: Enter an ISBN code: Enter an ISBN code: Enter an ISBN code:
1401601499 14016014999 123 1401607499
sum = 253 ISBN is wrong! ISBN is wrong! sum = 295
Legal ISBN Illegal ISBN

Alternate Logic:
long d, sum=0; long sum = 0;
for (int i=10; i>=1; i--) int i=10;
{ while (n>0)
d = n % 10; {
sum = sum + d * i; sum += n%10 * i;
n = n/10; n/=10;
} i--;
}

PUPIL TREE SCHOOL, BALLARI. Page 82 2023-2024


Computer Applications with Java & BlueJ Class X

2013 – Question 5. (Solution 2) [15 Marks]


// Program to check given ISBN is legal or not…
import java.util.*;

class ISBN
{
public static void main(String args[ ])
{ Scanner sc = new Scanner(System.in);

System.out.println(" Enter an ISBN code : ");


String isbn = sc.next( );

if ( isbn.length() != 10 )
{
System.out.println(" ISBN is wrong! ");
System.exit(0);
}

int sum = 0;

for (int i=0; i<isbn.length( ); i++)


{
char ch = isbn.charAt(i);
int d = ch - 48;
sum = sum + d * (i+1);
}

System.out.println(" sum = " + sum);

if ( sum%11 == 0 )
System.out.println(" Legal ISBN ");
else
System.out.println(" Illegal ISBN ");
}
}
OUTPUT 1: OUTPUT 2: OUTPUT 3:
Enter an ISBN code : Enter an ISBN code : Enter an ISBN code :
8750942123 123 8750942163
sum = 176 ISBN is wrong! sum = 212
Legal ISBN Illegal ISBN

Alternate Logic:

for (int i=0; i<isbn.length( ); i++) for (int i=0; i<isbn.length( ); i++)
{ {
char ch = isbn.charAt(i); int d = Integer.parseInt( isbn.charAt(i)+"" );
int d = ch - 48; sum = sum + d * (i+1);
sum = sum + d * (i+1); }
}

Note: The ASCII values of character ‘0’, ‘1’, ‘2’, ‘3’ … ‘9’ are 48, 49, 50 ...57. If we convert character ‘0’ to
Integer we get 48 but to get numeric 0 we have to subtract 48.
Example1: ‘0’ - 48 = 0 Example2: ‘9’ - 48 = 9

PUPIL TREE SCHOOL, BALLARI. Page 83 2023-2024


Computer Applications with Java & BlueJ Class X

2013 – Question 6. (Solution 1) [15 Marks]


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”.
Input(1): London Output(1): ONDONLAY
Input(2): Olympics Output(2): OLYMPICSAY

// Program to translate a word into piglatin word…

import java.util.*;

class Piglatin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter a word ");


String s = sc.next( ).toUpperCase( );

String a = "" ;
String b = "" ;

boolean vowelFound = false;

for (int i = 0; i < s.length(); i++)


{
char ch = s.charAt(i);

if (vowelFound)
b = b + ch;
else if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
b = b + ch;
vowelFound = true;
}
else
a = a + ch;
}

String piglatin = b + a + "AY";


System.out.println("Piglatin = " + piglatin);
}
}

OUTPUT 1: OUTPUT 2: OUTPUT 3:


Enter a word Enter a word Enter a word
Crocodile Ostrich Sky
Piglatin = OCODILECRAY Piglatin = OSTRICHAY Piglatin = SKYAY

PUPIL TREE SCHOOL, BALLARI. Page 84 2023-2024


Computer Applications with Java & BlueJ Class X

2013 – Question 6. (Solution 2) [15 Marks]

// Program to translate a word into Piglatin word…

import java.util.*;

class Piglatin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter a word ");


String s = sc.next( ).toUpperCase( );

int i;

for(i=0; i<s.length(); i++)


{
char ch = s.charAt(i);

if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')


break;
}

String a = s.substring(0,i);
String b = s.substring(i);

String piglatin = b + a + "AY";


System.out.println("Piglatin = " + piglatin);
}
}

OUTPUT 1: OUTPUT 2: OUTPUT 3:


Enter a word Enter a word Enter a word
Snake Elephant Gym
Piglatin = AKESNAY Piglatin = ELEPHANTAY Piglatin = GYMAY

PUPIL TREE SCHOOL, BALLARI. Page 85 2023-2024


Computer Applications with Java & BlueJ Class X

2013 – Question 6. (Solution 3 Using StringBuffer) [15 Marks]

// Program to translate a word into Piglatin word...


import java.util.*;

class Piglatin
{
public static void main(String[ ] args)
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter a word ");


String s = sc.next( ).toUpperCase( );

StringBuffer sb = new StringBuffer(s);

for(int i=0; i<sb.length(); i++)


{
char ch = sb.charAt(0);

if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')


break;

sb.append(ch);
sb.deleteCharAt(0);
}
sb.append("AY");
System.out.println("Piglatin = " + sb);
}
}

OUTPUT 1: OUTPUT 2: OUTPUT 3:


Enter a word Enter a word Enter a word
Python Android Hymn
Piglatin = ONPYTHAY Piglatin = ANDROIDAY Piglatin = HYMNAY

PUPIL TREE SCHOOL, BALLARI. Page 86 2023-2024


Computer Applications with Java & BlueJ Class X

2013 – Question 7. [15 Marks]


Write a program to input 10 integer elements in an array and sort them in descending order using the bubble
sort technique.

/* Program to sort 10 integers in descending order


using Bubble sort technique */

import java.util.*;

class BubbleSort
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

int n=10, i, j, temp;

int A[ ] = new int[n];

System.out.println(" Enter "+n+" elements ");


for(i=0; i<n; i++)
A[i] = sc.nextInt( );

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


{
for (j=0; j<n-i; j++)
if ( A[j] < A[j+1] )
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}

System.out.println(" Elements in descending order are below ");


for(i=0; i<n; i++)
System.out.print(A[i]+" ");
}
}

OUTPUT:
Enter 10 elements
-11 26 40 52 65 77 -10 98 50 13

Elements in descending order are below


98 77 65 52 50 40 26 13 -10 -11

PUPIL TREE SCHOOL, BALLARI. Page 87 2023-2024


Computer Applications with Java & BlueJ Class X

2013 – Question 8. [15 Marks]


Design a class to overload a function series( ) as follows:
(i) double series(double n) with one double argument and returns the sum of
series.
1 1 1 1
sum = 1 + 2 + 3 +. . . . . . + 𝑛

(ii) double series(double a, double n) with two double arguments and returns
the sum of the series.
1 4 7 10
sum = 2 + 5 + 8 + 11 +. . . . . . + to n terms
𝑎 𝑎 𝑎 𝑎

// Program to overload a function series( ) as follows…

import java.util.*;

class Overload
{
public static double series(double n)
{
double sum = 0;

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


sum = sum + 1.0/i;

return sum;
}

public static double series(double a, double n)


{
double sum = 0;

for (int i=1,x=2; i<=n; i++,x+=3)


sum = sum + (x-1) / Math.pow(a,x);

return sum;
}

public static void main(String args[ ])


{
System.out.println( series(5) );
System.out.println( series(2,11) );
}

OUTPUT:
2.2833333333
0.4072265625

PUPIL TREE SCHOOL, BALLARI. Page 88 2023-2024


Computer Applications with Java & BlueJ Class X

2013 – Question 9. [15 Marks]


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 than one factor 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 message should be displayed.

/* Menu-driven program using switch-case to check a number is a


composite number or not and to find the smallest digit of a number */

import java.util.*;

class Menu
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a number ");


int n = sc.nextInt();

System.out.println(" Enter 1 to check composite number or not ");


System.out.println(" Enter 2 to find the smallest digit of an integer ");

System.out.println(" Which is your choice? ");


int choice = sc.nextInt( );

switch ( choice )
{
case 1 : boolean flag = true;
for (int i=2; i<=n/2; i++)
{
if ( n%i == 0 )
{
flag = false;
break;
}
}
if ( flag )
System.out.println(n + " is Prime ");
else
System.out.println(n + " is Composite ");
break;

case 2 : int small, d;

PUPIL TREE SCHOOL, BALLARI. Page 89 2023-2024


Computer Applications with Java & BlueJ Class X

small = n%10;
n = n/10; int small=9, d;
while( n >= 1)
while(n >= 1) {
{ d = n%10;
d = n%10; OR n = n/10;
n = n/10;
if (small > d)
if (small > d) small = d;
small = d; }
}

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


break;

default : System.out.println(" Invalid choice ");


}
}
}

OUTPUT 1: OUTPUT 2:
Enter a number Enter a number
1947 13
Enter 1 to check composite number or not Enter 1 to check composite number or not
Enter 2 to find the smallest digit Enter 2 to find the smallest digit
Which is your choice? 2 Which is your choice? 1
Smallest digit is 1 13 is Prime

OUTPUT 3: OUTPUT 3:
Enter a number Enter a number
15 2658
Enter 1 to check composite number or not Enter 1 to check composite number or not
Enter 2 to find the smallest digit Enter 2 to find the smallest digit
Which is your choice? 1 Which is your choice? 2
15 is Composite Smallest digit is 2

PUPIL TREE SCHOOL, BALLARI. Page 90 2023-2024


Computer Applications with Java & BlueJ Class X

2014 – Question 4. [15 Marks]


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:
MovieMagic( ) Default constructor to initialize numeric data
members to 0 and String data member to “”(NULL).
void accept( ) To input and store year, title and rating.
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.

// Program to display a message based on movie rating…


import java.util.*;

class MovieMagic
{
int year;
String title;
float rating;

MovieMagic( )
{
year = 0;
title = "";
rating = 0.0F;
}

void accept( )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter the year of release of a movie ");


year = Integer.parseInt(sc.nextLine());
System.out.println(" Enter title of the movie ");
title = sc.nextLine( );
System.out.println(" Enter rating of the movie between 0.0 to 5.0 ");
rating = sc.nextFloat( );
}

PUPIL TREE SCHOOL, BALLARI. Page 91 2023-2024


Computer Applications with Java & BlueJ Class X

void display( )
{
if ( rating <= 2.0 )
System.out.println(title + " is Flop ");
else if ( rating <= 3.4 )
System.out.println(title + " is Semi-Hit ");
else if ( rating <= 4.5 )
System.out.println(title + " is Hit ");
else if ( rating <= 5.0 )
System.out.println(title + " is Super Hit ");
else
System.out.println(" You entered wrong rating ");
}

public static void main( String args[ ] )


{
MovieMagic obj = new MovieMagic( );
obj.accept( );
obj.display( );
}
}

OUTPUT 1: OUTPUT 2:
Enter the year of release of a movie Enter the year of release of a movie
2016 2018

Enter title of the movie Enter title of the movie


Avatar Kantara

Enter rating of the movie between 0.0 to 5.0 Enter rating of the movie between 0.0 to 5.0
5.0 5.1

The Avatar is Super Hit You entered wrong rating

PUPIL TREE SCHOOL, BALLARI. Page 92 2023-2024


Computer Applications with Java & BlueJ Class X

2014 – Question 5. [15 Marks]


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”.

// Program to check a number is special 2 digit number or not…


import java.util.*;

class Special2Digit
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a number ");


String s = sc.next( );

if ( s.length() != 2 )
{
System.out.println(" Input is not a two-digit number! ");
System.exit(0);
}

int n = Integer.parseInt( s );

int d1=n%10, d2=n/10;

if ( n == (d1+d2) + (d1*d2) )
System.out.println(" Special 2-digit number ");
else
System.out.println(" Not a special 2-digit number ");
}
}
OUTPUT 1: OUTPUT 2: OUTPUT 3:
Enter a number Enter a number Enter a number
1234 69 96
Input is not a two-digit number! Special 2-digit number Not a special 2-digit number

Alternate Logic:
System.out.println(" Enter a number ");
int n = sc.nextInt( );
if ( n<10 || n>99 )
{
System.out.println(" Invalid Input! ");
return;
}

PUPIL TREE SCHOOL, BALLARI. Page 93 2023-2024


Computer Applications with Java & BlueJ Class X

2014 – Question 6. [15 Marks]


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

// Program to extract the file name and file extension from a full path…

import java.util.*;

class PathProcess
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a full path ");


String fp = sc.nextLine( );

int lsp = fp.lastIndexOf("\\");


int dotp = fp.indexOf(".");

String path = fp.substring(0,lsp+1);


String fn = fp.substring(lsp+1,dotp);
String ext = fp.substring(dotp+1);

System.out.println(" Path: " + path);


System.out.println(" File name: " + fn);
System.out.println(" Extension: " + ext);
}
}

OUTPUT:
Enter a full path
D:\Songs\Hindi\SlumdogMillionaire\JaiHo.mp3

Path: D:\Songs\Hindi\SlumdogMillionaire\
File name: JaiHo
Extension: mp3

Note 1: In above program fp = full path, lsp = last slash position, dotp = dot position,
fn = file name and ext = extension.

Note 2: If we want to assign a full path in the program then include extra slash to distinguish escape sequence.

Example: String fp = “C:\\Users\\admin\\Pictures\\flower.jpg”

PUPIL TREE SCHOOL, BALLARI. Page 94 2023-2024


Computer Applications with Java & BlueJ Class X

2014 – Question 7. [15 Marks]


Design a class to overload a function area( ) as follows:
(i) double area(double a, double b, double c) with three double arguments, returns the
area of a scalene triangle using the formula:
(𝑎+𝑏+𝑐)
𝑎𝑟𝑒𝑎 = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐) 𝑤ℎ𝑒𝑟𝑒 𝑠 =
2
(ii) double area(int a, int b, int height) with three integer arguments, returns the area of
a trapezium using the formula:
1
𝑎𝑟𝑒𝑎 = 2 ℎ𝑒𝑖𝑔ℎ𝑡(𝑎 + 𝑏)
(iii) double area(double diagonal1, double diagonal2) with two double arguments, returns
the area of a rhombus using the formula:
1
𝑎𝑟𝑒𝑎 = (𝑑𝑖𝑎𝑔𝑜𝑛𝑎𝑙1𝑋𝑑𝑖𝑎𝑔𝑜𝑛𝑎𝑙2)
2

/* Program to find area of scalene triangle, trapezium and rhombus


by using function(method) overloading */

import java.util.*;

class Overload
{
public static 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 static double area(int a,int b, int height)


{
double area = 0.5*height*(a+b);
return area;
}

public static double area(double diagonal1, double diagonal2)


{
double area = 0.5*diagonal1*diagonal2;
return area;
}

public static void main(String args[ ])


{
System.out.println(area(122.0, 120.0, 22.0));
System.out.println(area(20, 14, 12));
System.out.println(area(6, 8));
}
}

OUTPUT:
1320.0
204.0
24.0

PUPIL TREE SCHOOL, BALLARI. Page 95 2023-2024


Computer Applications with Java & BlueJ Class X

2014 – Question 8. [15 Marks]


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:


1.Term Deposit
2.Recurring Deposit

For option 1:
accept principal(p), rate of interest(r) and time period in years(n). Calculate and output the maturity
amount(A) receivable using the formula:
𝑟 𝑛
𝐴 = 𝑝 [1 + ]
100

For option 2:
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:
𝑛(𝑛+1) 𝑟 1
A = 𝑝𝑋𝑛 + 𝑝𝑋 2
𝑋 100 𝑋 12

For an incorrect option, an appropriate error message should be displayed.

// Program to calculate maturity amount of term deposit or


// recurring deposit based on user choice...

import java.util.*;

class BankDeposit
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" 1.Term Deposit ");


System.out.println(" 2.Recurring Deposit ");

System.out.print(" Enter your choice : ");


int choice = sc.nextInt( );

double p, r, n, a;

switch(choice)
{
case 1 : System.out.print(" Enter Principal : ");
p = sc.nextDouble( );
System.out.print(" Enter Rate of Interest : ");
r = sc.nextDouble( );
System.out.print(" Enter Time period in years : ");
n = sc.nextDouble( );
a = p * Math.pow((1 + r/100),n);
System.out.println(" Maturity amount = "+ a);
break;

PUPIL TREE SCHOOL, BALLARI. Page 96 2023-2024


Computer Applications with Java & BlueJ Class X

case 2 : System.out.print(" Enter Monthly Installment : ");


p = sc.nextDouble( );
System.out.print(" Enter Rate of Interest : ");
r = sc.nextDouble( );
System.out.print(" Enter Time period in months : ");
n = sc.nextDouble( );
a = p * n + p * (n*(n+1)/2) * (r/100) * (1/12.0);
System.out.println(" Maturity amount = "+ a);
break;

default : System.out.print(" Wrong choice! ");


}
}
}

OUTPUT 1: OUTPUT 2:
1.Term Deposit 1.Term Deposit
2.Recurring Deposit 2.Recurring Deposit
Enter your choice : 1 Enter your choice : 2

Enter Principal : 1000 Enter Monthly Installment : 100


Enter Rate of Interest : 10 Enter Rate of Interest : 6
Enter Time period in years : 5 Enter Time period in months : 72

Maturity amount = 1610.51 Maturity amount = 8514.0

PUPIL TREE SCHOOL, BALLARI. Page 97 2023-2024


Computer Applications with Java & BlueJ Class X

2014 – Question 9. [15 Marks]


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 of input is located in the array. If not, output message “Record does not exist”.
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}

// Program to search year of graduation on the sorted array


// using Binary search algorithm…

import java.util.*;

class BinarySearch
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

int A[ ] = {1982,1987,1993,1996,1999,2003,2006,2007,2009,2010};

System.out.println(" Enter year of graduation ");


int year = sc.nextInt( );

int start=0, end=A.length-1;


int mid, pos=-1;

while(start <= end)


{
mid = (start + end) / 2;

if ( year == A[mid] )
{
pos = mid;
break;
}

if ( year > A[mid])


start = mid + 1;
else
end = mid -1;
}

if (pos == -1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}
}

OUTPUT 1: OUTPUT 2:
Enter year of graduation Enter year of graduation
2000 1987
Record does not exist Record exists

PUPIL TREE SCHOOL, BALLARI. Page 98 2023-2024


Computer Applications with Java & BlueJ Class X

2015 – Question 4. [15 Marks]


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 vehicle number 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.

// Program to compute the parking charge of a vehicle…


import java.util.*;

class ParkingLot
{
int vno, hours;
double bill;

void input( )
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter vehicle number ");


vno = sc.nextInt();
System.out.println(" Enter hours the vehicle is parked ");
hours = sc.nextInt();
}

void calculate( )
{
bill = 3 + (hours – 1) * 1.5;
}

void display( )
{
System.out.println(" Vehicle Number : "+ vno);
System.out.println(" No. of Hours Parked : "+ hours);
System.out.println(" Bill Amount : "+ bill);
}

public static void main( String args[ ] ) OUTPUT:


{ Enter vehicle number
ParkingLot p = new ParkingLot( ); 6174
Enter hours the vehicle is parked
p.input( ); 1
p.calculate( );
p.display( ); Vehicle Number : 6174
} No. of Hours Parked : 1
} Bill Amount : 3.0

PUPIL TREE SCHOOL, BALLARI. Page 99 2023-2024


Computer Applications with Java & BlueJ Class X

2015 – Question 5. [15 Marks]


Write two separate programs to generate the following patterns using iteration(loop) statements:

(a) (b)
* 5 4 3 2 1
* # 5 4 3 2
* # * 5 4 3
* # * # 5 4
* # * # * 5

// Program to display pattern 1

class Pattern1
{
public static void main( String args[ ] ) OUTPUT:
{ *
for (int i=1; i<=5; i++) * #
{ * # *
for (int j=1; j<=i; j++) * # * #
{ * # * # *
if ( j%2 == 0 )
System.out.print("#");
else
System.out.print("*");
}
System.out.println( );
}
}
}

// Program to display pattern 2

class Pattern2 OUTPUT:


{ 5 4 3 2 1
public static void main( String args[ ] ) 5 4 3 2
{ 5 4 3
for (int i=1; i<=5; i++) 5 4
{ 5
for (int j=5; j>=i; j--)
{
System.out.print( j );
}
System.out.println( );
}
}
}

PUPIL TREE SCHOOL, BALLARI. Page 100 2023-2024


Computer Applications with Java & BlueJ Class X

2015 – Question 6. [15 Marks]


Write a program 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:(The maximum
marks in the subject are 100)

𝑇𝑜𝑡𝑎𝑙𝑀𝑎𝑟𝑘𝑠
Average Marks =
3

Average Marks Remark

85 – 100 Excellent

75 – 84 Distinction

60 – 74 First Class

40 – 59 Pass

Less than 40 Poor

// Program to process student subject marks…


import java.util.*;

class Student
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter students strength : ");


int n = Integer.parseInt(sc.nextLine( ));

int rno[ ] = new int[n];


String name[ ] = new String[n];
int sub1[ ] = new int[n];
int sub2[ ] = new int[n];
int sub3[ ] = new int[n];

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


{
System.out.println(" Enter Roll number of student" + (i+1) );
rno[i] = Integer.parseInt(sc.nextLine( ));
System.out.println(" Enter Name of student" + (i+1) );
name[i] = sc.nextLine( );
System.out.println(" Enter 3 subjects marks ");
sub1[i] = Integer.parseInt(sc.nextLine( ));
sub2[i] = Integer.parseInt(sc.nextLine( ));
sub3[i] = Integer.parseInt(sc.nextLine( ));
}

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


{
double avg = (sub1[i]+sub2[i]+sub3[i]) / 3.0;

String remark;

PUPIL TREE SCHOOL, BALLARI. Page 101 2023-2024


Computer Applications with Java & BlueJ Class X

if ( avg >= 85 )
remark = " Excellent ";
else if ( avg >= 75 )
remark = " Distinction ";
else if ( avg >= 60 )
remark = " First class ";
else if ( avg >= 40 )
remark = " Pass ";
else
remark = " Poor ";

System.out.println("\n Details of Student" + (i+1) );


System.out.println(" *******************");
System.out.println(" Name : " + name[i] );
System.out.println(" Average : " + avg );
System.out.println(" Remark : " + remark );
}
}
}
OUTPUT:
Enter students strength : 2

Enter Roll number of student1


101
Enter Name of student1
Varun
Enter 3 subjects marks
82
65
90

Enter Roll number of student2


102
Enter Name of student2
Mukund
Enter 3 subjects marks
73
99
100

Details of Student1
*******************
Name : Varun
Average : 79.0
Remark : Distinction

Details of Student2
*******************
Name : Mukund
Average : 90.66
Remark : Excellent

PUPIL TREE SCHOOL, BALLARI. Page 102 2023-2024


Computer Applications with Java & BlueJ Class X

2015 – Question 7. [15 Marks]


Design a class to overload a function Joystring( ) as follows:
(i) void Joystring(String s, char ch1, char ch2) with one string argument 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 s = “Cloud computing means Internet based computing”
Output:
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”
Input value of s2 = “GAMES”
Output: “COMMON WEALTH GAMES”
(use library functions)

// Program to overload a function joystring( )...


import java.util.*;

class Overload
{
void joystring(String s, char ch1, char ch2)
{
s = s.replace(ch1,ch2);
System.out.println(s);
}

void joystring(String s)
{
System.out.println(" First Space Position = " + s.indexOf(" "));
System.out.println(" Last Space Position = " + s.lastIndexOf(' '));
}

void joystring(String s1, String s2) void joystring(String s1, String s2)
{ OR {
String s3 = s1 + " " + s2; String s3;
System.out.println(s3); s3 = s1.concat(" ");
} s3 = s3.concat(s2);
System.out.println(s3);
}
}

PUPIL TREE SCHOOL, BALLARI. Page 103 2023-2024


Computer Applications with Java & BlueJ Class X

2015 – Question 8. [15 Marks]

Write a program to input twenty names in an array. Arrange these names in descending order of alphabets,
using the bubble sort technique.

/* Program to sort given names in descending order of alphabets


using Bubble sort technique...*/

import java.util.*;

class BubbleSort
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.print(" How many names you want to enter? ");


int n = Integer.parseInt(sc.nextLine( ));

String A[ ] = new String[n];

System.out.println(" Enter " + n + " names ");


for(int i=0; i<n; i++)
A[i] = sc.nextLine( );

String temp;
int i,j;

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


{
for(j=0; j<n-i; j++)
if ( A[j].compareToIgnoreCase(A[ j+1] ) < 0)
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}

System.out.println(" Names in descending order of alphabets are below ");


for(i=0; i<n; i++)
System.out.println(A[i]);
}
}

PUPIL TREE SCHOOL, BALLARI. Page 104 2023-2024


Computer Applications with Java & BlueJ Class X

OUTPUT:
How many names you want to enter? 19
Enter 19 names
Shreyans
VadiRaj
Aneesh
Inchara
Keerthana
Suraj
Yojith
Shourya
Madhumithra
Rishika
Shrishanth
Harshit
Nehitha
Shreyas
Nikhil
Shwethadri
Sunaina
Yashasree
Anshuman

Names in descending order of alphabets are below


Yojith
Yashasree
VadiRaj
Suraj
Sunaina
Shwethadri
Shrishanth
Shreyas
Shreyans
Shourya
Rishika
Nikhil
Nehitha
Madhumithra
Keerthana
Inchara
Harshit
Anshuman
Aneesh

PUPIL TREE SCHOOL, BALLARI. Page 105 2023-2024


Computer Applications with Java & BlueJ Class X

2015 – Question 9. [15 Marks]


Using the 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: 5! = 1 X 2 X 3 X 4 X 5 = 120
For an incorrect choice, an appropriate error message should be displayed.
// Program using switch statement to display factors and factorial of n
import java.util.*;

class Menu
{
public static void main(String args[ ])
{ Scanner sc = new Scanner(System.in);

System.out.print(" Enter a number : ");


int n = sc.nextInt( );

System.out.println(" Press 1 to display Factors of " + n);


System.out.println(" Press 2 to display Factorial of " + n);

System.out.print(" Enter your choice : ");


int choice = sc.nextInt( );

switch(choice)
{
case 1 : for(int i=1; i<=n/2; i++)
{
if ( n%i == 0 )
System.out.print(i+", ");
}
break;
case 2 : int fact=1; int f=1;
for(int i=1; i<=n; i++) OR while(n>1)
fact = fact * i; {
System.out.println(fact); f = f * n;
break; n--;
}
System.out.println( f );
default : System.out.println(" Incorrect choice! ");
}
}
}
OUTPUT 1: OUTPUT 2: OUTPUT 3:
Enter a number : 18 Enter a number : 75 Enter a number : 6
Press 1 to display Factors of 18 Press 1 to display Factors of 75 Press 1 to display Factors of 6
Press 2 to display Factorial of 18 Press 2 to display Factorial of 75 Press 2 to display Factorial of 6
Enter your choice : 1 Enter your choice : 1 Enter your choice : 2
1, 2, 3, 6, 9 1, 3, 5, 15, 25 720

PUPIL TREE SCHOOL, BALLARI. Page 106 2023-2024


Computer Applications with Java & BlueJ Class X

2016 – Question 4. [15 Marks]


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 ₹1000 2% of price
More than ₹1000 and less than or equal to ₹3000 10% of price
More than ₹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.

// Program to calculate the discount on the book...

import java.util.*;

class BookFair
{
String bname;
double price;

BookFair( )
{
bname = "";
price = 0.0;
}

public void input( )


{
Scanner sc = new Scanner(System.in);

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


bname = sc.nextLine( );

System.out.print(" Enter book price : ");


price = sc.nextDouble( );
}

PUPIL TREE SCHOOL, BALLARI. Page 107 2023-2024


Computer Applications with Java & BlueJ Class X

public void calculate( )


{
double discount;

if ( price <= 1000 )


discount = price * 0.02;
else if ( price <= 3000 )
discount = price * 0.1;
else
discount = price * 0.15;

price = price - discount;


}

public void display( )


{
System.out.println(" Book Name : " + bname);
System.out.println(" Price after discount : " + price);
}

public static void main(String args[ ])


{
BookFair obj = new BookFair( );

obj.input( );
obj.calculate( );
obj.display( );
}
}

OUTPUT 1:
Enter book name : Wings of Fire
Enter book price : 800
Book Name : Wings of Fire
Price after discount : 784.0

OUTPUT 2:
Enter book name : My Experiments with Truth
Enter book price : 3000
Book Name : My Experiments with Truth
Price after discount : 2700.0

OUTPUT 3:
Enter book name : Gitanjali
Enter book price : 3500
Book Name : Gitanjali
Price after discount : 2975.0

PUPIL TREE SCHOOL, BALLARI. Page 108 2023-2024


Computer Applications with Java & BlueJ Class X

2016 – Question 5. [15 Marks]

Using the switch-case statement, write a menu-driven program for the following.

(i) To print the Floyd’s triangle. (ii) To display the following pattern.

1 I

I C
2 3
I C S
4 5 6
I C S E
7 8 9 10

11 12 13 14 15

For an incorrect option, an appropriate error message should be displayed.

// Program to display patterns based on user choice


// using switch-case statement...

import java.util.*;

class Menu
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Input 1 to display Floyds triangle ");


System.out.println(" Input 2 to display String Pattern ");

System.out.print(" Enter your choice : ");


int choice = Integer.parseInt(sc.nextLine( ));

switch(choice)
{
case 1 : System.out.println(" Enter value of n ");
int n = sc.nextInt( );
int x = 1;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=i; j++)
System.out.print( x++ );

System.out.println( );
}
break;

PUPIL TREE SCHOOL, BALLARI. Page 109 2023-2024


Computer Applications with Java & BlueJ Class X

case 2 : System.out.println(" Enter a String ");


String s = sc.nextLine( );
for (int i=0; i<s.length( ); i++)
{
for (int j=0; j<=i; j++)
System.out.print( s.charAt(j) );

System.out.println( );
}
break;

default : System.out.println(" Invalid choice! ");


}
}
}

OUTPUT 1: OUTPUT 2:
Input 1 to display Floyds triangle Input 1 to display Floyds triangle
Input 2 to display String Pattern Input 2 to display String Pattern

Enter your choice : 1 Enter your choice : 2


Enter value of n : 5 Enter a String : LINUX

1 L
23 LI
456 LIN
7 8 9 10 LINU
11 12 13 14 15 LINUX

Alternate Logic for case 2:

System.out.print(" Enter a String : ");


String s = sc.nextLine( );

for (int i=1; i<=s.length(); i++)


System.out.println( s.substring(0,i) );
break;

PUPIL TREE SCHOOL, BALLARI. Page 110 2023-2024


Computer Applications with Java & BlueJ Class X

2016 – Question 6. [15 Marks]


Special words are those words which starts and ends with the same letter.
Examples:
RIVER
COMIC
WINDOW
XEROX
Palindrome words are those words which read the same from left to right and vice-versa.
Examples:
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.

// Program to check word is palindrome or only special word...


import java.util.*;

class PalindromeSpecial
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in); OUTPUT 1:
Enter a word : Amma
System.out.print(" Enter a word : "); AMMA is Palindrome and Special
String s = sc.nextLine( ).toUpperCase( );
OUTPUT 2:
boolean flag = true; Enter a word : Australia
int n = s.length()-1; AUSTRALIA is Special word

for(int i=0; i<=n/2; i++) OUTPUT 3:


{ Enter a word : 1234321
if( s.charAt(i) != s.charAt(n-i) ) 1234321 is Palindrome and Special
{
flag = false; OUTPUT 4:
break; Enter a word : T
} T is Palindrome and Special
}
OUTPUT 5:
if ( flag ) Enter a word : Radar
{ RADAR is Palindrome and Special
System.out.println( s + " is Palindrome
and Special "); OUTPUT 6:
return; Enter a word : Afghanistan
} AFGHANISTAN is Not special word
if ( s.charAt(0) == s.charAt(n) )
System.out.println( s + " is Special word ");
else
System.out.println( s + " is Not special word ");
}
}

PUPIL TREE SCHOOL, BALLARI. Page 111 2023-2024


Computer Applications with Java & BlueJ Class X

2016 – Question 7. [15 Marks]


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:
𝑥 𝑥 𝑥 𝑥 𝑥
𝑠 = 1 − 2 + 3 − 4 + 5 . . . . . .to 𝑛 𝑡𝑒𝑟𝑚𝑠

(ii) void SumSeries( ) - To find and display the sum of the following series:
s = 1 + (1X2) + (1X2X3) + …… + (1X2X3X4…...X20)

// Program to find sum of series by function overloading…

import java.util.*;

class OverLoad
{
public static void SumSeries(int n, double x)
{
double sum = 0;

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


{
if (i%2 == 0)
sum = sum - x/i;
else
sum = sum + x/i;
}
System.out.println(" Sum of fractions = " + sum );
}

public static void SumSeries( )


{
int fact=1, sum=0;

for(int i=1; i<=5; i++)


{
fact = fact * i;
sum = sum + fact;
}
System.out.println(" Sum of factorials = " + sum );
}

public static void main(String args[])


{
SumSeries(6,10);
SumSeries( );
}
}

OUTPUT
Sum of fractions = 6.1666
Sum of factorials = 153

PUPIL TREE SCHOOL, BALLARI. Page 112 2023-2024


Computer Applications with Java & BlueJ Class X

2016 – Question 8. [15 Marks]

Write a program to accept a number and check and display whether it is a Niven number or not.
(Niven number is that number which is divisible by it sum of digits).

Example:
Consider the number 126
Sum of its digits is 1+2+6 = 9 and 126 is divisible by 9.

// Program to check a number is niven number or not...

import java.util.*;

class Niven
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter a number : ");


int n = sc.nextInt( );

int d, sum=0, x=n;

while( n >= 1 )
{
d = n % 10;
sum = sum + d;
n = n/10;
}

if ( x % sum == 0 )
System.out.println(" It is Niven number ");
else
System.out.println(" Not Niven number ");
}
}

OUTPUT 1
Enter a number : 126
It is Niven number

OUTPUT 2
Enter a number : 678
Not Niven number

PUPIL TREE SCHOOL, BALLARI. Page 113 2023-2024


Computer Applications with Java & BlueJ Class X

2016 – Question 9. [15 Marks]


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, Taj Mahal, Great Wall of
China, Machu Picchu, Petra, Colosseum.
Locations – Mexico, Brazil, India, China, Peru, Jordan, Italy

Example:
Country Name: India Output: INDIA – TAJ MAHAL
Country Name: USA Output: Sorry Not Found!

// Program to search a name of the country if found display its wonder...

import java.util.*;

class WorldWonders
{
public static void main(String args[ ])
{
String wonders[ ] = {"Chichen Itza","Christ the Redeemer","Taj Mahal", "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.println(" Enter a country to search... ");


String country = sc.nextLine( );

int pos = -1;

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


{
if ( country.equalsIgnoreCase(locations[i]) )
{
pos = i;
break;
}
}

if ( pos == -1)
System.out.println("Sorry Not Found!");
else
System.out.println(locations[pos]+" - "+wonders[pos]);
}
}

OUTPUT 1: OUTPUT 2:
Enter a country to search... Enter a country to search...
PERU Russia
Peru - Machu Picchu Sorry Not Found!

PUPIL TREE SCHOOL, BALLARI. Page 114 2023-2024


Computer Applications with Java & BlueJ Class X

2017 – Question 4. [15 Marks]


Define a class ElectricBill with the following specifications:
class : ElectricBill

Data Members (Instance variables)


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 be paid

Members 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 ₹ 2.00

Next 200 Units ₹ 3.00

Above 300 Units ₹ 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.

// Program to calculate electric bill as per the tariff...


import java.util.*;

class ElectricBill
{
String n;
int units;
double bill;

void accept( )
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter customer name : ");
n = sc.nextLine( );
System.out.println(" Enter units consumed : ");
units = sc.nextInt( );
}

PUPIL TREE SCHOOL, BALLARI. Page 115 2023-2024


Computer Applications with Java & BlueJ Class X

void calculate( )
{
if (units <= 100)
bill = units * 2;
else if (units <= 300)
bill = 200 + (units-100) * 3;
else
{
bill = 800 + (units-300) * 5;
bill = bill + bill * 0.025;
}
}

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 obj = new ElectricBill( );

obj.accept( );
obj.calculate( );
obj.print( );
}

OUTPUT

Enter the customer name : Enter the customer name : Enter the customer name :
Amruth Adil Alex
Enter units consumed : Enter units consumed : Enter units consumed :
81 290 500

Name of the customer : Amruth Name of the customer : Adil Name of the customer : Alex
Number of units consumed : 81 Number of units consumed : 290 Number of units consumed : 500
Bill amount : 162.0 Bill amount : 770.0 Bill amount : 1845.0

PUPIL TREE SCHOOL, BALLARI. Page 116 2023-2024


Computer Applications with Java & BlueJ Class X

2017 – Question 5. [15 Marks]


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 of its digits equals the product of its digits.)
Example: consider the number 1124
Sum of the digits = 1+1+2+4 = 8
Product of the digits = 1X1X2X4 = 8

// Program to check a number is spy number or not…

import java.util.*;

class Spy
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a number : ");


int n = sc.nextInt( );

int d, sum=0, product=1;

while(n >= 1)
{
d = n % 10;
sum = sum + d;
product = product * d;
n = n / 10;
}

if ( sum == product )
System.out.println(" It is Spy number ");
else
System.out.println(" Not a spy number ");
}
}

OUTPUT 1:
Enter a number :
1124
It is Spy number

OUTPUT 2:
Enter a number :
1729
Not a spy number

PUPIL TREE SCHOOL, BALLARI. Page 117 2023-2024


Computer Applications with Java & BlueJ Class X

2017 – Question 6. (Solution 1) [15 Marks]


Using switch statement, write a menu-driven program for the following:
(i) To find and display the sum of the series given below:
𝑆 = 𝑥 1 − 𝑥 2 + 𝑥 3 − 𝑥 4 + 𝑥 5 . . . . . . −𝑥 20
(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.

// Program to display the series and the sum of the series...


import java.util.*;

class MenuDriven
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" 1. To find sum of the series ");


System.out.println(" 2. To display series 1 11 111 1111 11111");

System.out.print(" Enter your choice : ");


int choice = sc.nextInt( );

switch(choice)
{
case 1 : int sum=0,x=2,term;
for(int i=1; i<=20; i++)
{
term = (int) Math.pow(x,i);
if ( i%2 == 1)
sum = sum + term;
else
sum = sum - term;
}
System.out.println(" Sum = "+sum);
break;

case 2 : for(int i=1; i<=5; i++)


{
for(int j=1; j<=i; j++)
System.out.print("1");

System.out.print(" ");
}
break;
default : System.out.println(" Incorrect option ");
}
}
}
OUTPUT
1. To find sum of the series
2. To display series 1 11 111 1111 11111
Enter your choice : 1
Sum = - 699050

PUPIL TREE SCHOOL, BALLARI. Page 118 2023-2024


Computer Applications with Java & BlueJ Class X

2017 – Question 6. (Solution 2) [15 Marks]

// Program to display the series and the sum of the series…


import java.util.*;

class MenuDriven
{
public static void main(String args[ ])
{ Scanner sc = new Scanner(System.in);

System.out.println(" 1. To find sum of the series ");


System.out.println(" 2. To display series 1 11 111 1111 11111");

System.out.print(" Enter your choice : ");


int choice = sc.nextInt( );

switch(choice)
{
case 1 : int sum=0, sign=1;
for(int i=1; i<=20; i++)
{
sum += sign * (int)Math.pow(2,i);
sign *= -1;
}
System.out.println(" Sum = "+sum);
break;

case 2 : int x=1;


for(int i=1; i<=5; i++)
{
System.out.print(x + " ");
x = x * 10 + 1;
}
break;

default : System.out.println(" Incorrect option ");


}
}
}
OUTPUT 1: OUTPUT 2:
1. To find sum of the series 1. To find sum of the series
2. To display series 1 11 111 1111 11111 2. To display series 1 11 111 1111 11111

Enter your choice : 1 Enter your choice : 2


Sum = -699050 1 11 111 1111 11111

Alternate Logic
int x=1; for(int i = 1; i <= 5; i++)
for(int i=1; i<=5; i++) {
{ int x = (int) (Math.pow( 10 , i ) – 1) / 9;
System.out.print(x + " "); System.out.print( x + " ");
x = x * 10 + 1; }
}

PUPIL TREE SCHOOL, BALLARI. Page 119 2023-2024


Computer Applications with Java & BlueJ Class X

2017 – Question 7. [15 Marks]


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.

// Program to find largest, smallest and sum of all elements of the array...
import java.util.*;

class Array
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" How many elements you want to enter? ");


int n = sc.nextInt( );

int A[ ] = new int [ n ];

System.out.println(" Enter " + n + " elements ");


for(int i=0; i<n; i++)
A[i] = sc.nextInt( );

int max,min,sum;
max = min = sum = A[0];

for (int i=1; i<A.length; i++)


{
if ( max < A[i] )
max = A[i];
else if ( min > A[i] )
min = A[i];

sum = sum + A[i];


}

System.out.println(" Minimum value : " + min );


System.out.println(" Maximum value : " + max );
System.out.println(" Sum of the elements : " + sum);
}
}

OUTPUT
How many elements you want to enter?
5
Enter 5 elements
50
80
30
90
70
Minimum value : 30
Maximum value : 90
Sum of the elements : 320

PUPIL TREE SCHOOL, BALLARI. Page 120 2023-2024


Computer Applications with Java & BlueJ Class X

2017 – Question 8. [15 Marks]


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 vowels from string s1, after converting it to
lower case.
Example:
Input:
s1 = “computer” Output: o u e

// Program to overload a function check( ) as follows...


import java.util.*;

class Overload
{
public static void check(String str, char ch)
{
int count=0;

for(int i=0; i<str.length( ); i++)


{
if ( ch == str.charAt(i) )
count++;
}
System.out.println(" Number of "+ch+" present is = "+count);
}

public static void check(String s1)


{
s1 = s1.toLowerCase( );

for(int i=0; i<s1.length(); i++)


{
char ch = s1.charAt(i);
if ( ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
System.out.println(ch);
}
}

public static void main(String args[ ])


{
check("success",'s');
check("computer");
}
}
OUTPUT
Number of s present is = 3
o
u
e

PUPIL TREE SCHOOL, BALLARI. Page 121 2023-2024


Computer Applications with Java & BlueJ Class X

2017 – Question 9. [15 Marks]


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.

// Program to arrange the words in descending order of alphabets


// using Selection sort…

import java.util.*;

class SelectionSort
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" How many words you want to enter? ");


int n = sc.nextInt( );

String A[ ] = new String[n];

System.out.println(" Enter "+ n + " words ");


for(int i=0; i<n; i++)
A[i] = sc.next( );

int pos;
String temp;

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


{
pos = i;
for(int j=i+1; j<n; j++)
{
if ( A[pos].compareToIgnoreCase(A[j]) < 0 )
pos = j;
}

temp = A[i];
A[i] = A[pos];
A[pos] = temp;
}

System.out.println(" Words in descending order of alphabets ");


for(int i=0; i<n; i++)
System.out.println(A[i]);
}
}

OUTPUT
How many words you want to enter?
6
Enter 6 words
Orange Apple Watermelon Papaya Banana Mango

Words in descending order of alphabets


Watermelon Papaya Orange Mango Banana Apple

PUPIL TREE SCHOOL, BALLARI. Page 122 2023-2024


Computer Applications with Java & BlueJ Class X

2018 – Question 4. [15 Marks]


Design a class RailwayTicket with 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

Member 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 member methods.
// Program to update railway ticket amount as per the coach selected…
import java.util.*;

class RailwayTicket
{ String name, coach;
long mobno;
int amt, totalamt;

void accept( )
{ Scanner sc = new Scanner(System.in);
System.out.print(" Enter customer name : ");
name = sc.nextLine( );
System.out.print(" Enter type of coach : ");
coach = sc.nextLine( );
System.out.print(" Enter mobile number : ");
mobno = sc.nextLong( );
System.out.print(" Enter basic amount of ticket : ");
amt = sc.nextInt( );
}

void update( )
{
if (coach.equalsIgnoreCase("First_AC"))
totalamt = amt + 700;
else if (coach.equalsIgnoreCase("Second_AC"))
totalamt = amt + 500;
else if (coach.equalsIgnoreCase("Third_AC"))
totalamt = amt + 250;
else if (coach.equalsIgnoreCase("Sleeper"))
totalamt = amt;
}

PUPIL TREE SCHOOL, BALLARI. Page 123 2023-2024


Computer Applications with Java & BlueJ Class X

void display( )
{
System.out.println(" Details of Customer ");
System.out.println(" **************** ");
System.out.println(" Name : " + name );
System.out.println(" Coach : " + coach );
System.out.println(" Mobile : " + mobno );
System.out.println(" Total amount : " + totalamt );
}

public static void main(String args[ ])


{
RailwayTicket obj = new RailwayTicket( );
obj.accept( );
obj.update( );
obj.display( );
}
}

OUTPUT 1:
Enter customer name : Shravya
Enter type of coach : first_ac
Enter mobile number : 9876543210
Enter basic amount of ticket :
150

Details of Customer
*****************
Name : Shravya
Coach : first_ac
Mobile : 9876543210
Total amount : 850

OUTPUT 2:
Enter customer name : Sahasra
Enter type of coach : Sleeper
Enter mobile number : 9876543210
Enter basic amount of ticket :
150

Details of Customer
*****************
Name : Sahasra
Coach : Sleeper
Mobile : 9876543210
Total amount : 150

PUPIL TREE SCHOOL, BALLARI. Page 124 2023-2024


Computer Applications with Java & BlueJ Class X

2018 – Question 5. (Solution 1) [15 Marks]


Write a program to input a number and check and print whether it is a Pronic number or not. (Pronic number
is the number which is the product of two consecutive integers)
Examples: 12 = 3 X 4
20 = 4 X 5
42 = 6 X 7

// Program to check whether a number is pronic number or not...


import java.util.*;

class Pronic
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter a number : ");


int n = sc.nextInt( );

boolean flag = false;

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


{
if ( i * (i+1) == n )
{
flag = true;
break;
}
}

if ( flag )
System.out.println(" It is pronic number ");
else
System.out.println(" Not a pronic number ");
}
}

OUTPUT
Enter a number : 12
It is pronic number

Enter a number : 15
Not a pronic number

Enter a number : 20
It is pronic number

Enter a number : 32
Not a pronic number

Enter a number : 42
It is pronic number

PUPIL TREE SCHOOL, BALLARI. Page 125 2023-2024


Computer Applications with Java & BlueJ Class X

2018 – Question 5. (Solution 2) [15 Marks]

// Program to check whether a number is pronic number or not...

import java.util.*;

class Pronic2
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter a number : ");


int n = sc.nextInt( );

int a = (int) Math.sqrt(n);

if ( a*(a+1) == n )
System.out.println(" It is pronic number ");
else
System.out.println(" Not a pronic number ");
}
}

OUTPUT
Enter a number : 12
It is pronic number

Enter a number : 15
Not a pronic number

Enter a number : 20
It is pronic number

Enter a number : 32
Not a pronic number

Enter a number : 42
It is pronic number

Enter a number : 50
Not a pronic number

Enter a number : 90
It is pronic number

Enter a number : 100


Not a pronic number

PUPIL TREE SCHOOL, BALLARI. Page 126 2023-2024


Computer Applications with Java & BlueJ Class X

2018 – Question 6. (Solution 1) [15 Marks]


Write a program in Java to accept a string in lowercase and change the first letter of every word to upper case.
Display the new string.
Sample input: we are in cyber world
Sample output: We Are In Cyber World

// Program to change the first letter of every word to upper case…

import java.util.*;

class FirstLetterUpper
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a string ");


String s = sc.nextLine( ).toLowerCase( );

s = ' ' + s;
String ns = "";

for(int i=0; i<s.length(); i++)


{
char ch = s.charAt(i);

if ( ch == ' ' )
{
if ( i != 0 )
ns += ch;

ns += Character.toUpperCase( s.charAt(++i) );
}
else
ns = ns + ch;
}

System.out.println( ns );
}
}

OUTPUT:
Enter a string
tHeRE'S nO enEmy oUTsIDe OuR souL. ThE reAl EnemIEs live insIdE Us: AnGeR, EGO, gReeD AnD haTE.

There's No Enemy Outside Our Soul. The Real Enemies Live Inside Us: Anger, Ego, Greed And Hate.

Note: where s = string & ns = new string

PUPIL TREE SCHOOL, BALLARI. Page 127 2023-2024


Computer Applications with Java & BlueJ Class X

2018 – Question 6. (Solution 2) [15 Marks]

// Program to change the first letter of every word to upper case…

import java.util.*;

class FirstLetterUpper2
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a string ");


String s = sc.nextLine( ).toLowerCase( );

s = ' ' + s;
String ns = "";

for(int i=1; i<s.length(); i++)


{
char ch = s.charAt(i);

if ( s.charAt(i-1) == ' ' )


ns += Character.toUpperCase(ch);
else
ns += ch;
}
System.out.println( ns );
}
}

OUTPUT
Enter a string
all ReliGions leAds to onE goD...
All Religions Leads To One God...

PUPIL TREE SCHOOL, BALLARI. Page 128 2023-2024


Computer Applications with Java & BlueJ Class X

2018 – Question 7. [15 Marks]


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 X 22/7 X 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 X R2 X 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=LXBXH

// Program to overload a function volume( ) as follows...

import java.util.*;

class Overload
{
double volume(double R)
{
double V = 4/3.0 * 22/7.0 * R * R * R;
return V;
}

double volume(double H, double R)


{
double V = 22/7.0 * R * R * H;
return V;
}

double volume(double L, double B, double H)


{
double V = L * B * H;
return V;
}

public static void main(String args[ ])


{
Overload obj = new Overload( );

System.out.println(obj.volume(5));
System.out.println(obj.volume(8,3));
System.out.println(obj.volume(12,10,6));
}
}

OUTPUT
523.80
226.28
720.0

PUPIL TREE SCHOOL, BALLARI. Page 129 2023-2024


Computer Applications with Java & BlueJ Class X

2018 – Question 8. [15 Marks]


Write a menu driven program to display the pattern as per user’s choice.

Pattern 1 Pattern 2
ABCDE B
ABCD LL
ABC UUU
AB EEEE
A

For an incorrect option, an appropriate error message should be displayed.

// Program to display patterns as per user choice...


import java.util.*;

class MenuPatterns
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

String s;

System.out.println(" Enter 1: To display ABCD pattern ");


System.out.println(" Enter 2: To display BLUE pattern ");
int choice = sc.nextInt( );

switch(choice)
{
case 1 : s = "ABCDE";
for(int i=s.length()-1; i>=0; i--)
{
for(int j=0; j<=i; j++)
System.out.print( s.charAt(j) );

System.out.println( );
}
break;

case 2 : s = "BLUE";
for(int i=0; i<s.length(); i++)
{
for(int j=0; j<=i; j++)
System.out.print( s.charAt(i) );

System.out.println( );
}
break;

default : System.out.println(" Wrong option! ");


}
}
}

PUPIL TREE SCHOOL, BALLARI. Page 130 2023-2024


Computer Applications with Java & BlueJ Class X

OUTPUT 1:
Enter 1: To display ABCD pattern
Enter 2: To display BLUE pattern
1
ABCDE
ABCD
ABC
AB
A

OUTPUT 2:
Enter 1: To display ABCD pattern
Enter 2: To display BLUE pattern
2
B
LL
UUU
EEEE

OUTPUT 3:
Enter 1: To display ABCD pattern
Enter 2: To display BLUE pattern
3
Wrong option!

Alternate Logic for Case 1:


for(char i='E'; i>='A'; i--)
{ String s = "ABCDE" ;
for(char j='A'; j<=i; j++) for (int i=s.length(); i>=0; i--)
System.out.print( j ); System.out.println( s.substring(0,i) );
break;
System.out.println( );
}
break;

PUPIL TREE SCHOOL, BALLARI. Page 131 2023-2024


Computer Applications with Java & BlueJ Class X

2018 – Question 9. [15 Marks]


Write a program to accept name and total marks of N number of students in two single subscript 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 ]

// Program to calculate average and deviation of each student…


import java.util.*;

class Deviation
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.print(" Enter students strength : ");


int n = sc.nextInt( );

String name[ ] = new String[n];


int totalmarks[ ] = new int[n];
double sum=0, avg;

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


{
System.out.print(" Enter name of student"+(i+1)+" : ");
name[i] = sc.next( );
System.out.print(" Enter total marks of "+ name[i] + " : ");
totalmarks[i] = sc.nextInt( );
sum += totalmarks[i];
}

avg = sum / n ;
System.out.println(" The average of total marks = "+avg);

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


{
double dev = totalmarks[i] - avg ;
System.out.println( name[i] + "'s Deviation : " + dev);
}
}
}

PUPIL TREE SCHOOL, BALLARI. Page 132 2023-2024


Computer Applications with Java & BlueJ Class X

OUTPUT
Enter students strength : 5

Enter name of student1 : Pratham


Enter total marks of Pratham : 92

Enter name of student2 : Virat


Enter total marks of Virat : 40

Enter name of student3 : Suhail


Enter total marks of Suhail : 75

Enter name of student4 : Samarth


Enter total marks of Samarth : 100

Enter name of student5 : Nishanth


Enter total marks of Nishanth : 68

The average of total marks = 75.0

Pratham's Deviation : 17.0


Virat's Deviation : -35.0
Suhail's Deviation : 0.0
Samarth's Deviation : 25.0
Nishanth's Deviation : -7.0

PUPIL TREE SCHOOL, BALLARI. Page 133 2023-2024


Computer Applications with Java & BlueJ Class X

2019 – Question 4. [15 Marks]


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 the mobile number of the customer
double cost - To store the cost of the items purchased
double dis - To store the discount amount
double amount - To store the amount to be paid after discount

Member methods:
ShowRoom( ) - default constructor to initialize data members
void input( ) - To input customer name, mobile number, cost
void calculate( ) - To calculate discount on the cost of purchased items, based on
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 name, mobile number, amount to be paid after
discount.

Write a main method to create an object of the class and call the above member methods.

// Program to calculate discount on the cost of purchased items...

import java.util.*;

class ShowRoom
{
String name;
long mobno;
double cost,dis,amount;

ShowRoom( )
{
name = "";
mobno = 0;
cost = dis = amount = 0.0;
}

PUPIL TREE SCHOOL, BALLARI. Page 134 2023-2024


Computer Applications with Java & BlueJ Class X

void input( )
{
Scanner sc = new Scanner(System.in);
System.out.print(" Enter customer name : ");
name = sc.next( );
System.out.print(" Enter mobile number : ");
mobno = sc.nextLong( );
System.out.print(" Enter cost of the items : ");
cost = sc.nextDouble( );
}

void calculate( )
{
if ( cost <= 10000 )
dis = cost * 0.05;
else if ( cost <= 20000 )
dis = cost * 0.1;
else if ( cost <= 35000 )
dis = cost * 0.15;
else
dis = cost * 0.2;

amount = cost - dis;


}

void display( )
{
System.out.println(" Customer name : "+ name );
System.out.println(" Mobile number : "+ mobno );
System.out.println(" Amount to be paid : "+ amount);
}

public static void main(String args[ ])


{
ShowRoom obj = new ShowRoom( );

obj.input( );
obj.calculate( );
obj.display( );
}
}

OUTPUT
Enter customer name : Swapnil
Enter mobile number : 9876543210
Enter cost of the items : 30000

Customer name : Swapnil


Mobile number : 9876543210
Amount to be paid : 25500.0

PUPIL TREE SCHOOL, BALLARI. Page 135 2023-2024


Computer Applications with Java & BlueJ Class X

2019 – Question 5. [15 Marks]


Using the switch-case statement, write a menu driven program to do the following:
(a) To generate and print Letters from A to Z and their Unicode
Letters Unicode
A 65
B 66
. .
. .
. .
Z 90

(b) Display the following pattern using iteration(looping) statement:


1
12
123
1234
12345

// Program to print unicode of letters or display pattern


// using switch-case menu driven program...

import java.util.*;

class MenuDriven
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter 1 to display Unicode ");


System.out.println(" Enter 2 to display Pattern ");
int n = sc.nextInt( );

switch(n)
{
case 1 : for(char ch='A'; ch<='Z'; ch++)
System.out.println( ch + "\t" + (int) ch );
break; OUTPUT
Enter 1 to display Unicode
case 2 : for(int i=1; i<=5; i++) Enter 2 to display Pattern
{ 1
for(int j=1; j<=i; j++) 12
System.out.print( j ); 123
System.out.println( ); 1234
} 12345

}
}
}

PUPIL TREE SCHOOL, BALLARI. Page 136 2023-2024


Computer Applications with Java & BlueJ Class X

2019 – Question 6. [15 Marks]


Write a program to input 15 integer elements in an array and sort them in ascending order using the bubble sort
technique.

/* Program to sort the integer elements in ascending order


using the Bubble sort technique. */
import java.util.*;

class BubbleSort
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

int n=15,temp;
int A[] = new int[n];

System.out.println(" Enter "+ n +" integer elements ");


for(int i=0; i<n; i++)
A[i] = sc.nextInt( );

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


{
for(int j=0; j<n-i; j++)
if ( A[j] > A[j+1] )
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}

System.out.println(" Sorted elements are below ");


for(int i=0; i<n; i++)
System.out.println(A[i]);
}
}
OUTPUT
Enter 15 integer elements Sorted elements are below
10 10
20 20
40 30
30 40
50 50
60 60
80 70
70 80
90 90
100 100
120 110
110 120
130 130
140 140
160 160

PUPIL TREE SCHOOL, BALLARI. Page 137 2023-2024


Computer Applications with Java & BlueJ Class X

2019 – Question 7. [15 Marks]


Design a class to overload a function series( ) as follows:
(a) void series(int x, int n) – To display the sum of the series given below:
x1+x2+x3+…...xn terms
(b) void series(int p) – To display the following series:
0, 7, 26, 63 …… p terms
(c) void series( ) - To display the sum of the series given below:
1 1 1 1
2
+ 3 + 4 . . . . . . 10

// Program to overload a function series( ) as follows...


import java.util.*;

class overload
{
public static void series(int x, int n)
{
double sum=0;

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


sum += Math.pow(x,i);

System.out.println("The sum of series = "+sum);


}

public static void series(int p)


{
for(int i=1; i<=p; i++)
System.out.print( i*i*i-1 + "," );
}

public static void series( )


{
double sum=0;

for(int i=2; i<=10; i++)


sum += 1.0 / i;

System.out.println("The sum of series = " + sum);


}

public static void main(String args[ ])


{
series(2,5);
series(6);
series( );
}
}
OUTPUT
The sum of series = 62.0
0, 7, 26, 63, 124, 215,
The sum of series = 1.928968253968254

PUPIL TREE SCHOOL, BALLARI. Page 138 2023-2024


Computer Applications with Java & BlueJ Class X

2019 – Question 8. [15 Marks]


Write a program to input a sentence and convert it into uppercase and count and display the total number of
words starting with a letter ‘A’.

Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.

Sample Output: Total number of words starting with letter ‘A’ = 4.

// Program to count total number of words starts with letter 'A'

import java.util.*;

class LetterCount
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a sentence ");


String s = sc.nextLine( ).toUpperCase( );

System.out.println(s);

s = ' ' + s;
int count=0;

for(int i=0; i<s.length(); i++)


{
if (s.charAt(i) == ' ' && s.charAt(i+1)=='A')
count++;
}

System.out.println(" Total number of words starting with letter 'A' = " + count);
}
}

OUTPUT
Enter a sentence
affordable apples available in america.
AFFORDABLE APPLES AVAILABLE IN AMERICA.
Total number of words starting with letter 'A' = 4

PUPIL TREE SCHOOL, BALLARI. Page 139 2023-2024


Computer Applications with Java & BlueJ Class X

2019 – Question 9. [15 Marks]


A tech number has even number of digits. If the number is split in two equal halves, then the square of sum of
these halves is equal to the number itself. Write a program to generate and print all four digits tech numbers.

Example:
Consider the number 3025
Square of sum of the halves of 3025 = ( 30 + 25 )2
= ( 55 )2
= 3025 is a tech number.

// Program to generate all four digits tech numbers...


import java.util.*;

class TechNumber
{
public static void main(String args[ ])
{
for(int i=1000; i<=9999; i++)
{
int sum = i/100 + i%100;

if ( sum * sum == i )
System.out.println( i );
}
}
}

OUTPUT
2025
3025
9801

PUPIL TREE SCHOOL, BALLARI. Page 140 2023-2024


Computer Applications with Java & BlueJ Class X

2020 – Question 4. [15 Marks]


A private Cab service company provides service within the city at the following rates:
AC CAR NON AC CAR
UPTO 5 KM ₹ 150/- ₹ 120/-
BEYOND 5 KM ₹ 10/- PER KM ₹ 08/- PER KM

Design a class CabService with the following description:


Member variables/data members:
String car_type - To store the type of car (AC or NON AC)
double km - To store the kilometer travelled
double bill - To calculate and store the bill amount

Member methods:
CabService( ) - Default constructor to initialize data members.
String data members to “” and double data members to 0.0

void accept( ) - To accept car_type and km (using Scanner class only).

void calculate( ) - To calculate the bill as per the rules given above.

void display( ) - To display the bill as per the following format


CAR TYPE:
KILOMETER TRAVELLED:
TOTAL BILL:
Create an object of the class in the main method and invoke the member methods.

// Program to calculate the bill of a private cab service...


import java.util.*;

class CabService
{
String car_type;
double km, bill;

CabService( )
{
car_type = "";
km = bill = 0.0;
}

void accept( )
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter car_type ");
car_type = sc.nextLine( ).toUpperCase( );
System.out.println(" Enter kilometer travelled ");
km = sc.nextDouble( );
}

PUPIL TREE SCHOOL, BALLARI. Page 141 2023-2024


Computer Applications with Java & BlueJ Class X

void calculate( )
{
if ( car_type.equals("AC") )
{
bill = 150 ;
if ( km > 5 )
bill += (km-5) * 10 ;
}

if ( car_type.equals("NON AC") )
{
bill = 120 ;
if ( km > 5 )
bill += (km-5) * 8 ;
}
}

void display( )
{
System.out.println(" CAR TYPE: " + car_type);
System.out.println(" KILOMETER TRAVELLED: " + km);
System.out.println(" TOTAL BILL: " + bill);
}

public static void main(String args[ ])


{
CabService cs = new CabService( );
cs.accept( );
cs.calculate( );
cs.display( );
}
}

OUTPUT 1: OUTPUT 2:
Enter car_type Enter car_type
ac AC
Enter kilometer travelled Enter kilometer travelled
2.5 13

CAR TYPE: AC CAR TYPE: AC


KILOMETER TRAVELLED: 2.0 KILOMETER TRAVELLED: 13.0
TOTAL BILL: 150.0 TOTAL BILL: 230.0
OUTPUT 3: OUTPUT 4:
Enter car_type Enter car_type
NON AC non ac
Enter kilometer travelled Enter kilometer travelled
17.5 5

CAR TYPE: NON AC CAR TYPE: NON AC


KILOMETER TRAVELLED: 17.5 KILOMETER TRAVELLED: 5.0
TOTAL BILL: 220.0 TOTAL BILL: 120.0

PUPIL TREE SCHOOL, BALLARI. Page 142 2023-2024


Computer Applications with Java & BlueJ Class X

2020 – Question 5. [15 Marks]


Write a program to search for an integer value input by the user in the sorted list given below using binary search
technique. If found display “Search Successful” and print the element, otherwise display “Search Unsuccessful”
{31,36,45,50,60,75,86,90}

// Program to search for an integer value in the sorted list


// using binary search technique...
import java.util.*;

class BinarySearch
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

int A[ ] = {31,36,45,50,60,75,86,90};

System.out.print(" Enter an element to search : ");


int x = sc.nextInt( );

int start=0, end=A.length-1;


int mid, pos=-1;

while(start <= end)


{
mid = (start+end) / 2;

if ( x == A[mid] )
{
pos = mid;
break;
}

if ( x > A[mid] )
start = mid + 1;
else
end = mid - 1;
}

if ( pos >= 0 )
{
System.out.println(" Search Successful ");
System.out.println(" The element found at position : "+(pos+1));
}
else
System.out.println(" Search Unsuccessful ");
}
}

OUTPUT 1: OUTPUT 2:
Enter an element to search : 60 Enter an element to search : 61
Search Successful Search Unsuccessful
The element found at position : 5

PUPIL TREE SCHOOL, BALLARI. Page 143 2023-2024


Computer Applications with Java & BlueJ Class X

2020 – Question 6. (Solution 1) [15 Marks]


Write a program to input a sentence and convert it into uppercase and display each word in a separate line.
Example: Input:
India is my country
Output:
INDIA
IS
MY
COUNTRY

// Program to input a sentence and convert it into uppercase


// and display each word in a separate line...

import java.util.*;

class MyString1
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a sentence ");


String s = sc.nextLine().toUpperCase();

for(int i=0; i<s.length(); i++)


{
char ch = s.charAt(i);

if ( ch == ' ' )
System.out.print("\n");
else
System.out.print(ch);
}
}
}

OUTPUT
Enter a sentence
Don'T say 2moRrow Do iT 2dAy!

DON'T
SAY
2MORROW
DO
IT
2DAY!

PUPIL TREE SCHOOL, BALLARI. Page 144 2023-2024


Computer Applications with Java & BlueJ Class X

2020 – Question 6. (Solution 2) [15 Marks]

// Program to input a sentence and convert it into uppercase


// and display each word in a separate line...

import java.util.*;

class MyString2
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Input a sentence ");


String s = sc.nextLine().toUpperCase();

String A[ ] = s.split(" ");

for(int i=0; i<A.length; i++)


System.out.println( A[i] );
}
}

OUTPUT
Input a sentence
It is easy to see the faults of others, but difficult to see one's own faults.

IT
IS
EASY
TO
SEE
THE
FAULTS
OF
OTHERS,
BUT
DIFFICULT
TO
SEE
ONE'S
OWN
FAULTS.

PUPIL TREE SCHOOL, BALLARI. Page 145 2023-2024


Computer Applications with Java & BlueJ Class X

2020 – Question 7. [15 Marks]


Design a class to overload a method Number( ) as follows:
(i) void Number(int num, int d) - To count and display the frequency of a digit in a number.
Example:
num = 2565685
d=5
Frequency of digit 5 = 3

(ii) void Number(int n1) – To find and display the sum of even digits of a number.
Example:
n1 = 29865
Sum of even digits = 16

Write a main method to create an object and invoke the above methods.

// Program to overload a method number( ) as follows...


import java.util.*;

class Overload
{
void Number(int num, int d)
{
int c=0;
while(num > 0)
{
if (num%10 == d)
c++;
num /= 10;
}
System.out.println(" Frequency of digit "+d+" = "+c);
}

void Number(int n1)


{
int s=0,d;
while(n1 > 0)
{
d = n1%10;
n1 = n1/10;
if (d%2 == 0)
s += d;
}
System.out.println(" Sum of even digits = " +s);
}

public static void main(String args[])


{
Overload obj = new Overload( );
obj.Number(2565685,5);
obj.Number(29865);
}
}
OUTPUT
Frequency of digit 5 = 3
Sum of even digits = 16

PUPIL TREE SCHOOL, BALLARI. Page 146 2023-2024


Computer Applications with Java & BlueJ Class X

2020 – Question 8. [15 Marks]


Write a menu driven program to perform the following operations as per user’s choice:

(i) To print the value of c = a2+2ab, where a varies from 1.0 to 20.0 with increment of 2.0
and b=3.0 is a constant.

(ii) To display the following pattern using for loop:


A
A B
A B C
A B C D
A B C D E

Display proper message for an invalid choice.

// Program to perform operations as per user choice...


import java.util.*;

class MenuDriven
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Input 1 to print the value of c ");


System.out.println(" Input 2 to display pattern ");
int choice = sc.nextInt( );

switch(choice)
{ OUTPUT
case 1 : for(double a=1,b=3; a<=20; a+=2) Input 1 to print the value of c
{
Input 2 to display pattern
double c = a*a + 2*a*b;
System.out.println(c); 1
} 7.0
break; 27.0
55.0
case 2 : for(int i=1; i<=5; i++) 91.0
{ 135.0
for(int j=0; j<i; j++)
187.0
System.out.print( (char)('A'+j) );
System.out.print("\n"); 247.0
} 315.0
break; 391.0
475.0
default : System.out.println(" Invalid choice ");
}

}
}

PUPIL TREE SCHOOL, BALLARI. Page 147 2023-2024


Computer Applications with Java & BlueJ Class X

2020 – Question 9. (Solution 1) [15 Marks]


Write a program to input and store integer elements in a double dimensional array of size 3X3 and find the sum of
elements in the left diagonal.

Example:
1 3 5
4 6 8
9 2 4

Output: Sum of the left diagonal elements = (1+6+4) = 11

// Program to find sum of elements of left diagonal of a matrix...


import java.util.*;

class Matrix1
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter order of the matrix ");


int n = sc.nextInt( );

System.out.println(" Enter " + n*n + " elements for the matrix ");

int A[ ][ ] = new int[n][n];


int sum = 0; OUTPUT
Enter order of the matrix
for(int i=0; i<n; i++)
3
for(int j=0; j<n; j++)
{ Enter 9 elements for the matrix
A[i][j] = sc.nextInt( ); 1
if (i==j) 3
sum += A[i][j]; 5
} 4
6
System.out.println(" Your Matrix is Below ");
8
for(int i=0; i<n; i++)
{ 9
for(int j=0; j<n; j++) 2
System.out.print(A[i][j]+" "); 4
Your Matrix is Below
System.out.println( ); 135
} 468
924

Sum of the left diagonal elements = 11

System.out.println(" Sum of the left diagonal elements = "+sum);


}
}

PUPIL TREE SCHOOL, BALLARI. Page 148 2023-2024


Computer Applications with Java & BlueJ Class X

2020 – Question 9. (Solution 2) [15 Marks]


Write a program to input and store integer elements in a double dimensional array of size 3X3 and find the sum of
elements in the left diagonal.

Example:
1 3 5

4 6 8

9 2 4

Output: Sum of the left diagonal elements = (1+6+4) = 11

// Program to find sum of elements of left diagonal of a matrix…


import java.util.*;

class Matrix2
{
public static void main(String args[ ])
{
int A[ ] [ ] = { {1, 3, 5},
{4, 6, 8},
{9, 2, 4}
};

int sum = 0;

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


sum += A[i][i];

System.out.println(" Sum of the left diagonal elements = "+sum);


}
}

OUTPUT
Sum of the left diagonal elements = 11

PUPIL TREE SCHOOL, BALLARI. Page 149 2023-2024


Computer Applications with Java & BlueJ Class X

2021 – Question 1. [15 Marks]


Design a class Hotel with the following description:
Member variables:
String name - to store the name of the customer
long mno - to store the mobile number of the customer
double bill - to store the bill amount
double gst - to store the GST amount
double st - to store the service tax
double tamt - to store the total amount to be paid by the customer

Member methods:
void accept( ) - to accept customer’s name, mobile number and bill amount.
void calculate( ) - to calculate GST, service tax and total amount to be paid by
the customer.
gst = 18% on bill
st = 12.5% on bill
tamt = bill + gst + st
void display( ) - to display the customer’s name, mobile number, GST, service tax
and total amount.
Write a main method to create an object and invoke the above member methods.

// Program to calculate GST, Service tax and total amount paid by customer...
import java.util.*;

class Hotel
{
String name;
long mno;
double bill,gst,st,tamt;

void accept( )
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter customer name, mobile number & bill amount");
name = sc.next();
mno = sc.nextLong();
bill = sc.nextDouble();
}

void calculate( )
{
gst = bill * 0.18;
st = bill * 0.125;
tamt = bill + gst + st;
}

void display( )
{
System.out.println(" Customer Details ");
System.out.println(" Name : " +name);
System.out.println(" Mobile : " +mno);
System.out.println(" GST : " +gst);
System.out.println(" Service tax : "+st);
System.out.println(" Total amount : "+tamt);
}

PUPIL TREE SCHOOL, BALLARI. Page 150 2023-2024


Computer Applications with Java & BlueJ Class X

public static void main(String args[])


{
Hotel h =new Hotel( );
h.accept();
h.calculate();
h.display();
}
}

OUTPUT 1:
Enter customer name, mobile number & bill amount
Achala
6655443322
2500

Customer Details
Name : Achala
Mobile : 6655443322
GST : 450.0
Service tax : 312.5
Total amount : 3262.5

OUTPUT 2:
Enter customer name, mobile number & bill amount
Praneetha
6622334455
5000

Customer Details
Name : Praneetha
Mobile : 6622334455
GST : 900.0
Service tax : 625.0
Total amount : 6525.0

OUTPUT 3:
Enter customer name, mobile number & bill amount
Sreeya
6644335522
7500

Customer Details
Name : Sreeya
Mobile : 6644335522
GST : 1350.0
Service tax : 937.5
Total amount : 9787.5

PUPIL TREE SCHOOL, BALLARI. Page 151 2023-2024


Computer Applications with Java & BlueJ Class X

2021 – Question 2. [15 Marks]


Design a class to overload a method called PattSeries( ) as follows:
(a) void PattSeries( ) – To generate and display the pattern given below:
54321
4321
321
21
1
(b) void PattSeries(int n) – To find and display the sum of the series given below:
1 2 3 𝑛
sum = 3 + 4 + 5…….(𝑛+2)

Write a main method to create an object of the class and call the above member methods.
// Program to overload a method called PattSeries() as follows...
class Overload
{
void PattSeries( )
{
for(int i=5; i>=1; i--)
{
for(int j=i; j>=1; j--)
System.out.print(j);

System.out.print("\n");
}
}

void PattSeries(int n)
{ double sum=0;

for(double i=1; i<=n; i++)


sum = sum + i / (i+2);

System.out.print(" Sum of the series = "+sum);


}

public static void main(String args[ ])


{ Overload obj = new Overload();
obj.PattSeries();
obj.PattSeries(3);
}
}

OUTPUT
54321
4321
321
21
1

Sum of the series = 1.4333

PUPIL TREE SCHOOL, BALLARI. Page 152 2023-2024


Computer Applications with Java & BlueJ Class X

2021 – Question 3. [15 Marks]


Write a program to input a number and check and print whether it is an EvenPal number or not.

The number is said to be an EvenPal number when the number is a palindrome number and the sum of its digits is
an even number.

Example:
Number 121 is a palindrome number and the sum of its digits is 1+2+1=4, which is an even number.

Therefore 121 is an EvenPal number.

// Program to check a number is EvenPal or not


import java.util.*;

class EvenPal
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a number ");


int n = sc.nextInt();

int sum=0,rev=0,d,x=n;

while(n>0)
{
d=n%10;
sum+=d;
rev=rev*10+d;
n/=10;
}

if (sum%2==0 && rev==x)


System.out.println(" It is EvenPal number ");
else
System.out.println(" Not EvenPal ");
}
}

OUTPUT 1:
Enter a number
1331
It is EvenPal number

OUTPUT 2:
Enter a number
515
Not EvenPal

PUPIL TREE SCHOOL, BALLARI. Page 153 2023-2024


Computer Applications with Java & BlueJ Class X

2021 – Question 4. [15 Marks]


A list contains marks in the subject Computer Applications for ‘N’ number of students. Write a program to create
an array of size ‘N’ and store the marks. Check and print the number of students falling into the different ranges
given below:
100-80, 79-60, 59-40, 39-20, 19-0

// Program to print number of students falling into the ranges


import java.util.*;

class Range
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter N number of students ");


int n = sc.nextInt();

int A[ ] = new int[n];

System.out.println(" Enter "+ n + " students marks ");

int r1=0,r2=0,r3=0,r4=0,r5=0;

for(int i=0; i<A.length; i++)


{
A[i] = sc.nextInt();

if (A[i]<=100 && A[i]>=80) r1++;


if (A[i]<=79 && A[i]>=60) r2++;
if (A[i]<=59 && A[i]>=40) r3++;
if (A[i]<=39 && A[i]>=20) r4++;
if (A[i]<=19 && A[i]>=0) r5++;
}

System.out.println(" Number of Students falling into ranges ");


System.out.println(" 100 - 80 = " + r1);
System.out.println(" 79 - 60 = " + r2);
System.out.println(" 59 - 40 = " + r3);
System.out.println(" 39 - 20 = " + r4);
System.out.println(" 19 - 0 = " + r5);
}
}
OUTPUT
Enter N number of students
12
Enter 12 students marks
60 7 35 52 76 99 25 68 44 100 81 59

Number of Students falling into ranges


100 - 80 = 3
79 - 60 = 3
59 - 40 = 3
39 - 20 = 2
19 - 0 = 1

PUPIL TREE SCHOOL, BALLARI. Page 154 2023-2024


Computer Applications with Java & BlueJ Class X

SEMESTER 1 EXAMINATION (50 Marks)


2021-2022
COMPUTER APPLICATIONS
Question 1.

Choose the correct answer:

(i) The blueprint that define the variables and the methods common to all of a certain kind is termed as:
(a) class
(b) object
(c) package
(d) method

(ii) A data type which contains integer as well as fractional part and occupies 32 bits space is:
(a) float
(b) char
(c) double
(d) byte

(iii) What is the final value stored in variable x?


double x = Math.ceil( Math.abs(-7.3) );
(a) 7.0
(b) 8.0
(c) 6.0
(d) 9.0

(iv) Which of the following keyword is used to crate symbolic constants in Java?
(a) final
(b) Final
(c) Constant
(d) Const

(v) Name the type of error in the statement given below:


double x; y; z;
(a) Logical error
(b) Syntax error
(c) Runtime error
(d) No error

Question 2.
Fill in the blanks with the correct options:
(i) The keyword to create an object of a class is ____________
(a) create
(b) new
(c) New
(d) NEW

(ii) The operator which acts on one operand is known as___________


(a) binary
(b) ternary
(c) unary
(d) relational

PUPIL TREE SCHOOL, BALLARI. Page 155 2023-2024


Computer Applications with Java & BlueJ Class X

(iii) The ASCII code of ‘B’ is _____________


(a) 67
(b) 66
(c) 98
(d) 99

(iv) A_________ method needs to be called with help of an object.


(a) void
(b) class
(c) non-static
(d) static

(v) Parameters used in the method call statement are___________


(a) Actual parameters
(b) Informal parameters
(c) Formal parameters
(d) Void parameters

Question 3.
Name the following:

(i) The concept of having more than one constructor with different types of parameters:
(a) Copy constructor
(b) Method overloading
(c) Constructor overloading
(d) Overloaded methods

(ii) The keyword which indicates that the method returns no value:
(a) public
(b) static
(c) void
(d) abstract

(iii) The process of binding the data and method together as one unit it called as:
(a) Encapsulation
(b) Inheritance
(c) Polymorphism
(d) Dynamic binding

(iv) The jump statement that is used inside a switch case construct to terminate a statement sequence:
(a) continue
(b) break
(c) return
(d) goto

(v) The program code written in any high-level language to solve a problem is:
(a) object code
(b) source code
(c) machine code
(d) bytecode

PUPIL TREE SCHOOL, BALLARI. Page 156 2023-2024


Computer Applications with Java & BlueJ Class X

Question 4.
State whether the statement is True or False:
(i) char is a non-primitive data type.
(a) True
(b) False

(ii) Multi line comments in Java start with /* and end with */.
(a) True
(b) False

(iii) == is an assignment operator.


(a) True
(b) False

(iv) Java compiler automatically creates a default constructor in case no constructor is present in the java class.
(a) True
(b) False

(v) System.exit(0) terminates the program from any point.


(a) True
(b) False

Question 5.
Choose the odd one:
(i) (a) >= (ii) (a) double (iii) (a) if else (iv) (a) nextInt( ) (v) (a) Robust
(b) % (b) int (b) if (b) nextDouble( ) (b) Platform Independent
(c) / (c) char (c) switch case (c) nextString (c) Inheritance
(d) String (d) next( ) (d) Multithreading
(d) * (d) while( )

Question 6.
Give the output of the following:
(i) int x=2, y=4, z=1; (ii) int x; (iii) int f=10, m=9;
int result = (++z) + y + (++x) + (z++); for( x=1; x<=3; x++) ; String e = (m%f == 9)? “YES” : “No” ;
(a) 11 System.out.print(x); System.out.print(e);
(b) 12 (a) 1 2 3 (a) YES
(c) 10 (b) 1 2 3 4 (b) No
(d) 9 (c) 4 (c) YESNO
(d) 1 (d) NOYES

(iv) switch(x)
{ (v) int v=5;
case ‘M’ : System.out.print(“ Microsoft Teams “); while( --v >= 0 )
break; {
case ‘G’ : System.out.print(“ Google Meet “); System.out.print(v);
default : System.out.print(“ Any software “); }
break;
case ‘W’: System.out.print(“ Web Ex”);
break; (a) 43210
} (b) 54321
When x=’g’ (c) 543210
(d) 4321
(a) Google Meet
(b) Any software
(c) Google Meet
Any software
(d) WebEx

PUPIL TREE SCHOOL, BALLARI. Page 157 2023-2024


Computer Applications with Java & BlueJ Class X

Question 7.
Given below is a class with following specifications:
class name : Number
void Display(int n ) - To extract and print each digit of given number from last digit of the first digit
on separate lines.
Example: n= 674
Output
4
7
6

void Display( ) – To print numbers from 0.5 to 5.0 with updation of 0.5.
Fill in the blanks of the given program with appropriate statements.
class (i) ___________
{
void Display(int n) {
while( (ii)__________)
{
int rem = (iii) _________
System.out.println(rem);
n = (iv) ___________
}
}

void Display( )
{
for(v)_______; x<=(vi)_______; x+=0.5)
System.out.println(x);
}
}

(i) (a) Number (ii) (a) n<0 (iii) (a) n%100; (iv) (a) n%10; (v) (a) double x=0.5 (vi) (a) x>=5.0
(b) number (b) n>0 (b) n/10; (b) n/10; (b) Double x=0.5 (b) x<=5.0
(c) NUMBER (c) n==0 (c) n%10; (c) n/100; (c) double x=0.0 (c) x==5.0
(d) NUM (d) n=0 (d) n/100 (d) n%100 (d) Double x=0.0 (d) x=5.0

Question 8.
A school is giving away merit certificates for the students who have scored 90 percentage and above in class 10.
The following program is based on the specification given below. Fill in the blanks with appropriate Java statements.
Class name: Student
Member variables:
String name : To enter name of a student
double per: To enter percentage obtained by the student
String cer: To store the message

Member method
void input( ) : To accept the detail
void merit( ): To check the percentage and award the merit
void display( ): To display the detail
void main( ): To create an object of the class and invoke the methods

import java.(i)________.*;
class Student
{
String name;
double per;
String cer;

PUPIL TREE SCHOOL, BALLARI. Page 158 2023-2024


Computer Applications with Java & BlueJ Class X

void input( )
{
Scanner obj = new Scanner(System.in);
System.out.println(“ Enter name, Percentage”);
name = obj.next( );
per = (ii) ________
}

void merit( )
{
if ( (iii)_______ )
{
cer = “AWARDED”;
}
else
cer = (iv)__________
}

void (v)_________
{
System.out.println(name);
System.out.println(per);
System.out.println(cer);
}

void main( )
{
Student s = new (vi)______
s.input( );
s.merit( );
s.display( );
}}
(i) (a) utility
(b) Util
(c) util
(d) UTILITY

(ii) (a) obj.nextDouble( );


(b) nextDouble( );
(c) obj.nextFloat( );
(d) nextFloat( );

(iii) (a) per >= 90


(b) per <= 90
(c) per > 90
(d) per == 90

(iv) (a) NOT AWARDED;


(b) “NOT AWARDED”;
(c) “AWARDED”;
(d) “NOT AWARDED”

(v) (a) display( )


(b) Display( )
(c) Print( )
(d) DISPLAY( )

(vi) (a) Student( );


(b) STUDENT( )
(c) student( )
(d) Student( )

PUPIL TREE SCHOOL, BALLARI. Page 159 2023-2024


Computer Applications with Java & BlueJ Class X

Question 9.
The following program segment checks whether number is an Abundant number or not. A number is said to be an Abundant
number when the sum of its factors (excluding the number itself) is greater than the number.

Example:
Factors of number 12 are 1,2,3,4,6
Sum of factors is 1+2+3+4+6 = 16
Fill in the blanks with appropriate java statements

void abundant(int n)
{
int s=0;
for( (i)_______; (ii)_________; i++)
{
if ( (iii) ________ )
s = s+i;
}

if ( (iv)______)
System.out.println(“ Abundant Number “);
else
System.out.println(“ Not Abundant Number “);
}

(i) (a) int i=1 (ii) (a) i<=n (iii) (a) n%i == 0 (iv) (a) s<n
(b) int i=0 (b) i<n (b) n%i == 1 (b) s>n
(c) int i=2 (c) i>n (c) n%2 == 0 (c) s==n
(d) Int i=1 (d) i>=n
(d) n/2 == 0 (d) s=n

Question 10.
Read the following case study and answer the questions given below by choosing the correct option:
Java compilation is different from the compilation of other high-level languages. Other high-level languages use interpreter or
compiler but in Java, the source code is first compiled to produce an intermediate code called the byte code, this byte code is
platform independent and is a highly optimized se of instructions designed to be executed by Java in the run time system
called JVM(Java Virtual Machine), JVM is a Java interpreter loaded in the computer memory as soon as java is loaded. JVM is
different for different platforms.

(i) Full form of JVM is: (ii) JAVA code is:


(a) Java Visual Machine (a) Compiled and interpreted
(b) Joint Vision Mechanism (b) Only compiled
(c) Java Virtual Machine (c) Only Interpreted
(d) Java virtual mechanism (d) Translated

(iii) JAVA source code is compiled to produce: (iv) JVM is an/a______


(a) Object code (a) Interpreter
(b) Byte code (b) Compiler
(c) Final code (c) Intermediate code
(d) Machine code (d) High level language

PUPIL TREE SCHOOL, BALLARI. Page 160 2023-2024


Computer Applications with Java & BlueJ Class X

COMPUTER APPLICATIONS
SEMESTER 2 EXAMINATION (50 Marks)
SECTION A (10 Marks)
Question 1
Choose the correct answers to the questions from the given options.
(Do not copy the question. Write the correct answers only.)

(i) Return data type of isLetter(char) is _________.


(a) Boolean
(b) boolean
(c) bool
(d) char

(ii) Method that converts a character to uppercase is _____________.


(a) toUpper( )
(b) ToUpperCase( )
(c) toUppercase( )
(d) toUpperCase(char)

(iii) Give the output of the following String methods:


“SUCESS”.indexOf(‘S’) + “SUCESS”.lastIndexOf(‘S’)
(a) 0
(b) 5
(c) 6
(d) -5

(iv) Corresponding wrapper class of float data type is ________.


(a) FLOAT
(b) float
(c) Float
(d) Floating

(v) _______ class is used to convert a primitive data type to its corresponding object.
(a) String
(b) Wrapper
(c) System
(d) Math

(vi) Give the output of the following code:


System.out.println(“Good”.concat(“Day”));
(a) GoodDay
(b) Good Day
(c) Goodday
(d) goodDay

(vii) A single dimensional array contains N elements. What will be the last subscript?
(a) N
(b) N-1
(c) N-2
(d) N+1

PUPIL TREE SCHOOL, BALLARI. Page 161 2023-2024


Computer Applications with Java & BlueJ Class X

(viii) The access modifier that gives least accessibility is:


(a) private
(b) public
(c) protected
(d) package

(ix) Give the output of the following code:


String A=”56.0”, B=”94.0”;
double C = Double.parseDouble(A);
double D = Double.parseDouble(B);
System.out.println( (C+D) );
(a) 100
(b) 150.0
(c) 100.0
(d) 150

(x) What will be the output of the following code?


System.out.println(“Lucknow”.substring(0,4));
(a) Lucknow
(b) Luckn
(c) Luck
(d) luck

SECTION B (40 Marks)


(Attempt any four questions from this Section)

Question 1 [10 Marks]


Define a class 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 is position, otherwise display the message “Search element not
found”.
2,5,7,10,15,20,29,30,46,50
Repeated question, refer page no 52, 98 & 143 for solutions

Question 2 [10 Marks]


Define a class to accept a string, and print the characters with uppercase and lowercase reversed, but all the other
characters should remain the same as before.
Example: Input: WelCoMe_2022
Output: wELcOmE_2022
Repeated question, refer page no 32 & 33 for solutions

PUPIL TREE SCHOOL, BALLARI. Page 162 2023-2024


Computer Applications with Java & BlueJ Class X

2022 – Question 3. [10 Marks]


Define a class to declare a character array of size ten, accept the characters into the array and display the
characters with highest and lowest ASCII (American Standard Code for Information Interchange) value.
EXAMPLE:
INPUT:
‘R’, ‘z’, ‘q’, ‘A’, ‘N’, ‘p’, ‘m’, ‘U’, ‘Q’, ‘F’
OUTPUT:
Character with highest ASCII value = z
Character with lowest ASCII value = A

// Program to display the characters with highest and lowest ASCII


import java.util.*;

class ASCII
{
public static void main(String args[ ])
{ Scanner sc = new Scanner(System.in);

char A[ ] = new char[10];


char max = 'A';
char min = 'z';

System.out.println(" Enter 10 characters ");


for (int i=0; i<A.length; i++)
{
A[i] = sc.next().charAt(0);

if (max < A[i])


max = A[i];

if (min > A[i])


min = A[i];
}

System.out.println(" Character with highest ASCII value = "+max);


System.out.println(" Character with lowest ASCII value = "+min);
}
}
OUTPUT
Enter 10 characters
R
z
q
A
N
p
m
U
Q
F
Character with highest ASCII value = z
Character with lowest ASCII value = A

Note: char min = 'z'; (Assign lower case z to variable min)

PUPIL TREE SCHOOL, BALLARI. Page 163 2023-2024


Computer Applications with Java & BlueJ Class X

2022 – Question 4. [10 Marks]


Define a class to declare an array of size twenty of double datatype, accept the elements into the array and
perform the following:
• Calculate and print the product of all the elements.

• Print the square of each element of the array.

/* Program to print the product of all elements and square of each element of the array */
import java.util.*;

class MyArray
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

int n=5;
int product=1;

int A[ ] = new int[n];

System.out.println(" Enter "+n+" elements ");


for(int i=0; i<n; i++)
A[i] = sc.nextInt();

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


{
product *= A[i];
System.out.println(A[i]*A[i]);
}

System.out.println(" Product of all elements = " + product);


}
}

OUTPUT
Enter 5 elements
11
12
13
14
15

121
144
169
196
225
Product of all elements = 360360

PUPIL TREE SCHOOL, BALLARI. Page 164 2023-2024


Computer Applications with Java & BlueJ Class X

2022 – Question 5. [10 Marks]


Define a class to declare an array to accept and store ten words. Display only those words which begin with the
letter ‘A’ or ‘a’ and also end with the letter ‘A’ or ‘a’.
Example:
Input: Hari, Anita, Akash, Amrita, Alina, Devi, Rishab, John, Farha, AMITHA
Output: Anita
Amrita
Alina
AMITHA
/* Program to accept ten words and display those words which starts and ends with letter 'A' or 'a' */
import java.util.*;

class DisplayWords
{ public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

int n=10;
String A[ ] = new String[n];

System.out.println(" Enter "+n+" words ");


for(int i=0; i<n; i++)
A[i] = sc.next();

boolean start,end;
System.out.println(" Words starts and ends with the letter 'A' or 'a' ");

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


{
start = A[i].startsWith("A") || A[i].startsWith("a");
end = A[i].endsWith("A") || A[i].endsWith("a");

if (start && end)


System.out.println(A[i]);
}
}
}
OUTPUT
Enter 10 words
Hari
anita
Akash
Amrita
alinA
Devi
Rishab
John
Farha
AMITHA

Words starts and ends with the letter 'A' or 'a'


anita
Amrita
alinA
AMITHA

PUPIL TREE SCHOOL, BALLARI. Page 165 2023-2024


Computer Applications with Java & BlueJ Class X

2022 – Question 6. [10 Marks]


Define a class to accept two strings of same length and form a new word in such a way that, the first character of
the first word is followed by the first character of the second word and so on.

Example: Input string 1 – BALL


Input string 2 – WORD

OUTPUT: BWAOLRLD

// Program form a new word from old words...


import java.util.*;

class Neword
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter two strings of same length ");


String a = sc.next( );
String b = sc.next( );

String c = "";

for(int i=0; i<b.length( ); i++)


c = c + a.charAt(i) + b.charAt(i);

System.out.println(c);
}
}

OUTPUT
Enter two strings of same length
BALL
WORD

BWAOLRLD

PUPIL TREE SCHOOL, BALLARI. Page 166 2023-2024


Computer Applications with Java & BlueJ Class X

2023 – Question 1. [15 Marks]


Design a class with the following specifications:
Class name: Student

Member variables:
name – name of student
age – age of student
mks – marks obtained
stream – stream allocated
(Declare the variables using appropriate data types)

Member methods:
void accept( ) – Accept name, age and marks using methods of Scanner class.
void allocation( ) – Allocate the stream as per following criteria:
mks stream
>= 300 Science and Computer
>=200 and <300 Commerce and Computer
>=75 and 200 Arts and Animation
<75 Try Again

void print( ) – Display student name, age, mks and stream allocated.

Call all the above methods in main method using an object.

// Program to input student marks and allocate the stream as per criteria...
import java.util.*;

class Student
{
String name,stream;
int age,mks;

void accept()
{
Scanner sc = new Scanner(System.in);

System.out.println(" Accept name, age and marks ");


name = sc.next();
age = sc.nextInt();
mks = sc.nextInt();
}

void allocation( )
{
if (mks >= 300)
stream = "Science and Computer";
else if (mks >= 200)
stream = "Commerce and Computer";
else if (mks >= 75)
stream = "Arts and Animation";
else
stream = "Try Again";
}

PUPIL TREE SCHOOL, BALLARI. Page 167 2023-2024


Computer Applications with Java & BlueJ Class X

void print( )
{
System.out.println(" Student Details ");
System.out.println(" Name : " + name);
System.out.println(" Age : " + age);
System.out.println(" Marks : " + mks);
System.out.println(" Stream : " + stream);
}

public static void main(String args[])


{
Student s = new Student( );
s.accept();
s.allocation();
s.print();
}
}

OUTPUT 1:
Accept name, age and marks
Rohan
15
300

Student Details
Name : Rohan
Age : 15
Marks : 300
Stream : Science and Computer

OUTPUT 2:
Accept name, age and marks
Sohan
15
299

Student Details
Name : Sohan
Age : 15
Marks : 299
Stream : Commerce and Computer

PUPIL TREE SCHOOL, BALLARI. Page 168 2023-2024


Computer Applications with Java & BlueJ Class X

2023 – Question 2. [15 Marks]


Define a class to overload the function print as follows:
void print( ) to print the following format
11111
22222
33333
44444
55555

void print(int n) To check whether the number is a lead number. A lead number is the one whose sum of even
digits are equals to sum of odd digits.
e.g. 3669 odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12
3669 is a lead number.

// Program to overload the function print as follows...

class Overload
{
void print( )
{
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
System.out.print(i);
System.out.println();
}
}

void print(int n) public static void main(String args[])


{ {
int d,esum=0,osum=0; Overload obj = new Overload();
obj.print();
while(n>0) obj.print(3669);
{ }
d=n%10; }
n=n/10;
if (d%2==0)
esum += d; OUTPUT
else 11111
osum += d; 22222
} 33333
44444
if (esum == osum) 55555
System.out.println(" Lead number ");
else Lead number
System.out.println(" Not a lead number ");
}

Note: For this question, main method not asked in exam.

PUPIL TREE SCHOOL, BALLARI. Page 169 2023-2024


Computer Applications with Java & BlueJ Class X

2023 – Question 3. [15 Marks]


Define a class to accept a String and print the number of digits, alphabets and special characters in the string.
Example: S = “KAPILDEV@83”
Output: Number of digits – 2
Number of Alphabets – 8
Number of Special characters -1

/* Program to accept a string and print number of digits, alphabets


and special characters */

import java.util.*;

class MyString
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println(" Enter a string ");


String s = sc.next();

int alpha=0,digits=0,special=0;

for(int i=0; i<s.length(); i++) for(int i=0; i<s.length(); i++)


{ {
char ch = s.charAt(i); char ch = s.charAt(i);

if ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z')) if ( Character.isLetter(ch) )


alpha++; OR alpha++;
else if (ch>='0' && ch<='9') else if ( Character.isDigit(ch) )
digits++; digits++;
else else
special++; special++;
} }

System.out.println(" Number of alphabets = " + alpha);


System.out.println(" Number of digits = " + digits);
System.out.println(" Number of special = " +special);
}
}

OUTPUT
Enter a string
BillGates456@gmail.com

Number of alphabets = 17
Number of digits = 3
Number of special = 2

PUPIL TREE SCHOOL, BALLARI. Page 170 2023-2024


Computer Applications with Java & BlueJ Class X

2023 – Question 4. [15 Marks]


Define a class to accept values in integer array of size 10. Find sum of one digit number and sum of two digit
numbers entered. Display them separately.
Example: Input: a[ ] = {2,12,4,9,18,25,3,32,20,1}
Output: Sum of one digit numbers: 2 + 4 + 9 + 3 + 1 = 19
Sum of two digit numbers: 12 + 18 + 25 + 32 + 20 = 107

// Program to find sum of one digit and two digit numbers separately...
import java.util.*;

class FindSum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

int A[ ] = new int[10];


int sum1=0,sum2=0;

System.out.println(" Enter 10 integer elements ");

for(int i=0;i<=9;i++)
{
A[i] = sc.nextInt( );
if (A[i]>=0 && A[i]<=9) sum1+=A[i];
if (A[i]>9 && A[i]<=99) sum2+=A[i];
}

System.out.println(" Sum of one digit numbers : " + sum1);


System.out.println(" Sum of two digit numbers : " + sum2);
}
}

OUTPUT
Enter 10 integer elements
2
12
4
9
18
25
3
32
20
1

Sum of one digit numbers : 19


Sum of two digit numbers : 107

PUPIL TREE SCHOOL, BALLARI. Page 171 2023-2024

You might also like