You can Download Chapter 12 Functions Questions and Answers, Notes, 1st PUC Computer Science Question Bank with Answers Karnataka State Board Solutions help you to revise complete Syllabus and score more marks in your examinations.

## Karnataka 1st PUC Computer Science Question Bank Chapter 12 Functions (Library Functions)

### 1st PUC Computer Science Functions One Mark Questions and Answers

Question 1.

Define function.

Answer:

A FUNCTION is a self-contained block of statements that perform a specific task with a name given to it.

Question 2.

How are functions classified?

Answer:

Functions are classified as,

- Library functions pre-defined functions and
- User-defined functions.

Question 3.

What is a predefined function in C++?

Answer:

Predefined functions are functions that are built into C++ language to perform some standard operations that are stand-alone and used for general purposes.

Question 4.

What is a header file?

Answer:

A header file is a file containing all the library function definitions that can be used in a program.

Question 5.

Answer:

List any two basic types of standard library functions.

- I/O Functions and
- Mathematical Functions – math.h

Question 6.

Write the names of any two C++ header files.

Answer:

Two header files are

- iostream.h and
- iomanip.h

Question 7.

List the names of any two functions of iostream.h header file.

Answer:

The two functions of iostream.h files are

- cin and
- cout

Question 8.

List any two functions of iomanip.h header file.

Answer:

Two functions of iomanip.h files are

- end1 and
- setw.

Question 9.

What is the main function of iostream.h header file?

Answer:

The iostream.h header file contains C++ streams and i/o routine functions. It is a header file which we include in our programs to perform basic input output operations. We use ‘cin’ in programs to get input from the keyboard. To use ‘cin’ and ‘cout’, we must add iostream. h header file.

Question 10.

What is the main function of iomanip.h header file?

Answer:

The iomanip.h header file contains various functions and macros for i/o manipulations for creating or parameterized manipulations.

Question 11.

Write any two math.h library functions.

Answer:

sqrt() and pow() are two libarary functions of math.h.

Question 12.

Write any two functions of ctype.h?

Answer:

Two functions of ctype.h are isalpha() and isdigit().

Question 13.

Write any two library functions of stdlib.h.

Answer:

Two functions of stdlib.h are atoi() and itoa().

Question 14.

Write any two library functions of string.h.

Answer:

Two functions of string.h are strlen() and strcpy().

Question 15.

Mention the function which can convert upper case letters to lower case letters.

Answer:

The function tolower() converts an uppercase letter to a lower case letter.

Question 16.

Give the name of ctype.h member function which can convert lower case letter to uppercase letters.

Answer:

The function toupper() converts lower case letters to upper case letters.

Question 17.

What is the output of ispunct (‘@’)?

Answer:

The output of ispunct (‘@’) is 1 means it is TRUE.

Question 18.

What is the output of toascii (‘A’)?

Answer:

The output of toascii (‘A’) is 65.

Question 19.

What is the output of isdigit (‘3’)?

Answer:

The output of isdigit (‘3’) is 1 means it is TRUE.

Question 20.

What is the use of isalnum() function?

Answer:

It returns True if the argument is a digit from 0 through 9 or an alphabetic character (either uppercase or lowercase) otherwise returns False.

Question 21.

What is the output of sqrt(25)?

Answer:

Theoutputofsqrt(25) is 5.

Question 22.

What is the output of ceil(25.456) function?

Answer:

The ceil() function gives the smallest integer not less than the given value.

Question 23.

What is the use of expO function?

Answer:

exp() is a mathematical function that returns the natural logarithm base “e” raised to the argument’s power.

Question 24.

What is the function of cos()?

Answer:

cos() is a mathematical function that returns the cosine value for the given radians.

Question 25.

What is the function of pow()?

Answer:

pow() is a mathematical function that returns the “base” value raised to the “exp” power.

Question 26.

How is pow() function used? Give an example.

Answer:

pow(2, 4) gives the result 16 as the output, ie. 2 raised to the power of 4.

Question 27.

Name the function that gives the square root of a number.

Answer:

The function sqrt() gives the square root of a number.

Question 28.

When is mathematical function “fabs()” used in C++?

Answer:

The fabs() mathematical function is used to return the absolute value of the given argument.

Question 29.

What is the function of sin()?

Answer:

The sin() is a mathematical function that returns sine value for the given argument represented in radians.

Question 30.

Define string.

Answer:

The term string is defined as a character array, terminated with a null character [‘\0’].

Question 31.

What is null in string?

Answer:

The null in a string is the control character ‘\0’ that indicates the end of the string.

Question 32.

How is a string declared?

Answer:

The declaration of string is char arrayname[size];

Question 33.

