0% found this document useful (0 votes)
858 views

Practice Quiz 1 To 8 ITE 6102 2013T Partner CPA Programming Essentials in C

The document contains 10 practice quiz questions with multiple choice answers. Each question tests knowledge of C++ programming concepts such as output of code snippets, data types, operators, functions, and pointers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
858 views

Practice Quiz 1 To 8 ITE 6102 2013T Partner CPA Programming Essentials in C

The document contains 10 practice quiz questions with multiple choice answers. Each question tests knowledge of C++ programming concepts such as output of code snippets, data types, operators, functions, and pointers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Practice Quiz 2

IncorrectQuestion 1
0 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int i = 2;
    float a = 3;
    bool f1 = i > a, f2 = a > i, f3 = a / i > i / a;
    if(f3)
        if(f2)
            i+=1;
        else
            i+=2;
    else
        if(f1)
            i+=3;
        else
            i+=4;
    cout << i << endl;
    return 0;
}
 

  
1
 

  
3
 

  
0
 

  
2
 
 
Question 2
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int i = 10;
    float a = 1000.0;
    while(i > 0) {
        i /= 2;
        a /= 10;
    }
    cout << a << endl;
    return 0;
}
 

  
1
 

  
0.01
 

  
0.1
 

  
10
 
 
Question 3
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int i = 10;
    float a = -1.0;
    while(a < 0) {
        a = a + 10.0 * a / -10;
        --i;
    }
    cout << i << endl;
    return 0;
}
 

  
10
 

  
8
 

  
9
 

  
7
 
 
Question 4
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    float a;
    int i = 0;
    for(a = .01; a < 1e2; a *= 1e1)
        i++;
    cout << i << endl;
    return 0;
}
 

  
6
 

  
4
 

  
3
 

  
5
 
 
Question 5
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int i = 1, j = 2;
    if(i > j && j > i)
        i++;
    if(i > j || j > i)
        j++;
    if(i | j)
        i++;
    if(i & j)
        j++;
    cout << i * j << endl;
    return 0;
}
 

  
6
 

  
2
 

  
8
 

  
4
 
 
Question 6
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int i = 1, j = 2, k;
    i = i << j;
    j = j << i;
    k = j >> i;
    cout << k << endl;
    return 0;
}
 

  
4
 

  
8
 

  
2
 

  
6
 
 
Question 7
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int t[5];
    for(int i = 0; i < 5; i++)
        t[i] = 2 * i * i;
    cout << t[4] / t[1] << endl;
    return 0;
}
 

  
32
 

  
8
 

  
16
 

  
64
 
 
Question 8
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    float arr[3][3] = {{.1,1.,10.},{10.,.1,1.},{.1,10.,1.}};
    float x = 1.;
    for(int i = 0; i < 3; i++)
        x *= arr[i][i];
    cout << x << endl;
    return 0;
}
 

  
0.1
 

  
0.01
 

  
10
 

  
100
 
 
Question 9
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
struct A {
    int a;
    float b;
};
struct B {
    int b;
    float a;
};
struct C {
    A a; B b;
};
int main(void) {
    C c1 = {1, 2, 3, 4}, c2 = {5, 6, 7, 8};
    cout << c1.b.a + c2.a.b << endl;
    return 0;
}
 

  
10
 

  
12
 

  
8
 

  
6
 
 
Question 10
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
struct A {
    int a;
    char b;
};
struct B {
    char a;
    int b;
};
int main(void) {
    A a = { 2, 'A' };
    B b = { 'C', 1 };
    cout << b.a - a.b - b.b + a.a << endl;
    return 0;
}
 

  
10
 
  
3
 

  
8
 

  
0
 
