+1 and +2 Computer Science

Previous Question paper answered , and Questions from Textbook

+1 and +2 Computer Application

Previous Question paper answered , and Questions from Textbook

Diploma in Computer Application (DCA)

Previous Question paper answered , and Questions from Textbook.

Master of Computer Applications (MCA)

Previous Question paper answered , and Questions from Textbook

True IQ Computer Online Academy

Contact : +91 9036433020 , mail2trueiq@gmail.com

Pageviews

Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

Tuesday, 28 July 2020

+1].10 - Functions Solved Questions Chapter Wise (Text book Questions )fu



                                       PLUS ONE COMPUTER SCIENCE


                           First year computer science Solved Questions and answers

                                                            10 - Functions

1. What is modular programming?

   Ans. 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. 

2. What is a function in C++?

    Ans.  function is a named unit of statements in a program to perform a specific task as part of the solution. It is not necessary that all the functions require some parameters and all of them return some value. C++ provides a rich collection of functions ready to use for various tasks. The functions clrscr(), getch(), sqrt(), etc. are some of them.

3. Name the header file required for using character functions.

   Ans. cctype

4. Name the function that displays the subsequent data in the specified width. 

5. Pick the odd one out and give reason: (a) strlen() (b) itoa() (c) strcpy() (d) strcat()

 Ans. b

6. Identify the most essential function in C++ programs.

     Ans. main( )

 7. List the three elements of a function header.

    Ans. The three elements of a function header are return data type,function name and function argument list

 8. What is function prototype?

  Ans.  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. This information is available in the function header and hence the header alone will be written as a statement before the function call.
The following is the format: data_type function_name(argument_list);

 9. Which component is used for data transfer from calling function to called function?

   Ans. Arguments or parameters

 10. What are the two parameter passing techniques used in C++?

    Ans. Call by value and Call by reference

11. If the prototype of a function is given immediately after the preprocessor directive, its scope will be _______.

    Ans. Global

 12. What is recursion?

    Ans. Usually a function is called by another function. Now let us define a function that calls itself. The process of calling a function by itself is known as recursion and the function is known as recursive function

 13. What is the scope of predefined functions in C++?

  Ans. Global

 14. The arguments of a function have ______ scope.

   Ans. Local

15. How do top down design and bottom up design differ in programming?

 16. What is a function in C++? 

17. The ability of a function to call itself is _________.

    Ans. Recursion

18. Write down the role of header files in C++ programs.

    Ans. hpp extension or no extension at all. The primary purpose of a header file is to propagate declarations to code filesHeader files allow us to put declarations in one location and then import them wherever we need them. This can save a lot of typing in multi-file programs

19. When will you use void data type for function definition?

  Ans. Many programming languages need a data type to define the lack of return value to indicate that nothing is being returned. The void data type is typically used in the definition and prototyping of functions to indicate that either nothing is being passed in and/or nothing is being returned.

20. Distinguish between actual parameters and formal parameters.

    Ans. 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 arguments are known as formal arguments. The constants, variables or expressions used in the function call are known as actual (original) arguments. If variables are used in function prototype, they are known as dummy arguments.

 21. Construct the function prototypes for the following functions (a) Total() takes two double arguments and returns a double (b) Math() takes no arguments and has no return value 

22. Distinguish between exit() function and return statement.

 23. Discuss the scope of global and local variables with examples.

        #include <iostream>
using namespace std;
int cb; //global variable
void test()//global function since defined above other functions
{
int cube(int n); //It is a local function
cb=cube(x); //Invalid call. x is local to main()
cout<<cb;
}
int main() // beginning of main() function
{
int x=5; //local variable
test(); //valid call since test() is a global function
cb=cube(x); //Invalid call. cube() is local to test()
cout<<cb;
}
int cube(int n)//Argument n is local variable
{
int val= n*n*n; //val is local variable
return val;
}
 ref page no.303.10th chapter .

 24. Distinguish between Call-by-value method and Call-by-reference method used for function calls.

             Call by Value Method                                                    Call by Reference Method

Ordinary variables are used as formal parameters.               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
• 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.

 25. In C++, function can be invoked without specifying all its arguments. How? 

26. Write down the process involved in recursion.

        A typical recursive function will be as follows:

           int function1()

          {
                  ............ ............

             int n = function1(); //calling itself

          ............ ............
 }

          In recursive functions, the function is usually called based on some condition only.