Give one example of string initialization.

Answer:

char name[50]=” Bharath”;

Question 34.

Name the function used for string input.

Answer:

The function getline() is used to read a string.

Question 35.

How is input terminated in getline()?

Answer:

The input gets terminated when a newline character is entered or when the length of the entered characters is equal to the size of the character array.

Question 36.

Name the function used for string output.

Answer:

The function write() is used for string output.

Question 37.

What is a null string?

Answer:

When the length of the string is 0 it is called a null string.

Question 38.

Name the function used to find the length of a string.

Answer:

The strlen() function is used to find the length of a string.

Question 39.

Name the function used to combine the two strings.

Answer:

The strcat() function is used to combine the two strings.

Question 40.

Give the function of strcpy().

Answer:

The strcpy() is used to copy a string into another string.

Question 41.

Name the function used to compare two strings.

Answer:

The function strcmp() is used to compare two strings.

Question 42.

What is the function of strrev()?

Answer:

The strrev() function is used to reverse the string.

Question 43.

Give the function of get()?

Answer:

It reads a single character from the stream.

Question 44.

Give the function of put()?

Answer:

It writes a single character into the stream.

Question 45.

Give the usage of get().

Answer:

char c;

c = cin.get();

Question 46.

Name any one function to generate a pseudo-random number.

Answer:

Rand() is the function used to generate a pseudo-random number.

### 1st PUC Computer Science Functions Two/Three Marks Questions and Answers

Question 1.

What are predefined functions? Name any two predefined functions.

Answer:

Predefined functions are functions that are built into the C++ Language to perform some standard operations. The functions that are stand-alone are used for general purposes and that are not dependant on any classes are stored in the Standard Function Library.

List of basic types of standard library functions:

- I/O Functions.
- String and Character Functions

Question 2.

Mention any two useful properties of functions.

Answer:

- It performs some well-defined task, which will be useful to other parts of the program.
- It might be useful to other programs as well; that is, we might be able to reuse it (and without having to rewrite it).

Question 3.

Explain any two advantages of functions.

Answer:

1. Modularity:

Functions facilitate the modular programming technique, i.e., functions can be used to divide a large bulky program into functionally independent modules or subprograms that can be tested, debugged and maintained independently.

2. Universal use:

User-defined functions can be made part of a library, in turn, which facilitates the usage of that function across other ‘C’ programs.

Question 4.

Give the usage of the mathematical function pow().

Answer:

pow() is a mathematical function that returns the “base” value raised to the “exp” power.

Syntax to find power of a number:

float pow (float base, float exp);

Question 5.

Give the usage of the mathematical function sqrt()

Answer:

sqrt() is a mathematical function that returns the square root of the given “num” value.

Syntax to find the Square root:

float sqrt( float num);

Question 6.

Give the usage of the mathematical function exp().

Answer:

exp() is a mathematical function that returns the natural logarithm base “e” raised to the argument’s power.

Syntax to find exponential value:

float exp( float num);

Question 7.

What are character functions? Name the header file for character functions.

Answer:

Character functions are functions used for the process of a character enclosed in single quotes. The ctype.h is the header for character functions.

Question 8.

Name the two types of character functions.

Answer:

Character functions are classified as

- Classification functions and
- Conversion functions

Question 9.

Give the difference between classification function and conversion function.

Answer:

1. Classification function:

name begins with the prefix is, and is used to determine the type of character.

2. Conversion function:

name begins with the prefix to and is used to determine the type of character.

Question 10.

Give the usage of isalpha().

Answer:

It returns a non-zero value if the argument is a A-Z or a-z letters. Otherwise it neutrons a zero.

For example, isalpha(‘a’) output: true-neutrons 1

isalpha(‘5’) output: false- neutrons 0.

Question 11.

Compare islower() and isupper() character functions with examples.

Answer:

islower(‘a’) output: true islower(‘A’) output: false

isupper(‘a’) output: false isupper(‘A’) output: false

Question 12.

Compare strcmp() and strcmpi().

Answer:

strcmp(“PUC”, “PUC”) output: negative integer. – This is case sensitive and compare two strings.

Strcmpi(“PUC”, “PUC”) output: 0 (zero). – This is not case sensitive and strings

Question 13.

What are the different outputs generated by strcmp() function?

Answer:

The strcmp() function generates any one of the following outputs. Zero if both the strings are the same,

- Positive integer if the first string is greater than the second string.
- Negative integer if the first string is lesser than the second string.

Question 14.

Give the usage of strcat() function with an example.

Answer:

Strcat() character function is used to combine two strings.

The syntax is strcat(target_string, source_string);

Question 15.

Give the usage of any two functions that generate pseudo-random numbers.

