Name:
The code below declares and defines variable x. True or False ?
extern int x;
In namespace foo, the function bar can access the variable x also declared in namespace foo ? True or False ?
#include <iostream>
namespace foo
{
void bar()
{
x++;
}
int x;
}
int main(int argc, char** argv)
{
return 0;
}
What is the output of the following program ?
#include <iostream>
using namespace std;
void swap1( int a, int b ) { int c=a; a=b; b=c; }
void swap2( int *a, int *b ) { int c=*a; *a=*b; *b=c; }
void swap3( int &a, int &b ) { int &c=a; a=b; b=c; }
int main( int argc, char **argv )
{
int a, b;
a = 1; b = 2; swap1(a,b);
cout << "a: " << a << ", " <<"b: " << b << endl;
a = 1; b = 2; swap2(&a,&b);
cout << "a: " << a << ", " <<"b: " << b << endl;
a = 1; b = 2; swap3(a,b);
cout << "a: " << a << ", " <<"b: " << b << endl;
}
What is the output of the program ?
#include <iostream>
struct A { unsigned int color; };
struct B : public A { };
struct C : public A { };
struct D : public B, public C { };
int main(int argc, char** argv)
{
D d;
d.color = 3;
std::cout << d.color << std::endl;
return 0;
}
How many times is "Hello World" printed by this program ?
#include <iostream>
struct A { A() { std::cout << "Hello World" << std::endl; } };
struct A1 : public A { };
struct A2 : public A { };
struct A3 : public A { };
struct A4 : public A { };
struct B : public A1, public A2, public A3, public A4 { };
int main(int argc, char** argv)
{
B b;
return 0;
}
What is the value of x, y & z ?
#include <iostream>
struct A
{
A(int n) : x(n++), y(n++), z(n++) {}
int x;
int y;
int z;
};
int main(int argc, char** argv)
{
Foo f(3);
std::cout << "x: " << f.x << std::endl;
std::cout << "y: " << f.y << std::endl;
std::cout << "z: " << f.z << std::endl;
return 0;
}
What value gets printed by the program?
#include <iostream>
int main(int argc, char** argv)
{
int x = 0;
int y = 0;
if (x++ && y++)
{
y += 2;
}
std::cout << x + y << std::endl;
return 0;
}
Which lines below should not compile ?
struct A
{
A(int x) : n(x) {}
int n;
};
int main(int argc, char** argv)
{
A a1;
A a2(2);
A a3(a2);
return 0;
}
Which of the following implementations of the reset function is best for initializing the array to all zero.
class foo{
public:
foo(){
reset();
}
private:
void reset(){
// A // memset(x, 0, 50);
// B // memset(x, 0, sizeof(x));
// C // memset(x, 0, 50 * 4);
// D // memset(x, 0, 50 * sizeof(x));
}
long x[50];
};
What is the output of the program ?
#include <iostream>
int main(int argc, char** argv)
{
// assume address of x is 0x822222222
int x = 3;
int*& rpx = &x;
std::cout << rpx << std::endl;
return 0;
}