[go: up one dir, main page]

0% found this document useful (0 votes)
117 views9 pages

Practice Worksheet 4

The document contains a practice worksheet on functions in Java with multiple choice and true/false questions about function concepts like return types, parameters, overloading, scope, etc. It also includes code snippets and questions to test understanding of function prototypes, calling functions, passing parameters by value vs reference, and defining pure and impure functions. The worksheet aims to help students practice and reinforce their knowledge of key concepts related to defining, calling and overloading functions in Java.

Uploaded by

Deepali Koirala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views9 pages

Practice Worksheet 4

The document contains a practice worksheet on functions in Java with multiple choice and true/false questions about function concepts like return types, parameters, overloading, scope, etc. It also includes code snippets and questions to test understanding of function prototypes, calling functions, passing parameters by value vs reference, and defining pure and impure functions. The worksheet aims to help students practice and reinforce their knowledge of key concepts related to defining, calling and overloading functions in Java.

Uploaded by

Deepali Koirala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PRACTICE WORKSHEET 4 CHAPTER : FUNCTIONS(UDF).

Q1. Choose the correct option.

a. The first line of the function definition is known as _________.


a. function signature.
b. constructor.
c. function prototype.
d. function parameters.

b. The ________ keyword indicates that the function is not returning any value.
a. return
b. final
c. continue
d. void

c. When a primitive datatype is passed , it is called by ________.


a. call by reference
b. call by value
c. call by method
d. none

d. Variables in the function call are called as _________.


a. parameters
b. actual parameters
c. formal parameters
d. values

e. A function returns True or False. The return type of the function is _______.
a. void
b. String
c. boolean
d. int

f. A function can contain ____ number of return statements


a. 1
b. 2
c. 3
d. as many as the programmer wants.

g. Function add() can be used only in the same class and takes 2 integer parameters and sends back the sum of the
numbers.
Which of the following is a correct prototype for function add().

a. public static void add( int a , int b)


b. private static void add( int a , b)
c. public static int add( int a, int b)
d. private static int add( int a , int b)

SHAILESH MANJREKAR – 98194 98104


h. Function check() takes a char and int parameter, it can be called only by creating an object and can be accessed
only in the package.
Which of the following is a correct prototype for function check().

a. protected static void check(char ch , int a)


b. static void check(char ch , int a)
c. void check(char ch , int a)
d. public static void check(char ch , int a)

i. Which of the following functions prototype violate the rules of overloading.


1.void convert(int n1,int n2)
2.int convert(int n2,int n1)
3.void convert(int n1,int n2,int n3)
4.double convert(double a,double b)
5.double convert(int a,int b,int c)

a. 1,2 and 3,5


b. 1,4,3
c. 2,5 and 3,5
d. 1,2

j. Choose the correct output for the following code.


public static void main()
{
int n=10;
String S="Cheese";
Student S1=new Student();
S1.rollno=10;
S1.name="Edward";

update(n , S , S1);

System.out.print(n);
System.out.print(S);
System.out.print(S1.rollno);
System.out.print(S1.name);
}

public static void update(int n, String S, Student S1)


{
n=20;
S="Butter";
S1.rollno=20;
S1.name="Bella";
}

SHAILESH MANJREKAR – 98194 98104


a.10Cheese10Edward
b.20Butter20Bella
c.10Cheese20Bella
d.20Butter10Edward

Q2. State true or false

a. The return type is used as a criteria for function overloading.


a.true
b.false

b. Variables declared inside a function are local to the function.


a.true
b.false

c. A void function can contain return keyword without a value


a.true
b.false

d. A function can return any number of values.


a.true
b.false

e. A functions argument list is also called as prototype.


a.true
b.false

Q3. Choose the odd one :

a. 1. arguments
2. actual parameters
3. formal parameters

b. 1. friendly
2. package
3. private

c. 1. void
2. return
3. continue
4. break

Q4. Choose the statement/keyword which defines the term below from the code below :

class demo
{
public static void test(double d,int i)
{
double ans = d+i;
System.out.println(ans);
SHAILESH MANJREKAR – 98194 98104
}
public static void main()
{
test(5.5 , 5);
}
}

a.prototype
1. public static void test(double d,int i)
2. public static void main()
3. both 1,2