Answer:

int rand (void);

Returns a pseudo-random integral number in the range between 0 and RAND_MAX.

void srand (unsigned int seed);

The pseudo-random number generator is initialized using the argument passed as seed.

### 1st PUC Computer Science Functions Five Marks Questions and Answers

Question 1.

Explain any five functions of <math.h> header.

Answer:

1. a cos() function:

It is a mathematical function that returns the arc cosine value for the given argument. The argument value must be in the range -1 to 1, otherwise, a domain error is triggered.

Syntax : float acos ( float arg);

2. cos() function:

It is a mathematical function that returns the cosine Value for the given radians.

Syntax: float cos( float arg );

3. ceil() function:

It is a mathematical function that returns the smallest integer value that is not less than the specified number. In other words, it returns the Round-Up value of a given number.

Syntax: float ceil( float num);

4. exp() function:

It is a mathematical function that returns the natural logarithm base “e” raised to the power equal to the argument.

Syntax: float exp( float num);

5. sqrt() function:

It is a mathematical function that returns the square root of the given “num” value.

Syntax: float sqrt( float num);

Question 2.

Write a short note on character functions.

Answer:

1. isalpha():

Returns a non-zero value if the argument is a A-Z or a-z letters.

For example, isalpha(‘a’) output: 1 means true isalpha(‘5’) output: 0 means false

2. islower():

Returns a non-zero value if argument is a small case alphabet.

For example, islower(‘a’) output: 1 islower(‘A’) output: 0

3. isupper():

it is a character function that returns a non-zero value if argument is an upper case alphabet.

For example, isupper(‘a’) output: 0 isupper(‘A’) output: 1

4. tolower( ):

It converts the capital alphabet character to lower case.

For example, tolower(‘A’) output: a

5. toupper( ):

It converts the lower case alphabet character to upper case.

For example, toupper(‘a’) output: A

Question 3.

Write a note on string handling functions.

Answer:

1. strlen( ) function:

It calculates the length of the given string and returns the number of characters in a string. The length does not include the NULL character.

Syntax: integer variable = strlen(string);

2. Strcpy( ) function:

It copies the content of one string to another. The strcpy( ) function works almost like a string assignment operator.

Syntax: strcpy(<stringl>,<string2>);

When strcpy( ) function is executed, it copies string2 to string1, String2 can be a character array or a character constant.

3. strcat() function:

It adds the characters of one string to the end of another string. It is used to join two strings together.

Syntax: strcat(string1, string2);

The contents of string2 are added attend in string 1, where string 1 is the target string and string 2 is the source string.

4. strrev() function:

It reverses the characters in a string.

Syntax: strrev(string);

eg: strrev(“ABCD”);

Output: DCBA

5. strlwr() function:

converts all characters in a string to lower (small letters) case.

Syntax: strlwr(string);

eg: strlwr(“BHARATH”)

Output: bharath.

Question 4.

Explain getline() function with a suitable example.

Answer:

getline() function is used to read the whole line of text that ends with a newline character.

Here getline() function is invoked for reading character input into the variable line. When we type \n, the reading is terminated or size-1 characters are read. The newline is accepted but converted to null character.

#include <iostream>

int main()

{

char city [50];

cout<< “City Name :“;

cin.getline(city, 50);

cout<< “City Name : “<< city << end1;

return 0;

}

Question 5.

Write a note on manipulators.

Answer:

Manipulator functions: Manipulator functions are special stream functions that change certain characteristics of the input and output. They are useful in the formatting of input and output streams.

Following are a few standard manipulators normally used in the stream classes:

- end1
- setbase
- setw
- setfill
- setprecision

1. end1:

The end1 is an output manipulator to generate a carriage return or line feed character. The end1 may be used several times in a C++ statement.

For example,

cout<< “ a “ << end1 << “b” << end1;

A program to display a message on two lines using the end1 manipulator and the corresponding output is given below.

2. setbase():

The setbase() manipulator is used to convert the base of one numeric value into another base. Dec, hex, and oct are the common base converters in C++.

3. setw():

The setw ( ) stands for the set width. The setw ( ) manipulator is used to specify the minimum number of character positions on the output field a variable will consume. The general format of the setw manipulator function is setw( int w ).

4. setfill():

The setfill ( ) manipulator function is used to specify a different character to fill the unused field width of the value.

The general syntax of the setfill () manipulator is setfill( char f)

which changes the fill character to f. The default fill character is a space.

5. setprccision():

The setprecision () is used to control the number of digits of an output stream display of a floating-point value.

The general syntax of the setprecision () manipulator is

setprecision (int p)

which sets the precision for floating-point insertions to p. The default precision is 6.