int factorial(int n)
 {
 if((n==1)||(n==0))
 return 1;
 else if (n>1)
 return (n * factorial(n-1));
else
return 0;
}
f = factorial(3);
return (3 * factorial(3-1));
In order to find 3 * factorial(2), it needs to find the value of factorial(2).
return (2 * factorial(2-1));
In order to find this returning value, it calls the factorial() function again with 1 as the value of the parameter n. Now, the condition in the if statement becomes true, hence it will return 1 as the value of the function call factorial(1);
return 2;
: return 3 * 2;
Now the value 6 is returned as the value of the function call factorial(3);. 

27. Look at the following functions:

 int sum(int a,int b=0,int c=0) 


return (a + b + c);

 } 

(a) What is the speciality of the function regarding the parameter list? 

(b) Give the outputs of the following function calls by explaining its working and give reason if the function call is wrong.

i) cout<<sum(1, 2, 3); 

(ii) cout<<sum(5, 2);

(iii) cout<<sum();

 (iv) cout<<sum(0);
28. The prototype of a function is: int fun(int, int);
 The following function calls are invalid. Give reason for each.

 (a) fun(2,4); 

(b) cout<>fun(a, b);

 (c) val=fun(2.5, 3.3);

 (e) z=fun(3);


Thursday, 14 November 2019

+1] 10 - Functions - Previous Questions Chapter wise



                 PLUS ONE COMPUTER SCIENCE

            First Year Computer Science Previous Questions Chapter wise ..


                                            Chapter 10. Functions


 1. Which one of the following is NOT equal to others?

a) pow(64, 0.5)

 b) pow(2,3)

 c) sqrt(64)

d) pow(3,2)

  Ans. a

2. Write a recursive C++ function that returns sum of the first n natural numbers.

         
  1. #include<iostream>
  2. using namespace std;
  3. int add(int n);
  4. int main()
  5. {
  6. int n;
  7. cout << "Enter a positive integer: ";
  8. cin >> n;
  9. cout << "Sum = " << add(n);
  10. return 0;
  11. }
  12. int add(int n)
  13. {
  14. if(n != 0)
  15. return n + add(n - 1);
  16. return 0;
  17. }
Output
Enter an positive integer: 10
Sum = 55

3. List any three string functions in C++ and specify the value returned by them.

      strlen( )
         strlen(string) To find the length of a string.

    strcpy( )

    strcpy(string1, string2) To copy one string into another

         strcmp( )

      strcmp(string1, string2) To compare two strings.

     Returns 0 if string1 and string2 are same.

    Returns a –ve value if string1 is alphabetically lower than string2.

   Returns a +ve value if string1 is alphabetically higher than string2.

4. Name the built in function to check whether a character is alphanumeric or not.

        Ans. isalpha( )

5. Read the function definition given below. Predict the output, if the function is called as
convert(7);

void convert(int n)
{
if (n>1)
convert(n/2);
cout<<n%2;
}

6. Explain the difference between call-by-value method and call-by-reference method with
the help of examples.

      Call by value

Call by value method copies the value of an argument into the formal parameter of that function. Therefore, changes made to the parameter of the main function do not affect the argument.
In this parameter passing method, values of actual parameters are copied to function's formal parameters, and the parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of the caller.

   void change(int n)

 {

n = n + 1;

 cout << "n = " << n << '\n';

 }

int main()

{

int x = 20;

 change(x);

 cout << "x = " << x;

}

        The following will be the output of the above code:

 n = 21

 x = 20
         Call by reference

Call by reference method copies the address of an argument into the formal parameter. In this method, the address is used to access the actual argument used in the function call. It means that changes made in the parameter alter the passing argument.
In this method, the memory allocation is the same as the actual parameters. All the operation in the function are performed on the value stored at the address of the actual parameter, and the modified value will be stored at the same address.

void change(int & n)

 {

 n = n + 1; 

cout << "n = " << n << '\n';

 }

 int main() 

{

 int x=20;

 change(x);

 cout << "x = " << x;

 } 

   Output

       n = 21 
       x = 21

7. Arguments used in call statement are formal arguments. State true or false.

      Ans, True.

          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 arguments are known as formal arguments.

