simulador 4
DESCRIPTION
javaTRANSCRIPT
Página 1 de 24
Examen: 1Z0-803 Oracle Certified Associate, Java SE 7 Programmer
Examen 4 de 10.
Calificación Global: 22.0%
No. Objetivo de certificación Porcentaje obtenido1 Using Operators and Decision Constructs 55.0%
2 Creating and Using Arrays 40.0%
3 Using Loop Constructs 17.0%
4 Working with Methods and Encapsulation 20.0%
5 Working with Inheritance 0.0%
6 Handling Exceptions 0.0%
7 Java Basics 17.0%
8 Working With Java Data Types 11.0%
Objective 1:
Using Operators and Decision Constructs
Use Java operators
Página 2 de 24
Use parenthesis to override operator precedenceTest equality between Strings and other objects using == and equals ()Create if and if/else constructsUse a switch statement
Objective 2:
Creating and Using Arrays
Declare, instantiate, initialize and use a one-dimensional arrayDeclare, instantiate, initialize and use multi-dimensional arrayDeclare and use an ArrayList
Objective 3:
Using Loop Constructs
Create and use while loopsCreate and use for loops including the enhanced for loopCreate and use do/while loopsCompare loop constructsUse break and continue
Objective 4:
Working with Methods and Encapsulation
Create methods with arguments and return valuesApply the static keyword to methods and fields Create an overloaded methodDifferentiate between default and user defined constructorsCreate and overload constructorsApply access modifiersApply encapsulation principles to a classDetermine the effect upon object references and primitive values when they are passed into methods that changethe values
Objective 5:
Working with Inheritance
Implement inheritanceDevelop code that demonstrates the use of polymorphismDifferentiate between the type of a reference and the type of an objectDetermine when casting is necessaryUse super and this to access objects and constructorsUse abstract classes and interfaces
Objective 6:
Handling Exceptions
Differentiate among checked exceptions, RuntimeExceptions and Errors
Página 3 de 24
Create a try-catch block and determine how exceptions alter normal program flowDescribe what Exceptions are used for in JavaInvoke a method that throws an exceptionRecognize common exception classes and categories
Objective 7:
Java Basics
Define the scope of variablesDefine the structure of a Java classCreate executable Java applications with a main methodImport other Java packages to make them accessible in your code
Objective 8:
Working With Java Data Types
Declare and initialize variablesDifferentiate between object reference variables and primitive variablesRead or write to object fieldsExplain an Object's Lifecycle (creation, "dereference" and garbage collection)Call methods on objectsManipulate data using the StringBuilder class and its methodsCreating and manipulating Strings
A modo de retroalimentación, te enlistamos las preguntas que fueron mal contestadas en esteejercicio de simulador:
Question 1
Consider the following two classes defined in two .java files.
//in file /root/com/foo/X.java
package com.foo;
public class X {
public static int LOGICID = 10;
public void apply(int i) {
System.out.println("applied");
}
}
//in file /root/com/bar/Y.java
package com.bar;
Página 4 de 24
//1 <== INSERT STATEMENT(s) HERE
public class Y {
public static void main(String[] args) {
System.out.println(X.LOGICID);
}
}
What should be inserted at //1 so that Y.java can compile without any error?
A.- import static X;
B.- import static com.foo.*;
C.- import static com.foo.X.*;
D.- import com.foo.*;
E.- import com.foo.X.LOGICID;
Question 2
Which of the following keywords may occur multiple times in a Java source file?(Choose four)
A.- import
B.- class
C.- private
D.- package
E.- public
Question 3
The options below contain the complete contents of a file (the name of the file is not specified). Which of these optionscan be run with the following command line once compiled?java main
A.-
//in file main.javaclass main {public void main(String[] args) {System.out.println("hello");}}
B.-
//in file main.javapublic static void main4(String args) {System.out.println("hello");}
C.-
//in file main.javapublic class anotherone{}class main {public static void main(String args) {System.out.println("hello");
Página 5 de 24
}}
D.-
//in file main.javaclass anothermain{public static void main2(String[] args) {System.out.println("hello2");}}class main {public final static void main(String[] args) {System.out.println("hello");}}
Question 4
Which of the following are keywords in Java? (Choose four)
A.- default
B.- NULL
C.- String
D.- throws
E.- long
F.- strictfp
Question 5
Given the following class, which of these are valid ways of referring to the class from outside of the packagecom.enthu?(Choose two)package com.enthu;public class Base{// ....// lot of code...}
A.- Base
B.- By importing the package com.* and referring to the class as enthu.Base
C.- importing com.* is illegal.
D.- By importing com.enthu.* and referring to the class as Base.
E.- By referring to the class as com.enthu.Base.
Question 6
Given:class EJavaGuruString2 {public static void main(String args[]) {String ejg = "game";ejg.replace('a', 'Z').trim().concat("Aa");ejg.substring(0, 2);
Página 6 de 24
System.out.println(ejg);}}What is the output?
A.- gZmeAZ
B.- gZmeAa
C.- gZm
D.- gZ
E.- game
Question 7
Which two are legal declaration and initialization of StringBuilder?
A.- StringBuilder s="A";
B.- StringBuilder s=new StringBuilder("A");
C.- StringBuilder s= new StringBuilder(128);
D.- StringBuilder s=new String("A");
Question 8
Given:public class Saludo2{public static void main(String[] args){String saludo = "Hola ";saludo.concat("Maria");saludo.toUpperCase();System.out.prinltn(saludo);}}How many object are created in the method main ?
A.- 1
B.- 2
C.- 3
D.- 4
Question 9
Given the following code fragment:if (value >= 0){if (value != 0) System.out.print("the ");elseSystem.out.print("quick ");if (value < 10)System.out.print("brown ");if (value > 30)System.out.print("fox ");
Página 7 de 24
else if (value < 50)System.out.print("jumps ");else if (value < 10)System.out.print("over ");elseSystem.out.print("the ");if (value > 10)System.out.print("lazy ");} elseSystem.out.print("dog");System.out.print("… ");What is the result if the integer value is 33?
A.- the fox jump lazy …
B.- the fox lazy …
C.- quick fox over lazy …
D.- quick fox the …
Question 10
Given the code fragment:String color = "Red";switch(color) {case "Red":System.out.println("Found Red");case "Blue":System.out.println("Found Blue");break;case "White":System.out.println("Found White");break;default:System.out.println("Found Default");}What is the result?
A.- Found Red
B.-Found RedFound Blue
C.-Found RedFound BlueFound White
D.-
Found RedFound BlueFound WhiteFound Default
Question 11
Which of the following sentences is suitable for testing a value in a switch construct?
Página 8 de 24
A.- The switch construct tests whether values are greater than or less than a single value.
B.- The switch construct tests against a single variable.
C.- The switch construct tests the value of a float, double, or boolean data type and String.
D.- The switch construct tests only primitive values
Question 12
public static void main(String[] args){ String message = "Hi everyone!";System.out.println(message = message.replace("X", "e"));}What is the result?
A.- message = Hi everyone!
B.- message = Hi XvXryonX!
C.- Hi everyone!
D.- Hi XvXryonX!
E.- A compile time error is produced.
Question 13
Given:public class Point2D {private int x, y;public Point2D(int x, int y) {this.x = x;y=y;}public String toString() {return "[" + x + ", " + y + "]";}public static void main(String[] args) {Point2D point = new Point2D(10, 20);System.out.println(point);}}Which one of the following options provides the output of this program when executed?
A.- point
B.- Point
C.- [0, 0]
D.- [10, 0]
E.- [10, 20]
Question 14
Consider the following program:class Increment {
Página 9 de 24
public static void main(String []args) {Integer i = 10;Integer j = 11;Integer k = ++i; // INCRSystem.out.println("k == j is " + (k == j));System.out.println("k.equals(j) is " + k.equals(j));}}Which one of the following options correctly describes the behavior of this program?
A.-When executed, this program printsk == j is falsek.equals(j) is false
B.-When executed, this program printsk == j is truek.equals(j) is false
C.-When executed, this program printsk == j is falsek.equals(j) is true
D.-When executed, this program printsk == j is truek.equals(j) is true
E.- When compiled, the program will result in a compiler error in the line marked with the comment INCR.
Question 15
public class Point2D {private int x=10, y=20;public Point2D(int x, int y) {this();}public Point2D() {y=0;x=0;}public String toString() {return "[" + x + ", " + y + "]";}public static void main(String[] args) {Point2D point = new Point2D(10, 0);System.out.println(point);}}Which one of the following options provides the output of this program when executed?
A.- point
B.- Point
C.- [0, 0]
D.- [10, 0]
E.- [10, 20]
Página 10 de 24
Question 16
public static void main(String []args) {ArrayList<String> myArrList = new ArrayList<>();myArrList.add("One");myArrList.add("Two");ArrayList<String> yourArrList = new ArrayList<>();yourArrList.add("Three");yourArrList.add("Four");myArrList.addAll(1, yourArrList);for (String val : myArrList)System.out.println(val);What is the result?
A.-OneThreeFour
B.-
OneTwoThreeFour
C.-
OneThreeFourTwo
D.- compilation fails
Question 17
public static void main(String []args) {ArrayList<String> myArrList = new ArrayList<String>();String one = "One";String two = new String("Two");myArrList.add(one);myArrList.add(two);ArrayList<String> yourArrList = myArrList;one.replace("O", "B");for (String val : myArrList)System.out.print(val + ":");for (String val : yourArrList)System.out.print(val + ":");}What is the result?
A.- One:Two:One:Two:
B.- Bne:Two:Bne:Two:
C.- One:Two:Bne:Two:
D.- Bne:Two:One:Two:
Página 11 de 24
Question 18
public static void main(String []args) {ArrayList myArrList = new ArrayList<>();StringBuilder one = new StringBuilder("One");String two = new String("Two");myArrList.add(one);myArrList.add(two);ArrayList yourArrList = myArrList;one.append("ww");for (Object val : myArrList)System.out.print(val + ":");for (Object val : yourArrList)System.out.print(val + ":");}
A.- Oneww:Two:Oneww:Two:
B.- One:Two:One:Two:
C.- Oneww:Two:One:Two:
D.- One:Two:Oneww:Two:
Question 19
Which two statements are true? (Choose two.)
A.- do-while loop Code executes at least once, even if the while condition initially evaluates to false.
B.- while loop Code never executes if while condition initially evaluates to false.
C.- The enhanced for loop can be used to iterate over multiple collections or arrays in the same loop.
D.- do-while loop Code executes at least once, even if the while condition initially evaluates to true.
Question 20
Given the code fragment:first:for (int i = 0; i < 4; ++i){ second: for (int j = 0; j < 4; ++j) { third: for (int k = 0; k < 4; ++k) { System.out.println ("inner start: i+j+k " + (i + j + k)); if (i + j + k == 5) continue third; if (i + j + k == 7) continue second; if (i + j + k == 8) break second; if (i + j + k == 9)
Página 12 de 24
break first; System.out.println ("inner stop: i+j+k " + (i + j + k)); } } }What is the result?
A.- The code runs with no output.
B.- Compilation fails.
C.-
inner start: i+j+k 0inner stop: i+j+k 0inner start: i+j+k 1inner start: i+j+k 1...
D.-
inner start: i+j+k 0inner stop: i+j+k 0inner start: i+j+k 1inner start: i+j+k 1inner start: i+j+k 0inner stop: i+j+k 0...
E.- An IllegalArgumentException is thrown at run time.
Question 21
Given the code fragment:int n;for(n = 0; n < 10; ++n) { continue;}System.out.println(n);
What is the result?
A.- 11
B.- The code runs with no output.
C.- Compilation fails.
D.- 10
Question 22
Given the code fragment:for(int n = 0; n < 10; ++n) { break;}System.out.println(n);
What is the result?
Página 13 de 24
A.- 0
B.- 10
C.- An IllegalArgumentException is thrown at run time
D.- Compilation fails.
Question 23
Given the code fragment:boolean isTrue = true;outer: for (int i = 0; i < 5; i++) {while (isTrue) {System.out.println("Hello");break outer;}System.out.println("Outer loop");}System.out.println("Good Bye")What is the result?
A.-HelloOuter loopGood Bye
B.-HelloGood Bye
C.-
HelloHelloHelloGood Bye
D.- An exception is thrown at runtime.
E.- Compilation fails.
Question 24
Given:class X {static void m(int j) {j = 7;if(false)return;j=19;}public static void main (String [] args) {int j = 12; m (j);System.out.println(j);}}What is the result?
A.- 7
Página 14 de 24
B.- 12
C.- 19
D.- Compilation fails
E.- An exception is thrown at run time
Question 25
Given the code fragment:public void localVariableInLoop() {for (int ctr = 0; ctr < 5; ++ctr) {System.out.println(ctr);}System.out.println(ctr);}What is the result?
A.- 01234
B.- 012345
C.- Compilation fails.
D.- An exception is thrown at run time.
Question 26
What is the output of the following code?class EJavaGuruPassPrim {public static void main(String args[]) {int ejg = 10;anotherMethod(ejg);System.out.println(ejg);someMethod(ejg);System.out.println(ejg);}static void someMethod(int val) {++val;System.out.println(val);}static void anotherMethod(int val) {val = 20;System.out.println(val);}}
A.-
20101111
B.-
20201110
Página 15 de 24
C.-
20101110
D.- Compilation error.
Question 27
Which class has a default constructor?class X{}class Y{Y(){}}class Z{Z(int i){}}
A.- X only
B.- Y only
C.- Z only
D.- X and Y
E.- Y and Z
F.- X and Z
G.- X, Y and Z
Question 28
Given:public class Circle{ double radius;public double area;public Circle (double r) { radius = r;}public double getRadius() {return radius;}public void setRadius(double r) { radius = r;}public double getArea() { return /* ??? */;}}class App {public static void main(String[] args){ Circle c1 = new Circle(17.4);c1.area = Math.PI * c1.getRadius() * c1.getRadius();}}This class is poorly encapsulated. You need to change the circle class to compute and return the area instead. What threemodifications are necessary to ensure that the class is being properly encapsulated?(Choose three)
A.-
Change the access modifier of the radius to private
B.-Change the getArea () method:public double getArea () { return area; }
C.-
When the radius is set in the Circle constructor and the setRadius () method, recomputed the area and store it into thearea field.
D
Change the getRadius () method:public double getRadius () {area = Math.PI * radius * radius;
Página 16 de 24
.-return radius;}
Question 29
Which two statements are benefits of encapsulation?
A.- allows a class implementation to change without changing the clients
B.- protects confidential data from leaking out of the objects
C.- prevents code from causing exceptions
D.- permits classes to be combined into the same package
E.- enables multiple instances of the same class to be created safely
Question 30
Given:public class Basic {private static int letter;public static int getLetter();public static void Main(String[] args){System.out.println(getLetter());}}Why will the code not compile?
A.- A static field cannot be private.
B.- The getLetter method has no body.
C.- There is no setletter method.
D.- The letter field is uninitialized.
E.- It contains a method named Main instead of main.
Question 31
public class Example {private static int letter;public static int getLetter(){ return 1;}
}class Example2{ public static void main(String[] args){System.out.println(Example.letter);}}What is the result?
A.- Compilations fails
Página 17 de 24
B.- An exception is thrown at runtime
C.- No output
D.- 0
E.- 1
Question 32
public class Example {private int letter;
public Example(int letter) { }
public int getLetter(){ return letter;}public static void main(String[] args){ Example ej = new Example(2); ej.letter = 1; System.out.print(ej.letter); System.out.print(ej.getLetter());}}What is the result?
A.- Compilations fails
B.- An exception is thrown at runtime
C.- No output
D.- 0 0
E.- 1 1
F.- 1 2
G.- 2 1
Question 33
public class Example {private int letter;
public Example(int letter) { letter = letter; }
public int getLetter(){ return letter;}
Página 18 de 24
}
class Example2 { public static void main(String[] args){ Example ej = new Example(1); System.out.println(ej.getLetter());}}What is the result?
A.- Compilations fails
B.- An exception is thrown at runtime
C.- No output
D.- 0
E.- 1
Question 34
public class Circle{ float radius;public double area;public Circle (float r) { radius = r;}public float getRadius() {return radius;}public void setRadius(float r) { radius = r;System.out.println(r);}public double getArea() { return area;}}class App {public static void main(String[] args){ Circle c1 = new Circle(17.4);c1.area = Math.PI * c1.getRadius() * c1.getRadius();}}What is the result?
A.- 951.1485500957435
B.- no output
C.- Compilation fails
D.- 17.4
Question 35
Given:1. interface DoStuff2 {2. float getRange(int low, int high); }3.4. interface DoMore {5. float getAvg(int a, int b, int c); }6.
Página 19 de 24
7. abstract class DoAbstract implements DoStuff2, DoMore { }8.9. class DoStuff implements DoStuff2 {10. public float getRange(int x, int y) { return 3.14f; } }11.12. interface DoAll extends DoMore {13. float getAvg(int a, int b, int c, int d); }What is the result?
A.- The file will compile without error.
B.- Compilation fails. Only line 7 contains an error.
C.- Compilation fails. Only line 12 contains an error.
D.- Compilation fails. Only line 13 contains an error.
E.- Compilation fails. Only lines 7 and 12 contain errors.
F.- Compilation fails. Only lines 7 and 13 contain errors.
G.- Compilation fails. Lines 7, 12, and 13 contain errors.
Question 36
Given:21. abstract class C1 {22. public C1() { System.out.print(1); }23. }24. class C2 extends C1 {25. public C2() { System.out.print(2); }26. }27. class C3 extends C2 {28. public C3() { System.out.println(3); }29. }30. public class Ctest {31. public static void main(String[] a) { new C3(); }32. }What is the result?
A.- 3
B.- 23
C.- 32
D.- 123
E.- 321
F.- Compilation fails.
G.- An exception is thrown at runtime.
Question 37
Given:11. class Alpha {12. public void foo() { System.out.print("Afoo "); }13. }14. public class Beta extends Alpha {
Página 20 de 24
15. public void foo() { System.out.print("Bfoo "); }16. public static void main(String[] args) {17. Alpha a = new Beta();18. Beta b = (Beta)a;19. a.foo();20. b.foo();21. }22. }What is the result?
A.- Afoo Afoo
B.- Afoo Bfoo
C.- Bfoo Afoo
D.- Bfoo Bfoo
E.- Compilation fails.
F.- An exception is thrown at runtime.
Question 38
Given:11. abstract class Vehicle { public int speed() { return 0; }}12. class Car extends Vehicle { public int speed() { return 60; }}13. class RaceCar extends Car { public int speed() { return 150; }} ......21. RaceCar racer = new RaceCar();22. Car car = new RaceCar();23. Vehicle vehicle = new RaceCar();24. System.out.println(racer.speed() + ", " + car.speed()25. + ", " + vehicle.speed());What is the result?
A.- 0, 0, 0
B.- 150, 60, 0
C.- Compilation fails.
D.- 150, 150, 150
E.- An exception is thrown at runtime.
Question 39
Given:10. public class SuperCalc {11. protected static int multiply(int a, int b) { return a * b;}12. }and:20. public class SubCalc extends SuperCalc{21. public static int multiply(int a, int b) {22. int c = super.multiply(a, b);23. return c;24. }25. }
Página 21 de 24
and:30. SubCalc sc = new SubCalc ();31. System.out.println(sc.multiply(3,4));32. System.out.println(SubCalc.multiply(2,2));What is the result?
A.- 12 4
B.- The code runs with no output.
C.- An exception is thrown at runtime.
D.- Compilation fails because of an error in line 21.
E.- Compilation fails because of an error in line 22.
F.- Compilation fails because of an error in line 31.
Question 40
Given:1. class TestA {2. public void start() { System.out.println("TestA"); }3. }4. public class TestB extends TestA {5. public void start() { System.out.println("TestB"); }6. public static void main(String[] args) {7. ((TestA)new TestB()).start();8. }9. }What is the result?
A.- TestA
B.- TestB
C.- Compilation fails.
D.- An exception is thrown at runtime.
Question 41
Given:31. class Foo {32. public int a = 3;33. public void addFive() { a += 5; System.out.print("f "); }34. }35. class Bar extends Foo {36. public int a = 8;37. public void addFive() { this.a += 5; System.out.print("b " ); }38. }Invoked with:Foo f = new Bar();f.addFive();System.out.println(f.a);What is the result?
A.- b 3
Página 22 de 24
B.- b 8
C.- b 13
D.- f 3
E.- f 8
F.- f 13
G.- Compilation fails.
Question 42
Given:1. interface A { public void aMethod(); }2. interface B { public void bMethod(); }3. interface C extends A,B { public void cMethod(); }4. class D implements B {5. public void bMethod(){}6. }7. class E extends D implements C {8. public void aMethod(){}9. public void bMethod(){}10. public void cMethod(){}11. }What is the result?
A.- Compilation fails because of an error in line 3.
B.- Compilation fails because of an error in line 7.
C.- Compilation fails because of an error in line 9.
D.- If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.
E.- If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 5.
F.- If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.
Question 43
What will be the result of compiling and running the following program ?class NewException extends Exception {}class AnotherException extends Exception {}public class ExceptionTest{public static void main(String[] args) throws Exception{try{m2();}finally{m3();}catch (NewException e){}}public static void m2() throws NewException { throw new NewException(); }public static void m3() throws AnotherException{ throw new AnotherException(); }}
Página 23 de 24
A.- It will compile but will throw AnotherException when run.
B.- It will compile but will throw NewException when run.
C.- It will compile and run without throwing any exceptions.
D.- It will not compile.
E.- None of the above.
Question 44
What will the following code print when compiled and run?abstract class Calculator{abstract void calculate();public static void main(String[] args){System.out.println("calculating");Calculator x = null;x.calculate();}}
A.- It will not compile.
B.- It will not print anything and will throw NullPointerException
C.- It will print calculating and then throw NullPointerException.
D.- It will print calculating and will throw NoSuchMethodError
E.- It will print calculating and will throw NoSuchMethodError
Question 45
A Java programmer is developing a desktop application. Which of the following exceptions would be appropriate for himto throw explicitly from his code?
A.- NullPointerException
B.- ClassCastException
C.- ArrayIndexOutofBoundsException
D.- Exception
E.- NoClassDefFoundError
Question 46
What will the following code print?public class Test{public int luckyNumber(int seed){if(seed > 10) return seed%10;int x = 0;try{if(seed%2 == 0) throw new Exception("No Even no.");else return x;}catch(Exception e){return 3;}
Página 24 de 24
finally{return 7;}}public static void main(String args[]){int amount = 100, seed = 6;switch( new Test().luckyNumber(6) ){case 3: amount = amount * 2;case 7: amount = amount * 2;case 6: amount = amount + amount;default :}System.out.println(amount);}}
A.- It will not compile.
B.- It will throw an exception at runtime.
C.- 800
D.- 200
E.- 400