Plus Two Computer Application Chapter Wise Previous Questions Chapter 3 Functions

Kerala Plus Two Computer Application Chapter Wise Previous Questions Chapter 3 Functions

Plus Two Computer Application Functions 1 Mark Important Questions

Question 1.
function is used to check whether a character is alphanumeric. (MAY-2016)
a) isdigit( )
b) inalnum( )
c) isupper( )
d) islower( )
Answer:
b) isalnum( );

Question 2.
char s1 [10]=”hello”,s2[10]; (MAY-2017)
strcpy (s2, s1);
cout<<s2;
What will be the output?
Answer:
a) hello

Plus Two Computer Application Functions 2 Marks Important Questions

Question 1.
Consider the followjjig code: (MARCH-2016)
char S1[ ] = “program”
char S2[ ] = “PROGRAM” int n;
n=strcmpi (S1, S2)
What is the value of n?
a) n=0
b) n=1
c) n>1
d) n<0
Answer:
a) n =0

Question 2.
Explain two stream functions for input operation with example. (MARCH-2017)
Answer:
Input functions : The input functions like get( )(to read a character from the keyboard) and getline( ) (to read a line of characters from the keyboard) is used with cin and dot(.) operator.

Question 3.
Explain how allocation of string takes place in memory. (MAY-2017)
Answer:
A string is automatically appended by a null character(‘\0’). The null character is treated as one character. charname[20];
Here we can store a name with character up to 19.

Question 4.
Explain gets( ) and puts ( ) functions (MAY-2017)
Answer:
gets( ) function is used to get a string from the key board including spaces.
puts( ) function is used to print a string on the screen. To use gets( ) and puts ( ) function the header file cstdio must be included.

Plus Two Computer Application Functions 3 Marks Important Questions

Question 1.
Explain any three stream function for I/O operation.(MARCH-2016)
Answer:
Stream functions for I/O operation are given below
1) get ( ):- To read a character from the key board
eg: cin.get(ch);

2) getline ( ):- To read a line of characters from the keyboard
eg: cin.getline(str, len);

3) put( ):- To print a characters on the screen
eg:cout.put(‘A’);

4) write ( ):- To print a line of characters on the screen
eg: cout.write (str, len);

Question 2.
Write a code to do the following: (MARCH-2016)
a) A function named largest accept two integer numbers and return the largest number.
b) Use this function to find the largest of two numbers.
Answer:
a) int largest (int a, int b)
{
if (a>b)
return a;
else
return b;
}

b) void main ( )
{
int x, y;
cout<<“Enter 2 numbers”; cin>>x>>y;
cout<< “The largest among these is”
<< largest (x,y);
}

Question 3.
Explain any three string function with example. (MAY-2016)
Answer:
Functions in C++
Some functions that are already available in C++ are called pre defined or built in functions.
In C++, we can create our own functions for a specific job or task, such functions are called user defined functions.
A C++ program must contain a main( ) function. A C++ program may contain many lines of statements(including so many functions) but the execution of the program starts and ends with main( ) function.
Pre defined functions
To invoke a function that requires some data for performing the task, such data is called parameter or argument. Some functions return some value back to the called function.
String functions
To manipulate string in C++ a header file called string.h must be included.
a) strlen( )- to find the number of characters in a string(i.e. string length).
Syntax: strlen(string);
Eg- cout<<strlen(“Computer”); It prints 8.

b) strcpy( )- It is used to copy second string into first string.
Syntax: strcpy(string1 ,string2);
Eg. strcpy(str,”BVM HSS”); cout<<str; It prints BVM HSS.

c) strcat( )- It is used to concatenate second string into first one.
Syntax: strcat(string1 ,string2)
Eg. strcpy(str1,’’Hello”); strcpy(str2,” World”); strcat(str1,str2);
cout<<str1; It displays the concatenated string “Hello World”

d) 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) string1 is greater than string2
if it is less than 0(i.e. -ve) string2 is greater than string1
Eg.
include
#include using namespace std; int main()
{
char str1 [10],str2[10];
strcpy(str1 ,”Kiran”);
strcpy(str2,”Jobi”);
cout<<strcmp(str1 ,str2);
}
It returns a +ve integer.

e) strcmpi( )- It is same as strcmp( ) but it is not case sensitive. That means uppercase and lowercase are treated as same.
Eg. “ANDREA” and “Andrea” and “andrea” these are same.
# include
#include
using namespace std;
int main( )
{
char str1 [10],str2[10];
strcpy(str1,”Kiran”);
strcpy(str2,”KIRAN”);
cout<<strcmpi(str1 ,str2);
}
It returns 0. That is both are same.

Question 4.
Write a function that accept 3 numbers of type float as argument and return the average of three numbers. Write program which use this function to find the average of three numbers using C++. (MAY-2016)
Answer:
# include
using namespace std;
float avg(float n1, float n2, float n3)
{
return ((n1 +n2+n3)/3);
}
int main( )
{
float x, y, z;
cout <<“Enter 3 nos”; cin>> x>>y>>z;
cout <<“the average is”<<avg(x,y,z);
}

Question 5.
“Initialized formal arguments are called default arguments.” Using this concept write the function prototype and definition of a user defined function Sum() which accept two or three integer numbers and return their sum. (MARCH-2017)
Answer:
#include
using namespace std;
int sum(int x=100,int y=50,int z=10)
{
retum(x+y+z);
}
int main( )
{
cout<<sum( )<<endl; cout<<sum(1)<<endl;
cout<<sum(1,2)<<endl;
cout<<sum(1,2,3)<<endl;
}

Question 6.
Write the output of the following code segment. (MARCH-2017)
char S1[25]=”Computer”;
char S2[15]=”Applications”;
strcat(S1 ,S2);
cout<<S1;
Answer:
The output is “ComputerApplicatios”. That is the second string is concatenated to the first string.

Question 7.
Explain three string functions in C++. (MAY-2017)
Answer:
a) strlen( )- to find the number of characters in a string(i.e. string length).
Syntax: strlen(string);
Eg. cout<<strlen(“Computer”);
It prints 8.

b) strcpy( )- It is used to copy second string into first string.
Syntax: strcpy(string1, string2);
Eg. strcpy(str,”BVM HSS”);
cout<<str;
It prints BVM HSS.

c) strcat( )- It is used to concatenate second string into first one.
Syntax: strcat(string1,string2)
Eg. strcpy(str1,’’Hello”);
strcpy(str2,” World”);
strcat(str1 ,str2);
cout<<str1;
It displays the concatenated string “Hello World”

Question 8.
Write a program using a function to interchange the value of two variables. (Use call by reference method for passing arguments.) (MAY-2017)
Answer:
#include
using namespace std;
void swap(int &x,int &y)
{
int temp; temp=x;
x=y;
y=temp;
}
intmain()
{
intx=100,y=200;
cout<<“values before swap”<<endl;
cout<<“x=”<<x<<“,y=”<<y<<“\n”;
swap(x,y);
cout<<“values after swap”<<endl;
cout<<“x=”<<x<<“,y-‘<<y<<“\n”;

Plus Two Computer Application Chapter Wise Previous Questions and Answers