Vidyalelo
C++ Programming · all questions

Arrays and Strings in C++
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

72

Questions

4/4

Page

Pick an option on a question to see the right answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string str ("I like to code in C");
    unsigned sz = str.size();
    str.resize (sz + 2, '+');
    str.resize (14);
    cout << str << '\n';
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>  
#include <cstring>
#include <string>
using namespace std;
int main () 
{
    string str ("Steve jobs");
    char * cstr = new char [str.length() + 1];
    strcpy (cstr, str.c_str());
    char * p = strtok (cstr," ");
    while (p != 0)
    {
        cout << p << '\n';
        p = strtok(NULL," ");
    }
    delete[] cstr;
    return 0;
}

Select an option to see the answer and solution.

Which constant member functions does not modify the string?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    char str[5] = "ABC";
    cout << str[3];
    cout << str;
    return 0;
}

Select an option to see the answer and solution.

What is the index number of the last element of an array with 9 elements?

Select an option to see the answer and solution.

Which header file is used to manipulate the string?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string str ("Test string");
    for ( string :: iterator it = str.begin(); it != 5; ++it)
        cout << *it;
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string str ("Steve jobs founded the apple");
    string str2 ("apple");
    unsigned found = str.find(str2);
    if (found != string :: npos)
        cout << found << '\n';
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string name ("Jobs");
    string family ("Steve");
    name += " Apple ";
    name += family;
    name += '\n';
    cout << name;
    return 0;
 }

Select an option to see the answer and solution.

Which of the following correctly declares an array?

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <stdio.h>
#include<iostream>
using namespace std;
int main ()
{
    int array[] = {0, 2, 4, 6, 7, 5, 3};
    int n, result = 0;
    for (n = 0; n < 8; n++) 
    {
        result += array[n];
    }
    cout << result;
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string str ("Steve jobs");
    unsigned long int found = str.find_first_of("aeiou");
    while (found != string :: npos)
    {
        str[found] = '*';
        found = str.find_first_of("aeiou", found + 1);
    }
    cout << str << '\n';
    return 0;
}

Select an option to see the answer and solution.