Chapter 9 Exercise Set 1: CPE PracticeΒΆ
What is the output of the following program?
#include <iostream> using namespace std; int main() { int a[6]; for (int i = 0; i < 6; i++) a[i] = 2 * i + 4; cout << a[4] / a[1] << endl; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; int main() { int a = 2, b = 3; if (a > b && b > a) a++; if (a > b || b > a) b++; if (a | b) a++; if (a & b) b++; cout << a * b << endl; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; int main() { int a = 1, b = 2, c; a = a << b; b = b << a; c = b >> a; cout << c << endl; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; int main() { float a[3][3] = {{.1, 1., 10.}, {10., .1, 1.}, {.1, 10., 1.}}; float f = 10.; for (int i = 0; i < 3; i++) f *= a[i][i]; cout << f << endl; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; int main() { int nums[4] = {16, 8, 4, 2}; int *p1 = nums + 2, *p2 = p1 - 1; p1++; cout << *p1 << ' ' << *p2 << ' ' << p1 - p2 << ' '; cout << *p1 + nums[p1 - p2] << endl; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; void swap(int n, int m) { int t = n; n = m; m = t; } void swap(int* ip1, int* ip2) { int t = *ip1; *ip1 = *ip2; *ip2 = t; } int main() { int x = 7, y = 11; swap(x, y); cout << x << ' ' << y << ' '; swap(&x, &y); cout << x << ' ' << y << endl; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; int main() { int i = 0; for (float f = .008; f < 1e2; f *= 1e1) i++; cout << i << endl; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; char f1(char *p) { return ++(*p); } char *f2(char *p) { char c = *p - 3; *p = c; return p; } char *f3(char &n) { return &--n; } int main() { char mystery = 'z'; cout << f1(f2(f3(mystery))) << endl; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; int main() { int *ip[2] = {new int[2], new int[2]}; for (int i = 0; i < 4; i++) ip[i % 2][i / 2] = i * 3; cout << ip[0][0] << ' ' << ip[0][1] << ' '; cout << ip[1][0] << ' ' << ip[1][1] << endl; delete[] ip[0]; delete[] ip[1]; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; int main() { int *ia[3] = { new int[3], new int[3], new int[3] }, *ip; for (int i = 0; i < 3; i++) { ip = ia[i]; *ip = ip[1] = *(ip + 2) = i + 5; } cout << ia[0][0] << ' ' << ia[0][1] << ' ' << ia[0][2] << ' '; cout << ia[1][0] << ' ' << ia[1][1] << ' ' << ia[1][2] << ' '; cout << ia[2][0] << ' ' << ia[2][1] << ' ' << ia[2][2] << endl; delete[] ia[0]; delete[] ia[1]; delete[] ia[2]; return 0; }
What is the output of the following program?
#include <iostream> using namespace std; int main() { int* ia[3]; for (int i = 0; i < 3; i++) { ia[i] = new int[i + 1]; for (int j = 0; j < i + 1; j++) ia[i][j] = 10 * i + j + 2; } cout << ia[0][0] << endl; cout << ia[1][0] << ' ' << ia[1][1] << endl; cout << ia[2][0] << ' ' << ia[2][1] << ' ' << ia[2][2] << endl; for (int i = 0; i < 3; i++) delete[] ia[i]; return 0; }