+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

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

+1] 9 - String Handling and I/O Functions - Previous Questions Chapter wise



                          PLUS ONE COMPUTER SCIENCE

            First Year Computer Science Previous Questions Chapter wise ..

                           Chapter-9 String Handling and I/O Functions



1. Consider the following C++ program.

#include<iostream>

using namespace std;

int main()

{

char str[20];

cin>>str;

cout<<str;

}
What will be the output if we input the string “Vande Mataram”. Justify your answer.

2. What is the advantage of using gets() function in C++ program to input string data?
Explain with an example.

          The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space-separated strings.

3. a) Write the declaration statement for a variable ‘name’ in C++ to store a string of maximum length 30.
 b) Differentiate the statements cin>>name; and gets(name); for reading data to the variable ‘name’.

       
Ans. a)  char name[30];  

b). Usually the input device is the keyboard. cin is the instance of the class istream and is used to read input from the standard input device which is usually keyboard.The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keboard.

        A string can be input using the statement cin>>name; This statement can store a string without any white space (that is, only one word). If we want store strings containing white spaces (strings having more than one word) we can use gets() function.That is gets(name);


4.  Consider the following C++ statements:

 char word[20];

cin>>word;

 cout<<word;

 gets(word);

puts(word);

 If the string entered is “HAPPY NEW YEAR”, predict the output and justify your answer.

Ans. "HAPPY NEW YEAR"     17 characters including white spaces between the word,/0 is represent end of the string and 3 white spaces after the word .That is total 20 characters of the word.

5. a) my_name is a variable contains a string. Write two different C++ statements to display the string.     

b) …… function is used to copy a string to another variable.

Ans.

        b) strcpy( )

6. Read the following code:

char str[30];

 cin >> str;

cout << str;

If we give the input “Green Computing”, we get the output “Green”. Why is it so? How can you correct that?

   To change the 

Saturday 2 November 2019

+1] 8 - Arrays - Previous Questions Chapter wise



                                 PLUS ONE COMPUTER SCIENCE

            First Year Computer Science Previous Questions Chapter wise ..
                                                      
                                                          Chapter 8. Arrays


1. An array is declared as follows: int a[5] = {1, 2, 3, 4, 5}; What will be the value of a[2] + a[3]?

 a) 7

 b) 5

c) 6

d) 4

 Ans. a

2. Write C++ statements to double each element of the array int A[100].

 3. Consider an array A with the following elements sorted in ascending order. 5, 9, 10, 13, 16, 17, 19, 20, 25, 37 Explain the working of binary search algorithm to search 25. How many comparisons are required for this searching?

 4. Find the value of score[4] based on the following declaration statement:

 int score[5] = {98, 87, 92, 79, 85};

     Ans. 85

5. Suppose M[5][5] is a 2D array that contains the elements of a square matrix. Write C++ statements to find the sum of the diagonal elements.

     #include <iostream>
using namespace std;
int main()
{ int mat[5][5], n, i, j, s=0;
cout<<"Enter the rows/columns of square matrix: ";
cin>>n;
cout<<"Enter the elements\n";
for(i=0; i<n; i++)
for(j=0; j<n; j++)
cin>>mat[i][j];
cout<<"Major diagonal elements are\n";
for(i=0; i<n; i++)
{
cout<<mat[i][i]<<"\t";
s = s + mat[i][i];
}
cout<<"\nSum of major diagonal elements is: ";
cout<<s;
return 0;
}

6. Write an algorithm for arranging elements of an array in ascending order using bubble sort.

       Step 1. Start

      Step 2. Accept a value in N as the number of elements of the array

      Step 3. Accept N elements into the array AR

     Step 4. Repeat Steps 5 to 7, (N - 1) times

      Step 5. Repeat Step 6 until the second last element of the list

        Step 6. Starting from the first position, compare two adjacent elements in the list. If they are not in proper order, swap the elements.

      Step 7. Revise the list by excluding the last element in the current list.

     Step 8. Print the sorted array AR

        Step 9. Stop

 7. ………………. search method is an example for ‘divide and conquer method’.

   Ans. Binary search

8. What is an array? Write C++ program to declare and use a single dimensional array for storing the computer science marks of all students in your class.

       An array is a collection of elements of the same type placed in contiguous memory locations. Arrays are used to store a set of values of the same type under a single variable name. Each element in an array can be accessed using its position in the list called index number or subscript.

9. a) Write C++ program for sorting a list of numbers using Bubble sort method.

    b) Write different passes of sorting the following numbers using Bubble sort:

         32, 21, 9, 17, 5

    Ans. a.

  #include <iostream>
using namespace std;
int main()
{
int AR[25],N;
int I, J, TEMP;
cout<<"How many elements? ";
cin>>N;
cout<<"Enter the array elements: ";
for(I=0; I<N; I++)
cin>>AR[I];
for(I=1; I<N; I++)
for(J=0; J<N–I; J++)
if(AR[J] > AR[J+1])
{
TEMP = AR[J];
AR[J] = AR[J+1];
AR[J+1] = TEMP;
}
cout<<"Sorted array is: ";
for(I=0; I<N; I++)
cout<<AR[I]<<"\t";
}

10. Declare a two dimensional array to store the elements of a matrix with order 3 x 5.

    Ans. int M[3][5];

11. Define an array. Also write an algorithm for searching an element in the array using any one method that you are familiar with.

        An array is a collection of elements of the same type placed in contiguous memory locations. Arrays are used to store a set of values of the same type under a single variable name. Each element in an array can be accessed using its position in the list called index number or subscript.

      Algorithm for Linear Search

 Step 1. Start

Step 2. Accept a value in N as the number of elements of the array

 Step 3. Accept N elements into the array AR

Step 4. Accept the value to be searched in the variable ITEM

 Step 5. Set LOC = -1

 Step 6. Starting from the first position, repeat Step 7 until the last element

Step 7. Check whether the value in ITEM is found in the current position. If found then store the position in LOC and Go to Step 8, else move to the next position.

 Step 8. If the value of LOC is less than 0 then display "Not Found", else display the value of LOC + 1 as the position of the search value.

 Step 9. Stop

12. int num[10];

The above C++ statement declares an array named num that can store maximum …. integer location.

 a) 9

b) 10

c) N

d) none of these

 Ans. b

 13. Explain the memory allocation for the following declaration statement. int A[10][10];

 14. Write a C++ program to illustrate array traversal.

         #include<iostream>

      using namespace std; 

      int main() 

      { 

          int a[10], i; 

      cout<<"Enter the elements of the array :";

      for(i=0; i<10; i++) 

       cin >> a[i];

      for(i=0; i<10; i++) 

     a[i] = a[i] + 1;

     cout<<"\nEntered elements of the array are...\n"; 

     for(i=0; i<10; i++) 

     cout<< a[i]<< "\t";

    return 0;

  }

15. Read the following C++ statement: int MAT[5][4];

(a) How many bytes will be allocated for this array?

(b) Suppose MAT[4][4] is a 2D array that contains the elements of a square matrix. Write C++ statements to find the sum of all the elements in the array.

    Ans. 
            a. 4*5*4=80 bytes

16. Write the names of two searching methods in arrays. Prepare a chart that shows the comparison of two searching methods.

        Linear search and Binary search.

         Linear search method                                                 Binary search method 

  • The elements need not be in any order                           • The elements should be in sorted order

 • Takes more time for the process                                     • Takes very less time for the process

 • May need to visit all the elements                                   • All the elements are never visited

 • Suitable when the array is small                                      • Suitable when the array is large