Short questions on Java Functions
- Categories Java, Functions in Java, Functions
- Give the output and what does this test function do?
public class output3
{
void test()
{
int n=5732,result=0;
do
{
result *= 10;
int digit = n%10;
result += digit ;
n = n/10;
}while(n>0);
System.out.println(“Output = ” + result);
}
}
Ans
2375 (it reverse the number.)
- In the program given below, state the name and the value of the function :
- method argument or argument variable
- local variable
- class variable
- instance variable
class myClass{
static int x = 7; int y = 2;
public static void main(String args[] ){
myClass obj= new myClass();
System.out.println( x );
obj.sampleMethod(5);
int a = 6;
System.out.println( a );
}
void sampleMethod(int n){
System.out.println(n);
System.out.println(y);
}
}
Ans
- Method argument or argument variable is: n
- The class variable is : x
- The local variable is: a
- The instance variable is: y
- Write the prototype of a function ADD() for each of the following purpose:
- To return and integer value.
- To receive one integer, one double type argument and returns a double type value
- To receive a character and an integer as parameters and no returning
- To receive an integer as parameter and returns true or false.
Ans.
- public int ADD) or public static int ADD) or int ADD)
- public double ADD ( int y, double m) or public static double ADD( int y, double m) or double ADD( int y, double m)
- public void ADD( char ch, int y) or public static void ADD( char ch, int y) or void ADD( char ch, int v)
- public boolean ADD( int y) or public static boolean ADD( int y) or boolean ADD( int y)
- Write the prototype of a method stud() which takes a string argument and a character argument and returns an integer.
Ans.
public static int stud ( String str, char ch ) OR public int stud ( String str, char ch) OR int stud ( String str, char ch)
- Write the prototype of a function Count which takes character argument and string argument and returns a true or false.
Ans.
public static boolean Count( char ch, String str ) OR public boolean Count (char ch, String str) OR boolean Count(char ch, String str )
- What are the values 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;
}
Ans. a=50 and b=30
- In the program given below, state the name and the value of the :
- method argument or argument variable
- local variable
- class variable
- instance variable
class myClass{
static int x = 7; int y = 2;
public static void main(String args[] ){
myClass obj= new myClass();
System.out.println( x );
obj.sampleMethod(5);
int a = 6;
System.out.println( a );
}
void sampleMethod(int n){
System.out.println(n);
System.out.println(y);
}
}
Ans.
- Method argument or argument variable is: n
- The class variable is : x
- The local variable is: a
- The instance variable is: y
- Write a small program code to show function call by value method.
Ans.
class program{ public static void simply( int a, int b){ a= a/2; b= b+10; System.out.println("The value of A = " + a +" and B " + b); } public static void run(){ // this function calls the above function simply() int x= 20, y= 10; simply(x, y); //calling function simply() }
- Give the prototype of a function search which receives a sentence snt and a character ch and returns 1 or 0 ?
Ans.
public int function ( String snt, char ch )
- Write a function prototype of the following:
A function PChar which takes an integer argument and a character argument and returns an integer value.
Ans. public int PChar(int n, char c)
You may also like
Functions or methods Programs
22 June, 2021
User – defined Methods
5 June, 2021
Forming Math Expressions
13 June, 2020