0% found this document useful (0 votes)
59 views

MCQ User Defined Method

The document discusses user defined methods in Java through multiple choice questions and examples. It covers key concepts related to methods like parameters, return types, overriding, recursion, pure and impure methods. Sample programs are provided to demonstrate method invocation and passing arguments by value and reference.

Uploaded by

Goutam Jena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

MCQ User Defined Method

The document discusses user defined methods in Java through multiple choice questions and examples. It covers key concepts related to methods like parameters, return types, overriding, recursion, pure and impure methods. Sample programs are provided to demonstrate method invocation and passing arguments by value and reference.

Uploaded by

Goutam Jena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

MCQ’S

USER DEFINED METHODS IN JAVA

Multiple Choice Questions


Question 1

The method which changes the state of an object is known as:

1. pure method
2. impure method
3. replace method
4. none of the above

Answer

impure method

Reason — The method which changes the state of an object is known as impure method.

Question 2

Parameters used in method call statement are known as:

1. defined parameter
2. passed parameter
3. actual parameter
4. formal parameter

Answer

actual parameter

Reason — Parameters used in method call statement are known as actual parameter.

Question 3

Parameters used in the method definition are called:

1. forward parameter
2. actual parameter
3. formal parameter
4. none of the above

Answer

formal parameter
Reason — Parameters used in the method definition are called formal parameters.

Question 4
The process of calling a method in such a way that the change in the formal arguments reflects on the actual
parameter is known as:

1. call by reference
2. call by value
3. call by method
4. none

Answer

call by reference

Reason — The process of calling a method in such a way that the change in the formal arguments reflects on the
actual parameter is known as call by reference.

Question 5

A method with many definitions is called:

1. multiple method
2. method overloading
3. floating method
4. none

Answer

method overloading

Reason — A method with many definitions is called method overloading.

Question 6

A method may be associated with:

1. return
2. call
3. promote
4. none

Answer

return

Reason — A method may be associated with return.

Question 7

Which of the following type can be used for a non-returnable method?

1. int
2. float
3. double
4. void

Answer

void

Reason — void type is used for a non-returnable method.

Question 8

A method body is enclosed within a pair of:

1. {}
2. []
3. ()
4. under a rectangular box

Answer

{}

Reason — A method body is enclosed within a pair of { }.

Question 9

A method is invoked through an:

1. object
2. system
3. parameter
4. none

Answer

object

Reason — A method is invoked through an object.

Fill in the blanks


Question 1

A method can return only one value to its caller program.

Question 2

If a method does not return any value its return type is void.

Question 3
A method indicating the method name, return type along with method arguments is known as method
header/prototype.

Question 4

The variables used to receive the values in method header are known as formal parameters.

Question 5

Method in a Java program resides in package.

Question 6

The entire method body is enclosed under curly brackets.

Question 7

The procedural method performs some actions without returning any output.

Question 8

A method contains header and body.

Question 9

Methods used with same name but different types of arguments are known as method overloading.

Question 10

A method that is called by itself in its body is known as recursive method.

Write TRUE or FALSE


Question 1

Calling and invoking a method is same.


True

Question 2

A method can use a single return statement.


True

Question 3

Overloading of methods even depends on return type.


False

Question 4
A method cannot be defined without parameters.
False

Question 5

A method body is enclosed within curly brackets.


True

Give output of the following method definitions and also write what
mathematical operations they carry out

Question 1

void test1(int n)
{
for(int x = 1; x <= n; x++)
if(n % x == 0)
System.out.println(x);
}
if 12 is passed to n.

Answer

Output

1
2
3
4
6
12

Explanation

This method finds the factors of n.

Question 2

void test2(int a, int b)


{
while( a != b)
{
if ( a > b)
a = a — b;
else
a = b — a;
}
System.out.println(a);
}
if 4 and 17 are passed to the method.
Answer

Output

Infinite Loop

Explanation

Initial value of a is 4 and b is 17 as given in the question. As a and b are not equal, condition of while loop is
true, first iteration starts. a is less than b so if condition is false, a = b - a is executed and a becomes 17 - 4
= 13. Condition of while loop is true so second iteration starts. Again, if condition is false. This
time a becomes 17 - 13 = 4. Like this, the value of a keeps oscillating between 13 and 4 resulting in an
infinite loop.

Question 3

void test3(char c)
{
System.out.println( (int) c);
}
if 'm' is passed to c.

Answer

Output

109

Explanation

This method prints the ASCII code of its argument. When 'm' is passed to this method, its ASCII code which is
109 gets printed as the output.

