CPA – Programming Essentials in C++ Chapter 2 Summary Test

Question 1:
What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
        char i = '1';
        switch(i)
        {
            case '1':
                cout<<"Hello ";
            case '2':
                cout<<"world "; break;
            case '3':
                cout<<"!";
        }        return 0;
}
 
Answer: Hello world


Question 2: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
    float x = 0.9, y=-0.5;
    int i,j = 1;
    i = x + j + y;
    cout << i;
    return 0;
}
Answer: 1


Question 3: What is the output of the following snippet?
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(void)
{
    string s;
    s = "abcd";
    s.append(s);
    s.resize(s.size() / 2);
    cout << s;
    return 0;
}
Answer: abcd


Question 4: What is the output of the following snippet?
#include <iostream>
using namespace std;
char fun(char *p)
{
    char c = *p;
    (*p)++;
    return c;
}
int main()
{
        char array[3]={'a', 'b', 'c'};
        fun(array + 1);
        cout << fun(array + 1);
        return 0;
}
Answer: c


Question 5: What is the output of the following snippet?
#include <iostream>
using namespace std;
class C1 {
friend class C2;
protected:  int y;
public:     int z;
private:    int x;
public:     C1() { x = y = z = 11; }
};
   
class C2 {
public: C1 a;
        C2 () { a.x = 22; };
        void foo() {
            cout << a.x << a.y << a.z;
        }
};
int main()
{
    C2 c;
    c.foo();
    return 0;
}
Answer: 221111


Question 6: Which code, inserted into function main, generates the output abba? Choose all correct answers.
#include <iostream>
#include <string>
using namespace std;
string fun(string s)
{
        return s.substr(0,1)+s.substr(1,1)+s.substr(1,1)+s.substr(0,1);
}
int main()
{
        string *s = new string("ab");
        //insert code here
        return 0;
}
Answer: cout << fun(“ab”);


Question 7: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
    char str[] = "Hello\0World\0";
    cout << str;
    return 0;
}
Answer: Hello


Question 8: What is the output of the following snippet?
#include <iostream>
    
using namespace std;
    
int main()
{
        int val = 0, *adr = &val;
        cout << *val;
    
    
        return 0;
}
Answer: Compilation fails


Question 9: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main (int argc, const char * argv[])
{
        double dbl = -5.55;
        cout << (int)dbl;
}

Answer: -5


Question 10: What is the output of the following snippet?
#include <iostream>
using namespace std;
   
int main (int argc, const char *argv[])
{
    int a = 1, b = 1, c = 1, i = 1;
    i = b < a < c;
    cout << i;
    return 0;
}
Answer: 1


Question 11: Which of the following is user defined data type? Choose all correct answers.
1:
        enum Enum {One, Two, Three};
    
2:
        int Int=2;
            
3:        
        char Char;
            
4:
        struct Struct
        {
                char Char;
                int Int;
        };
Answer: 1 and 4


Question 12: Which code, inserted into the Class, generates the output abcd?
#include <iostream>
using namespace std;
class Class {
public:
    static char value;
    Class() { value++; };
    ~Class () { value++; };
    //insert code here
    void print() { cout << value;}
};
   
char Class::value = 'a';
    
int main () {
    Class a,*b;
    b = new Class();
    b->set('a');
    b->print();
    delete b;
    a.print();
    a.set('c');
    a.print();
    a.set();
    a.print();
    return 0;
}
Answer:
void set(char c = ‘d’) { value = c; }


Question 13: Which of the following statements are correct about the following array?
char array[255];
Answer: The array can store 255 elements.




Question 14: Which code, inserted into the One class, generates the output 123?
#include <iostream>
using namespace std;
class One {
public:
    //insert code here
};
class Two : public One {
public:
        void foo(){ cout << 2; }
};
class Three : public Two {
public:
        void foo(){ cout << 3; }    
};
int main()
{
        One   o1;
        Two   o2;
        Three o3;
        One   *o = &o1;
        o->foo();
        o = &o2; o->foo();
        o = &o3; o->foo();
}
Answer: virtual void foo(){ cout << 1; }


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


Question 16: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main ()
{
    enum answer { yes, no, whoknows };
    enum answer a[3];
    a[0] = no;
    a[2] = yes;
    a[1] = whoknows;
    for(int i = 0; i < 3; i++)
      cout << a[i];
    return 0;
}

Answer: 120