b.arguments
1. 5.5 , 5
2. ans
3. d , i

c.signature
1. public static void test()
2. (double d,int i)
3. test(5.5 , 5)

d.formal parameters
1. 5.5 , 5
2. ans
3. d , i

e.local variables
1. 5.5 , 5
2. ans
3. d , i

Q5. Fill in the blanks :

a. The following code returns the power of a to b

public static ___1__ myPower (int a, int b)


{
long ans=1;
for (int i=1; ____2___; i++)
{
ans *=___3___;
}
return ___4___ ;
}

SHAILESH MANJREKAR – 98194 98104


b. With the info. below overload function reverse() which returns back the converted value
1 int parameter - returns the reversed number
1 char parameter - changes the case (uppercase to lowercase & viceversa)

public static ______1__________


{
while(n>0)
{
r = n%10;
n = n/10;

rev = ________2________;
}
____3________;
}
public static _______4____________
{
if(Character.isUppercase(ch))
{
ans = ________5________;
}
else
{
ans = _________6________;
}
return ans;
}

Q6. Answer the following questions

1. Write the output of the following :


class globallocal
{
int num=10;

public void fun1()


{
int num=20;
System.out.println(num);
}

public void fun2()


{
System.out.println(num);
}

// calling the above functions for output


public void call()
{
fun1();
fun2();
SHAILESH MANJREKAR – 98194 98104
}
}

2. Write the output of the following :


//UDT is used in the main program
class box
{
//data member
int size;

//constructor to set default value


public box()
{
size=1;
}
}

// main program
class callBy
{
public static void main()
{
//setting initial values to various datatypes
int i =10;
String s="max";
boolean b=false;
box x=new box();

// passing to functions
changeValues(i , s , b , x );

// printing values
System.out.println(i);
System.out.println(s);
System.out.println(b);
System.out.println(x.size);
}

public static void changeValues(int i, String s, boolean b, box x)


{
i=20;
s="min";
b=true;
x.size=2;
}
}

3. Write the prototypes for the following function descriptions

SHAILESH MANJREKAR – 98194 98104


a. Function product() can be called only an object of the class
It takes a whole number and decimal number, it returns the product of the 2 numbers and can be used in any class.

b. Function encrypt() takes a string and a number and returns a string, this function can be used only in the same
class. It can be called with the class name.

4. With UDT given below, Complete the code to indicate pure and impure functions

class Student
{

int rollno;
String name;
}

public void pureFunction(Student S1) public void impureFunction(Student S1)


{ {

_______________________ ; _______________________ ;

} }

5. In the code given below, Identify Actual parameters, Formal parameters, Prototype, Signature, Return type. Define
each of them.

class coffee{
public static void main()
{
int N =10;
String S = compare( N,100);
System.out.println(S);

public static String compare(int S, int S1)


{
if (S>S1)

return “White”;
else
return “Red”;
}
}

Q7. Fill in the blanks to complete the program

Write a function LuckyTen() which takes an int parameter and returns boolean value (true) if the sum of the digits
is 10 and (false) otherwise. Using the function print all LuckyTen numbers between 1 and 1000.
SHAILESH MANJREKAR – 98194 98104
class lucky
{
public static void main()
{
int i;
for(i=1 ; i<=____1___ ; i++)
{
boolean x = ____2_____ ;
if( ___3_____ )
System.out.println(i);
}
}
public static ___4___ luck( ____5____ )
{
int r,s=0;
while( ___6___ )
{
r= ____7____ ;
n=n/10;
s=___8___;
}
if( ____9_____ )
return true;

else
______10______;
}
}

Overload function named printSeries() which takes


a. 1 int parameter and prints all numbers till the number.
b. 2 parameters and prints all the numbers in the range.

class printseries
{
public ___1__ void main()
{
printseries(10);
____2_____ (5,15);
}
public static void ___3__ ( ___4___ )
{
int i;
for(i=1 ; ___5__ ; i++)
{
________6______ ;
}
}
public static ___7____ ___8____(int n1 , int n2)
{
int i;
SHAILESH MANJREKAR – 98194 98104
for( _____9_______ )
{
_______10__________ ;
}
}
}

SHAILESH MANJREKAR – 98194 98104

You might also like