9 oct 2012
Preparación para Certificación SCJP 1Z0-851. Tema 1
Hace unos días que he empezado un cursillo para sacar una certificación de java 6, el antiguo CX-310-065, que ahora que Oracle ha decidido cambiar el código para estandarizar con sus formaciones pasa a ser el 1Z0-851.
Quiero compartir algo de material que voy a usar para la preparación del test, os dejo el test que he realizado sobre el tema 1, si buscáis estas mismas preguntas las podéis encontrar en otras webs.
1. Which is true? (Choose all that apply.)
A. The invocation of an object's finalize () method is always the last thing that happens before an object is garbage collected (GCed).
B. When a stack variable goes out of scope it is eligible for GC.
C. Some reference variables live on the stack, and some live on the heap.
D. Only objects that have no reference variables referring to them can be eligible for GC.
E. It 's possible to request the GC via methods in either java. Lang. Runtime or java.lang.System classes.
Answer:
C and E are correct. When an object has a reference variable, the reference variable lives inside the object, on the heap.
A is incorrect, because The order is different: First the object is collected. Then the object is finalized. B is incorrect-stack variables are not dealt with by the GC. D is incorrect because objects can live in "islands of isolation" and be GC eligible.
2. Which are valid declarations? (Choose all that apply.)
A. int $x;
B. int 123;
C. int _123;
D. int #dim;
E. int %percent;
F. int *divide;
G. int central_sales_region_Summer_2005_gross_sales;
Answer:
A, C, and G are legal identifiers.
B is incorrect because an identifier can't start with a digit. D, E, and F are incorrect because identifiers must start with $, _, or a letter.
3. Given:
1. class Maybe {
2. public static void main(String[] args) {
3. boolean b1 = true;
4. boolean b2 = false;
5. System.out.print(!false ^ false);
6. System.out.print(" " + (!b1 & (b2 = true)));
7. System.out.println(" " + (b2 ^ b1));
8. }
9. }
Which are true?
Seleccione al menos una respuesta.
a. Line 5 produces true.
b. Line 5 produces false.
c. Line 6 produces false.
d. Line 7 produces true.
e. Line 7 produces false.
f. Line 6 produces true.
Answer:
The exit is true false false. So A, C, E are true
4. class Fizz {
int x = 5;
public static void main(String args[]) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1, f2);
System.out.println((f1 == f3) + " " + (f1.x == f3.x));
}
static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6;
return z;
}
}
Answer:
The outcome for the above code is
'true true'
The reason given in the book is:
"The references f1, z and f3 all refer to the same instance of Fizz. The final modifier assures that a reference variable cannot be referred to a different object but final doesnt keep the objects state from changing."
5. Given the following code:
public class OrtegorumFunction { public int computeDiscontinuous(int x) { int r = 1; r += X; if ((x > 4) && (x < 10)) { r += 2 * x; } else (x <= 4) { r += 3 * x; } else { r += 4 * x; } r += 5 * x; return r; } public static void main(String [] args) { OrtegorumFunction o = new OrtegorumFunction(); System.out.println("OF(11) is: " + o.computeDiscontinuous (11)); } }
What is the result?
a. OF(11) is: 45
b. OF(11) is: 56
c. OF(11) is: 89
d. OF(11) is: 111
e. Compilation fails.
f. An exception is thrown at runtime.
Answer:
E is correct. The if statement is illegal. The if-else-else must be changed to if-else if-else , which would result in OF (11) is: 111 .
6. Given:
.class Foozit {
. public static void main(String[] args) {
. Integer x = 0;
. Integer y = 0;
. for(Short z = 0; z < 5; z++)
. if((++x > 2) || (++y > 2))
. x++;
. System.out.println(x + " " + y);
. }
.}
What is the result?
Seleccione una respuesta.
a. 8 3
b. 8 1
c. 10 3
d. 8 2
e. 5 3
f. 10 2
g. 5 1
h. 5 2
Answer:
The good is D, we have to see that the second expression of the if only evaluates when the first is false.
7. class Titanic {
. public static void main(String[] args) {
. Boolean b1 = true;
. boolean b2 = false;
. boolean b3 = true;
. if((b1 & b2) | (b2 & b3) & b3)
. System.out.print("alpha ");
. if((b1 = false) | (b1 & b3) | (b1 | b2))
. System.out.print("beta ");
. }
.}
What is the result?
Seleccione una respuesta.
a. An exception is thrown at runtime.
b. No output is produced.
c. Compilation fails.
d. alpha beta
e. alpha
f. beta
Answer: B
8. Given:
1. class Comp2 {
2. public static void main(String[] args) {
3. float f1 = 2.3f;
4. float[][] f2 = {{42.0f}, {1.7f, 2.3f}, {2.6f, 2.7f}};
5. float[] f3 = {2.7f};
6. Long x = 42L;
7. // insert code here
8. System.out.println("true");
9. }
10. }
And the following five code fragments:
F1. if(f1 == f2)
F2. if(f1 == f2[2][1])
F3. if(x == f2[0][0])
F4. if(f1 == f2[1,1])
F5. if(f3 == f2[2])
What is true?
Seleccione una respuesta.
a. Three of them will compile, exactly three will be true.
b. Three of them will compile, only one will be true.
c. Three of them will compile, exactly two will be true.
d. Two of them will compile, two will be true.
e. Two of them will compile, only one will be true.
f. One of them will compile, only one will be true.
Answer: B
9. Given:
.class Sixties {
. public static void main(String[] args) {
. int x = 5; int y = 7;
. System.out.print(((y * 2) % x));
. System.out.print(" " + (y % x));
. }
.}
What is the result?
Seleccione una respuesta.
a. 2 1
b. 1 1
c. Compilation fails.
d. An exception is thrown at runtime.
e. 4 1
f. 1 2
g. 2 2
h. 4 2
Answer: H
10. Given:
.class Mixer {
. Mixer() { }
. Mixer(Mixer m) { m1 = m; }
. Mixer m1;
. public static void main(String[] args) {
. Mixer m2 = new Mixer();
. Mixer m3 = new Mixer(m2); m3.go();
. Mixer m4 = m3.m1; m4.go();
. Mixer m5 = m2.m1; m5.go();
. }
. void go() { System.out.print("hi "); }
.}
What is the result?
Seleccione una respuesta.
a. Compilation fails
b. hi
c. hi hi
d. hi hi hi
e. hi hi, followed by an exception
f. hi, followed by an exception
Answer: E
11. Given:
1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = e1;
11. e1 = null;
12. }
13. Eco e;
14. }
At what point is only a single object eligible for GC?
Seleccione una respuesta.
a. After line 11 runs.
b. After line 10 runs.
c. After line 9 runs.
d. An exception is thrown at runtime.
e. After line 8 runs.
f. Compilation fails.
g. Never in this program.
Answer: D
12.
Given:
1. class Crivitch {
2. public static void main(String [] args) {
3. int x = 0;
4. // insert code here
5. do { } while (x++ < y);
6. System.out.println(x);
7. }
8. }
Which, inserted at line 4, produces the output 12?
Seleccione una respuesta.
a. int y = 12;
b. int y = x;
c. int y = 13;
d. int y = 10;
e. int y = 11;
f. None of the above will allow compilation to succeed.
Answer: E
13. Given:
.class Knowing {
. static final long tooth = 343L;
. static long doIt(long tooth) {
. System.out.print(++tooth + " ");
. return ++tooth;
. }
. public static void main(String[] args) {
. System.out.print(tooth + " ");
. final long tooth = 340L;
. new Knowing().doIt(tooth);
. System.out.println(tooth);
. }
.}
What is the result?
Seleccione una respuesta.
a. 343 340 340
b. 343 341 340
c. 343 341 343
d. Compilation fails.
e. 343 340 342
f. An exception is thrown at runtime.
g. 343 341 342
Answer: B
14. Given:
.class Feline {
. public static void main(String[] args) {
. Long x = 42L;
. Long y = 44L;
. System.out.print(" " + 7 + 2 + " ");
. System.out.print(foo() + x + 5 + " ");
. System.out.println(x + y + foo());
. }
. static String foo() { return "foo"; }
.}
What is the result?
Seleccione una respuesta.
a. 72 foo47 4244foo
b. 9 foo425 86foo
c. 9 foo47 86foo
d. 72 foo425 86foo
e. 72 foo47 86foo
f. Compilation fails.
g. 72 foo425 4244foo
h. 9 foo425 4244foo
i. 9 foo47 4244foo
Answer: D
15. Which are legal declarations? (Choose all that apply.)
Seleccione al menos una respuesta.
a. short [] y2 = [5];
b. short[5] x2;
c. short [] z [] [];
d. short z2 [5];
e. short [] y;
f. short x [];
Answer: C, E, F