Data Assignment, Comparison, and Calculation
For each question below, choose the letter that best applies:
a) does not compile
b) type promotion
c) explicit casting
d) type promotion and explicit casting
e) compiles, but does use neither type promotion nor explicit casting
Answers
1) int a = 5;
int b = a;
2) double a = 5.0;
int b = a;
3) int a = 5;
double b = a;
4) double a = 5.0;
int b = (double)a;
5) double a = 5;
double b = a;
6) double a = 5.0;
int b = (int)a;
7) double a = 5;
int b = (int)a;
8) char c = 65;
9) char c = 'A';
10) char c = '65';
11) char c = '6';
12) int i = 6;
char c = i;
13) int i = 6;
char c = (int)i;
14) int i = 6;
char c = (char)i;
15) int i = 6;
char c = (char)i;
double d = c;
16) char c = '6';
double d = c;
17) char c = '6';
double d = c;
c = d;
18) char c = '6';
double d = c;
c = (char)d;
For exercises 19 - 26, assume that classes Super and Sub have been properly declared
Answers
19) Super s1;
Super s2;
s1 = s2;
20) Sub s1;
Super s2;
s1 = s2;
21) Super s1;
Sub s2;
s1 = s2;
22) Sub s1;
Super s2;
s1 = (Sub)s2;
23) Sub s1;
Super s2;
s1 = (Super)s2;
24) Super s1;
Sub s2;
s1 = s2;
s2 = s1;
25) Sub s1;
Super s2;
s1 = s2;
s2 = (Sub)s1;
26) Sub s1;
Super s2;
s1 = (Sub)s2;
s2 = s1;
Questions 27 - 40 assume the following has already been done:
Answers
int i;
double d;
char c;
27) (i <= i + 1)
28) (i == i - 2.3)
29) i = ++i;
30) ((double)i != 12.0)
31) ((dobule)i != 12)
32) ('c' <= 5)
33) ((double)d <= d)
34) ((double)((char)i) < 18.3)
35) (i + d > c)
36) ((c + i > 20) || (d - i == (char)0))
37) (true || false)
38) (false && (d < i))
39) c++;
40) java;
Give the resulting value of the following operations, provided the following
declarations and definitions.
Answers
int i = 2;
int j = 8;
double m = 1.6;
41) j / m;
42) j % i;
43) (int)m + i;
44) i * 1.0;
45) j / i;
46) i / j;
47) i / (double)j;
48) (int)(i / (double)j + m);
49) (int)m * m;
50) (int)m * (int)mm
51) (int)(m * m);
52) (int)m / i + (double)j;