8. Differentiate the string functions strcmp() and strcmpi().

      The strcmpi() function is same as that of the strcmp() function but the only difference is that strcmpi() function is not case sensitive and on the other hand strcmp() function is the case sensitive

 strcmp(string1, string2) To compare two strings.

     Returns 0 if string1 and string2 are same.

    Returns a –ve value if string1 is alphabetically lower than string2.

   Returns a +ve value if string1 is alphabetically higher than string2.

  strcmpi( string1,string2) To compare two strings.If same Returns 0 otherwise 1.

9. Differentiate break and continue statements in C++.

   
Both “break” and “continue” are the ‘jump’ statements, that transfer control of the program to another part of the program. The main difference between break and continue is that break is used for immediate termination of loop. On the other hand, ‘continue’ terminate the current iteration and resumes the control to the next iteration of the loop.Break and continue

The break statement is primarily used as the exit statement, which helps in escaping from the current block or loop. Conversely, the continue statement helps in jumping from the current loop iteration to the next loop. C++ supports four jump statements, namely ‘return’, ‘goto’, ‘break’ and ‘continue’.

10. Explain recursive function with the help of a suitable example.

             Recursion is the process of calling a function by itself and the function is known as recursive function.


12. Suggest most suitable built-in function in C++ to perform the following tasks:

(a) To find the answer for 5^3
.
(b) To find the number of characters in the string “KERALA”.

(c) To get back the number 10 if the argument is 100.

  Ans. a) pow(5,3);

           b) n= strlen("KERALA");

           c) sqrt(100);

13. A function can call itself for many times and return a result.

(a) What is the name given to such a function?

(b) Write a function definition of the above type to find the sum of natural numbers
from 1 to N. (Hint: If the value of N is 5, the answer will be 1+2+3+4+5=15)

       Ans. a) Recursive function

   Ref Qn no.2

14. Explain two types of variable according to its scope and life.

                  Local variable

          • 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.

                   Global variable

 • Declared within a function or a block of statements and defined after the calling function.

• Accessible only within that function or the block.

 • Memory is allocated just before the execution of the program and freed when the program stops execution.

15. Write a C++ Program to display the simple interest using function.

    #include<iostream>
using namespace std;

class Simple_interest {
public:
    float si, amount, r;

    void calculate(float amt, float rate) {
        amount = amt;
        r = rate;
    }

    void calculate(float time) {
        si = (amount * r * time) / 100;

        cout << "\nSimple interest is : " << si;
    }
};

int main() {

    float amt, rate, time;

    cout << "Enter amount : ";
    cin>>amt;
    cout << "\nEnter rate : ";
    cin>>rate;
    cout << "\nEnter time : ";
    cin>>time;

    Simple_interest obj;

    obj.calculate(amt, rate);
    obj.calculate(time);

    return 0;
}

Output:

Enter amount : 200
Enter rate : 5
Enter time : 3
Simple interest is : 30
16. The function which calls itself is called a _______.

      Ans. Recursion

17. Construct the function prototype for the following functions:

(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 return a float type
value.

18. Name the different methods used for passing arguments to a function. Write the
difference between them with examples.

           Ref.Qn no.6

Friday, 12 July 2019

+2].3. Functions - Solved questions from text book


                                                              Chapter 3. Functions
(+2 Computer Application , Text book Questions and Answers)


1.  What is modular programming?

     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. Each sub task will be considered as a module and we write programs for each module. The process of breaking large programs into smaller sub programs is called modularization.

2. What is meant by a function in C++?

    Function is a named unit of statements in a program to perform a specific task as part of the solution.  There are two types of functions in C++.

 a). Predefined functions or Built in Functions : Functions that are already written , compiled and their definitions are grouped and stored in header files.
  Eg. sqrt(),  toupper()

b). User Defined Functions: Functions that are written by the user to carry on some task.
  Eg.  main( )

3.  Name the header file required for using character functions.

  ctype

4.  Predict the output of cout<<sqrt(49).

output
   7

5. Pick the odd one out and give reason:

a. strlen()

b. pow()

c. strcpy()

d. strcat( )

Ans b.

6.   Name the header file needed for the function pow( ). 

    math

7. Predict the output when we compare the strings "HELLO" and "hello" using the function strcmpi(). 

Returns strings are same.Because  the uppercase and lowercase letters will be treated as same during the comparison.

8.   What will be output of the statement
 cout<<strlen("smoking kills"); ? 

output

 13

9. Which function converts the alphabet 'P' to 'p'? 

    tolower( )

10.  Identify the name of the function which tests whether the argument is an aphabet or number.

      isalpha( )

