C++ Essentials CPPE2: Part 2 Summary Test
-
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; }
111122
- Compilation fails
221111
112211
-
What is the output of the following snippet?
#include <iostream> using namespace std; class SupClass { public: void show(int par){ cout << par + 1;} }; class SubClass : public SupClass { public: void show(float par) { cout << par + 2;} }; int main() { SubClass o; o.show(2.0); }
2
1
4
3
-
What is the output of the following snippet?
#include <iostream> void fun(int *i) { *i = *i >> *i - 1; } using namespace std; int main() { int i = 2; fun(&i); cout << i; return 0; }
4
2
1
0
-
Which code, inserted into funtion
main
,generates the outputabba
? 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; }
cout << fun(*s);
cout << fun("abba");
cout << fun("ab");
cout << fun(s);
-
What is the output of the following snippet?
#include <iostream> using namespace std; class Zero { public: void out(){ cout << 0;} }; class One: public Zero { public: void out(){ cout << 1;} }; class Two : public Zero { public: void out(){ cout << 2;} }; int main() { Zero *obj; One obj1; obj = &obj1; obj->out(); Two obj2; obj = &obj2; obj->out(); return 0; }
00
12
01
02
-
Which of the following statements are correct about the following array?
char array[255];
- The array may be initialized at the time of declaration.
- The expression
tab[225]
designates the last element in the array. - The expression
tab[2]
designates the second element in the array. - The array can store 255 elements.
-
What is the output of the following snippet?
#include <iostream> using namespace std; void foo(int ¶meter) { parameter *= 2; } int main() { int var = 2; foo(var); cout << var; return 0; }
1
8
2
4
-
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; }
11
12
22
21
-
Which code, inserted into the
One class
, generates the output123
?#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(); }
static void foo(){ cout << 1; }
void foo(){ cout << 1; }
virtual void foo(){ cout << 1; }
- None of these
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int i = 2; if(i--==1) cout << i; else cout << i + 1; return 0; }
1
8
i + 1
2
-
What is the output of the following snippet?
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string s; s = "abcd"; s.append(s); s.resize(s.size() / 2); cout << s; return 0; }
abcdabcd
- It prints an empty string
- Compilation fails
abcd
-
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; }
1.1
- Compilation error
- None of these
2.2
-
Variable
c
in classB
, will be:class A { int a; protected: int b; public: int c; }; class B : public A { float f; public: void foo() { cout << f << c; } };
- protected
- public
- none of these
- private
-
What is the output of the following snippet?
#include <iostream> #include <string> using namespace std; int main() { string s1 = "foo"; string s2; getline(cin,s2); cout << s2.append(s1); return(0); }
foo
barfoo
bar
foobar
-
What is the output of the following snippet?
#include <iostream> using namespace std; int var = -1; int static Static(int i) { static int y = 0; y += ++i; return y; } int main() { var++; Static(var++); cout << var << Static(var); }
13
9
7
11
-
Which code, inserted into the
B
class, generates the outputef
?#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; }
cout << char('a' + r) << s;
cout << p << r;
cout << r << s;
cout << 'a' + r << s;
-
How can we pass argument to functions?
- None of these
- By default
- By invocation
- By telepathy
-
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; }
9
6
2
3
-
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,d); cout << c << d; return 0; }
1-2
-12
-1-2
12
-
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; }
-6
-5.5
0
-5
-
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; }
b
a
- Compilation fails
c
-
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; }
d
c
c
a
-
What is the output of the following snippet?
#include <iostream> using namespace std; int main() { int va1 = 0, *adr = &va1; cout << *va1; return 0; }
- 0
- 1
- Compilation fails
- It prints address of
val
-
Which code, inserted into the
main
function, generates the outputza
#include <iostream> using namespace std; namespace SpaceOne{char a = 'a';} namespace SpaceTwo{char a ='z';} int main(){ // insert code here return 0; }
cout << SpaceTwo::a << SpaceOne::a;
cout << a << a;
cout << SpaceOne::a <<SpaceTwo::a;
- None of these
-
Which code, inserted into the
main
function, generates the outputa 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; }
- None of these
using Space::a;
using namespace Space::a;
using namespace Space;
-
What is the output of the following snippet?
#include <iostream> #include <string> #include <exception> using namespace std; class a { public: virtual string whose() {return "mine";} }; class b { public: virtual string whose() {return "yours";} }; int main(){ a b; try{throw b;} catch(a& e){cout << e.whose() <<endl;} return 0; }
yours
mineyours
yoursmine
mine
-
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); }
A_A
A__Z
A_A_Z
A_A__Z
-
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; } }
It prints nothing
Hello world
Hello
Hello world !
-
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; }
- Compilation fails
20
02
22
-
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; }
void set(char c = 'd'){value = c;}
void set() {value = 'd';}
void set (char c){cout << c;}
void set (char c) {value = c;}
Subscribe
0 Comments
Newest