CPA –Programming Essentials in C++ Chapter 1 Module 4

Question 1: What is the output of the following snippet?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    vector<string> t = { "alpha", "beta", "gamma" };
    int *cnt = new int[3], *p = &cnt[0];
    for(int i = 0; i < t.size(); i++)
        *p++ = t[i].length();
    cout << cnt[0] << cnt[1] << cnt[2];
    delete [] cnt;

Answer: 545


Question 2: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main() {
    string s1 = "1";
    string s2 = "12";
    cout << s1.compare(s2);
}
Answer: -1

Question 3: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main() 
{
    string s1 = "Ab";
    string s2 = "Abc";
    cout << s1.compare(s1);
}
Answer: 0


Question 4: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
    int *it[3];
    for(int i = 0; i < 3; i++) {
        it[i] = new int [i + 1];
        for(int j = 0; j < i + 1; j++)
            it[i][j] = 10 * i + j;
    }
    cout << it[2][2];
    for(int i = 0; i < 3; i++)
        delete [] it[i];

Answer: 22


Question 5: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;  
int main() {
    string s1 = "ab";
    string s2 = "Abc"; 
    if(s1 > s2)
        cout << "yes";
    else
        cout << "NO";
}
Answer: Yes


Question 6: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main() 
{
    string s1 = "aleph";
    string s2 = "alpha";
    string s;
    s1.swap(s2);
    s2.swap(s);
    s.swap(s2);
    cout << s2;
}

Answer: Aleph


Question 7: What is the output of the following snippet? 
#include <iostream>
#include <string>
using namespace std;
int main() 
{
    string s = "a";
    cout << s  << "b" + "c";
}
Answer: abc


Question 8: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main() 
{
    int i = 2;
    float f = 4.4;
    cout << f % static_cast<float>(i);
}
Answer: Compilation fails


Question 9: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main() 
{
    int i = 2;
    string s = "2";
    cout << s + i;
}
Answer: Compilation fails


Question 10: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main() 
{
    string s = "a";
    cout << s + "b" + "c";
}
Answer: abc


Question 11: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main() 
{
    short s = 1;
    int i = 2;
    float f = 4.4;
    double d = 8.8;
    cout << s/i + f/i + d/s;
}
Answer: 11


Question 12: What is the output of the following snippet?
#include <iostream>
#include <vector>
using namespace std;
int main() 
{
    vector<float *> ft = { new float[3], new float[3], new float[3] }; 
    float *p = nullptr;
    for(int i = 0; i < 3; i++) {
        p = ft[i];
        *p = p[1] = *(p + 2) = 10 * i;
    }
    cout << ft[1][1];
    delete [] ft[0];
    delete [] ft[1];
    delete [] ft[2];
}
Answer: 10


Question 13: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main() 
{
    short s = 1;
    int i = 2;
    float f = 4.4;
    double d = 6.6;
    cout << i/static_cast<float>(s) + int(f)/i + (long)d/s;
    return 0;
}
Answer: 10


Question 14: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main() 
{
    string s = "0123";
    cout << s.substr(1,3).substr(2).substr();
}
Answer: 3


Question 15: What is the output of the following snippet?
#include <iostream>
using namespace std;
namespace S {
    int A = 1;
}
namespace S {
    int B = A + 2 ;
}
int main() 
{
    S::A = S::A + 1;
    { 
        using namespace S;
        ++B;
    }
    cout << S::B << S::A;
    return 0;
}
Answer: 42


Question 16: What is the output of the following snippet?
#include <iostream>
namespace SpaceA {
    int A;
}
namespace SpaceB {
    int A;
}
using namespace SpaceA, SpaceB;
int main() 
{
    SpaceA::A = SpaceB::A = 1;
    std::cout << A + 1;
}
Answer: Compilation fails


Question 17: What is the output of the following snippet?
int main() {
    string s1 = "1";
    string s2 = "12"; string s2 = "12";
using namespace std;
namespace S1 {
    int A = 1;
}
namespace S2  {
    int A = 2 ;
}
int main() 
{
    { using namespace S1;
        S2::A = A + 1;
    }
    { using namespace S2;
        S1::A = A + 1;
    }
    cout << S1::A << S2::A;
}
Answer: 32


Question 18: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main() 
{
    string s = "AB";
    s.append(s).push_back(s[s.length() - 1]);
    cout << s;
}
Answer: ABABB


Question 19: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int main() 
{
    string s = "ABC";
    for(int i = 1; i < s.length(); i += 2)
        s[i - 1] = s[i] + 'a' - 'A';
    cout << s;
}
Answer: bBC


Question 20: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main() 
{
    int i = 2;
    float f = 5.8;
    f = static_cast<int>(f);
    i = static_cast<float>(i);
    cout << f/i;
}
Answer: 2.5s

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.