Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

351.

Define function.

Answer»

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

352.

What is the function of pow()?

Answer»

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

353.

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.

354.

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

Answer»

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

355.

What is Parameter and list its types?

Answer»

Arguments or parameters are the means to pass values from the calling function to the called function. The variables used in the function definition as parameters are known as formal parameters. The constants, variables or expressions used in the function call are known as actual parameters

Types : Default arguments and Constant Arguments.

356.

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.

357.

What are docstrings ? How are they useful ?

Answer»

A docstring is just a regular python triple- quoted string that is the first thing in a function body or a module or a class. When executing a functionbody the docstring does not do anything like comments, but Python stores it as part of the function documen-tation. This documentation can later be displayed using help)) function. So even though docstrings appear like comments but these are different from comments.

358.

How are functions classified?

Answer»

Functions are classified as,

  • Library functions pre-defined functions
  • User-defined functions.
359.

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.

360.

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.

361.

Mention any two useful properties of functions.

Answer»

1. It performs some well-defined task, which will be useful to other parts of the program.

2. It might be useful to other programs as well; that is, we might be able to reuse it (and without having to rewrite it).

362.

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:

1. I/O Functions.

2. String and Character Functions

363.

Differentiate the outputs of the following C++ statements and also give reason 1. cout<<strcmp("world", "WORLD");2. cout<<strcmpi("world", "WORLD");

Answer»

1. >0 Here first string “world” is greater than “WORLD”. 

2. It prints 0. Because strcmpi is same as strcmp() but it is not case sensitive. That means uppercase and lowercase are treated as same.

364.

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

Answer»

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

365.

......... keyword is used to give a value back to the called function.

Answer»

Return keyword is used to give a value back to the called function.

366.

A function is defined as follows int sum (int a, int b=2) {return (a+b);}Check whether each of the following function calls is correct or wrong, Justify your answer1. cout<<sum(2, 3);2. cout<<sum(2);3. cout<<sum();

Answer»

Here the function is declared with one optional argument. So the function call with minimum one argument is compulsory. 

1. 0 It is valid. Here a becomes 2 and b becomes 3. 

2. It is also valid . Here a becomes 2 and b takes the default value 2. 

3. It is not a valid call. One argument is compulsory.

367.

One of the following is not necessary in the function declaration. What is it? (a) name of the function (b) return type (c) number and type of arguments(d) name of the parameters

Answer»

(d) name of the parameters.

368.

Considerthe following declaration int sum(int a , int b){return a+b;}From the following which is the valid function call. (a) n=sum(10) (b) n=sum(10, 20) (c) n=sum(10, 20, 30)(d) n=sum()

Answer»

(b) n=sum(10, 20)

369.

A function declaration is also called ........

Answer»

A function declaration is also called prototype.

370.

The parameter used to call a function is called .........

Answer»

actual parameter.

371.

Consider the following function declaration int sum(int a, int b) { Body } Here the arguments are passed by ......

Answer»

call by reference method.

372.

.......... parameter is used when the function call does not supply a value for parameters.

Answer»

Default parameter is used when the function call does not supply a value for parameters.

373.

consider the following declaration int main() { int x;–} Here x is a ........ variable.

Answer»

Here x is a local variable.

374.

Consider the following declaration int x; int main(){-}Here x is a ....... variable.

Answer»

Here x is a global variable.