Question 1
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    bool ints = sizeof(long) >= sizeof(int) && sizeof(int) >= sizeof(short);
    bool floats = sizeof(double) < sizeof(float);
    bool chars = sizeof(char) == 1;
    int v = ints && floats && chars;
    cout << v << endl;
    return 0;
}
 

  
true
 

  
1
 

  
0
 

  
false
 
 
Question 2
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int t[4] = { 8, 4, 2, 1 };
    int *p1 = t + 2, *p2 = p1 - 1;
    p1++;
    cout << *p1 - t[p1 - p2] << endl;
    return 0;
}
 

  
1
 

  
2
 

  
-1
 

  
-2
 
 
Question 3
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int fun(int p) {
    ++p;
    return p++;
}
int main(void) {
    int a = 1, b;
    b = fun(a);
    cout << a + b << endl;
    return 0;
}
 

  
3
 
  
2
 

  
4
 

  
1
 
 
Question 4
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int fun(int p) {
    int cnt = 0;
    for(p = 2 * p; p > 0; p >>= 2)
        cnt++;
    return cnt;
}

void foon(int p) {
    for(int cnt = fun(p); cnt > 0; cnt--)
        cout << "*";
}

int main(void) {
    foon(2);
    cout << endl;
    return 0;
}
 

  
**
 

  
****
 

  
***
 

  
*****
 
 
Question 5
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int fun1(int p) {
    ++p;
    return p++;
}
int fun2(int &p) {
    ++p;
    return p++;
}

int main(void) {
    int a = 1, b, c;
    b = fun1(a);
    c = fun2(b);
    cout << a + b + c << endl;
    return 0;
}
 

  
6
 

  
10
 

  
8
 

  
4
 
 
Question 6
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int fun(int p = 2, int q = 3) {
    return p + q;
}

int main(void) {
    cout << fun() + fun(1) + fun(1,2) << endl;
    return 0;
}
 

  
10
 

  
6
 

  
12
 

  
8
 
 
Question 7
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int fun(void) {
    return 1;
}
int fun(int p) {
    return 1 + p;
}
int fun(int p, int q) {
    return 2 + p + q;
}

int main(void) {
    cout << fun() + fun(1) + fun(1,2) << endl;
    return 0;
}
 

  
6
 

  
8
 

  
12
 

  
10
 
 
Question 8
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int fun(void) {
    return 1;
}
int fun(int p) {
    return p > 1 ? 1 : 0;
}
int fun(int p, int q) {
    return q > p ? q - p : p - q;
}

int main(void) {
    cout << fun() + fun(1) + fun(1,2) << endl;
    return 0;
}
 

  
2
 

  
4
 

  
9
 

  
5
 
 
Question 9
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int *fun(void) {
    return new int[2];
}
int fun(int *p) {
    delete [] p;
    return 0;
}
void fun(int *p, int q) {
    p[q] *= 2;
}
void fun(int *p, int q, int r) {
    p[q] = r;
}

int main(void) {
    int *v = fun();
    fun(v,0,1);
    fun(v,1,2);
    fun(v,0);
    cout << v[1] + v[0] << endl;
    fun(v);
    return 0;
}
 

  
1
 

  
2
 

  
3
 

  
4
 
Practice quiz 3
 
Question 10
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
char f1(char c) {
     return c == 'z' ? 'a' : c + 1;
}
char f2(char &c) {
    c = f1(c);
    return c;
}
int main(void) {
    char x = 'x';
    cout << f2(x);
    cout << f2(x);
    cout << f2(x) << endl;
    return 0;
}
 

  
xyz
 

  
yza
 

  
XYZ
 

  
YZA
 
Practice quiz 4

