User-defined Methods
Section A
2005
Question 2
(d) (i) What is call by value?
(ii) How are the following passed?
(1) Primitive types
(2) Reference types. [3]
(i)
Call by value: A method is called by passing primitive values. Copies of values are passed to method definition.
Process are done with these copies and changes that taken place reflect in the copy only and original data remain unchanged.
(ii)
(1) Call by value
(2) Call by reference
2006
Question 2
(c) Define an impure function. [2]
(c) An impure function is a function that modifies the state of the received object.
E.g.:
public static void increment(A x)
{
x.a=x.a+10;
}
Here A is a class, x is its object and a is its data member.
Question 3
(a) What is meant by private visibility of a method? [3]
(a) A private method is accessible only within the same class. Even methods of sub classes do not have access.
(c) Explain function overloading with an example. [2]
(i) Explain the function of a return statement. [4]
The return statement is a jump statement which is used to jump the execution from a method to its method call statement.
2007
Question 2
(a) Differentiate between Formal Parameter and Actual Parameter. [2]
The arguments that resides in function prototype with data type which receives values from function call is the Formal Parameter and the arguments which resides in function call without data type which passes values to Formal Parameter is the Actual Parameter.
area(l,b) → l and b is the Actual Parameter
public static void area(int l, int b) → int l, int b is the Formal Parameter
Question 3
(e) (i) What is the role of the keyword void in declaring functions?
(ii) If a function contains several return statements, how many of them will be executed?
(iii) Which OOP principle implements function overloading? [3]
(e) (i) The void keyword declares that the function definition does not return a value.
(ii) Only one will be executed.
(iii) Polymorphism.
2008
Question 2
(c) Explain instance variable. Give an example. [2]
Instance variables are the variables declared for objects that are declared in class level without static keyword. Each object gets separate copy of these variables. In the following example double l, b are instance variables.
public class Rectangle
{
double l,b; //instance variables
}
(e) Explain any two types of access specifier. [2]
public : When we use public with a class and method then the class and method can be accessed by any other method in any class in any package.
protected : When we use protected with a method then these are accessible by methods of same package classes and any sub classes that are in other packages.
Question 2
(c) Differentiate between actual parameter and formal parameter. [2]
The actual parameter is the arguments which reside in a function call (without data type).
E.g.: l,b in area(l,b).
The formal parameter is the arguments which reside in a function prototype (with data type).
E.g.: int l, int b in public void area(int l, int b).
2009
Question 2
(a) Write the function prototype for the function “sum” that takes an integer variable (x) as its argument and returns a value of float data type. [2]
public static float sum(int x)
Question 2
(d) Name the keyword that:
(ii) causes the control to transfer back to the method call.
[1]
(ii) return
Question 2
(e) Differentiate between pure and impure function. [2]
A pure function is the one that takes objects or primitives as arguments but does not modify the
values of passing arguments.
An impure function is the one that modifies state of the received object.
Question 3
(d) When there are multiple definitions with the same function name, what makes them different from each another? [2]
The number of arguments and data type of arguments makes the functions different when the function names are same.
2010
Question 2
(e) Write two advantages of using functions in a program [2]
1) Reduce complexity (simplification): By dividing complex problems into smaller units we can solve
them easily.
2) Re-use: We can re-use the functions whenever and wherever we need it and as many time we require it.
(c) Differentiate between static and non-static data members. [2]
The static data members are variables declared in class level with static keyword. There is
only one memory location possible for a static data member. All objects have to share this single copy of memory.
The non-static data members are variables declared in class level without static keyword. For a non-static
data member multiple copies can be created. Each object gets a separate copy of this data member.
(e) Differentiate between private and protected visibility modifiers. [2]
A function or data member with private visibility modifier can be accessed only by the members
of the same class. The members of the other classes cannot access them.
A class or a function or a data member with protected visibility modifier can be accessed only by the members
of the same class or members of sub classes in the same package or other packages. The classes which are not sub
classes cannot access a protected class or member function or data member.
Question 3
(d) Give prototype of a function check which receives a character ch and integer n and returns true or false. [2]
public static boolean check(char ch, int n)
2011
Question 2
(d) What are the two ways of invoking functions? [2]
Call by value and call by reference are the two ways of invoking functions.
Examples:
i) swap(a,b); where a and b are primitive values.
ii) swap(ob); where ob is an object.
(e) What is the role of keyword void in declaring functions? [2]
If a function does not return a value then the function should be void.
Example:
public void area(int x,int y)
{
System.out.println(x*y);
}
Question 3
(d) Give prototype of a function search which receives a sentence sentnc and a word wrd and
returns 1 or 0. [2]
public static int search(String sentnc, String wrd)
(g) Name the keyword that
(ii) distinguishes between instance variables and class variables.
(ii) static
2012
Question 2
(c) Differentiate between call by value and call by reference [2]
When a method is invoked by a method call by passing primitive values then it is call by value.
When a method is invoked by a method call by passing reference values like objects then it is call by reference.
In call by value copies of values are passed to method definition. Process is done with these copies and
changes happen only on these copies and the original data remain unchanged.
In call by reference, reference of object or array is passed to method definition and process is done with
this reference value and the change that happens is reflected in the original data.
2013
Question 2
(d) Name the keyword that:
(i) indicates that a method has no return type [2]
(d) (i) void
2014
Question 3
(i) Consider the following class:
public class myClass {
public static int x=3,y=4;
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.
[2]
(i)
(i) int a, b
(ii) static int x,y
2015
Question 2
(e) What are the values of a and b after the following function is executed, if the values are passed are 30 and 50:
void paws(int a, int b)
{
a=a+b;
b=a-b;
a=a-b;
System.out.println(a+“, ”+b);
}
[2]
50, 30
Question 3
(d) The access specifier that gives the most accessibility is …… and the least accessibility is …… [2]
publicprivate
2016
Question 3
(c) Differentiate between formal parameter and actual parameter. [2]
Formal parameter is the parameter that resides in function prototype.
E.g.: The double r in area(double r)
Actual parameter is the parameter that resides in function call.
E.g.: The r in area(r);
(d) Write function prototype for the following:
A function PosChar which takes a string argument and a character argument and return an integer value.
[2]
public static int PosChar(String x, char y)
(e) Name any two types of acceess specifiers. [2]
1) public
2) protected
2017
Question 2
(b) What are the two ways of invoking functions? [2]
1) Call by value,
2) Call by reference
2018
Question 1
(g) How are private members of a class different from public members? [2]
Private members of a class can be accessed by the members of the same class only.
E.g.: private int a; This varibale can be accessed by a method of the same class only.
Public members of a class can be accessed by the members of all class in any package.
E.g.: public int a; This varibale can be accessed by any method of any class in any package.
Question 2
(d) Write the prototype of a function check which takes an integer argument and returns a character. [2]
public static char check(int x)
2019
Question 1
(c) Name the keyword which:
(i) indicates that a method has no return type.
(ii) makes the variable as class variable
[2]
(i) void
(ii) static
Question 3
(e) State a difference between call by value and call by reference. [2]
(i)
(1) When a method is called by passing primitive values then it is call by value. When a method is called by passing reference values like objects then it is known as call by reference.
(2) In call by value copies of values are passed to method definition and these copies are received by formal parameters. Process are done with these copies and any change that takes place reflects in the copy only and original data remain unchanged.