0% found this document useful (0 votes)
5 views2 pages

Java Function Concepts and Examples

The document consists of a function assignment with multiple questions related to programming concepts, specifically in Java. It covers topics such as defining functions, advantages of methods, function syntax, and differences between pure and impure functions. Additionally, it includes programming tasks that require writing Java programs for various scenarios, such as calculating the factorial, checking for palindromes, and finding the volume of geometric shapes.

Uploaded by

onlyforfunnnier
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Java Function Concepts and Examples

The document consists of a function assignment with multiple questions related to programming concepts, specifically in Java. It covers topics such as defining functions, advantages of methods, function syntax, and differences between pure and impure functions. Additionally, it includes programming tasks that require writing Java programs for various scenarios, such as calculating the factorial, checking for palindromes, and finding the volume of geometric shapes.

Uploaded by

onlyforfunnnier
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Function Assignment

Question 1.
a) What is a function?
b) What are the advantages of defining a method in a program?
c) What is the syntax for defining a function?
d) What is the difference between pure and impure function?
e) What is function prototype?
f) Differentiate between formal and actual parameters.
g) What do you know about call by value and call by reference?
h) Explain the function of a return statement in java programming.
i) What is the role of the keyword void in declaring functions?
j) What are the two ways of invoking functions?

Question 2. Predict output.

i) void test1(int n)
{
for( int x=1; x<=n;x++)
if( n%x==0)
[Link](x);
}
If 12 is passed to n.
ii) void test2(int a, int b)
{
while(a!=b)
{
if(a>b)
a= a-b;
else
a=b-a;
}
[Link](a);
}
If 4 and 17 are passed to the function.
iii) void test3(String x, String y)
{
if( [Link](y)>0)
[Link](x);
else
[Link](y);
}

If “SALMAN” and “SHAHRUKH” are passed to the function.


Java programs

1. Write a program to input the sides of a triangle. Pass the sides to the function decide(int x, int y,
int z) which checks and prints whether the triangle is equilateral, isosceles or scalene.
2. Write a function fact(int n) to find the factorial of a number n. Include a main class to find the
value of S where.
S= n!/(m!(n-m)!
3. Write a program to accept a string. Pass it to a function freq(String x). the function find and
prints the frequency of each letter.
4. Write a program to accept 10 numbers in SDA. Pass the array to a function swap(int a[]) to
interchange the number in such a way the a[0] is interchanged with a[1], a[2] with a[3] and so
on. Finally print all the numbers of the array.
5. Write a menu driven program to accept a number from the user and check whether it is
palindrome or perfect number using methods.
i) Palindrome number( A number is a palindrome which when read in reverse order is
same as in the right order)
Example 11, 101, 151 etc.
ii) 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
6. Write a program using switch case to find the volume of a cube a sphere and a cuboid. For an
incorrect choice, an appropriate message should be displayed.
(Hint : volume of cube- s*s*s
Volume of sphere 4/3*π*r*r
Volume of cuboid l*b*h )
7. Write a program using functions to enter a number (max. 9 digits) and print the following.
a) Total number of even and odd digits.
b) The reverse of the number
Total number of zero’s present in the number
8. Design a class over loading a function display as follows
i) void display(String str, char p) with one string argument and one character
argument. It displays all vowels if p is ‘v’ otherwise displays all alphabets.
ii) void display(String str, int p) with one string argument and one integer argument
It displays all uppercase characters if p is 1(one) otherwise displays all lowercase
characters.
9. Write a menu driven program using the method number() to perform the following tasks.
i) Accept a number from the user and and display its binary equivalent.
e.g. Sample input: (21)10 Sample output = (10101) 2
ii) Accept a number from the user and and display its binary equivalent.
e.g. Sample input: (11101)2 Sample output = (29) 10

You might also like