What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main (int argc, const char * argv[])
{
int x,y;
union t
{
char tab[2];
int i;
};
union t u;
u.tab[0] = 1;
u.tab[1] = 2;
u.i
= 0;
x = u.tab[0];
y = u.tab[1];
cout << x << "," << y << "," << u.i;
return 0;
}
A.
compilation fails
B.
It prints: 0,0,0
C.
It prints: 1,2,0
D.
None of these
What is the output of the program?
#include
#include
using namespace std;
struct Person {
int age;
};
class First
{
Person *person;
public:
First() {person = new Person;
person?>age = 20;
}
void Print(){
cout << person?>age;
}
};
int main()
{
First t[2];
for (int i=0; i<2; i++)
t[i].Print();
}
A. It prints: 10
B. It prints: 2020
C. It prints: 22
D. It prints: 00
Which of the structures is incorrect?
1: struct s1{ int x; long int li; }; 2: struct s2{ float f; struct s2 *s; };
3:
struct s3{ float f; struct s3 s; };
A.
1
B.
2
C.
3
D.
2, 3
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main (int argc, const char * argv[])
{
int tab[5]={1,2,3};
for (int i=0; i<5; i++)
cout < return 0; } A. compilation fails B. It prints: 12300 C. It prints: 12345 D. It prints: 00000
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class Second;
class Base {
int age;
public:
Base () { age=5; };
friend void set(Base andob, Second andso);
void Print() { cout << age;}
};
class Second {
string name;
public:
friend void set(Base andob, Second andso);
void Print() { cout << name;}
};
void set(Base andob, Second andso) {
ob.age = 0; so.name = "Bill";
}
int main () {
Base a;
Second b;
set(a,b);
a.Print();
b.Print();
return 0;
}
A. It prints: 0Bill
B. Compilation error
C. It prints: Bill0
D. None of these
What happens when you attempt to compile and run the following code?
#include
using namespace std;
int f(int a, int b);
int main()
{
float b;
b = f(20,10);
cout << b;
return 0;
}
int f(int a, int b)
{
return a/b;
}
A. It prints: 2
B. It prints: 5
C. It prints: 10
D. It prints: 0
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
int f(int i);
int main()
{
int i=0;
i++;
for (i=0; i<=2; i++)
{
cout< } return 0; } int f(int a) { return a+a; } A. It prints: 202020 B. It prints: 012 C. It prints: 024 D. It prints: 0
What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class complex{
double re, im;
public:
complex() : re(1),im(0.3) {}
complex(double n) { re=n,im=n;};
complex(int m,int n) { re=m,im=n;}
complex operator+(complex andt);
void Print() { cout << re << " " << im; }
};
complex complex::operator+ (complex andt){
complex temp;
temp.re = this?>re + t.re;
temp.im = this?>im + t.im;
return temp;
}
int main(){
complex c1(1),c2(2),c3;
c3 = c1 + c2;
c3.Print();
}
A. It prints: 1 1.5
B. It prints: 2 1.5
C. It prints: 3 3
D. It prints: 0 0
What happens when you attempt to compile and run the following code?
A. It prints: 33
B. It prints: ?1
C. It prints: ??
D. It prints: ?3
Which definitions are correct?
A. int age;
B. int double;
C. char c;
D. int char;