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

Question 1:

What is the output of the following snippet?

using namespace std;

float Float(float x)

{

    return 2 * x / (.5 * x);

}

void Void(int n)

{

    float v = n;

    v = Float(v);

    cout << v;

}

int main()

{

    Void(1);

}

Answer:

4

Question 2:

What is the output of the following snippet?

#include <iostream>
using namespace std;
int main() 
{
    int *val = new int;
    *val = sizeof(val) / sizeof(int *);
    int *tab = new int[*val];
    tab[0] = *val;
    delete val;
    cout << *tab;
    delete [] tab;
}

Answer:

1

Question 3:

What is the output of the following snippet?

#include <iostream>
using namespace std;

int fun(float a, float b) 
{
    return a / b;
}
int main() 
{
    cout << fun(fun(1., 2.),fun(2., 1.));
}

Answer:

0

Question 4:

What is the output of the following snippet?

#include <iostream>
#include <vector>
using namespace std;
int f1(int *a) 
{
    return *a + 1;
}
int *f2(int *a) 
{
    return a + 1;
}
int *f3(int &a) 
{
    return &a + 1;
}
int main() 
{
    vector<int> t = {0, 1, 2, 3};
    cout << f1(f3(*f2(t.data())));
}

Answer:

3

Question 5:

What is the output of the following snippet?

#include <iostream>
using namespace std;
int f1(int a) 
{
    return ++a;
}
int f2(int &a) 
{
    return ++a;
}
int f3(int *a) 
{
    return *a + 1;
}
int main() 
{
    int value = 2;
    cout << f1(value);
    cout << f2(value);
    cout << f3(&value);
}

Answer:

334

Question 6:

What is the output of the following snippet?

#include <iostream>
using namespace std;
int fun(int a, int b) 
{
    return a + b;
}
int fun(int a, char b) 
{
    return b - a;
}
int fun(float a, float b) 
{
    return a * b;
}
int main() 
{
    cout << fun(1,0) << fun('a','c') << fun(2.f,2.f);
}

Answer:

124

Question 7:

What is the output of the following snippet?

#include <iostream>
using namespace std;
int fun(long a, long b = 1) 
{
    return a << b;
}
int fun(int a, char b = 'z') 
{
    return b - a;
}
int fun(float a, float b = 0) 
{
    return a * b;
}
int main() 
{
    cout << fun(1L) << fun('x') << fun(2e0f);
}

Answer:

220

Question 8:

What is the output of the following snippet?

#include <iostream>
using namespace std;
int f1(int *a) 
{
    return *a;
}
int *f2(int *a) 
{
    return a;
}
int *f3(int &a) 
{
    return &a;
}
int main() 
{
    int value = 2;
    cout << f1(f2(f3(value)));
}

Answer:

2

Question 9:

What is the output of the following snippet?

#include <iostream>
using namespace std;
struct S {
    float *f;
};
void make(S *p, float x = 10) 
{
    float *f = new float; 
    *f = sizeof(*f) / sizeof(float) * 10;
    p->f = f;
}
int main() 
{
    S *ss = new S;
    make(ss);
    cout << *(*ss).f;
    delete ss->f;
    delete ss;
}
Answer:
10

Question 10:

What is the output of the following snippet?

#include <iostream>
using namespace std;
int fun(long a) 
{
    return a / a;
}
int fun(int a) 
{
    return 2 * a;
}
int fun(double a = 3.0) 
{
    return a;
}
int main() {
    cout << fun(1000000000L) << fun (1L) << fun(1.f);
}

Answer:

111

Question 11:

What is the output of the following snippet?

#include <iostream>
#include <string>
using namespace std;
string fun(string t, string s = "x", int r = 1) 
{
    while(--r)
        s += s;
    t = t + s;
    return s;
}
int main() 
{
    string name = "a";
    cout << fun(name);
    cout << name;
}

Answer:

Xa

Question 12:

What is the output of the following snippet?

#include <iostream>
#include <vector>
using namespace std;
int fun(int* t) 
{
    t[0] += t[1];
    return t[0];
}
int main() 
{
    vector<int> t = { 5,6,7 };
    cout << fun(&t[1]);
    cout << t[0];
}

Answer:

135

Question 13:

What is the output of the following snippet?

#include <iostream>
#include <vector>
using namespace std;
int main() 
{
    vector<float> f(2);
    float *p1 = f.data(), *p2 = p1 + 1;
    cout << (p2 - p1) / sizeof(float);
}

Answer:

0

Question 14:

What is the output of the following snippet?

#include <iostream>
#include <string>
using namespace std;
string fun(string &t, string s = "", int r = 1) 
{
    while(--r)
        s += s;
    t = t + s;
    return s;
}
int main() 
{
    string name = "x";
    cout << fun(name, name);
    cout << name;
}

Answer:

Xxx

Question 15:

What is the output of the following snippet?

#include <iostream>
#include <vector>
using namespace std;
int main() 
{
    vector<int> tab = { 1, 2, 4 };
    int *p1 = &tab[0], *p2 = p1 + 2;
 
    tab[1] = p2[-1] + *p1;
    cout << tab[1];
}

Answer:

3

Question 16:

What is the output of the following snippet?

#include <iostream>
using namespace std;
int main() 
{
    float x = 3.14, *p = &x;
    p[0] = ++x;
    cout << x;
}
Answer:
4.14

Question 17:

What is the output of the following snippet?

#include <iostream>
#include <vector>
using namespace std;
int main() 
{
    vector<int> t = { 3, 2, 1 };
    int *ptr = t.data() + 1;
    (*(ptr + 1))++;
    (*(ptr - 1))++;
    cout << t[0] << t[1] << t[2];
}

Answer:

422

Question 18:

What is the output of the following snippet?

#include <iostream>
using namespace std;
int fun(int p1 = 1, int p2 = 1) 
{
    return p2 + p1;
}
int main() 
{
    cout << fun(fun(), fun(2));
}

Answer:

5

Question 19:

What is the output of the following snippet?

#include <iostream>
using namespace std;
int *make(int v) 
{
    int *p = new int;
    *p = v + 1;
    return p;
}
int *play(int &v) 
{
    cout << ++v;
    return &v;
}
void remove(int *v) 
{
    delete v;
}
int main() 
{
    remove(play(*make(3)));
}
Answer:
5

Question 20:

What is the output of the following snippet?

#include <iostream>
using namespace std;
float fun(float arg) 
{
    return arg * arg + arg + 1;
}
int main() 
{
    cout << fun(fun(1.0));
}
Answer:
13

Post a Comment

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