Sunday, 19 October 2014

C++ basic questions and answers


1. Which header file is used to manipulate the string?a) iostream
b) iomanip
c) string
d) container
2. How many maximum number of parameters does a string constructor can take?a) 1
b) 2
c) 3
d) 4
3. Which constant member functions does not modify the string?a) bool empty()
b) assign
c) append
d) None of the mentioned
4. What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("I like to code in C");
  7.         unsigned sz = str.size();
  8.         str.resize (sz + 2, '+');
  9.         str.resize (14);
  10.         cout << str << '\n';
  11.         return 0;
  12.     }
a) I like to code in c
b) I like to code
c) I like to code in c++
d) None of the mentioned
5. What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Steve jobs");
  7.         cout << str.capacity() << "\n";
  8.         return 0;
  9.     }
a) 9
b) 10
c) 11
d) 12
6. What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Steve jobs founded the apple");
  7.         string str2 ("apple");
  8.         unsigned found = str.find(str2);
  9.         if (found != string :: npos)
  10.             cout << found << '\n';
  11.         return 0;
  12.     }
a) apple
b) 12
c) 23
d) Steve jobs founded the
7. What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Steve jobs");
  7.         unsigned found = str.find_first_of("aeiou");
  8.         while (found != string :: npos)
  9.         {
  10.             str[found] = '*';
  11.             found = str.find_first_of("aeiou", found + 1);
  12.         }
  13.         cout << str << '\n';
  14.         return 0;
  15.     }
a) Steve
b) jobs
c) St*v* j*bs
d) St*v*
8. What is the output of this program?
  1.     #include   
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         string str ("Steve jobs");
  8.         char * cstr = new char [str.length() + 1];
  9.         strcpy (cstr, str.c_str());
  10.         char * p = strtok (cstr," ");
  11.         while (p != 0)
  12.         {
  13.             cout << p << '\n';
  14.             p = strtok(NULL," ");
  15.         }
  16.         delete[] cstr;
  17.         return 0;
  18.     }
a) Steve jo
b) Steve jobs
c) Steve
jobs
d) None of the mentioned
9. What is the difference between unsigned int length() and unsigned int size()?a) Returns a different value
b) They are same
c) Both a & b
d) None of the mentioned
10. How many parameters does a resize method can take?a) 1
b) 2
c) 3
d) 4

No comments:

Post a Comment