Question 17: What is the output of the following snippet?
#include <iostream>
using namespace std;
int main()
{
        int a = -1, *p = &a;
        cout << ((p == NULL) ? 1.1 : 2.2);
        return 0;
}
Answer: 2.2


Question 18: What is the output of the following snippet?
#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    string s1[]= {"A","Z"};
    string s="";
   
    for (int i=0; i<2; i++) 
            cout << s.append(s1[i]).insert(1,"_");
    return( 0 );
}
Answer: A_A__Z


Question 19: What is the output of the following snippet?
#include <iostream>
using namespace std;
class foo {
        int p;
protected:
        int q;
public:
        int r;
};
    
    
class bar : public foo {
public:
        void assign() {
            p = q = r = 2;
        }
        void out() { cout << q << r; }
};
int main () {
        bar b;
        b.assign();
        b.out();
        return 0;
}

Answer: Compilation fails


Question 20: What is the output of the following snippet?
#include <iostream>
using namespace std;
void foo(int &parameter)
{
            parameter *= 2;
}
int main()
{
        int var = 2;
        foo(var);
        cout << var;
        return 0;
}
Answer: 4


Question 21: Which code, inserted into the B class, generates the output ef?
#include <iostream>
#include <string>
using namespace std;
class Alpha {
        int p;
protected:
        int q;
public:
        int r;
        Alpha():p(2),q(3),r(4) {}
};
class Beta : public Alpha {
        string s;
public:
        int y;
        void assign() {  y = 4; s = "f";   }
        void out() {
            // insert code here
        }
};
int main () {
        Beta b;
        b.assign();
        b.out();
        return 0;
}
Answer: cout << char(‘a’ + r) << s;


Question 22: What is the output of the following snippet?
#include <iostream>
using namespace std;
class one {  
public :
    void foo() { cout << 1; }  
};
class two {
public :
    void foo() { cout << 2; }   
};
int main() {
    two objects[2];
    two *object = objects;
    for(int i = 0; i < 2; i++)
        (object++)->foo();
    return 0;
}
Answer: 22


Question 23: How can we pass arguments to functions?
Answer: By default


Question 24: What is the output of the following snippet?
#include <iostream>
using namespace std;
int sub(int x, int y)
{
        x -= y;
        return x;
}
int main()
{
        int a = 0, b = 1, c, d;
        c = sub(a,b);
        d = sub(c,b);
        cout << c << d;
        return 0;
}
Answer: -1-2

Question 25: What is the output of the following snippet?
#include <cstdlib>
#include <iostream>
using namespace std;
char c;
char* inc(char par1, int par2)
{
        c = par1 + par2;
        return &c;
}
 
 
int main()
{
        int a = 'a', b = 3;
        char *f;
        f = inc(a,b);
        cout << *f;
        return 0;
}
Answer: d

Question 26:
What is the output of the following snippet?
#include <iostream>
using namespace std;
int main(){
    int *Int = new int;
    *Int = 1 / 2 * 2 / 1. * 2. / 4 * 4;
    cout << *Int;     
    return 0;
}
Answer: 0


Question 27: What is the output of the following snippet?
#include <iostream>
#include <string>
using namespace std;
int f(int a)
{
        return a + a;
}
int main()
{
        int i = 0;
        for(int a = 0; a < 2; a++)
            i = f(i + 1);
        cout << i;
        return 0;
}
Answer: 6


Question 28: Which code, inserted into the main function, generate the output a 2?
#include <iostream>
using namespace std;
namespace Space
{  char a = 'a', b = 'b';  }
int a = 1, b = 2;
int main () {
    
    // insert code here
    cout << a << " " << b;
    return 0;
}
Answer: using Space::a;


Question 29: Variable c in class B, will be:
class A {
    int a;
protected:
    int b;
public:
    int c;
};      
class B : public A {
    float f;
public:
    void foo() {
        cout << f << c;
    }
};
Answer: Public
Question 30: Which code, inserted into the main function, generates the output za
#include <iostream>
using namespace std;
namespace SpaceOne {  char a = 'a'; }
namespace SpaceTwo {  char a = 'z'; }
int main () {
    // insert code here
    return 0;
}
Answer: cout << SpaceTwo::a << SpaceOne::a;

Question 31:
What is the output of the following snippet?
#include <iostream>
using namespace std;
class One
{
    public:
            void out(){ cout<<"One";}
};
    
int main()
{
    One arr[2];
    for(int i = 0; i < 1; i++)
            arr[i].out();
}
Answer: One

Post a Comment

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