Sie sind auf Seite 1von 23

Question 9

Defind a clas Electric Bill with the folloeing specifications :


Class _ Electric Bill
Instance variables / data member :
String n _to store the name of the customer
Int units _ to store the number of units consumed
Double bill _to store the amount to be paid
Membermethods :
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 surchaege of 2.5% charged if the units consumed of above 300 units.
Void print ( ) _ To print the details as follows :
Name of the constomer : ..
Number of units consumed :..
Bill amount : .
Write a main method to creae an object of the class and call the above member
method.
Question 10.
Write a progeam to accept a number and check and display whether it is a
spy number or not. (A number is spy if the 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 = 1 * 1 *1 *2*4 = 8
Answer.
Import java.unil.Scanner;
Class spy
{
Public ststic void main ( )
{
Int n;
Scanner s = new Scanner (System.in);
System.out.printIn (Enter a number);
n= s.nextInt ( );
Int sum = 0, P + 1;
Int d;
While (n ! = 0)
{
d =n % 10;
Sum = sum + d;
P = P * d;
N + n/10;
}
If (sum = = P)
System.out.printIn (It is a spy number);
Else
System.out.PrintIn (It is not a spy number);
}
{
Question 11.
Using switch statement, write a menu driven program for the following :
(i) To find anddisplay the sum of the series given below :
S = x1 x2 + x3 x4 + x5 - x20
(where x = 2)
1 11 111 1111 11111
For an incorrect option, an appropriate error message should be displayed.
Question 12
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.
Question 13.
Design a class to overload a function check ( ) as follows :
(i) Void chrck (String str, char ch) to find print the frequency of a
character in a strng.
Example :
input : Output :
Str = success number of s persent is = 3
Ch = s
Void check(String a1) to display only vowels from string s1, after
converting
it to lower case.
Exampele :
input : s1 = computer Output : o u e
Answer.
Class overload
{
Void check (String str, c;
For (int I = 0; I <str.lenght ( ); I ++)
{
Char c;
S1 = s1.toLowerClass ( );
For (int i = 0; I < s1.length( ); I ++)
{
c = s1.charAt (i);
If (c= = a II c = = e II c = = I II c = = o I I c = = u)
System.out.printIn ;
}
}}

Question 14.
Write a program to input forty woder in an array. Arrange these words in
descending order of alphabets, using selection sort technique. Print the
sorted array.
Question 15.
Defind Encapsulation.
(a) What are keywords ? Give an example.
(b) Name any to library packages.
(c) Name the type of errer (syntax, runtime or logical errer) in each case given
below :
(i) Math.sqrt (36 _45)
(ii) Int a;b;c;
(d) If int x [ ] = { 4, 3, 7, 8, 9, 10}; what are the values of p and q ? {2}
(i) P = x.length
(ii) Q = x[2] + x{5} * x{1}

Question 16.
(a) give the output of the following string functions :
(i) MISSISSIPPI.indexOf(S) + MISSISSPPI.lastndexOf(T)
(ii) CABLE.compareTo(CADET)
(b) Give the output of the following Math functions :
(i) Math.ceil)4.2)
(ii) Math.abs(-4)
(c) What is a parameterized constructor ?
(d) Write down java expression for :
T = 2 + 2
(e) Rewrite the following using ternary operator :
If(x%2 == 0)
System.out.print(EVEN);
Else
System.out.print(ODD);
(f) Convert the following while loop to the corresponding for loop :
Int m = 5, n = 10;while (n>=1)
{
System.out.printIn(m*n);
n--;
}
(g) Write one difference between primitive data types and composite data
types
(h) Analyxe the gaven program segment and answer the following questions :
(i) Write the output of the program segment.
(ii) How many times does the body of the loop gets executed ?
For(int m=5; m<=20; m+=5)
{ if(m%3==0)
Else
If(m%5==0)
System.out.println(m);
Continue;
}
(i) Give the output of the following expression :
A+=a++ + ++A + --a + a-- ; when a = 7
(j) Write the return type of the following expression :
(i) isLetterOrDigit(char)
Question 17.
Define a class named Book Fair with the following description :
Instance variables / Data member :
String Bname _ stores the name of the book
Double price _ stores the name of the book
Member methods :
(i) book Fair() _ 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 calculated 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 then or equal to 3000 10% of price
More then 3000 15% of price
(iv) void display() _ To display the name and price of the book after
discount.
Write a main method to create on object of thr class and call the above member
Methods.
Question 18.
Using the switch statement, write a menu driven program for the following :
[15]
(i) To print the Floyds triangale [given below]
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(ii) To display the following pattern :
I
I C
I C S
I C S E
For an incorrect option,an appropriate error massage should be displayrd.
Answer.
(i) import java.unit.Scanner;
Class pattern
{
Public static void main ( )
{
Scanner sc = new Scsnner (Systam. In);
System.out.printIn (1. Pattern 1 \n 2. Pattern 2);
System.out.pattern (Enter choice);
Int c = sc.nextInt ( )
;switch (c)
{
Case 1 :
Int a, b, c = 1;
For (a = 1; a < = 5; a++)
{
For (b = 12; b < = a; b++)
{
System.out.print (c );
C++
}
System.out.printIn ( );
}
Break;
(ii) String s = ICSE; int 1 = s.length( );
For (int I = 0; I < = I; j++)
{
System.out.print (charA+(j));
}
Break ;
Default :
Sysetem.out.printIn (Invalid choice);
}
}
}
Question 19.
Special words are those words which starts and ends with the same letter.
Examples :
EXISTENCE
COMIC
WINDOW
Palindrome word 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.
Question 20.
(a) What are the default values of the primitive data type int and float ?
(b) Name any two OOPs principles.
(c) What are identifiers ?
(d) Identify the literals listed below :
(i) 0.5 (ii) A (iii)false (iv) a
(e) Name the wrapper classes of char type and boolean type.

Question 21.
(a) Evaluate the value of n if value of p = 5, q = 19
Int n = (q p)>: (p q) ? (q p) : (p q);
(b) Arrange the following primitive data types in an ascending order of their
size : (i) char (ii) byte (iii) double (iv)
int.
(c) What is the value stored in variable res given below :
Doble res = Math.pow (345.indexOf(5), 3);
(d) Name the two types of a and b after the following function is executed, if
the Values passed are 30 and 50 :
Void paws(int a, int b)
{ a=a + b;
b=a b;
a=a b;
System.out.printIn(a+ , +b);
}
Question 22.
(a) state the data type and value of y after the following is executed :
Char x=7;
Y=Character.isLetter(x);
(b) What is the function of catch block in exception handling ? Where
does it appear
In a program ?
(c) State the putput when the fillowing program segment is executed :
String a =Smartphon, b=Graphic Art;
String h=a.substring(2, 5);
String k=b .substring(8).toUpperCase();
System.out.printIn(h);
System.out.prientIn(k.equalsIgnoreCase(h));
(d) The access specifier that gives the most accessibility is ...and
the least
Accessibility is
(e) (i) Name the mathematical function which is used to find sine of an
angle
Given in radians.
(ii) Name a string function which removes the blank spaces provided in
the prefix and suffix of a steing.
(f) (i) What will this coad print ?
Int arr[] =new int[5];
System.out.printIn(arr);
(i) 0
(ii) value stored in arr[0]
(iii) 0000
(iv) garbage value
(ii) Name the keyword which is used to resolve the conflict between
method
Parameter and instance variables/fields.
(g) State the package that contains the class :
(i) BufferedReader
(ii) Scanner.
(h) Write the output of the following program coad :
Char ch;
Int x=97;
Do
{
Ch= (char) x;
System.out.print(ch + );
If(x%10 == 0)
Break;
++x;
}while(x<= 1090);
(i) Write the Java expressions for 2 +
2ab
(j) If int y = 10 then find int z = (++y * (y++ +5));
[2]
Question 23.
Define a class named ParkingLot with the following description :
Instance varirables / data member :
Int vnu _ To store the vehical number
Int hours _ To store the number if hours the vehical is parkd
in the parking lot
Double bill _ To store the bill amount
Member methods :
Void input( ) _ To input and store the vno and hours.
Void calculate( ) _ To comput the parking charge at the rate of 3 for
the first houre or part thereof,and 1.50 for each
additional
Hour or part thereof.
Void display( ) _ To display the detail
Write a main methd to create an object of the class and call the avove
methods.
Question 24.
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

Question 25.
Write a program to input and store roll numbers, names and marks in 3
subjects
Of n number students in five single dimensional array and display the
remark
Based on average marks as given below : (The maximum marks in the
subject are
100)
Total Marks
Average marks =3
Average marks Remarks
85-100 EXCELLENT
75-84 DISTINCTION
60-74 FIRST CLASS
40-59 PASS
Less thrn 40 POOR
Question 26.
Design a class to overload joystring( ) as follows :
(i) void joystring (String s, char ch1 char ch2) with one satring argument
and two character arguments that replaces the character
argument ch 1 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 : TECHNALAGY
(ii) void joystring (String s) with one string argument that prints the
position
Of the first space and the last space of the given string s.
Example :
Input value of = Cloud computing means Intertent based
computing
Out : First index : 5
Last index : 36
(iii) void Joystring (String s1, String s2) with two string arguments that
combines
The two string with a space between them and prints the resultant
string.
Example :
Input value ofd s1 =COMMON WELTH
Input value ofd s2 \=GAMES
Output : COMMON WELTH GAMES
(use library functions)
Question 27.
Write a program to input twenty names in an array. Arrange these names i
Descending order of alphabets, using the bubble sort technique.
Question 28.
Using the switch statement, Write a menu driven program to :
(i) To find and display all the factors of a number uinput by the user
(including
1 and excluding number itself).
Example :
Sample Iutput : n=15
Sample Output : 1, 3, 5.
(ii) To fine 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
intergers less
Then or equal to n.
Example :
Sample Input : n=5
Sample Output : 5! = 1 * 2 * 3 * 4 * 5 = 120.
For an incorrect choice, an appropriate error massage should
displayed.

Question 29.
(a) Which of the following are valid comments ?
(i) /* comment */
(ii) /* comment
(iii) / / comment
(iv) */comment */
(b) What is meant by a package ? Name any two java Application
Programming
Interface packages.
(c) Name the primitive data type in java that is :
(i) a 64-bit integer and is used when you need a range of values wider
Then those provided by int.
(ii) a single 16-bit Unicode character whose default value is \u0000
(d) State one difference between the following point literals float and double.
(e) Find the errors in the given program segment and re-write the seatements
correctly
To assign values to an integer array.
Int a = new int (5);
For (int I = 0; I < =5; i++) a [i] = I;
Question 30.
(a) Operators with higher precedence are evaluated before operators with
relatively
Lower precedence. Arrange the operators given below in higher
precedence
To lower precedence.
(i) && (ii) % (iii) > = (iv) ++
(d) Identify the statements listed below as assignment, increment, method
invocation
Or object creation statements.
(i) System.out.printin(Java);
(ii) costPrice = 457.50;
(iii) Car hybrid = new Car ( );
(iv) peteolPrice++;
(c) Give two differences between the switch statement and the If-else
statement.
(d) What is an infinite loop ? Write an infinite loop statement.
(e) What is constructor ? when is it invoked ?
Question 31.
(a) List the variables from those given below that are composite data type :
(i) static int x; (ii) arr[i] = 10; (iii) obj.display( );
(iv) Boolean b; (v) private char chr; (vi) String str;
(b) State the output of the following program segment :
String str 1 = grate;String str2 = minds;
System.out.printIn (str 1.substring (0,2).concat(str2.substring (1)));
System.out.printIn ((WH+(str1.substring (2).toUpperCase())));
(c) What is the final values stored in variable x and y below ?
Double a = - 6.35;
Double b = 14.74;
Double x = Math.abs(Math.ceal(a));
Double y = Math.rint (Math max (a,b));
(d) Rewrite the following program segment using if-else statement instead of
the
Ternary operator :
String grade = )mark>=90) ? A : (mark>=80) ? B : C
(e) give the output of the following method :
Public static void main (String [ ] args){
Int a = 5;
a++;
System.out.printIn(a);
A - = (a--) (- - a);
System.out.printIn(a); }
(f) what is the data type returned by the library functions :
(i) compareTo( )
(ii) equals( )
(g) state the value of characteristic and mantissa when the following coad is
executed :
String s = 4.3756;
Int n = s.indexOf(.);
Int characteristic=Integer.parseInt (s.substring(O,n));
Int mantissa=Integer.valuOf(s.substring(n+1));
(h) Study the method and answer the given questions.
Public void sampleMathod()
{ for (int i=0; I < 3; i++)
{ for( int j = 0; j<2;j++)
{ int number = (int) (Math.random() * 10);
System.out.printIn(number); }}}
(i) How many times does the loop execute ?
(ii) What is the range of possible values stored in the variable number ?
(i) Consider the following class :
Public class myClass {
Public static int x=3, y=4;
Public int a=2, b=3; }
(i) Name the variables for whoch each object of the class will have its
own distinct
Copy.
(ii) Name the variable that are common to all objects of the class.
(j) What will be the output when the following coad segments are executed ?
(i) String s = 1001;
Int x = Intger.valueOf(s);
Double y = Double.valueOf(s);
System.out.printIn(x=+x);
System.out.printIn(y=+y);
(ii) System.out.printIn(The king said\Begin at beginning!\to me.);
Question 32.
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
floor rating - to store the popularity rating of the movie
(minimum rating=0.0 and maximum rating=5.0)
Member methods :
(i) movieMagiuc( ) Default constructor to initialize numeric data
member to 0 and String data member to ,
(ii) void accept( ) To input and store year, title and rating.
(iii) void display( ) To display the title of a movie and a
massage based on the rating as per the table
below.
Rating Massage 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 Suyper Hit
Write a main method to create an object of the class and call the above
member
methods.
Question 33.
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 * 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
massage
Special 2-digit number otherwise, output the massage Not a special 2-
digit
number.
Question 34.
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
Question 35.
Design a class to overload a function area( ) as follows :
(i) double area (double a, double b, double c) with three double
arguments,
Return the area of a scalene triangle using the formula :
area =
where s =
(ii) double area (int a, int b, int height) with three inter integer
arguments, returns
the area of a trapezium using the formula :
area =
(iii) double area (double diagonal 1, double diagonal 2) with two double
Arguments, returns the area of a rhombus using the formula :
Area =
Question 36.
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 :
(a) Term Deposit
(ii) Recurring Deposit
For option (i) accept principal (P), rate of interest and time period in years(n).
Calculate and put the maturipal amount (A) receivable using the formula
A=P
For option (ii) accept Monyhly Installment (P), rate of interest and time period
In months (n). Calculate and putput the manturity amount (A) receivable using
the
Formula
A=P8n+P*
For an incorrect op-tion, an appropriate error massage should be displayed
Question 37.
Wright 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 massage Record exists. If the value input is located in the
array. If
Not, output the massage Record does not exist.
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}.
Question 38.
(a) What is meant by precedence of operators ?
(b) What is a literal ?
(c) State the Java concept that is implemented through :
(i) a superclass and a subclass.
(ii) the act of representing essential features without including
background
Details.
(d) Give a difference between a constructor and a method.
(e) What are the types of casting shown by the following examples?
(i) double x = 15.2;
Int y = (int) x;
(ii) int x = 12;
Long y x;
Question 39.
(a) Name any two wrapper classes.
(b) What is the difference between a break statement and a continue
statement
When they occur in a loop ?
(c) Weite Statement to show how finding the length of a character array char
Differs from finding the length oif a string object str.
(d) Name te Java keyword that :
(i) indicates that a method has no return type.
(ii) store the address of the currently-calling object.
(e)What is an exception ?
Question 40.
(a) Write a Java statement to create an object mp 4 of class digital.
(b) State the values stored in the variables str 1 and str 2
String s1 = good; String s2 = world / matters;
String str 1 = s2. Substring (5). replace (t, n);
String str 2 = s1. Concat (str 1);
(c) What does a class encapsulate ?

