FUNCTION
Ques 1 what is function ?Write advantage of using function
Ans :- Functions are methods that contain codes to perform a specific task and returns values for further computation .
OR
Functions are the modules from which java programs are built. Modular code is easy to debug and maintain.
Function provides as
1. Reusability of the code
2. they hide low level details
3. They make program easy to debug and maintain.
Ques 2- What are actual and formal parameters of a functions?
Ans :- Arguments are known as actual parameters. Parameters are the variables defined by a method that receives values known as formal parameters.
Actual parameter appears in function call where as formal parameters appears in function signature.
Ques 3:- What is the role of a return statement in a function?
Ans :-public int add(int n, int m)
{
return n+m;
}
The return statement will send the sum of n and m to the calling body.
Ques 4:- What is the role of void keyword in declaring functions.
Ans :- a void keyword signifies that the method does not return any value to its caller.
Ques 5:- Give difference between call by value and call by reference.
Ans :- Call By Value:- this method copy the entire arguments passed to the function. It copies the value of an argument into the formal parameter of the subroutine. As a copy of the argument is being made and passed to the function, any changes made to the parameter inside the function do not effect the original argument.
Call By Reference:- When we pass a reference data type as argument to a function. It is called pass by reference.
Ques 6:-What is function overloading? 2006
Ans :- the process of having two or more methods or functions with in a class, with same name but different parameters declaration is known as function overloading.
Ques 7:- What is function signature?
Ans :- Parameter of the function along with function name. It is also known as function signature.
Ques 8- What is parameters?
Ans :- List of variable that recives the value of the arguments passed to the function when is called. This can be empty is there are no parameters.
Ques 9;- what do you understand by early binding and static binding?
Ans :- the compiler at compile time knows the number and type of arguments. The compiler selects the appropriate function for a call at the compile time itself. This is known as early binding or static binding.
Ques 10- What is the role of static keyword in function prototype
Ans:- the keyword static signifies that function can be called without creating its object.
Ques 11:- What is ‘this’ keyword?
Ans :- ‘this’ is a reference to the object on which the function was invoked. In other word ‘this ‘ keyword is used to refer to the current object.
Ques 12. Give the difference between pure and impure function ?
Ans:- Pure :- These types of functions uses the object received for various process but do not changes the state of the object.
Impure:- this type of functions uses the object received for various processes as well as changes the state of the object.
Quest 13. How does a function declaration different form a function call?
Ans:- Type specifier is mentioned in function declaration which is absent in function call.
Function declaration should not end with a semicolon but function statement should end with a semicolon.
Ques 14:- What is function prototype?
Ans :- A function prototype is and interface to the compiler. It is the first line of the function definition that tells the program about the type of the value returned by the function and the number and type of parameters.
Ques 15:- What do you mean by the term “access specifier”?
Ans:- It means the type of access to the function .i.e. from where and how the function can be called.
Ques 16:- Name any two library function which implements function overloading? [2006]
Ans :- indexOf() and substring().
Ques 17 What are the condition which must satisfy to implement function overloading?
Ans:- To implement function overloading function signature must be different in either of three ways
i. Number of the arguments
ii. Data type of the arguments
iii. Sequence of the arguments
Ques 18 Define an impure function. [2006]
An impure function takes primitive data types or object as arguments and modifies the state of the received data or object .
Ques 19 Explain the function of return statement. [2006]
There are two functions performed by the return statement.
i. It causes the termination of the execution of the function in which it is written.
ii. It may also return a value to the calling function.
POINT TO REMEMBER
- Functions are methods that contain codes to perform a specific task and returns values for further computation.
- Functions may be categorized as pureand impure functions.
- Pure functions, also called accessors, are used for getting data for an object.
- Accessors begin with term get to indicate that they receive values.
- Impure functions, also called mutators orupdaters are used for changing the object’s state.
- Mutators usually begin with the term setto indicate that they reset the values of the object.
- Every function has a unique signature: its name, return type and a list ofparameters.
8. More than one function bearing same name with different parameters is calledfunction overloading.
- Primitive variables store the values directly in the memory.
- Object variables also called reference variables store only the reference (value of the memory location).
- Values are passed though arguments to the function.
- When the values passed are of primitive type it is termed as pass-by-value.
- When the values passed are of class type it is called pass-by-reference.
- If a variable has no objects to refer to, it is called null reference.
15. Inaccessible objects can be garbagecollected in Java.
- Function, which has its parameter variable name same as that of class variables, the key word this is used to specify the current instant variable.
- Method this()can be useful for calling a constructor within another constructor.
Ques 1 write a program to display all prime number between 1 to 1000
class dispprime
{
static boolean isprime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return false;
}
return true;
}
public static void main(String args[])
{
int num;
for(num=1;num<=1000;num++)
{
if(isprime(num))
System.out.println( num);
}
}
}
Ques 2 program to find sum of the series using function int fact(int);
X-X3/3!+X5/5!-X7/7!.................XN/N!
import java.util.*;
class seriessum
{
static int factorial(int x)
{
int i,fact=1;
for(i=1;i<=x;i++)
{
fact=fact*i;
}
return fact;
}
public static void main(String a[]) throws Exception
{
int x,n,f,sign,i;
double sum,term;
Scanner Sn=new Scanner(System.in));
System.out.println( "enter x and n");
x=Sn.nextInt();
n=Sn.nextInt();
sum=0.0;
sign=1;
for(i=1;i<=n;i=i+2)
{
f=factorial(i);
term =Math.pow(x,i)/f;
sum=sum+term*sign;
sign=sign*-1;
}
System.out.println( sum);
}
}
Ques 3-Write a menu driven program to find volume of a cube/cubiod/sphere using overloaded function [2008]
import java.util.*;
class overload
{
static int volume(int s)
{
return s*s*s;
}
static int volume(int l, int b,int h)
{
return l*b*h;
}
static double volume(double r)
{
return 4*3.14*r*r*r/3;
}
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int side,len,wid,hei,choice;
double red;
choice=1;
while(choice!=0)
{
System.out.println( "1 . volume of cube");
System.out.println( "2. volume of cubiod ");
System.out.println( "3. volume of sphere");
System.out.println( "0. exit");
System.out.println( "enter your choice");
choice =Sn.nextInt();
switch(choice)
{
case 1: System.out.println( "enter side");
side=Sn.nextInt();
System.out.println( volume(side));
break;
case 2 : System.out.println( " enter l,b,h");
len=Sn.nextInt();
wid=Sn.nextInt();
hei=Sn.nextInt();
System.out.println( volume(len,wid,hei));
break;
case 3 :System.out.println( "enter radius");
red=Sn.nextDouble()
System.out.println( volume(red));
case 0 : break;
default: System.out.println( "invalid input");
break;
}
}
}
}
PROGRAM FOR PRACTICE
Ques1 write a program to input principal, rate and time and calculate simple interest using function int simple_interest(int p,int r, int t) which calculates and returns simpleinterest.
Ques 2 write a program to input principal, rate and time and calculate compound interest using function int compound_interest(int p,int r, int t) which calculates and returns compoundinterest.
Ques 3. Write a program to input two numbers and display maximum of two number using function . int max(int, int) which returns maximum out of 2 numbers.
Ques 4. Write a program to input two numbers and display maximum of three number using function . int max(int, int,int ) which returns maximum out of 3 numbers.
Ques 5 Write a program to input 2 number and display highest common factors. Using function int hcf(int,int) which returns highest common factor of 2 numbers.
Ques 6 A number is said to be perfect number if its sum of factors is equal to itself. write a program to input a number and display it is perfect number or not. Using function int sum_of_factor(int) which returns sum of factors of a number.
Ques 7 Write a program to number and display number of digits and sum of digit of the number using function void number(int) calculates and display number of digit and sum of digit.
Ques 8 Create a class education and a function double calculate() with three parameters velocity u, time t and acceleration acc all of double types, find and return the value of ‘v’ from the following relation: where v=u+at
Write a main function to input value for ‘u’, ‘a’, ‘t’and by calling function calculate() print the value of u,a,t and v.
Ques 9 Create a class interchange and a function change(interchange s) which receives two strings by refrence. Interchange the strings stored in the object s.
Write a another function to input two strings and interchange these strings by invoking function by reference. Print strings before and after interchange.
Ques 10. Write a program to display all palindrome number between 1 to 1000. Use the function int reverse(int n) which returns reverse of n..
Ques 11 Write a program to display all prime numbers between 1 ti 1000 . use the function int isprime(int n) which returns 1 if n is prime number otherwise return 0.
Ques 12 Write a program to find sum of prime number of an array of 20 elements. use the function int isprime(int n) which returns 1 if n is prime number otherwise return 0.
Ques 13 Write a program to display all twin prime number between 1 to 1000. Using isprime(). Twin prime numbers are consecutive odd prime number. as (1,3) , (11,13)
Ques 14 Write a program to store all non-prime numbers in an array of 25 elements. Using isprime().
Ques 15 Write a program to input a string and display the character stored at prime position. E.g Input :- INTERNATIONAL
Output:- I,N,T,R,A,N,L
Ques 16 Write a program to input a sentence and display number of vowels in it. Using int isvowel(char) return 1 of character supplied to it is vowel otherwise returns 0.
Ques 17 Write a program to input a sentence and count vowel pairs in it. Using int isvowel(char) return 1 of character supplied to it is vowel otherwise returns 0.
Ques 18 Create a function int GCD_FINDER(int m, int n) to find and return HCF/GCD of m and n, using subtraction method. Write a main function to input two numbers and prin their HCF.
Ques 19 Create a function int GCD_FINDER(int m, int n) to find and return HCF/GCD of m and n, using LONG DIVISION method. Write a main function to input two numbers and prin their HCF.
Ques 20 define following functions:
i. int cube(int x) to return the cube of integer z.
ii. void Armstrong()- by invoking function cube() find and print only Armstrong numbers between 50 to 300.
Write a program to print only Armstrong numbers from 100 to 500 by using above functions.
Ques 21. Write a program to print all perfec numbers between two limits entered by the user with the help of following functions:
boolean isperfect(int num) to retrun ‘true’ if num is perfect, otherwise ‘false’
void showperfectnumbers(int start, int end) – to print all perfect numbers between sta and end limitby using function isperfect()
Ques 22:- Design a class sample for the following function to print cash memo for a product.
void product(): create this function wht product code(integer), unit price of product(int decimal) and quantity fo product(in decimal) as parameters. Calculate total cost of product discount as 12% on total cost and net price to be paid for the product.
Write a main function to invoke function product() by passing required values and print all the data members including total, discount and net price of product with suitable message.
Ques 23 create a class distance with function double distance() with three parameters u, time and acc all of double types, find and return the value of d from the following relation:
d=ut +1/2 a t2
Write a main function to input velocity (v) , time (t) and acceleration (a) and by invoking function distance() print the value of d,v,t and a.
Ques 24 Write a progam to define overloaded function sum()
void sum() which takes an array of 10 element as argument and returns sum of its elements
void sum() whick takes an array of 10 element and a character. If character is O the it returns sum of odd numbers if character is E the returns sum of even number otherwise return zero.
Ques 25 Write a progam to generate the sum of given series
S= 1+x2/2! + x3/3! + x4/4!.........................xn/n!
Make use of user define function
power() which takes two integer argument x and n and returns x to the power n and
factorial() which takes an integer and returns factorial of the number.
Ques 26 Specify the class finder with the following overloaded function:
int findmax(int n1, int n2) – to decide and return largest from n1 ,n2. If both the numbers are same the return 0.
int findmax(int n1,int n2,int n3) –to decide and return largest from n1,n2,n3. If all the numbers are same then return 0.
Write main function to call above function depending upon user’s choice.
Question 27 int factorial(int x) returns the factorial of x
int power(int p,int q) calculates and returns p to the power q (without using pow().
Write a program to display the sum of given series.
Sum= X2/2! – X4/4! + X6/6!-X8/8! …………… XN/N!
Question 28 Design following functions:
Int fact(int q) – to find and return the factorial of q.
Void printnumber() by invoking function fact() find and print special numbers between 1 to 500.
[ if sum of factorial of each digits of a numbers is equal to the given number itself. Then the number is a special number.]
Ques 29 Create a function intcountletters(Strings nam, char ch) – to get a string in nam and a character in ch . count and return the occurance of ch in nam if ch is present in nam otherwise return a value -1.
Write a main function to input a string and a character. Print occuremce of ch in nam by invoking the function countletters()
Ques 30 Write a program to print all prime numbers between two numbers entered by the user with the help of following function:
Boolean isprime(int n) – to return
‘true’ if n is prime, otherwise ‘false’
Void showprime(int s,int e) – to print all prime numbers between start(s) and end(e) limits by invoking function isprime(). Makes sure s<e otherwise print suitable message.
Ques 31 Write a java class that contains a method compound() and a main method. The 'compound' method computes the compound interest, based on the following conditions if time is less than or equal to 1 yr then r=3%, if time is between 1 and 2 yrs then r=5% and if time>2yr, r=7%.
The method returns the interest to main( ), where the result is displayed. Assume principal amount (P) and time (T) to be inputted from the console. The interest should be calculated for 10 investors.
Ques 32 Write a program using a function called area() to compute area of a
(i) circle (pi*r*r) where pi=3.14
(ii) square(side*side)
(iii) rectangle( length*breadth)
Display the menu to output the area as per User's Choice. [2005]
No comments:
Post a Comment
any problem in any program comment:-