Question 4

void test4(String x, String y)


{
if(x.compareTo(y) > 0)
System.out.println(x);
else
System.out.println(y);
}
if "AMIT" and "AMAN" are passed to the method.

Answer

Output

AMIT

Explanation
The first differing characters of "AMIT" and "AMAN" are 'I' and 'A', respectively. So output of
"AMIT".compareTo("AMAN") will be ASCII Code of 'I' - ASCII Code of 'A' ⇒ 73 - 65 ⇒ 8. The if condition is
true so string x which is "AMIT" gets printed as the output.

Case-Study based question


Question 1

A method is a program module that is used simultaneously at different instances in a program. It helps a user to
reuse the same module multiple times in the program. Whenever you want to use a method in your program, you
need to call it through its name. Some of the components/features of a method are as described below:

(a) It defines the scope of usage of a method in the user's program.

(b) It is the value passed to the method at the time of its call.

(c) It is a return type that indicates that no value is returned from the method.

(d) It is an object oriented principle which defines method overloading.

Write the appropriate term used for each component/feature described above.

Answer

(a) Access specifier

(b) Actual parameter

(c) void

(d) Polymorphism

Answer the following

Question 1

Define a method. What is meant by method prototype?

Answer

A program module used at different instances in a program to perform a specific task is known as a method or a
function.

First line of method definition that contains the access specifier, return type, method name and a list of
parameters is called method prototype.

Question 2

What are the two ways of invoking methods?

Answer

Two ways of invoking methods are:


1. Pass by value.
2. Pass by reference.

Question 3

When a method returns the value, the entire method call can be assigned to a variable. Do you agree with the
statement?

Answer

Yes, when a method returns a value, we can assign the entire method call to a variable. The given example
illustrates the same:

public class Example {

public int sum(int a, int b) {


int c = a + b;
return c;
}

public static void main(String args[]) {


Example obj = new Example();
int x = 2, y = 3;
int z = obj.sum(x, y);
System.out.println(z);
}
}

Question 4

When a method is invoked how many values can be returned from the method?

Answer

A method can only return a single value.

Question 5

Debug the errors and rewrite the following method prototypes:

(a) int sum(x,y);

(b) float product(a,int y);

(c) float operate(int x, float=3.4);

(d) float sum(int x,y);

Answer

(a) int sum(int x, int y)


(b) float product(float a, int y)
(c) float operate(int x, float y)
(d) float sum(int x, float y)
Question 6

Write down the main method which calls the following method:

int square(int a)
{
return(a * a);
}
Answer

public static void main(String args[]) {


int sq = square(4);
}

Question 7

What happens when a method is passed by reference? Explain.

Answer

Pass by reference means that the arguments of the method are a reference to the original objects and not a copy.
So any changes that the called method makes to the objects are visible to the calling method. Consider the
example given below:

class PassByReferenceExample {
public void demoRef(int a[]) {
for (int i = 0; i < 5; i++) {
a[i] = i;
}
}

public static void main(String args[]) {


PassByReferenceExample obj = new PassByReferenceExample();
int arr[] = { 10, 20, 30, 40, 50 };
System.out.println("Before call to demoRef value of arr");
for (int i = 0; i < 5; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
obj.demoRef(arr);
System.out.println("After call to demoRef value of arr");
for (int i = 0; i < 5; i++) {
System.out.print(arr[i] + " ");
}
}
}
The output of this program is:

Before call to demoRef value of arr


10 20 30 40 50
After call to demoRef value of arr
0 1 2 3 4
Here demoRef changes the values of array a and these changes are reflected in the array in the main method as
well.

Question 8

In what situation does a method return a value?

Answer

For a method to return a value, it should have a return type other than void in its method prototype and it should
return a value of the corresponding type using the return statement in the method body.

Question 9

Differentiate between pure and impure methods.

Answer

Pure method Impure method

Pure methods take objects and/or primitive data types as arguments Impure methods change the state of
but do not modify the objects. received objects.

Pure methods don't have side effects. Impure methods have side effects.

Question 10

Write a method which is used to swap the values of two memory locations.

(a) by using a third variable.

(b) without using a third variable.

Answer

(a) Swap the values of two memory locations by using a third variable:

void swap(int a, int b) {


int c = a;
a = b;
b = c;
System.out.println("a = " + a + "\t" + "b = " + b);
}
(b) Swap the values of two memory locations without using a third variable:

void swap(int a, int b) {


a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a + "\t" + "b = " + b);
}

Question 11

