CPA Chapter 4 Assessment Answers 100%
1. What happens when you attempt to compile and run
the following code?
#include <iostream>
using namespace std;
int main() {
char t[3][3], *p = (char *)t;
for(int i = 0; i < 9; i++)
*p++ = ‘a’ + i;
cout << t[1][1];
return 0;
}
It prints g
It prints c
Compilation fails
It prints e
2. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
int main() {
float *ft[3] = { new float[3], new float[3], new float[3] }, *p;
for(int i = 0; i < 3; i++) {
p = ft[i];
*p = p[1] = *(p + 2) = 10 * i;
}
cout << ft[1][1];
delete [] ft[0];
delete [] ft[1];
delete [] ft[2];
return 0;
}
It prints 20
It prints 30
It prints 10
Compilation fails
, 3. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
int main() {
int *it[3];
for(int i = 0; i < 3; i++) {
it[i] = new int [i + 1];
for(int j = 0; j < i + 1; j++)
it[i][j] = 10 * i + j;
}
cout << it[2][2];
for(int i = 0; i < 3; i++)
delete [] it[i];
return 0;
}
It prints 33
It prints 22
It prints 11
Compilation fails
4. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
int main() {
short s = 1;
int i = 2;
long l = 3;
float f = 4.4;
double d = 6.6;
cout << s/i + f/i + d/s;
return 0;
}
It prints 4.4
Compilation fails
It prints 6.6
It prints 8.8
5. What happens when you attempt to compile and run the
following code?
1. What happens when you attempt to compile and run
the following code?
#include <iostream>
using namespace std;
int main() {
char t[3][3], *p = (char *)t;
for(int i = 0; i < 9; i++)
*p++ = ‘a’ + i;
cout << t[1][1];
return 0;
}
It prints g
It prints c
Compilation fails
It prints e
2. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
int main() {
float *ft[3] = { new float[3], new float[3], new float[3] }, *p;
for(int i = 0; i < 3; i++) {
p = ft[i];
*p = p[1] = *(p + 2) = 10 * i;
}
cout << ft[1][1];
delete [] ft[0];
delete [] ft[1];
delete [] ft[2];
return 0;
}
It prints 20
It prints 30
It prints 10
Compilation fails
, 3. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
int main() {
int *it[3];
for(int i = 0; i < 3; i++) {
it[i] = new int [i + 1];
for(int j = 0; j < i + 1; j++)
it[i][j] = 10 * i + j;
}
cout << it[2][2];
for(int i = 0; i < 3; i++)
delete [] it[i];
return 0;
}
It prints 33
It prints 22
It prints 11
Compilation fails
4. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
int main() {
short s = 1;
int i = 2;
long l = 3;
float f = 4.4;
double d = 6.6;
cout << s/i + f/i + d/s;
return 0;
}
It prints 4.4
Compilation fails
It prints 6.6
It prints 8.8
5. What happens when you attempt to compile and run the
following code?