375.

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(<string1>,<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.

376.

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.

377.

The ability to access a variable or a function from some where in a program is called ...........

Answer»

The ability to access a variable or a function from some where in a program is called scope.

378.

A variable ora function declared within a function is have ..........scope.

Answer»

A variable ora function declared within a function is have local scope.

379.

Explain Inverse Function?

Answer»

Inverse function: 

If f: A → B is a one-one onto function (bijection), then for every x ∈ A, we have a y ∈ B, such that y = f (x). A new function f –1 from B to A which associates each element y ∈ B to its pre-image x = f –1(y) ∈ A can be defined and this function f –1 is called the inverse of function f. 

Thus, f: A B is a bijection and f: a b, then f –1: b a.

Method to find f –1 if f is invertible 

Step 1. Put y = f (x). 

Step 2. Solve y = f (x) and express x in terms of y. 

Step 3. The value of x obtained in step 2 gives f –1(y) 

Step 4. Replace y by x in f –1(y) to obtain f –1(x).

380.

Find the inverse of the function f(x) = 5x – 8, where x ∈ R.

Answer»

Given:

f (x) = 5x – 8, x ∈ R.

For a function to be invertible, it should be a bijection, i.e., one-one onto function

For all x1, x2 ∈ R, f (x1) = f (x2) ⇒ 5x1 – 8 = 5x2 – 8 ⇒ x1 = x2 ⇒ f (x) is one-one. 

Let y = f (x) = 5x – 8 ⇒ 5x = y + 8 ⇒ x = \(\frac{y+8}{5}\)  

Thus, for all y ∈ R,  x = \(\frac{y+8}{5}\)  ∈ R 

⇒ f (x) is onto. 

f being one-one onto ⇒ f is invertible.

Let y = f (x) = 5x – 8 

⇒ x = \(\frac{y+8}{5}\) 

⇒ f –1(y) = \(\frac{y+8}{5}\)  

⇒ f –1(x) = \(\frac{x+8}{5}\)

381.

Define Even and odd Function?

Answer»

• If f (x) is a function of x such that f (– x) = f (x) for every x ∈ domain, then f (x) is an even function of x. 

Ex. 

(i) f (x) = cos x is an even function as f (– x) = cos (– x) = cos x = f (x) 

(ii) f (x) = x4 + 2 is an even function as f (– x) = (– x) 4 + 2 = x4 + 2 = f (x). 

• If f (x) is a function of x such that f (– x) = – f (x) for every x ∈ domain, then f (x) is an odd function of x. 

Ex. 

(i) f (x) = sin x is an odd function as f (– x) = sin (– x) = – sin x = – f (x) 

(ii) f (x) = x3 + 6x is an odd function as f (– x) = (– x) 3 + 6(– x) = – x3 – 6x 

= – (x3 + 6x) = – f (x) 

• Some functions as y = x– 5x + 3 are neither even nor odd.

382.

Define Constant Function?

Answer»

If f: A → B is such that for all x ∈ A f (x) = c (a constant value) then f is a constant function. 

Its domain is the set A and range is a singleton set containing a single value c. 

Ex. 

f: R → R such that f (x) = 5 for all x ∈ R is a constant function.

383.

Find the product of the identitly function by the reciprocal function.

Answer» Let `f:RtoR:f(x)=xandg:R-{0}toR:g(x)=(1)/(x)` be the identity function and the reciprocal function respectively.
Then, dom `(fg)="dom "(f)nn"dom "(g)=RnnR-{0}=R-{0}`
`:.(fg):Rto{0}toR:f(g)(x)=f(x).g(x)=(x""xx(1)/(x))=1`.
Hence, `(fg)(x)=1" for all "x""inR-{0}`.
384.

Find the sum and product of identity function and the modulus function.

Answer» Let `f:RtoR:f(x)=xandg:RtoR:g(x)=|x|` be the identity function and the modulus function respectively.
Then, dom `(fg)="dom "(f)nn"dom "(g)=RnnR=R`.
`(fg):RtoR:(fg)(x)=f(x).g(x)`.
Now, `(fg)(x)=f(x),g(x)=x.|x|`
`=x.{{:(x",when "xge0),(-x" ,when "lt0):}={{:(x^(2)",when "xge0),(-x^(2)",when "xlt0):}`
Hence, `(fg)(x)={{:(x^(2)",when "xge0),(-x^(2)",when "xlt0):}`.
385.

Check if the following relation are function:

Answer»

No.

Reason: An element of set A has been assigned more than one element from set B.

386.

Find the sum ofdifference of the identity function and the modulus function.

Answer» Let `f:RtoR:f(x)=x` be the identity function.
And, let `g:RtoR:g(x)=|x|` be the modulus function.
Then, dom (f) = R and dom (g)=R.
`:."dom "(f)nn"dom "(g)=RnnR=R`.
(i) dom `(f+g)="dom "(f)nn"dom "(g)=R`.
Now, `(f+g):RtoR` is given by
`(f+g)(x)=f(x)+g(x)`
`=x+|x|=x+{(x",when "xge0),(-x",when "xlt0):}`
`={{:(x+x",when "xge0),(x-x",when "lt0):}={{:(2x",when "xge0),(0",when "xlt0):}`
Hence, `(f+g)(x)={{:(2x",when "xge0),(0",when "xlt0):}`
(ii) dom `(f-g)="dom "(f)nn"dom "(g)=R`.
`:.(f-g)(x)=f(x)=g(x)`
`=x-|x|=x-{{:(x",when "xge0),(-x",when "xlt0):}`
`={{:(x-x",when "xge0),(x+x",when "xlt0):}={{:(0",when "xge0),(2x",when "xlt0):}`
`:.(f-g)(x)={{:(0",when "xge0),(2x",when "xlt0):}`
387.

Let `X={2,3,4,5}andY={7,9,11,13,15,17}`. Define a relation f from X to Y by: `f={(x,y):x""inX,yinYandy=2x+3}`. (i) Write in roster form. (ii) Find dom (f) and range (f). (iii) Show that f is a function from X to Y.

Answer» Here `X={2,3,4,5}andY=2x+3`.
Now, `x=2impliesy=(2xx2+3)=7`,
`x=3impliesy=(2xx3+3)=9`,
`x=4impliesy=(2xx4+3)=11`,
`x=5impliesy=(2xx5+3)=13`
(i) `:.f={(2,7),(3,9),(4,11),(5,13)}`.
(ii) Clearly , dom `(f)={2,3,4,5}` and range `(f)={7,9,11,13}subY`.
(iii) It is clear that no two distinct ordered pairs in f have the same first coordinate.
`:. f is function from X to Y.
388.

Check if the relation given by the equation represents y as function of x.x2 – y = 25

Answer»

x2 – y = 25

∴ y = x2 – 25

∴ For every value of x, there is a unique value of y.

∴ y is a function of x.

389.

Show that the function f : R → R : f(x) = 2x + 3 is invertible and find f-1.

Answer»

To Show: that f is invertible

To Find: Inverse of f

[NOTE: Any functions is invertible if and only if it is bijective functions (i.e. one-one and onto)]

one-one function: A function f : A B is said to be a one-one function or injective mapping if different

elements of A have different images in B. Thus for x1, x2 ∈ A & f(x1), f(x2) ∈ B, f(x1) = f(x2) ↔ x1= x2 or x≠ x2 ↔ f(x1) ≠ f(x2)

onto function: If range = co-domain then f(x) is onto functions.

So, We need to prove that the given function is one-one and onto.

Let x1, x2 ∈ R and f(x) = 2x+3.So f(x1) = f(x2) → 2x1+3 = 2x2+3 → x1=x2

So f(x1) = f(x2) ↔ x1= x2, f(x) is one-one

Given co-domain of f(x) is R.

Let y = f(x) = 2x+3 , So x = \(\frac{y-3}{2}\) [Range of f(x) = Domain of y]

So Domain of y is R(real no.) = Range of f(x)

Hence, Range of f(x) = co-domain of f(x) = R

So, f(x) is onto function

As it is bijective function. So it is invertible

Invers of f(x) is f-1(y) = \(\frac{y-3}{2}\)

390.

Check if the relation given by the equation represents y as function of x.x + y2 = 9

Answer»

x + y2 = 9

\(\therefore\) y2 = 9 - x

\(\therefore\) y = \(\pm\sqrt{9-x}\) 

\(\therefore\) For one value of x, there are two values of y.

\(\therefore\) y is not a function of x.

391.

Check if the following relation are function:

Answer»

No.

Reason: Not every element of set A has been assigned an image from set B.

392.

Check if the relation given by the equation represents y as function of x.2x + 3y = 12

Answer»

2x + 3y = 12

\(\therefore\)  y = \(\frac{12-2x}3\) 

∴ For every value of x, there is a unique value of y.

∴ y is a function of x.

393.

Check if the relation given by the equation represents y as function of x.2y + 10 = 0

Answer»

∴ y = -5

∴ For every value of x, there is a unique value of y.

∴ y is a function of x.

394.

Check if the relation given by the equation represents y as function of x.3x – 6 = 21

Answer»

3x – 6 = 21

∴ x = 9

∴ x = 9 represents a point on the X-axis.

There is no y involved in the equation. So the given equation does not represent a function.

395.

Let f: R → R and g: C → C be two functions defined as f(x) = x2 and g(x) = x2. Are they equal functions?

Answer»

Given as

f: R → R ∈ f(x) = x2 and g : R → R ∈ g(x) = x2

f is the defined from R to R, the domain of f = R.

g is the defined from C to C, the domain of g = C.

The two functions are equal only when the domain and co-domain of both the functions are equal.

In this case, the domain of f ≠ domain of g.

Thus, f and g are not equal functions.

396.

If f(m) = m2 – 3m + 1, findf(1/2)

Answer»

f(1/2) = (1/2)2 - 3(1/2) + 1

= 1/4 - 3/2 + 1

\(\frac{1-6+4}4\) = \(-\frac14\) 

397.

If f(m) = m2 – 3m + 1, find f(-3)

Answer»

f (-3) = (-3)2 – 3(-3) + 1

= 9 + 9 + 1

= 19

398.

If f(m) = m2 – 3m + 1, find  f(0)

Answer»

f(m) = m2 – 3m + 1

 f(0) = 02 – 3(0) + 1 = 1

399.

If f(m) = m2 – 3m + 1, findf(x + 1)

Answer»

f(x + 1) = (x + 1)2 – 3(x + 1) + 1

= x2 + 2x + 1 – 3x – 3 + 1

= x2 – x – 1

400.

If f(m) = m2 – 3m + 1, find f(-x)

Answer»

f(-x) = (-x)2 – 3(-x) + 1

= x2 + 3x + 1