Pageviews

Tuesday 28 July 2020

+1] 9 -String handling And Input Output functions - Solved Questions chapter wise



                                  PLUS ONE COMPUTER SCIENCE

                First year computer science Solved Questions and answers

                                                              Chapter  -  
                                         String Handling and I/O Functions





1. Which character is used to delimit the string in memory?

  Ans. Null character(/0)

 2. Write the statement to declare a variable for storing "Save Earth".

 Ans. char name[11];

3. Name the header file required for using console I/O functions.

 Ans.csdtio

4. How many bytes are required to store the string "Be Positive"?

    Ans. 12 bytes.

5. How does puts("hello"); differ from cout<<"hello";?

   Ans.  C++ gives a console function named puts() to output string data. The function puts() is a console output function used to display a string data on the standard output device (monitor). Its syntax is: puts(string_data);
puts("hello");
puts(str); the ouput will be hello.
Using cout<<“hello”; and cout<<str; instead of the
puts() functions and see the difference. The output will be in the same line without
a space in between them in the case of cout statement.

6. Name the stream function to input a character data.

  Ans. get( )

7. Write a C++ statement to display the string "Smoking is injurious to health" using write() function.

    char str[31]="Smoking is injurious to health";

    cout.write(str,31);

8. Name the header file required for using stream I/O functions.

    Ans.  iostream

9. Write down the syntax of getline() function.

  Ans. getline(str,len);

10. What will the output of the statement: putchar(97); be?

  Ans. a (small letter )

 11. Distinguish between console functions and stream functions.

    Ans.  C++ also provides some functions for performing input/ouput operations on characters. These functions require the inclusion of header file cstdio in the program.getchar(),putchar() are the console functions in I/O functions.
C++ provides another facility to perform input/output operations on character and strings. It is in the form of functions that are available in the header file iostream. These functions are generally called stream functions since they allow a stream of bytes (data) to flow between memory and objects.

 12. Write a C++ statement to input the string “Computer” using get() function.

     Ans. char ch, str[9]; 

              ch=cin.get(ch);

 13. Write down the output of the following code segment:

      puts(“hello”);

       puts(“friends”);

Ans. hello

         friends 

14. Predict the output of the following code segment:

       char str[] = “Program”;

       for (int i=0; str[i] != ‘\0’; ++i)

     {

       putchar(str[i]);

      putchar(‘–’);

}

   Ans. P-r-o-g-r-a-m

15. Identify the errors in the following program and give reason for each.

#include<iostream>
using namespace std;
int main()
{
char ch, str[10];
write(“Enter a character”);
ch=getchar();
puts(“Enter a string”);
cin.getline(str);
cout<< “The data entered are “ <<ch;
putchar(str);
}

16. Observe the following functions. If the statement is valid, explain what happens
when they are executed. If invalid, state reasons for it. (Assume that the variables
are valid)

(a) getchar(ch);

 (b) gets(str[5]);

(c) putchar(“hello”);

(d) cin.getline(name, 20, ‘,’);

 (e) cout.write(“hello world”, 10);

    Ans.

                a. Invalid.Correct statement ch=getchar(ch);

                 b. Invalid. Correct statement gets(str);

                  c. Invalid. Putchar is displaying only single character

                   d. Valid

                   e. valid

17. Read the following statements:

char name[20];
cin>>name;
cout<<name;

What will be the output if you input the string “Sachin Tendulkar”? Justify
your answer. Modify the code to get the entered string as the output.

  Ans. The output will be displayed Sachin.cin statement read till a white space

  char name[20];
  gets(name);
  puts(name);

                     Essay questions  refer  text book chapter 9

18 Explain the console I/O functions with examples.

19. Briefly describe the stream I/O functions with examples.