Differentiate between call by value and call by reference.

Answer

Call by value Call by reference

Actual parameters are copied to formal parameters. Formal parameters refer to actual parameters. The
Any changes to formal parameters are not reflected changes to formal parameters are reflected onto
onto the actual parameters. the actual parameters.

All primitive data types are passed using Call by All reference data types like arrays and objects of
value. classes are passed using Call by reference.

Question 12

What are the advantages of defining a method in a program?

Answer

The advantages of defining methods in a program are:

1. Methods help to manage the complexity of the program by dividing a bigger complex task into smaller,
easily understood tasks.
2. Methods are useful in hiding the implementation details.
3. Methods help with code reusability.

Question 13

What is meant by method overloading? In what way it is advantageous?

Answer

Method overloading is the process of defining methods within a class, that have the same name but differ in the
number and/or the data types of their arguments.

The advantages of method overloading are as follows:

1. Method overloading is one of the ways in which Java implements the object oriented concept of
Polymorphism.
2. With method overloading, programmers don't have to create and remember different names for functions
doing the same thing for different data types.

Question 14

Define the following:


(a) Return data type

(b) Access specifier

(c) Parameter list

(d) Recursive method

(e) Method signature

Answer

(a) Return data type — Return data type specifies the type of value that the method should return. It is
mentioned before the method name in the method prototype. It can be any valid primitive or composite data type
of Java. If no value is being returned, it should be void.

(b) Access specifier — Access specifiers determine the type of access to the method. It can be either public,
private or protected.

(c) Parameter list — Parameter list is a comma-separated list of variables of a method along with their
respective data types. The list is enclosed within a pair of parentheses. Parameter list can be empty if the method
doesn't accept any parameters when it is called.

(d) Recursive method — A method that calls itself inside its body is called a recursive method.

(e) Method signature — Method signature comprises of the method name and the data types of the parameters.
For example, consider the given method:

int sum(int a, int b) {


int c = a + b;
return c;
}
Its method signature is:
sum(int, int)

Question 15

Explain the function of a return statement in Java programming.

Answer

A method returns a value through the return statement. Once a return statement is executed, the program control
moves back to the caller method skipping the remaining statements of the current method if any. A method can
have multiple return statements but only one of them will be executed. For example, consider the given method:

int sum(int a, int b) {


int c = a + b;
return c;
}
It uses a return statement to return a value of int type back to its caller.

Question 16
Differentiate between formal parameter and actual parameter.

Answer

Formal parameter Actual parameter

Formal parameters appear in method definition. Actual parameters appear in method call statement.

They represent the values received by the called They represent the values passed to the called
method. method.

Question 17

What is the role of the keyword void in declaring methods?

Answer

The keyword 'void' signifies that the method doesn't return a value to the calling method.

Question 18

If a method contains several return statements, how many of them will be executed?

Answer

A method can have multiple return statements but only one of them will be executed because once a return
statement is executed, the program control moves back to the caller method, skipping the remaining statements
of the current method.

Question 19

Which OOP principle implements method overloading?

Answer

Polymorphism implements method overloading.

Question 20

How are the following data passed to a method?

1. Primitive types
2. Reference types

Answer

1. By value
2. By reference
ICSE Class-10 Computer Application MCQ Type Questions of User-
defined Methods

Question 1: Write the output of the following code :

long series (int p)

long f=1;

for(i=1;i<=p;i++)

f=f*i

}return f;

(a) Fibonacci Series

(b) Factor of a number

(c) Factorial of a number

(d) None Of these

Answer (c) Factorial of a number

Question 2: It is compulsory to return a value from a function

(a) True

(b) False

(c) True if it is a pure function


(d) True if it is an impure function

Answer (b) False

Question 3: A function in java is …………… by default

(a) Private

(b) Public

(c) Protected

(d) None

Answer (b) Public

Question 4: Alternative way to send multiple values from a function is:

(a) Arrays

(b)Tuples

(c) Lists

(d) None

Answer (a) Arrays

Question 5: The default return type of a User defined function

(a) float

(b) int

(c) Void

(d) Null

Answer (b) int

Question 6: The keyword used to return a value from a function is:


(a) returns

(b) return

(c) None

(d) send

Answer (b) return

Question 7: Static functions can access

(a) Static data members

(b) Non static data members

(c) Both static and non static data members

(d) None

Answer (a) Static data members

Question 8: A function name can begin with

(a) An alphabet

(b) Sign

(c) A digit

(d) Space

Answer (a) An alphabet

Question 9: The return type must be specified while defining a user defined function

