• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Functions in Java

User – defined Methods

  • Categories Functions in Java, Notes

In this post we will cover loops in java. You can watch our videos on Java Functions & Examples And Java Function Exam Questions

Q. 1 What is a Method or Function? Why do we use function (or method)?
Ans. A function is a named block of code that together performs a specific task. It can be called any number of times. Functions are used to:

  • Divide a large code into modules, due to this we can easily debug and maintain the code.
  • It helps in reusing the code.
  • Program becomes easy to understand.
Syntax of method

Q. 2 What is void return type?
Ans. Void is a keyword used in Java to indicate a method does not return any value.

Q. 3 Do we need to write return in method body which has a return type of void?
Ans. No. return is optional.

Q. 4 Can we call return from the middle/start of method body?
Ans. Yes. A program can have multiple return statements but only one first encountered will be executed

Q. 5 Give the prototype/method signature of following

  1. Function find which receives a sentence sent and word wrd and returns 1 or 0
  2. Method isCorrect which receives a character ch and a integer n and returns true or false
  3. Function power that takes in in two integer values and returns result of double type.

Ans.

  1. int find( String sent, String wrd)
  2. boolean isCorrect(char ch, int n)
  3. double power ( int a, int b)

Q. 6 Why main() function so special?
Ans. The main() method is the entry point into the application. The main() function is invoked in the system by default. Hence as soon as the command for execution of the program is used, control directly reaches the main() function.

Q. 7 Define Formal and Actual arguments. Explain with the help of one example.
Ans.

Formal arguments : The list of variable(s) along with their data type(s) given within the brackets with function prototype and function definition are known as formal arguments (or parameters).

Actual arguments : The list of variables without any data type given within the brackets at the time function call (or invoke) is known as actual arguments (or parameters).

Example:

public static void process(int a, float b){
a= a+ 2;  // ‘a’ and ‘b’ are formal arguments
b= b* 3;
System.out.println(a + "t" + b);
}

public static void main(){
int x= 2; float y= 4;
process(x, y );   // ’x’ and ‘y’ are actual arguments
}

Q. 8 What is static variable and static method in Java?
Ans.     The Static variables are shared by all instances of class. The static variable is also known as class variable and declared using keyword “static”.
The method which begins with static keyword is known as static method. These function uses only static variable(s).

Q. 9 What is the feature of static keyword in connection with function?
Ans. A static function is a function that belongs to a class rather than an instance of a class.

  • The static keyword makes a method/ function as a class function ( also known as static method or function),
  • They do not require an instance of class to be created.
  • It can access only static methods of the class.

Q. 10 What is the difference between function call by value and function call by reference?
Ans. 

Call by valueCall by reference
In this method fundamental or primitive data types are passed (eg. short, int, float, long, char, double etc.).In this only reference type
arguments (object, arrays etc.) or non-
primitive data types are passed.
The changes made in formal arguments (if any) does not reflect to actual argumentsThe changes made in formal arguments (if any) are automatically reflected in the actual arguments

Q. 11 What do you mean by pure function? Give one example.
Ans. The function which does not change or modify the state of the argument/variable that are received when called or invoked it is known as pure function.
Example:

class purefun{
    public int show(int x, int y){    //function definition with int x, int y
         if(x>3)
             return x;             // value x is only checked and not changed
        else
             return y;
     }
     public void main()             //main function invokes pure function show()
    {
         int a=4, b=3;                      //variable initialization
         int r= show(a,b);
         System.out.println(r);
    }
}

Q. 12 Define an impure functions?
Ans. Impure Function change the state of the object arguments they have received. The following functions is the example of an impure function:

public static Time increment(Time obj, double secs)
{
time.seconds+=secs;
return(Time);
}

Q. 13 What is the difference between formal and actual parameter?
Ans.

Actual parameterFormal parameter
Parameters which appear in the function call statement are known as actual parameter.Parameters which appear in the function definition statement are known as formal parameter.
Example:
simply(125,70,80) // here 125, 70,80 are actual parameters.


Above example values are passed to the parameters given in function definition
Example:
public static void simply(int code, float price)
{
Set of statements;
}

Above example arguments receive actual values from the function call statement.

Q. 14 What is the difference between pure and impure function?
Ans.

Pure FunctionImpure Function
It does not modify the argumentsIt modifies the state of arguments received or
Relies only on parameters passed to it.Refers to variables outside of method.
e.g.
public class TestMax {
/* Return the max between two int */
public static int max(int n1, int n2)
{ …;
Return n1;
}
e.g.
public class TestMax {
highestNo = 23;
public static int max(int n1, int n2)
{
…
if (n1 < highestNo)
result = highestNo;
… }

Q. 15 What is the difference between Local and Instance variables? 
Ans. 

Local VariablesInstance Variables
Local variables, also called member variables are defined and used only within the scope of the method.Instance variables are defined inside the class and their value is unique to each instance of the class.
e.g. void f() {
int i;
i = 1; // OK: in scope }
e.g. class Student {
int age;
}

Q. 16 What is the difference between Global and Class variables? 
Ans.

Global VariablesClass Variables
public and static class variables are called as Global variables.When a single copy of variable exists irrespective of number of instances of variable, it is called Class Variable
e.g. public class mathFunctions{
public static float PI =3.14;
}
e.g. class School {
static int totalStudents;
}

Q. 17 What is exit function in Java?
Ans. This function is used to terminate the entire program from any stage and cause the control to got to the last closing brace ‘}’ of the class.
            Syntax:   System.exit(0);

Q. 18 What is Function Overloading?
Ans.
When we define two or more methods within the same class that share the same name, with different parameter declarations, the methods are said to be overloaded, and the process is referred to as method overloading.
Method overloading is one of the ways that Java implements polymorphism.
Overloaded methods must differ in the type and/or number of their parameters. You cannot overload return types
e.g.

public static int max(int num1, int num2) {
if (num1 > num2)
   return num1;
else
   return num2;
}
public static double max(double num1, double num2) {
if (num1 > num2)
   return num1;
else
  return num2;
}
  • Share:
author avatar
Simply Coding

Previous post

Solve any character pattern program in Java
June 5, 2021

Next post

Star Pattern Programs In java
June 6, 2021

You may also like

Functions or methods Programs 1
Functions or methods Programs
22 June, 2021
Generation of Computers
Generations of Computers
20 September, 2020
Data Backup
Data backup and Recovery
19 August, 2020

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now