Continuamos la serie sobre preguntas de preparación para el exámen de certificación de Java SCJP 1Z0-851.
Question 1
Given:
.class Alien {
. String invade(short ships) { return "a few"; }
. String invade(short... ships) { return "many"; }
.}
.class Defender {
. public static void main(String [] args) {
. System.out.println(new Alien().invade(7));
. }
.}
What is the result?
a. The output is not predictable.
b. a few
c. An exception is thrown at runtime.
d. Compilation fails.
e. many
Answer: D
Question 2
Given:
.class Polish {
. public static void main(String[] args) {
. int x = 4;
. StringBuffer sb = new StringBuffer("..fedcba");
. sb.delete(3,6);
. sb.insert(3, "az");
. if(sb.length() > 6) x = sb.indexOf("b");
. sb.delete((x-3), (x-2));
. System.out.println(sb);
.} }
What is the result?
Seleccione una respuesta.
a. ..fezba
b. An exception is thrown at runtime.
c. Compilation fails.
d. ..azba
e. .faza
f. .fazba
g. .fzba
Answer: D
Question 3
Given:
. class Eggs {
. int doX(Long x, Long y) { return 1; }
. int doX(long... x) { return 2; }
. int doX(Integer x, Integer y) { return 3; }
. int doX(Number n, Number m) { return 4; }
. public static void main(String[] args) {
. new Eggs().go();
. }
. void go() {
. short s = 7;
. System.out.print(doX(s,s) + " ");
. System.out.println(doX(7,7));
.} }
What is the result?
a. 2 1
b. 3 3
c. 1 1
d. 4 3
e. 3 1
f. 2 3
g. 4 1
Answer: D
Question 4
Given:
1. class Convert {
2. public static void main(String[] args) {
3. Long xL = new Long(456L);
4. long x1 = Long.valueOf("123");
5. Long x2 = Long.valueOf("123");
6. long x3 = xL.longValue();
7. Long x4 = xL.longValue();
8. Long x5 = Long.parseLong("456");
9. long x6 = Long.parseLong("123");
10. }
11. }
Which will compile using Java 5, but will NOT compile using Java 1.4? (Choose all that apply.)
a. Line 8
b. Line 4
c. Line 6
d. Line 9
e. Line 7
f. Line 5
Answer: B, E, A
Question 5
Given:
.import java.util.regex.*;
.class Regex2 {
. public static void main(String[] args) {
. Pattern p = Pattern.compile(args[0]);
. Matcher m = p.matcher(args[1]);
. boolean b = false;
. while(b = m.find()) {
. System.out.print(m.start() + m.group());
. }
. }
.}
And the command line:
java Regex2 "\d*" ab34ef
What is the result?
a. 12334567
b. 0123456
c. Compilation fails.
d. 01234456
e. 234
f. 2334
g. 334
Answer: D
Question 6
Given:
.class TKO {
. public static void main(String[] args) {
. String s = "-";
. Integer x = 343;
. long L343 = 343L;
. if(x.equals(L343)) s += ".e1 ";
. if(x.equals(343)) s += ".e2 ";
. Short s1 = (short)((new Short((short)343)) / (new Short((short)49)));
. if(s1 == 7) s += "=s ";
. if(s1 < new Integer(7+1)) s += "fly ";
. System.out.println(s);
. }
.}
Which of the following will be included in the output String s? (Choose all that apply.)
a. None of the above.
b. .e1
c. .e2
d. An exception is thrown at runtime.
e. fly
f. =s
g. Compilation fails.
Answer: C, E, F
Question 7
Given:
1. import java.text.*;
2. class DateOne {
3. public static void main(String[] args) {
4. Date d = new Date(1123631685981L);
5. DateFormat df = new DateFormat();
6. System.out.println(df.format(d));
7. }
8. }
And given that 1123631685981L is the number of milliseconds between Jan. 1, 1970, and sometime on Aug. 9, 2005, what is the result? (Note: the time of day in option A may vary.)
a. 8/9/05 5:54 PM
b. An exception is thrown at runtime.
c. 1123631685981L
d. Compilation fails due to a single error in the code.
e. Compilation fails due to multiple errors in the code.
Answer: E
Question 8
Which are true? (Choose all that apply.)
a. The DateFormat.getDate() is used to convert a String to a Date instance.
b. A single instance of NumberFormat can be used to create Number objects from Strings and to create formatted numbers from numbers.
c. Both Currency and NumberFormat objects must be constructed using static methods.
d. If a NumberFormat instance's Locale is to be different than the current Locale, it must be specified at creation time.
e. Both DateFormat and NumberFormat objects can be constructed to be Locale specific.
Answer: B, C, D, E
Question 9
Which will compile and run without exception? (Choose all that apply.)
a. System.out.printf("%d", 123.45);
b. System.out.printf("%d", 123);
c. System.out.format("%b", 123);
d. System.out.printf("%f", 123.45);
e. System.out.format("%s", new Long("123"));
f. System.out.printf("%f", 123);
g. System.out.format("%c", "x");
Answer: C, B, D, E
Question 10
Which about the three java.lang classes String, StringBuilder, and StringBuffer are true? (Choose all that apply.)
a. According to the API, StringBuffer will be faster than StringBuilder under most implementations.
b. The value of an instance of any of these three types can be modified through various methods in the API.
c. Objects of type StringBuffer are thread-safe.
d. All three classes have overloaded append() methods.
e. The "+" is an overloaded operator for all three classes.
f. All three classes have a length() method.
Answer: C, F