Question 1
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int *t[2] = { new int[2], new int[2] };
    for(int i = 0; i < 4; i++)
        t[i % 2][i / 2] = i;
    cout << t[0][1] + t[1][0] << endl;
    delete [] t[0];
    delete [] t[1];
    return 0;
}
 

  
1
 

  
4
 

  
2
 

  
3
 
 
Question 2
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int i = 2, j = 3;
    float a = 2, b = 3;
    cout << float(j) / (float)i << " " << (int)b / int(a) << endl;
    return 0;
}
 

  
1.5 1.5
 

  
1 1.5
 

  
11
 

  
1.5 1
 
 
Question 3
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    double d = 987654321.123456789;
    float f = d;
     int i = d == f;
    cout << i << endl;
    return 0;
}
 

  
0.0
 
  
1
 

  
0
 

  
1.0
 
 
Question 4
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    double d = 0.999999;
    float f = .0;
     int i = (int)d == int(f);
    cout << i << endl;
    return 0;
}
 

  
0
 

  
0.0
 

  
1
 

  
1.0
 
 
Question 5
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int Int = 1;
    char Char = 3;
    float Float = 4.9;

    Int = Int + Char + Float;


    cout << Int << endl;
    return 0;
}
 

  
9.0
 

  
8
 

  
8.9
 

  
8.0
 
 
Question 6
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <string>
using namespace std;
int main(void) {
    string s = "Skipper", t = "Private";
    int i = s.compare("Kowalski") > 0;
    int j = s.compare("Rico") < 0;
    cout << i + j << endl;
    return 0;
}
 

  
true
 

  
0
 

  
false
 

  
1
 
 
Question 7
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <string>
using namespace std;
int main(void) {
    string s = "Abc", t = "A";
    s = s + t;
    t = t + s;
    int i = s.compare(t) > 0;
    int j = s.length() < t.length();
    cout << i + j << endl;
    return 0;
}
 

  
2
 
  
0
 

  
1
 

  
3
 
 
Question 8
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <string>
using namespace std;
int main(void) {
        string str1, str2;
        str1 = "ABCDEF";
        str2 = str1.substr(1,1) + str1.substr(4) + str1.substr();
    str1 = str2.substr(1,1) + str2.substr(4) + str2.substr();
        cout << (str2 > str1) << endl;
        return 0;
}

  
false
 

  
0
 

  
true
 

  
1
 
 
Question 9
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <string>
using namespace std;
int main(void) {
        string s = "A";
        s.append(s).append(s).append(s);
        cout << s.length() << endl;
        return 0;
}

  
6
 

  
3
 

  
1
 

  
8
 
 
Question 10
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
namespace alpha { int var = 1; }
namespace beta { int var = alpha::var + 1; }
int main(void) {
    beta::var += alpha::var;
    {
    using namespace beta;
        cout << var << endl;
    }
        return 0;
}

  
4
 

  
3
 

  
2
 

  
1
 
Practice quiz 5

Question 1
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
    int a;
};

int main(void) {
    A a;
    a.a = 1/2;
    cout << a.a << endl;
    return 0;
}
 

  
0.5
 

  
The program will cause a compilation error.
 

  
0
 

  
1
 
 
Question 2
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
    int a;
public:
    int b;
    A(void) { a = b = 1; }
};

int main(void) {
    A a;
    a.b /= 2;
    cout << a.b << endl;
    return 0;
}
 

  
0
 

  
The program will cause a compilation error.
 

  
1
 

  
0.5
 
 
Question 3
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
    int a;
public:
    A(void) { a = 1; }
    int b(void) { return ++a; }
};

int main(void) {
    A a;
    a.b();
    cout << a.b() << endl;
    return 0;
}
 

  
The program will cause a compilation error.
 

  
1
 

  
3
 

  
2
 
 
Question 4
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    A() { a[0] = 1; a[1] = 0; }
    int a[2];
    int b(void) { int x=a[0];a[0]=a[1];a[1]=x; return x; }
};

int main(void) {
    A a;
    a.b();
    cout << a.b();
    cout << a.a[1] << endl;
    return 0;
}
 

  
The program will cause a compilation error.
 

  
00
 

  
01
 

  
10
 

  
11
 
 
Question 5
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    A() { a.a = a.b = 1; }
    struct { int a,b; } a;
    int b(void);
};
int A::b(void) { int x=a.a;a.a=a.b;a.b=x; return x; };
int main(void) {
    A a;
    a.a.a = 0;
    a.b();
    cout << a.b();
    cout << a.a.b << endl;
    return 0;
}
 

  
00
 

  
The program will cause a compilation error.
 

  
01
 

  
11
 

  
10
 
 
Question 6
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    int a;
    A() { a = 1; }
    A(int aa) { a = 2; }
    A(A &aa) { a = 3; }
};