11. Identify  the most essential function in C++ programs?

      function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.

12.  List the three elements of a function header.

data_type function_name(argument_list)
 {
 statements in the body;
 }

The data_type is any valid data type of C++. The function_name is a userdefined word (identifier). The argument_list, which is optional, is a list of parameters, i.e. a list of variables preceded by data types and separated by commas. The body comprises of C++ statements required to perform the task assigned to the function.

13.  What is function prototype? 
         
        . 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. 
data_type function_name(argument_list);

14.  Which component is used for data transfer from calling function to called function? 

          Parameters or Arguments

 15. What are the two parameter passing techniques used in C++? 

         Call by value and Call by reference.

16. Identify the name of the function call where & symbol is used along with formal arguments.

      Reference variable

17.  The built-in function to find the length of a string is  _________. 

      strlen( )

18.  Write down the role of header files in C++ programs.

       In C++ the header file contain the group of function declarations that may need for the proper working of a program. So there is no need to add the function definitions of all the functions separately. 
Eg.  cmath  for mathematical functions. 

19.  When will you use void data type for function definition? 

        In most of the functions, return is placed at the end of the function. The functions defined with void data type may not have a return statement within the body. But if we use return statement, we cannot provide any value to it.Void means nothing to return datatype.

20.  Distinguish between actual parameters and formal parameters. 

         The arguments given at the calling of a function is called actual (original) arguments or actual parameters since they are the actual data passed to the function for processing.   
 Eg.  a = SimpleInterest(x, y, z);
 The arguments used in the function definition are known as formal parameter. 

 Eg.  float SimpleInterest(int P, int N, float R) 

21.  Construct the function prototypes for the following functions

 a. Total() takes two double arguments and returns a double
 b. Math() takes no arguments and has no return value

Ans.  a).      double Total (double  ,  double );
  b).      void Math(); 

22.   Discuss the scope of global and local variables with examples.

        A local variable  is one that declared within a function or a block of statements. It is available only within that function or block.
 If a variable  is declared before the main() function t is the function can be used at any place in the program. This scope is known as global scope. Also declaration of a variable outside  all functions also have global scope.

23.  How a local function differs from a global function?

 A function which is declared inside the function body of another function is called   a  local function as it can be used within that function only. A function declared outside the function body of any other function is called a   global function and its scope is the entire program



eg. #include <iostream>

using namespace std;

int cb; //global variable

void test() //global function

{

int cube(int n); // local function

cb=cube(x); //Invalid call. x is local to main()

cout<<cb;

}

int main()

{

int x=5; //local variable

test(); //valid call since test() is a global function

cb=cube(x); //Invalid call. cube() is local to test()

cout<<cb;

}

int cube(int n) //Argument n is local variable

{

int val= n*n*n; //val is local variable

return val;

} 24.  Identify the built-in functions needed for the following cases



a. To convert the letter 'c' to 'C'

b. To check whether a given character is alphabet or not.

c. To combine strings "comp" and "ter" to make "computer"

d. To find the square root of 25

e. To return the number 10 from -10




a. To convert the letter 'c' to 'C'

Ans: toupper()

b. To check whether a given character is alphabet or not.

Ans: isalpha()

c. To combine strings "comp" and "ter" to make "computer"

Ans: strcat()

d. To find the square root of 25

Ans: sqrt()

e. To return the number 10 from -10

Ans: ab

25.  Look at the following functions:

int sum(int a,int b=0,int c=0) 

return (a + b + c); 

a. What is the speciality of the function regarding the parameter list?
 b. Give the outputs of the following function calls by explaining its working and give reason if the function call is wrong. 

i. cout << sum (1, 2, 3); 
ii. cout << sum(5, 2); 
iii. cout << sum(); 
iv. cout << sum(0); 

26.  The prototype of a function is: int fun(int, int); 

The following function calls are invalid. Give reason for each. 
a. fun("hello",4);
 b. cout<<fun(); 
c. val = fun(2.5, 3.3); 
d. cin>>fun(a, b); 
e. z=fun(3); 

Ans:  a). In Prototype declaration formal variables are integers but actual parameters contains string type.
  b). No actual parameters. 
 c). Actual parameters are not integer type.  
d). calling a function by cin is error. 
 e). Not enough actual parameters. 

27.  Consider the following program and predict the output if the radius is 5. Also write the reason for the output.

