Kerala Plus One Computer Science Chapter Wise Previous Questions Chapter 10 Functions
Question 1.
A function can call itself for many times and return a result. What is the name given to such a function? (March – 2016)
Answer:
Recursive function
Question 2.
“Arguments used in call statement are formal arguments”. State true or false. (Say – 2016)
Answer:
False
Question 3.
Name the built-in function to check whether a character is alphanumeric or not (March – 2017)
Answer:
isalnum( )
Question 4.
Construct the function prototypes for the following functions. (March – 2015)
a) The function Display ( ) accepts one argument of type double and does not return any value.
b) Total ( ) accepts two arguments of type int, float respectively and returns a float type value.
Answer:
a) void Display(double);
b) float Total(int, float);
Question 5.
a) Explain two types of variable according to its scope and life. (Say – 2015)
Answer:
Scope and life of variables and functions
a) Local scope-A variable declared inside a block can be used only in the block. It cannot be used any other block.
Answer:
Eg-
#include<iostream> using namespace std; int sum(int n1, int n2) { int s; s=n1+n2; return (s); } int main( ) { int n1,n2; cout<<“Enter 2 numbers cin>>n1>>n2; cout<<“The sum is “<<sum(n1, n2); }
Here the variable s is declared inside the function sum and has local scope;
b) Global scope- A variable declared outside of all blocks can be used any where in the program.
#include<iostream> using namespace std; int s; int sum(int n1,int n2) { s=n1+n2; return(s); } int main( ) { int n1,n2; cout<<“Enter 2 numbers cin>>n1>>n2; cout<<“The sum is “<<sum(n1,n2); }
Here the variable s is declared out side of all functions and we can use variable s any where in the program)
Question 6.
Differentiate between the string functions strcmpQ and strcmpi( ). (Say – 2016)
Answer:
strcmp( )- It is used to compare two strings and returns an integer.
Syntax: strcmp(string1,string2)
- if it is 0 both strings are equal.
- if it is greater than 0(i.e. +ve) stringl is greater than string2
- if it is less than 0(i.e. -ve) string2 is greaterthan stringl
strcmpi( )- It is same as strcmpO but it is not case sensitive. That means uppercase and lowercase are treated as same.
Eg. “ANDREA” and “Andrea” and “andrea” these are same.
Question 7.
Read the function definition given below. Predict the output, if the function is called as convert (7); (March – 2017)
void convert (int n) { if (n>1) convert (n/2); cout<<n%2; }
Answer:
The out put is 111. convert( ) is a recursive function
Question 8.
Name the different methods used for passing arguments to a function. Write the difference between them with examples. (March – 2015)
Answer:
Methods of calling functions
Two types call by value and call by reference.
1) Call by value : In call by value method the copy of the original value is passed to the function, if the function makes any change will not affect the original value.
Example
#include<iostream.h> #include<conio.h> void swap(int a, int b) { int temp; temp=a; a=b; b=temp; } main( ) { clrscr( ); int a,b; cout<<‘‘Enter values for a and b:- “; cin>>a>>b; cout<<“The values before swap a=”<<a<<“ and b=”<<b; swap(a,b); cout<<“\nThe values before swap a=”<<a<<“ and b=”<<b; getch( ); }
2) Call by reference : In call by reference method the address of the original value is passed to the function, if the function makes any change will affect the original value.
Example
#inc!ude<iostream.h> #include<conio.h> void swap(int &a, int &b) { int temp; temp=a; a=b; b=temp; } main( ) { clrscr( ); int a,b; cout<<“Entervalues fora and b> cin>>a>>b; cout<<“The values before swap a=”<<a<<“ and b=”<<b; swap(a,b); cout<<“\nThe values before swap a=”<<a<<“ and b=”<<b; getch( ); }
Question 9.
Write a C++ program to display the simple interest using function. (Say – 2015)
Answer:
include < iostream> using namespace std; float Simplelnt (float p, int n, float r) { return (p*n*r/100); } int main ( ) { float p, r, SI; int n; cout<<“Enter values for p,n,r:”; cin>>p>>n>>r; SI = Simplelnt (p,n,r); cout<<“simple interest is”<<SI; }
Question 10.
Write a function’s definition of the above type to find the sum of matural numbers from 1 to N. (Hint: If the value of N is 5, the answer will be 1 + 2 + 3 + 4 + 5 = 15) (March – 2016)
Answer:
int sum (int n) { if (n==0) return 0; else return (n + sum (n-1)); }
Question 11.
Explain recursive functions with the help of a suitable example. (Say – 2016)
Answer:
A function calls itself is called recursive function.
#include<iostream> using namespace std; void convert(int n) { if(n>1) convert(n/2); cout<<n%2; } int main( ) { convert(7); }
Here the function convert is a recursive function, that means it calls itself and output of the above program is 111.
Question 12.
Name the built-in function to check whether a character is alphanumeric or not (March – 2017)
Answer:
isalnum( )
Question 13.
Briefly explain the three components in the structure of a C++ program. (March – 2017)
Answer:
1) Pre processor directives: A C++ program starts with the pre processor directive i.e., # .include, #define, #undef, etc, are such a pre processor directives. By using #include we can link the header files that are needed to use the functions. By using #define we can define some constants.
Eg. #define x 100. Here the value of x becomes 100 and cannot be changed in the program.
No semicolon is needed.
2) Header files: A header file is a pre stored file that helps to use some operators and functions. To write C++ programs the header files are must. Following are the header files
iostream
iomanip
cstdio
cctype
cmath
cstring
The syntax for including a header file is as follows
#include<name of the header file>
Eg. #include<iostream>
3) The main function : The main function is the first function which is invoked at the time of execution and the program ends within main( ). The other functions are invoke from main( ).
Question 14.
Explain the difference between call-by-value method and call-by-reference method with the help of examples (March – 2017)
Answer:
Two types are call by value and call by reference.
1. Call by value : In call by value method the copy of the original value is passed to the function, if the function makes any change wilt not affect the original value.
Eg:
#include<iostream> using namespace std; void swap(int x,int y) { int temp; temp=x; x=y; y=temp; } int main( ) { int a,b; cout<<“Enter 2 nos”; cin>>a>>b; cout<<“The values before swap a=”<<a<<“ and b=”<<b; swap(a,b); cout<<“\nThe values after swap a=”<<a<<“ and b=”<<b; }
There is no change in the values.
2. Call by reference : In call by reference method the address of the original value is passed to the function, if the function makes any change will affect the original value.
Eg:
#include<iostream> using namespace std; void swap(int &x,int &y) { int temp; temp=x; x=y; y=temp; } int main( ) { int a,b; cout<<“Enter 2 nos”; cin>>a>>b; cout<<“The values before swap a=”<<a<<“ and b=”<<b; swap(a,b); cout<<“\nThe values after swap a=”<<a<<“ and b=”<<b; }
There is a change jn the values.
Question 15.
Write a C++ program to display the roots of quadratic equation. (Say – 2015)
Answer:
# include <iostream> # include <cmath> using namespace std; int main ( ) { inta,b,c,d; float r1,r2; cont<<“Enter the coefficients”; cin>> a >> b >> c; d = b*b-4*a*c; if (d==0) { cout<<‘The roots are equal”; r1=r2= -b/(2.0*a); cout<<“The roots are r1=r2=”<<r1; } elseif (d>0) { cout<< “The roots are real and unequal”; r1=(-b+sqrt(d))/(2.0*a); r2=(- b-sq rt (d))/(2.0*a); cout<<“\n The roots are given below \n”; cout<<“r1 =”<<r1 <<“and r2=”<<r2; } else { cout<<“The roots are real and imaginery”; r1 =(-b+sqrt(abs(d))/2.0*a); r2=(-b-sqrt(abs(d))/(2.0*a); cout<<“\n The roots are given below \n”; cout<<“r1 =”<<r1 <<“i and r2=”<<r2<<“i”; } }
Question 16.
Explain ‘Call by Value’ and ‘Call by Reference’ methods of function calling with the help of a suitable example. (Say – 2016)
Answer:
Two types are call by value and call by reference. 1. Call by value : In call by value method the copy of the original value is passed to the function, if the function makes any change will not affect the original value.
Eg:
#include<iostream> using namespace std; void swap(int x,int y) { int temp; temp=x; x=y; y=temp; } int main( ) { int a,b; cout<<“Enter 2 nos”; cin>>a>>b; cout<<“The values before swap a=”<<a<<“ and b=’’<<b; swap(a,b); cout<<“\nThe values after swap a=’’<<a<<“ and b=”<<b; }
There is no change in the values.
2. Call by reference: In call by reference method the address of the original value is passed to the function, if the function makes any change will affect the original value.
Eg:
#include<iostream> using namespace std; void swap(int &x,int &y) { inttemp; temp=x; x=y; y=temp; } int main( ) { int a,b; cout<<“Enter 2 nos”; cin>>a>>b; cout<<“The values before swap a=”<<a<<“ and b=”<<b; swap(a,b); cout<<“\nThe values after swap a=”<<a<<” and b=”<<b; }
There is a change in the values.
Question 17.
Define network topology. Explain any four network topologies in detail. (Say – 2016)
Answer:
Network topologies : Physical or logical arrangement of computers on a network is called structure or topology. It is the geometrical arrangement of computers in a network. The major topologies developed are star, bus, ring, tree and mesh.
1) Star Topology : A star topology has a server all other computers are connected to it. If computer A wants to transmit a message to computer B. Then computer A first transmit the message to the server then the server retransmits the message to the computer B. That means all the messages are transmitted through the server. Advantages are add or remove workstations to a star network is easy and the failure of a workstation will not effect the other. The disadvantage is that if the server fails the entire network will fail.
2) Bus Topology : Here all the computers are attached to a single cable called bus. Here one computer transmits all other computers listen. Therefore it is called broadcast bus. The transmission from any station will travel in both the direction. The connected computers can hear the message and check whether it is for them or not.
Advantages are add or remove computer is very easy. It requires less cable length and the installation cost is less. Disadvantage is fault detection is very difficult because of no central computer.
3) Ring Topology : Here all the computers are connected in the shape of a ring and it is a closed loop. Here also there is no central computer. Here a computer transmits a message, which is tagged along with its destination computer’s address. The message travels in one direction and each node check whether the message is for them. If not, it passes to the next node.
It requires only short cable length. If a single node fails, at least a portion of the network will fail. To add a node is very difficult.
4) Hybrid Topology: It is a combination of any two or more network topologies. Tree topology and mesh topology can be considered as hybrid topology.
a) Tree Topology : The structure of a tree topology is the shape of an inverted tree with a central node and branches as nodes. It is a variation of bus topology. The data transmission takes place in the way as in bus topology. The disadvantage is that if one node fails, the entire portion will fail.
b) Mesh Topology: In this topology each node is connected to more than one node. It is just like a mesh (net). There are multiple paths between computers. If one path fails, we can transmit data through another path.