int main(void) {
    A a(1),b(a);
    cout << a.a + b.a << endl;
    return 0;
}
 

  
5
 

  
3
 

  
The program will cause a compilation error.
 

  
1
 
 
Question 7
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int z = 1;
class A {
public:
    int a;
    A() { a = 1; z++; }
    A(A &aa) { a = 3; z++; }
    ~A() { z--; }
};
void fun(void) {
    A a,b(a),c(b);
}
int main(void) {
    cout << z << endl;
    return 0;
}
 

  
5
 
  
3
 

  
The program will cause a compilation error.
 

  
1
 
 
Question 8
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    static int a;
    A() { a = 1; a++; }
    A(A &aa) { a++; }
};
int main(void) {
    A a,b(a),c(b);
    cout << A.a << endl;
    return 0;
}
 

  
1
 

  
5
 

  
3
 
  
The program will cause a compilation error.
 
 
Question 9
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    static int a;
    A() { a = 0; a++; }
    A(A &aa) { a++; }
};
int A::a = 1;
int main(void) {
    A a,b(a),c(b);
    cout << a.a << endl;
    return 0;
}
 

  
5
 

  
1
 

  
3
 

  
The program will cause a compilation error.
 
 
Question 10
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    int a;
    A() { a = 0; }
    A(int b) { a = b + 1; }
};
class B {
public:
    A a;
    B() : a(0) { }
};

int main(void) {
    B *b = new B();
    cout << b->a.a << endl;
    return 0;
}
 

  
The program will cause a compilation error.
 

  
3
 

  
5
 

  
1
 
Practice quiz 6

Question 1
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    char c;
};
class B : A {
};
int main(void) {
    B b;
    A a;
    a.c = b.c = '?';
    cout << int(a.c - b.c) << endl;
    return 0;
}
 

  
2
 

  
4
 

  
The program will cause a compilation error.
 

  
1
 
 
Question 2
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    int x;
    void d() { x /= 2; }
};
class B : public A {
public:
    int y;
    void d() { y /= 4; }
};
int main(void) {
    B b;
    b.x = b.y = 4;
    b.d();
    cout << b.x / b.y << endl;
    return 0;
}
 

  
2
 

  
1
 

  
4
 

  
The program will cause a compilation error.
 
 
Question 3
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    int x;
    void d() { x /= 2; }
};
class B : public A {
public:
    int y;
    void d() { A::d(); }
};
int main(void) {
    B b;
    b.x = b.y = 4;
    b.d();
    cout << b.y / b.x << endl;
    return 0;
}
 

  
2
 

  
The program will cause a compilation error.
 

  
4
 

  
1
 
 
IncorrectQuestion 4
0 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
     int work(void) { return 4; }
};
class B : public A {
public:
    int relax(void) { return 2; }
};
class C : public A {
public:
    int relax(void) { return 1; }
};
int main(void) {
    A *a0 = new A, *a1 = new B, *a2 = new C;
    cout << a0 -> work() + static_cast<C*>(a2) -> relax() / static_cast<B*>(a1) -> relax()
<< endl;
    return 0;
}
 

  
The program will cause a compilation error.
 

  
2
 

  
4
 

  
1
 
 
Question 5
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:
    int p(void) { return 2; }
};
class B : public A {
public:
    int p(void) { return 1; }
};
int main(void) {
    A *a = new B;
    cout << static_cast<A*>(a)->p() << endl;
    return 0;
}
 

  
4
 

  
1
 

  
2
 

  
The program will cause a compilation error.
 
 
Question 6
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
void f(const int &v) {
    ++v;
    return v + 1;
}
int main(void) {
    int i = 1, j = f(i);
    cout << j - i << endl;
    return 0;
}
 

  
The program will cause a compilation error.
 

  
1
 

  
2
 

  
4
 
 
Question 7
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A { friend class B;
    int a;
public:
    A() : a(1) {}
    int f() { return a; }
};
class B {
public:
    static void f(A &a) { a.a++; }
};
int main(void) {
    A a;
    B::f(a);
    cout << a.f() << endl;
    return 0;
}
 

  
1
 

  
4
 

  
The program will cause a compilation error.
 

  
2
 
 
Question 8
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A { friend void i(A*);
    int a;
public:
    A() : a(2) {}
    int f() { return a; }
};
void i(A *a){
    a->a *= 2;
}
int main(void) {
    A a;
    i(&a);
    cout << a.f() << endl;
    return 0;
}
 

  
4
 

  
2
 
  
The program will cause a compilation error.
 

  
1
 
 
Question 9
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A { friend void i(int);
    int a;
public:
    A() : a(4) {}
    int f() { return a; }
};
void i(int a){
    a /= 2;
}
int main(void) {
    A a;
    i(a.a);
    cout << a.f() << endl;
    return 0;
}
 

  
4
 

  
1
 

  
The program will cause a compilation error.
 
  
2
 
 
Question 10
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class B;
class A {
    friend class B;
    int a;
public:    A() : a(4) {}
    void f(B &b,A &a);
    int out(void) { return a; }
};
class B {
    friend class A;
    int b;
public:    B() : b(2) {}
    void f(A &a) { a.a /= b; }
};
void A::f(B &b,A &a){ b.f(*this); }
int main(void) {
    A a;
    B b;
    a.f(b,a);
    cout << a.out() << endl;
    return 0;
}
 

  
2
 

  
1
 

  
4
 

  
The program will cause a compilation error.
 