#include<iostream>
using namespace std;
float area(float &);
int main()
{
float r, ans;
cout<<"Enter radius :";
cin>>r;
ans = area(r);


cout<<area;
cout<<r;
return 0;
}
float area(float &p)
{
float q;
q = 3.14 * p * p;
 p++;
return q;
}

         output

 Enter the radius: 78.5
 







Tuesday, 9 July 2019

+2 ] 3.Functions.- Previous questions Chapter wise


PLUS TWO COMPUTER APPLICATION

Second Year Computer Application(Commerce) Previous Questions Chapter wise ..

Chapter 3  Function



1.  . ………. Function is used to check whether a character is alphanumeric.

  • (a) isdigit( )
  •  (b) isalnum( ) 
  • (c) isupper( )
  •  (d) islower( )
  Ans. b

2.  Consider the following code : 

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 
Ans.  a

3. Explain any three string function with example  

          i. strlen() : This function is used to find the length of a string(Number of characters ). Its return value is an integer.

 n = strlen(str); 

ii. strcpy() : This function is used to copy one string into another. 

 Syntax : strcpy(string1, string2);
Eg: strcpy(s1,s2);

 iii. strcat() : This function is used to append one string to another string. The length of the resultant string is the total length of the two strings.

 Syntax: strcat(string1, string2);
 Eg. strcat(s1,s2); 

4. . Explain any three stream functions for I/O operation

         Stream functions allow a stream of bytes (data) to flow between memory and objects like Key Board or Monitor A.

 Input functions 

a). get() : It can accept a single character or multiple characters (string) through the keyboard. To accept a string, an array name and size are to be given as arguments.

 Eg cin.get(str,10) 

b). getline() : It accepts a string through the keyboard. The delimiter will be Enter key, the number of characters or a specified character.
 Eg. cin.getline(str,len); 
 cin.getline(str,len,ch); 

 Output functions

 a). put() : It is used to display a character constant or the content of a character variable given as argument. 
 Eg. cout.put('B'); cout.put(65); 

b). write() : This function displays the string contained in the argument.
 Eg. cout.write(str,10); 8

5. . Write a code to do the following : 
(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

#include <iostream>
using namespace std;
int largest(int a, int b)
 {
 Int large;

 if(a > b)
 large = a;
 else
 large = b;
 return large;
 }
int main()
{
 int n,m, p;
 cout<<"Enter the Number for checking :";
 cin>>n<<m;
 p = largest(n,m);
 cout<< “ The largest number is : “<<p<<endl; 
return0;
}

6.  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++

#include <iostream>
using namespace std;
int average(float a, float b, float c)
 {
 float av;
 av=(a + b + c) / 3;
 return av;
 }
int main()
{
 float n, m, o, p;
 cout<<"Enter 3 Number for finding Average :";
 cin>>n<<m<<o;
 p = largest(n,m,o);
 cout<< “ The average is : “<<p<<endl; 
return0;
}

7.  ---------------- function used to append one string to another string in C++

Ans. strcat( )

8. Define the term  function  in C++

          Function is a named unit of statements in a program to perform a specific task as part of the solution.  There are two types of functions in C++.

 a). Predefined functions or Built in Functions : Functions that are already written , compiled and their definitions are grouped and stored in header files.
 Eg. sqrt(),  toupper()

b). User Defined Functions: Functions that are written by the user to carry on some task'
 eg. main( )

9. In C++ declare a function prototye "Display" with two arguments?

 int add(int a, int b);

It is not necessary to define prototype if user-defined function exists before main() function.

10. Define the function "calcsum( )" with return value as sum of three integer variables?

11. What are the difference between Call by reference and Call by value of function calling in   C++? 
Call by Value Method                                              Call by Reference Method
Ordinary variables are used as formal                   Reference variables are used as formal parameters. 
 parameters. 

Actual parameters may be constants,                    Actual parameters will be variables only. 
 variables or expressions.

The changes made in the formal                         The changes made in the formal arguments do  
arguments do not reflect in actual                        reflect in actual arguments.
arguments. 

Exclusive memory allocation is required            Memory of actual arguments is shared by formal 
for the formal arguments                                       arguments.    
     
12. Differentiate between global and local variables?

              If a variable  is declared before the main() function t is the function can be used at any place in the program. This scope is known as global scope. Also declaration of a variable outside  all functions also have global scope. 
           
                  A local variable  is one that declared within a function or a block of statements. It is available only within that function or block.