ICSE Questions On Functions1
ICSE Questions On Functions1
ICSE Questions On Functions1
3 In the program given below, state the name and the value of the
(i) method argument or argument variable
(ii) class variable
(iii) local variable
(iv) instance variable
class myClass
{ int y = 2;
public static void main(String args[ ])
{ myClass obj = new myClass( );
System.out.println( x );
2012
obj.sampleMethod( 5 );
int a = 6;
System.out.println( a );
}
void sampleMethod( int n )
{ System.out.println( n );
System.out.println( y );
}
}
https://sites.google.com/site/javaprogramsisc/ ICSE Questions
3
4 Consider the following class:
public class myClass
{
public static int x = 3, y = 4;
2014
public int a = 2, b = 3;
}
(i) Name the variables for which each object of the class will have its own distinct copy.
(ii) Name the variables that are common to all objects of the class.
5 What will be the output of the following code when function xy() is executed:
class abc
{
final int x=10;
int y=20;
public void xy()
{ System.out.println("Value of x and y "+ x + y);
int x=50;
ax(x);
System.out.println("Value of x and y "+ x + y);
}
void ax(int x)
{ System.out.println("Value of x and y "+ x + y);
int t=x;
x=y;
y=t;
System.out.println("Value of x and y "+ x + y);
}
}
6 Give the output for the following:
public boolean fun (int a)
{ int c = a% a++ + ++a - ++a;
System.out.println (c) ;
boolean d = false ;
return d ;
}
What will be the output and the returned value of the above defined method for fun (2)
7 Explain the concept of constructor overloading with an example. 2011
8 Explain function overloading with an example. 2006
9 Which OOP principle implements function overloading? 2007
10 When there are multiple definitions with the same function name, what makes them different
2009
from each other?
11 What is meant by private visibility of a method? 2006
12 Differentiate between private and protected visibility modifiers. 2010
13 Differentiate between public and private modifiers for members of a class. 2012
14 What is the scope of the keyword protected in accessing methods? 2007R
15 Explain any two types of access specifier. 2008
https://sites.google.com/site/javaprogramsisc/ ICSE Questions
4
16 Differentiate between base and derived class. 2008
1 Define a class named movieMagic with the following description: 2014
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 ""
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.05 Hit
4.6 to 5.0 Super-Hit
2 Define a class named FruitJuice with the following description: 2013
Instance variables/data members:
int product_code - stores the product code
String flavour - stores the flavour of the juice ( e.g. orange, apple, etc. )
String pack_type - stores the type of packing ( 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:
FruitJuice( ) - Default constructor to initialize integer data members to 0
and String data members to “ ”.
void input( ) - To input and store the product code, flavor, pack type, pack size
and product price.
void discount( ) - To reduce the product price by 10.
void display( ) - To display the product code, flavor, pack type, pack size and
product price.
3 Define a class called mobike with the following description: 2011
Instance variables/data members:
int bno - to store the bike’s number
int phno - to store the phone number of the customer
String name - to store the name of the customer
int days - to store the number of days the bike is taken on rent
int charge - to calculate and store the rental charge
Member methods:
void input( ) - to input and store the detail of the customer
void compute( ) - to compute the rental charge
The rent for a mobike is charged on the following basis:
First five days Rs 500 per day
Next five days Rs 400 per day
Rest of the days Rs 200 per day
void display( ) - to display the details in the following format:
Bike No. Phone No. Name No. of days Charge
https://sites.google.com/site/javaprogramsisc/ ICSE Questions
5
--------- ------------ ---------- ---------- ---------
9 Write a Java program to display the menu and perform the following as per the user’s choice
using the functions given below:
(a) Print the sum of all the factors of a number
(b) Check and print if a number is perfect or not. A number is a perfect number if the sum
of all the factors of the number excluding itself is equal to the number.
Use the following to perform the above:
int SumOfFactors(int N) to calculate sum of all factors of N, and return it to the
calling function.
boolean chkPerfect(int Num) to check whether the value in Num is perfect or not
void MyMain( ) to implement user’s choice
10 Write a Java program to input two numbers and print the sum of all the Prime numbers
between them, using the following functions:
int CountFactors(int Num) to return the count of all factors of Num
void AddPrimes( int A , int B) to print the sum of all the Prime numbers between A and
B using the other function of the class.
https://sites.google.com/site/javaprogramsisc/ ICSE Questions
7
16 Write a class with the name volume using function overloading that computes the volume of 2008
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 (where = 3.14 or 22/7 )
volume of a cuboid vcd = l * b * h
17 Using function overloading in Java, design a class Overload which contains the method
Palin( ) to perform the following:
(a) To check whether the number is a palindrome or not. A number is said to be a
palindrome if it reads the same from left to right and vice versa. For example 6226
(b) To check whether a word is a palindrome or not. A word is said to be a palindrome if it
reads the same from left to right and vice versa. For example ARORA
Display the menu to accept choice from the user and perform the above tasks.
18 Write a class TriangleArea in Java that contains the overloaded methods area – one that
accepts the length of the base and the height of the triangle as parameters. The other area
method accepts the length of three sides of a triangle. The two formulae for computing the
are:
Area = ½ * base * height when length and height are given
Area = √(s(s-a)(s-b)(s-c)) when length of the sides are given
where s = (a+b+c)
2
Write the main method that accepts the various inputs from the user and calls the above
methods depending on user’s choice
19 Write a program in Java to design a class Box which contains the following:
Data Members – length, breadth and height
Parameterised constructor – to initialize data members by the values passed
void vol( ) - To compute and print the volume of the box
void sarea( ) - To compute and print the total surface area of the box
Define main method to create the object of the class Box and call the above.
20 Write a program that output the results of the following evaluations based on the number 2006
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.
https://sites.google.com/site/javaprogramsisc/ ICSE Questions
9
21 Design a class in Java to input name and percentage of a student and print his name and
grade using the following functions:
(a) void InputDetails( String Nm, double Per) - to input name and percentage of a student
(b) void Display( ) – to print the name and grade of a student.
EXTRA QUESTIONS
23 A class Telcall calculates the monthly phone bill of a consumer. Some of the members of the class
are given below:
Class name: : Telcall
Data members/ instance variables :
phno : phone number
name : name of customer
n : number of calls made
amt : bill amount
Member functions/methods
Telcall( ) : Parameterised constructor to assign values to data members
void compute( ) : to calculate the phone bill amount based on the slabs
given below
void dispdata( ) : to display the details in the specified format
24 A class quad contains the following data members and member functions to find the roots of a
quadratic equation.
Class name: : quad
Data members/ instance variables :
a, b, c, x1, x2 : float type
Member functions/methods
quad(float, float, float ) : constructor to assign values to the data members.
float discriminant( ) : to return the discriminant [ b2 – 4ac ]
void root_equal( ) : to display the root if both roots are equal
void imag( ) : to display the roots, if roots are imaginary
void root_real( ) : to display the two real, unequal roots
void root( ) : to call other appropriate functions to find the
solution of the problem.
If ax2 + bx + c = 0 is the quadratic equation, then
If b2 – 4ac > 0 – roots are real, unequal
where x1 = -b + (b2 – 4ac )
2a
x2 = -b - (b2 – 4ac )
2a
If b2 – 4ac = 0 - roots are real, equal
since x1 = x2 = -b / 2a
https://sites.google.com/site/javaprogramsisc/ ICSE Questions
11
If b2 – 4ac < 0 – roots are imaginary
25 Class Employee contains the following member:
Class name : Employee
Data members/instance variables:
empname : To store the name of employee
int empcode : To store the employee code
double basicpay : To store the basic pay of the employee.
26 Design a program to calculate the tax for the people living in Utopia. Specify a class taxpayer
whose class description is given below.
Class name : taxpayer
Data members :
int pan : to store the personal account number
char name[20] : to store the name of a person
float taxableinc : to store the total annual taxable income
float tax : to store the tax that is calculated
Member functions :
void inputdata( ) : to enter the data for a taxpayer
void displaydata( ) : to display the data a taxpayer
void computetax( ) : to compute tax for a taxpayer
The tax is calculated according to the following rules :
TOTAL ANNUAL TAXABLE INCOME RATE OF TAXATION
Up to 60000 0%
Any amount above 60000 but up to 150000 5%
Any amount above 150000 but up to 500000 10%
Any amount above 500000 15%
In the main program, create an object of the type taxpayer. Calculate the tax for the taxpayer and
output it in the following format :
Pan No. Name Total Annual Taxable Income Tax