Practice quiz 7

Question 1
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int a = 8, b = 0, c = 6;
    try {
        c = a / b;
    }
    cout << c << endl;
    return 0;
}
 

  
The program will cause a compilation error.
 

  
4
 

  
6
 

  
8
 
 
Question 2
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
int main(void) {
    int b = 4, *c = NULL, i = -1;
    try {
        c = new int [i];
        b--;
    } catch(exception &e) {
        c = new int[1];
        b++;
    }
    cout << b << endl;
    return 0;
}
 

  
4
 

  
3
 

  
The program will cause a compilation error.
 

  
5
 
 
Question 3
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public: static int a;
        A() { a++; }
};
int A::a = 1;
void f(void) {
    A a;
    throw string("?");
}
int main(void) {
    A a;
    try { f(); }
    catch (string &s) {
    }
    cout << A::a << endl;
    return 0;
}
 

  
5
 

  
4
 

  
The program will cause a compilation error.
 

  
3
 
 
Question 4
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public: static int a;
        A() { a++; }
};
int A::a = 0;
void f(void) {
    A a;
    throw A();
}
int main(void) {
    A a;
    try { f(); }
    catch (...) {
    }
    cout << A::a << endl;
    return 0;
}
 

  
5
 

  
4
 

  
The program will cause a compilation error.
 

  
3
 
 
Question 5
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
void f(void) {
    throw domain_error("err");
}
int main(void) {
    int a = 4;
    try { f(); }
    catch (runtime_error &e) {
        a--;
    }
    catch (...) {
        a++;
    }
    cout << a << endl;
    return 0;
}
 

  
The program will cause a compilation error.
 

  
3
 

  
4
 

  
5
 
 
Question 6
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <exception>
using namespace std;
void f(void) {
    throw exception("?");
}
int main(void) {
    int a = 4;
    try { f(); }
    catch (...) {
        a++;
    }
    catch (exception &e) {
        a--;
    }
    cout << a << endl;
    return 0;
}
 
  
5
 

  
The program will cause a compilation error.
 

  
4
 

  
3
 
 
Question 7
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <exception>
using namespace std;
int i = 1;
void f(void) {
    throw 1;
    i++;
}
void g(void) {
    i++;
    try { f(); }
    catch(int &i) {
        throw ++i;
    }
}
int main(void) {
    try { g(); i++; }
    catch(...) { i++; }
    cout << i << endl;
    return 0;
}
 
  
4
 

  
3
 

  
5
 

  
The program will cause a compilation error.
 
 
Question 8
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <exception>
using namespace std;
int i = 1;
void f(void) {
    i++;
    throw 1;
    i++;
}
void g(void) {
    i++;
    f();
    i++;    
}
int main(void) {
    try { g(); i++; }
    catch(...) { i++; }
    cout << i << endl;
    return 0;
}
 

  
5
 

  
4
 

  
The program will cause a compilation error.
 

  
3
 
 
Question 9
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
int i = 1;
class A : public logic_error {
public:    A() : logic_error("?") {}
};
class B : public logic_error {
public:    B() : logic_error("!") {}
};
void f(void) {
    i++;
    throw B();
    i++;
}
void g(void) {
    try { f(); }
    catch(A &a) { throw A(); }
}
int main(void) {
    try { g(); i++; }
    catch(logic_error &l) { i++; }
    catch(...) { i++; }
    cout << i << endl;
    return 0;
}
 

  
The program will cause a compilation error.
 

  
5
 

  
4
 

  
3
 
 
Question 10
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
int i = 3;
class A : public runtime_error {
public:    A() : runtime_error("?") {}
};
class B : public logic_error {
public:    B() : logic_error("!") {}
};
void f(void) {
    i++;
    throw B();
    i++;
}
void g(void) {
    try { f(); }
    catch(A &a) { throw A(); }
}
int main(void) {
    try { g(); i++; }
    catch(logic_error &l) { i++; }
    catch(...) { i++; }
    cout << i << endl;
    return 0;
}
 

  
4
 

  
The program will cause a compilation error.
 

  
3
 

  
5
 
