[ Pobierz całość w formacie PDF ]
Q' The value after the : is the if false return .
Casting
Q' Implicit casting (you write no code) happens when a widening conversion
occurs.
Q' Explicit casting (you write the cast) happens when a narrowing conversion
occurs.
Q' Casting a floating point to an integer type causes all digits to the right of the
decimal point to be lost (truncated).
Q' Narrowing conversions can cause loss of data the most significant bits
(leftmost) can be lost.
Logical Operators (Sun Objective 5.3)
Q' There are four logical operators: &, |, &&, ||.
Q' Logical operators work with two expressions that must resolve to boolean
values.
Q' The && and & operators returntrueonly if both operands are true.
Q' The || and | operators returntrueif either or both operands are true.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 3
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 3
Composite Default screen
Chapter 3: Operators and Assignments
50
Q' The && and || operators are known as short-circuit operators.
Q' The && operator does not evaluate the right operand if the left operand
is false.
Q' The || does not evaluate the right operand if the left operand is true.
Q' The & and | operators always evaluate both operands.
Passing Variables into Methods (Sun Objective 5.4)
Q' Methods can take primitives and/or object references as arguments.
Q' Method arguments are always copies of either primitive variables or
reference variables.
Q' Method arguments are never actual objects (they can be references to
objects).
Q' In practice, a primitive argument is a completely detached copy of the
original primitive.
Q' In practice, a reference argument is another copy of a reference to the
original object.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 3
Composite Default screen
Self Test
51
SELF TEST
The following questions will help you measure your understanding of the material presented in this
chapter. Read all of the choices carefully, as there may be more than one correct answer. Choose all
correct answers for each question.
Java Operators (Sun Objective 5.1)
1. Which two are equal? (Choose two.)
A. 32/4;
B. (8>>2)
C. 2^5;
D. 128>>>2;
E. (23);
F. 2>>5;
2. Given the following,
1. import java.awt.*;
2. class Ticker extends Component {
3. public static void main (String [] args) {
4. Ticker t = new Ticker();
5.
6. }
7. }
which two of the following statements, inserted independently, could legally be inserted into
line 5 of this code? (Choose two.)
A. booleantest=(Componentinstanceoft);
B. booleantest=(tinstanceofTicker);
C. booleantest=t.instanceof(Ticker);
D. booleantest=(tinstanceofComponent);
E. booleantest=t.instanceof(Object);
F. booleantest=(tinstanceofString);
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 3
Composite Default screen
Chapter 3: Operators and Assignments
52
3. Given the following,
1. class Equals {
2. public static void main(String [] args) {
3. int x = 100;
4. double y = 100.1;
5. boolean b = (x = y);
6. System.out.println(b);
7. }
8. }
what is the result?
A. true
B. false
C. Compilation fails
D. An exception is thrown at runtime
4. Given the following,
1. import java.awt.Button;
2. class CompareReference {
3. public static void main(String [] args) {
4. float f = 42.0f;
5. float [] f1 = new float[2];
6. float [] f2 = new float[2];
7. float [] f3 = f1;
8. long x = 42;
9. f1[0] = 42.0f;
10. }
11. }
which three statements are true? (Choose three.)
A. f1==f2
B. f1==f3
C. f2==f1[1]
D. x==f1[0]
E. f==f1[0]
5. Given the following,
1. class BitShift {
2. public static void main(String [] args) {
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 3
Composite Default screen
Self Test
53
3. int x = 0x80000000;
4. System.out.print(x + " and ");
5. x = x >>> 31;
6. System.out.println(x);
7. }
8. }
what is the output from this program?
A. -2147483648and1
B. 0x80000000and0x00000001
C. -2147483648and-1
D. 1and-2147483648
E. None of the above
6. Given the following,
1. class Bitwise {
2. public static void main(String [] args) {
3. int x = 11 & 9;
4. int y = x ^ 3;
5. System.out.println( y | 12 );
6. }
7. }
what is the result?
A. 0
B. 7
C. 8
D. 14
E. 15
7. Which of the following are legal lines of code? (Choose all that apply.)
A. intw=(int)888.8;
B. bytex=(byte)1000L;
C. longy=(byte)100;
D. bytez=(byte)100L;
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 3
Composite Default screen
Chapter 3: Operators and Assignments
54
Logical Operators (Sun Objective 5.3)
8. Given the following,
1. class Test {
2. public static void main(String [] args) {
3. int x= 0;
4. int y= 0;
5. for (int z = 0; z
6. if (( ++x > 2 ) || (++y > 2)) {
7. x++;
8. }
9. }
10. System.out.println(x + " " + y);
11. }
12. }
what is the result?
A. 53
B. 82
C. 83
D. 85
E. 103
F. 105
9. Given the following,
1. class Test {
2. public static void main(String [] args) {
3. int x= 0;
4. int y= 0;
5. for (int z = 0; z
6. if (( ++x > 2 ) && (++y > 2)) {
7. x++;
8. }
9. }
10. System.out.println(x + " " + y);
11. }
12. }
What is the result?
A. 52
B. 53
C. 63
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 3
Composite Default screen
Self Test
55
D. 64
E. 75
F. 85
10. Given the following,
1. class SSBool {
2. public static void main(String [] args) {
3. boolean b1 = true;
4. boolean b2 = false;
5. boolean b3 = true;
6. if ( b1 & b2 | b2 & b3 | b2 )
7. System.out.print("ok ");
8. if ( b1 & b2 | b2 & b3 | b2 | b1 )
9. System.out.println("dokey");
10. }
11. }
what is the result?
A. ok
B. dokey
C. okdokey
D. No output is produced
E. Compilation error
F. An exception is thrown at runtime
11. Given the following,
1. class Test {
2. public static void main(String [] args) {
3. int x=20;
4. String sup = (x
5. System.out.println(sup);
6. }
7. }
what is the result of compiling and running this code?
A. small
B. tiny
C. huge
D. Compilation fails
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 3
Composite Default screen
Chapter 3: Operators and Assignments
56
12. Given the following,
1. class BoolArray {
2. boolean [] b = new boolean[3];
3. int count = 0;
4.
5. void set(boolean [] x, int i) {
6. x[i] = true;
7. ++count;
8. }
9.
10. public static void main(String [] args) {
11. BoolArray ba = new BoolArray();
12. ba.set(ba.b, 0);
13. ba.set(ba.b, 2);
14. ba.test();
15. }
16.
17. void test() {
18. if ( b[0] && b[1] | b[2] )
19. count++;
20. if ( b[1] && b[(++count - 2)] )
21. count += 7;
22. System.out.println("count = " + count);
23. }
24. }
what is the result?
A. count=0
B. count=2
C. count=3
D. count=4
E. count=10
F. count=11
Passing Variables into Methods (Sun Objective 5.4)
13. Given the following,
1. class Test {
2. static int s;
3.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 3
Composite Default screen
Self Test
57
4. public static void main(String [] args) {
5. Test p = new Test();
6. p.start();
7. System.out.println(s);
[ Pobierz całość w formacie PDF ]