Kerala Plus One Computer Science Chapter Wise Questions and Answers Chapter 10 Functions
Very Short Answer Type Questions
(Score 1)
Question 1.
Name the header file required for using character functions,
a. iostream.h
b. ctype.h
c. conio.h
d. process.h
Answer:
b. ctype.h
Question 2.
Name the function that displays the subsequent data in the specified width.
a. sqrt( )
b. atoi( )
c. setw( )
d. isalnum( )
Answer:
c. setw( )
Question 3.
Pick the odd one out.
a. strlen( )
b. itoa()
c. strcpy( )
d. strcatf ( )
Answer:
b. itoa( )
Question 4.
Which component is used for data transfer from calling function to called function?
Answer:
argument or parameter
Question 5.
If the prototype of a function is given immediately after the pre processor directive, its scope will be
Answer:
global
Question 6.
What is the scope of predefined functions in C++?
Answer:
global
Question 7.
The arguments of a function have scope.
Answer:
local
Question 8.
For the manipulation of strings, we should include header file.
a. cstring.h
b. ctype.h
c. conio.h
d. process.h
Answer:
a. cstring.h
Question 9.
……. function is used to find the absolute value of a floating point number.
a. abs( )
b. fabs( )
c. cos( )
d. pow( )
Answer:
b. fabs( )
Question 10.
The process of breaking large programs into smaller sub programs is known as
Answer:
modularization
Question 11.
Identify the most essential function in C++ programs.
Answer:
main () function is the most essential function in C++ programs.
Question 12.
What are the two parameter passing techniques used in C++?
Answer:
Call by value method and call by reference method.
Short Answer Type Questions
(Score 2)
Question 1.
What is modular programming?
Answer:
In programming, the entire problem will be divided into small sub problems that can be solved by writing separate programs.This kind of approach is known as modular programming.
Question 2.
List the three elements of a function header.
Answer:
The three elements of a function header are return data type, function name and function argument list.
Short Answer Type Questions
(Score 3)
Question 1.
What is function prototype?
Answer:
A function prototype is the declaration of a function by which compiler is provided with the information about the function such as the name of the function, its return type, the number and type of arguments, and its accessibility. This information is essential for the compiler to verify the correctness of the function call in the program.
Question 2.
What are the advantages of modular of programming?
Answer:
- It reduces size of the program.
- Less chances of error.
- Modularization reduces the programming complexity.
- Improves reusability
Question 3.
Distinguish between the scope of variables and functions.
Answer:
Scope of local variables:
- Declared within a function or a block of statements.
- Available only within that function or block.
- Memory is allocated when the function or block is active and freed when the execution of the function or block is completed.
Scope of global variables:
- Declared outside all the functions.
- Available to all the functions of the program.
- Memory is allocated just before the execution of the program and freed when the program stops execution.
eg., #include <iostream> using namespace std; int cube(int n) { int cb; cout<< “The value of x passed to n is” < < x; /*This is an error because the variable x is declared within the main () function. So it cannot be used in other functions. */ cb = n * n * n; return cb; } int main( ) { int x, result; cout << “Enter a number : ”; cin >> x; result = cube(x); cout << “Cube = ” << result; cout << “\nCube = ”<<cb; }
In the above program, scope of the variable cb is in the cube() function because it is declared within that function. Hence this variable cannot be used outside the function. This scope is known as local scope.
Scope of functions are:
Local:
- Declared within a function or a block of statements and defined after the calling function.
- Accessible only within that function or the block.
Global:
- Declared or defined outside all other functions.
- Accessible by all the functions in the program.
Long Answer Type Questions
(Score 5)
Question 1.
Write a program to combine two strings if they are different and find its length.
Answer:
#include <iostream> #include <cstring> using namespace std; int main( ) { char s1[2o], S2[20], S3[20]; cout<<“Enter two strings: cin>>s1>>s2; int n=strcmp(s1, S2); if (n= = o) cout<<“\nThe input strings are same”; else { cout<<“\nThe input strings are not same”; strcpy(s3, s1); strcat(s3, S2); cout<<“\nString after concatenation is: ”<<S3; cout<<“\nLength of the new string is” <<strlen(s3); > return o; }
Question 2.
Program to find the length of the sides of a right angled triangle using mathematical functions.
Answer:
#include<iostream> #include<cmath> using namespace std; int main() { const float pi=22.o/7; int angle, side; float radians, length, opp_side, adj_side, hyp; cout<<“Enter the angle in degree: ”; cin>>angle; radians=angle*pi/180; cout <<“\n1. Opposite Side” <<“\n2. Adjacent Side”<<“\n3.Hypotenuse”; cout <<“\n Input 1, 2 or 3 to specify the side: ”; cin>>side; cout<<“Enter the length: ”; cin>>length; switch(side) { case 1: opp_side=length; adj_side=opp_side / tan(radians); hyp=sqrt(pow(opp_side,2)+ pow(adj _side,2)); break; case 2: adj_side=length; hyp=adj_side / cos(radians); opp_side=sqrt(pow(hyp,2)- pow(adj_ side,2)); break; case 3: hyp=length; opp_side=hyp * sin(radians); adj_side=sqrt(pow(hyp,2)- pow(opp_ side,2)); } cout<<“Angle in degree =”<<angle; cout<<“\nOpposite Side=”<<opp_ side; cout<<“\nAdjacent Side=”<<adj_ side; cout<<“\nHypotenuse =”<<hyp; return 0; }
Question 3.
Program to count different types of characters in the given string.
Answer:
#include <iostream> #include <cstdio> #include <cctype> using namespace std; int main( ) { char text[8o]; int Ucase=o, Lcase=o, Digit=o, i; cout << “Enter a line of text: ”; gets(text); for(i=0; text[i]!=‘\o'; i++) if (isupper(text[i])) Ucase++; else if (islower(text[i])) Lcase++; else if (isdigit(text[i])) Digit++; cout << “\nNo. of uppercase letters=”<< Ucase; cout << “\nNo. of lowercase letters =”<< Lease; cout << “\nNo. of digits = ”<< Digit; cout << “\nThe string in uppercase form is\n”; i=o; while (text[i]!=‘\o') { putchar(toupper(text[i])); i + + ; } cout << “\nThe string in lowercase form is\n”; i=o; do { putchar(tolower(text[i])); i+ + ; } while(text[i]!=‘\o'); return 0; }
Question 4.
Write a program to display date of birth in date format.
Answer:
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; int main() { char dd[10],mm[10],yy[10], dob[3o]; int d, m, y; cout<<“Enter day, month and year in your Date of Birth: ”; cin>>d>>m>>y; itoa(d, dd, 10); itoa(m, mm, 10); itoa(y, yy, 10); strcpy(dob, dd); strcat(dob, strcat(dob,mm); strcat(dob, strcat(dob,yy); cout<<“Your Date of Birth is ”<<dob; return o; }
Question 5.
Program to find the value of nCr.
Answer:
#include<iostream> using namespace std; int fact(int); int main() { int n,r; int ncr; cout<<“Enter the values of n and r: ”; cin>>n>>r; ncr=fact(n)/(fact(r)*fact(n-r)); cout<<n<<“C”<<r<<“ = ”<<ncr; return o; } int factfint N) { int f; for(f=i; N>o; N- -) f=f*N; return f; }
Question 6.
Write a program for swapping the values of two variables.
Answer:
#include <iostream> using namespace std; void swap (int & x, int &y) { int t = x; x = y; y = t; } int main( ) { int m, n; m = 10; n = 20; cout<<“Before swapping m= ”<< m <<“ and n= ”<<n; swap(m, n); cout<<“\nAfter swapping m= ”<< m <<“ and n= ”<<n; return o; }
Question 7.
Find the binary equivalent of a decimal number.
Answer:
#include<iostream> using namespace std; void Binary(int); int main( ) { int decimal; cout<<“Enter an integer number: ”; cin> >decimal; cout<<“Binary equivalent of”<< decimal<<“ is ”; Binary(decimal); return o; } } void Binary(int n) { if (n>1) Binary(n/2); cout<<n%2; }
Textual Questions & Answers
Question 1.
How do top down design and bot-tom up design differ in programming? (2)
Answer:
Top down design begins the design with the main or top level module, and progresses downward to the lowest level modules. Bottom up design begins the design
with the lowest level modules and progresses upward to the main program, module.
Question 2.
What is a function in C++? ( 1)
Answer:
Function is a named unit of statements in a program to perform a specific task as part of the solution.
Question 3.
The ability of a function to call itself is (1)
Answer:
Recursion
Question 4.
Write down the role of header files in C++ programs. (l)
Answer:
We can place any number of functions in a single header file and include that header file in any program to make use of these functions.
Question 5.
When will you use void data type for function definition? (1)
Answer:
When there is no return values from a function, we will use void data type for function definition.
Question 6.
Distinguish between actual parameters and formal parameters. (2)
Answer:
The variables used in the function definition as arguments are known as formal arguments (formal parameters). The constants, variables or expressions used in the function call are known as actual (original) arguments or actual parameters.
Question 7.
Construct the function prototypes for the following functions:
a. Total() takes two double arguments and returns a double. (1)
b. Math() takes no arguments and has no return value. (1)
Answer:
a. double Total(double, double);
b. void Math( );
Question 8.
Distinguish between exit() function and return statement. (2)
Answer:
The return statement returns a value to the calling function and transfer the program control back to the calling function. The exit() function terminates the execution of program itself.
Question 9.
Discuss the scope of global and lo-cal variables with examples. (5)
Answer:
Scope of local variables:
- Declared within a function or a block of statements.
- Available only within that function or block.
- Memory is allocated when the function or block is active and freed when the execution of the function or block is completed.
Scope of global variables:
- Declared outside all the functions.
- Available to all the functions of the program.
- Memory is allocated just before the execution of the program and freed when the program stops execution.
eg., #include <iostream> using namespace std; int cube(int n) { int cb; cout<< “The value of x passed to n is” < < x; /*This is an error because the variable x is declared within the main () function. So it cannot be used in other functions. */ cb = n * n * n; return cb; } int main( ) { int x, result; cout << “Enter a number : ”; cin >> x; result = cube(x); cout << “Cube = ” << result; cout << “\nCube = ”<<cb; }
In the above program, scope of the variable cb is in the cube() function because it is declared within that function. Hence this variable cannot be used outside the function. This scope is known as local scope.
Question 10.
Distinguish between Call-by-value method and Call-by-reference method used for function calls. (5)
Answer:
Call by value method:
- Ordinary variables are used as formal parameters.
- Actual parameters may be constants, variables or expressions.
- The changes made in the formal arguments are not reflected in actual arguments.
- Exclusive memory allocation is required for the formal arguments.
Call by reference method:
- Reference variables are used as formal parameters.
- Actual parameters will be variables only.
- The changes made in the formal arguments are reflected in actual arguments.
Memory of actual arguments is shared by formal arguments.
Question 11.
In C+ + , function can be invoked without specifying all its arguments. How? (2)
Answer:
Functions can be defined with arguments assigned with initial values, which is called function with default argument. When a function is called without specifying the values to the default arguments, the assigned values are used for execution.
Question 12.
Write down the process involved in recursion. (2)
Answer:
The process of calling a function by itself is known as recursion and the function is known as recursive function. Some of the complex algorithms can be easily simplified by using the method of recursion. In recursive functions, the function is usually called based on some condition only.
Question 13.
Look at the following functions:
int sum(int a, int b=o, int c=o)
{
return (a + b + c);
}
a. What is the speciality of the function regarding the parameter list? (1)
b. Give the outputs of the following function calls by explaining its working and give reason if the function call is wrong. (3)
i. cout<<sum(1, 2, 3);
ii. cout<<sum(5, 2);
iii. cout<<sum( );
iv. cout<<sum(o);
Answer:
a. The speciality is the function with default arguments,
b.
i. 6, here three values are supplied a = 1, b = 2, c= 3.
ii. 7, here two values are supplied a = 5, b = 2, c takes default value.
iii. error, no argument supplied.
iv. o, here one value is supplied a = o, then b and c take default values.
Question 14.
The prototype of a function is: int fun(int, int); The following function calls are invalid.
Give reason for each. (5)
a. fun(2,4);
b. cout<<fun();
c. val=fun(2.5, 3.3);
d. cin>>fun(a, b);
e. z=fun(3);
Answer:
a. The variable is not receive the value returned by fun();
Correct is val =fun(2,4);
b. No arguments are used with function call.
cout<<fun(a,b);
c. Arguments in function call are not int type.
val =fun(2,3);
d. Invalid cin statement, correct statement is
cin>>a>>b;
cout<<fun(a,b);
e. Only one argument is used instead of two.
z=fun(3, 2);
Leave a Reply