(a) True

(b) False

(c) Error
(d) None of these

Answer (b) False

Question 10: A function in java can be written

(a) Inside a class

(b) Outside a class

(c) Inside main) function

(d) None

Answer (a) Inside a class

Question 11: An overloaded function is

(a) A function with same name but different return types

(b) A function with same name but different number of parameters

(c) A function with same name but different types of parameters

(d) Both (b) and (c)

Answer (d) Both (b) and (c)

Question 12: Which is odd in given options

(a) Function overloading

(b) Polymorphism

(c) One this in many different forms

(d) Inheritance

Answer (d) Inheritance

Question 13: A local variable of a function has more priority than


(a) A variable defined globally

(b) A variable defined in an imported class

(c) A variable defined in main

(d) All of the above

Answer (d) All of the above

Question 14: Which is odd in given options

(a) function

(b) method

(c) A block of code in { }

(d) Package

Answer (d) Package

Question 15: The function that modifies its parameters is called a

(a) Virtual function

(b) Pure function

(c) Impure function

(d) None

Answer (c) Impure function

Question 16: The prototype of a function take( that returns an int and receives an array is

(a) public int take(int ar [ ])

(b) public int take(int ar)

(c) public int take(int int ar [ ])


(d) None of these

Answer (a) public int take(int ar [ ])

Question 17: Given the code:

int Change(int p,int q)

{
p=p*q;

return p;

The statement to call the above function will be

(a) Change(10,20)

(b) r=Change(10,20)

(c) Change(9.5,6.7)

(d) None

Answer (a) Change(10,20)

Question 18: Which is odd in given options

(a) import statement

(b) function prototype

(c) signature of a function

(d) function declaration

Answer (a) import statement

Question 19: Function prototype means

(a) Return value of a function


(b) Name of the function

(c) Return type of the function

(d) Return type, Name and Inputs to a function

Answer (d) Return type, Name and Inputs to a function

Question 20: The prototype of a function “Show ()” that returns a float and takes two ints is

(a) public void Show ()

(b) public void Show(int a, int b)

(c) public void Show(int, float)

(d) public float Show(int, int)

Answer (d) public float Show(int, int)

Question 21: An array is passed by ………… to a User Defined Functions

(a) Reference

(b) Value

(c) Address

(d) None of the above

Answer (a) Reference

Question 22: All the functions of a package can be imported by using the ……………….. character with
import statement

(a) _

(b) /

(c) *

(d) @
Answer (c) *

Question 23: A User Defined Function can have ………. number of parameters

(a) 2

(b) 3

(c) 4

(d) Any number

Answer (d) Any number

Question 24: A deceleration can occur ………… in a body of a JAVA method.

(a) in the beginning

(b) in the middle

(c) anywhere

(d) at the end

Answer (c) anywhere

Question 25: A user defined function in Java can return ……………….. number of values

(a) 2

(b) 3

(c) 1

(d) None of these

Answer (c) 1

Question 26: Defining two or more methods in the same class with same name is called ……………

(a) constructor overloading


(b) Function overloading

(c) Static function

(d) none of these

Answer (b) Function overloading

Question 27: A private function is accessible to …………..

(a) Own class only

(b) Subclass

(c) Subclass and classes in same package

(d) None

Answer (a) Own class only

Question 28: Which feature of OOPS is illustrated by function overloading

(a) Inheritance

(b) Polymorphism

(c) Encapsulation

(d) Inheritance

Answer (b) Polymorphism

Question 30: What is the access specifier for the function int recall (int, int)

(a) Private

(b) Public

(c) Protected

(d) None of these


Answer (d) None of these

Question 31: Which is odd in given options

(a) Parameter

(b) Class

(c) inputs to a function

(d) argument

Answer (b) Class

Question 32: The function call in which the data in actual parameters get changed is called …………

(a) call by value

(b) call by reference

(d) None of the above

(c) formal call

Answer (b) call by reference

Question 33: To receive an array arr of size 10 in a function result() he correct prototype is:

(a) void result( int arr [ ])

(b) void result( int arr[10])

(c) Both a and b

(d) None of the above

Answer (c) Both a and b

Question 34: main() function is a

(a) User defined function


(b) System defined function

(c) Library defined function

(d) None

Answer (a) User defined function

Question 35: The function Callitself(n) gives:

public int callitself(intn)

if (n==1)

return 1;

else

return n*callitself(n-10);

If it is called as cllitself(6)

(a) 120

(b) 620

(c) 720

(d) 400

Answer (c) 720

You might also like