Practice quiz 8

Question 1
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:    int v;
    A():v(1) {}
    A(int i):v(i) {}
    void operator<<(int a) { v+=a; }
};
int main(void) {
    A i = 2;
    i << 2;
    cout << i.v << endl;
    return 0;
}
 

  
2
 

  
4
 

  
The program will cause a compilation error.
 

  
1
 
 
Question 2
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:    int v;
    A():v(1) {}
    A(int i):v(i) {}
    void operator<<(int a) { v>>=1; }
};
ostream& operator<<(ostream &o, A &a) {
    return o<<a.v;
}
int main(void) {
    A i = 2;
    i << 2;
    cout << i << endl;
    return 0;
}
 

  
1
 

  
2
 

  
The program will cause a compilation error.
 

  
4
 
 
IncorrectQuestion 3
0 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:    int v;
    A():v(1) {}
    A(int i):v(i) {}
    void operator&&(int a) { v = -v; }
};
int main(void) {
    A i = 2;
    i && 2;
    cout << i << endl;
    return 0;
}
 

  
The program will cause a compilation error.
 

  
1
 

  
4
 

  
2
 
 
Question 4
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
enum e { a,b,c,d };
int main(void) {
    e f = e(a + c);
    cout << f << endl;
    return 0;
}
 

  
2
 

  
4
 

  
The program will cause a compilation error (or warning in some compilers),
 

  
1
 
 
Question 5
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
enum e { a=1,b,c,d };
e& operator++(e &x) {
    x = a; return x;
}
int main(void) {
    e f = c;
    cout << int(++f) << endl;
    return 0;
}
 

  
4
 

  
The program will cause a compilation error.
 

  
1
 

  
2
 
 
Question 6
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
enum e { a=-1,b=1,c,d };
enum i { f,g,h,a=-1 };
int main(void) {
    i j = i(f + h);
    cout << f << endl;
    return 0;
}
 

  
1
 

  
2
 

  
4
 

  
The program will cause a compilation error.
 
 
Question 7
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class Class {
public:    int operator() (int x, int y) {
        return x * y * y;
    }
};
int main(void) {
    Class object;

    cout << object(1,2) << endl;


    return 0;
}

  
The program will cause a compilation error.
 

  
4
 

  
2
 

  
1
 
 
Question 8
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
class A {
public:    int v;
    A():v(1) {}
    A(int i):v(i) {}
    void operator**(int a) { v *= a; }
};
int main(void) {
    A i = 2;
    i ** 2;
    cout << i.v << endl;
    return 0;
}
 

  
4
 

  
2
 

  
The program will cause a compilation error.
 

  
1
 
 
Question 9
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
enum e { a=-1,b=1,c,d };
int main(void) {
    e f = e(a + c);
    cout << f << endl;
    return 0;
}
 

  
4
 

  
1
 

  
2
 

  
The program will cause a compilation error (or warning in some compilers),
 
 
Question 10
1 / 1 pts
What is the output of the following program?
 
#include <iostream>
using namespace std;
enum e { a=1,b,c,d };
e& operator--(e &x) {
    x = b; return x;
}
int main(void) {
    e f = c;
    cout << int(f--) << endl;
    return 0;
}
 

  
3
 

  
1
 

  
The program will cause a compilation error (or warning in some compilers),
 

  
4
 

You might also like