(d) Rewring the following program segment using the ifelse statement
comm. = (sale >> 15000) ? Dale x 5 / 100 : 0;
(e) How meny times will the following loop execute ? What value will be
returned ?
Int x = 2, y = 50;
Do {
++ x;
Y - = (x < = 10);
return y;
(f) What is the data type that the following library functions return ?
(i) is Whitespace (char ch)
(ii) Math.random( )
(g) Write a Java expression for ut + ft2
(h) If int n ] = { 1, 2, 3, 5, 7, 9, 13, 16}, what are the values of x and y ?
X = Math.pow (n[4], n [2]);
Y = Math.sqrt (n[5]+n[7];
(i) What is the final value of ctr when the iteration process given below,
executes ?
Int ctr = 0;
for (int I = 1; I < = 5; I ++)
for (int j = 1; j < = 5; j + = 2)
++ ctr;
(j) Name the methods of Scanner class that :
(i) is used to input an integer data from the standard input stream
(ii) is used to input a String data from the standard input stream.
Question 41.
Write a program to input 10 integer elements in an array and sort them in
Descending order using the dubble sort technique.

Question 42.
a class to overload a function series ( ) as follows :
(i) double series (double n) with one double argument and return the
sum of the 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 + 2 + 2 + 2 + to n terms

Question 43.
(a) Give one example each of a primitive data type and a composite data type.
(b) Give one point of difference between unary and binary operators.
(c) Differentiate between call by value or pass by value and call by reference
or
Pass by reference.
(d) Write a Java expression for
(e) Name the type of error (syntax, runtime or logical error) in each ease given
Below :
(i) division by a variable that contains a value of zero.
(ii) Multiplication operator used when the operation should be division.
(iii) Missing semicolon.
Question 44.
Write a program to accept a string. Convert the string to uppercase. Count
and
output the number of double letter sequences that exist in the string.
Sample Input : SHE WAS FEEDING THE LITTLE RABBIT WITH AN
APPLE
sample output : 4
Question 45.
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 coad, or else display the massage Serch Unsuccessful, No
such
City in the list.
Question 46.
(a) State the total size in bytes, of the arrays a [4] of char data type and p[4]
of
Float data type.
(b) (i) Name the package that contains Scanner class.
(ii) Which unit of the class gets called, when the object of the class is
created ?
(c) Give the output of the following :
String n=Computer knowledge.;
String m=Computer Applications.;
System.out.printIn(n.substring(0,8).concat(m.substring(9)));
System.out.printIn(n.ends with(e));
(d) Write the output of the following :
(i) System.out.printIn(Character.isUpperCase(R));
(ii) System.out.printIn(Character.isUpperCase(j));
(e) What is the role of keyword void in declaring functions ?
Question 47.
Write a program to input and store the weight of ten people. Sort and
display
Them in descending order using the selection sort technique.
Question 48.
Write a program to accept a word and convert it into lowercase if it is in
Uppercase, and display the new word by replacing only the vowels with
the
Character following it :
Example :
Sample Input : computer
Sample Ourput : cpmpvtfr
Question 49.
Design a class to overload a function compare() as follows :
(a) void compare(int, int) - to compare two int\rger values and print the
greater
of the two integers.
(b) void compare(char, char) - to compare the numeric value of two
characters
and print the character with higher numeric value.
(c) void compare - to compare the length of the two strings
(String, Strimg) and print the longth of the two.
Question 50.
Write a menu driven program to perform the following : (Use switch-case
statement)
(a) To print the series 0, 3, 7, 15, 24 .. n terms (value of
n is to be an
Input by the user).
(b) To find the sum of the series given below :
S = +5/6 + 7/8 .19/20.
Question 51.
Write a class with the name volume using function overloading that
computrs
The volume of a cube, a sphere and a cuboid.
Formula volume of a cube (vc) = s*s*s
volume of a sphere (vs) = 4/3* r* r* r*r
(where = 3.14 or 22/7
volume of a cuboid (vcd) = 1* b* h
Question 52.
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 to be input by the user).
Question 53.
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 argument that
draws a
Filled rectangle of length x and
breadth y,
Using the symbol @
(iii) void polygon( ) with no argument that draws a filled
Triangle shown below.
Example :
(i) Input value of n=2, ch=O
Output : O O
O O
(ii) Input value of x=2, y=5
Output : @@@@@
@@@@@
(iii) Output : *
**
***
Question 54.
Define a class and store the given city name 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

Das könnte Ihnen auch gefallen