Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Revisão OCPJP7 - Class Design (parte 01)

188 views

Published on

Revisão OCPJP7 - Class Design (parte 01) - Questões e Respostas

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

Revisão OCPJP7 - Class Design (parte 01)

  1. 1. OCPJP Objetivo: Java Class Design
  2. 2. Questão Dado: package com.sun.scjp; public class Geometry { public static final double DIAMETER = 0.72; // kilometers } Quais 2 podem acessar o membro DIAMETER corretamente da classe Geometry? (Escola 2 opções) A. import com.sun.scjp.Geometry; public class Ground { public double halfway { return Geometry.DIAMETER/2.0; } } B. import static com.sun.scjp.Geometry; public class Ground { public double halfway { return DIAMETER/2.0; } } C. import static com.sun.scjp.Geometry.*; public class Ground { public double halfway { return DIAMETER/2.0; } } D. package com.sun.scjp; public class Ground { public double halfway { return DIAMETER/2.0; } }
  3. 3. Questão Resolvida Dado: package com.sun.scjp; public class Geometry { public static final double DIAMETER = 0.72; // kilometers } Quais 2 podem acessar o membro DIAMETER corretamente da classe Geometry? (Escola 2 opções) A. import com.sun.scjp.Geometry; public class Ground { public double halfway { return Geometry.DIAMETER/2.0; } } B. import static com.sun.scjp.Geometry; public class Ground { public double halfway { return DIAMETER/2.0; } } C. import static com.sun.scjp.Geometry.*; public class Ground { public double halfway { return DIAMETER/2.0; } } D. package com.sun.scjp; public class Ground { public double halfway { return DIAMETER/2.0; } } Correto Correto
  4. 4. Questão As classes abaixo estão definidas em 2 arquivos separados 1. package processors; 2. public class ByteProcessor { 3. public static void process(byte[] b) { /* more code here */ } 4. } 1. package apps; 2. public class ByteApp { 3. public static void main(String[] args) { 4. byte[] bytes = new byte[256]; 5. // insert code here 6. } 7. } O que é necessário na linha 5 da classe ByteApp para usar o método da classe ByteProcessor? a. process(bytes); b. ByteProcessor.process(bytes); c. processors.ByteProcessor.process(bytes); d. ByteApp cannot use methods in ByteProcessor. e. import processors.ByteProcessors.*; process(bytes);
  5. 5. Questão Resolvida As classes abaixo estão definidas em 2 arquivos separados 1. package processors; 2. public class ByteProcessor { 3. public static void process(byte[] b) { /* more code here */ } 4. } 1. package apps; 2. public class ByteApp { 3. public static void main(String[] args) { 4. byte[] bytes = new byte[256]; 5. // insert code here 6. } 7. } O que é necessário na linha 5 da classe ByteApp para usar o método da classe ByteProcessor? a. process(bytes); b. ByteProcessor.process(bytes); c. processors.ByteProcessor.process(bytes); d. ByteApp cannot use methods in ByteProcessor. e. import processors.ByteProcessors.*; process(bytes); Correto
  6. 6. Questão As classes abaixo estão definidas em 2 arquivos separados 1. package processors; 2. public class ByteProcessor { 3. private static void process(byte[] b) { /* more code here */ } 4. } 1. package apps; 2. public class ByteApp { 3. public static void main(String[] args) { 4. byte[] bytes = new byte[256]; 5. // insert code here 6. } 7. } What is required at line 5 in class ByteApp to use the process method of ByteProcessor? a. process(bytes); b. ByteProcessor.process(bytes); c. apps.ByteProcessor.process(bytes); d. processors.ByteProcessor.process(bytes); e. import processors.ByteProcessor.*; process(bytes); f. ByteApp cannot use the process method in ByteProcessor.
  7. 7. Questão Resolvida As classes abaixo estão definidas em 2 arquivos separados 1. package processors; 2. public class ByteProcessor { 3. private static void process(byte[] b) { /* more code here */ } 4. } 1. package apps; 2. public class ByteApp { 3. public static void main(String[] args) { 4. byte[] bytes = new byte[256]; 5. // insert code here 6. } 7. } What is required at line 5 in class ByteApp to use the process method of ByteProcessor? a. process(bytes); b. ByteProcessor.process(bytes); c. apps.ByteProcessor.process(bytes); d. processors.ByteProcessor.process(bytes); e. import processors.ByteProcessor.*; process(bytes); f. ByteApp cannot use the process method in ByteProcessor. Correto
  8. 8. Questão Um desenvolvedor está criand a classe Book, que necessita de acessar a classe Paper. A classe Paper encontra-se em um arquivo JAR chamado myLib.jar. Quais 3 dos abaixo que usados independentemente permitirão ao desenvolvedor usar a classe Paper ao compilar a classe Book? (Escolha três opções.) a. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar. b. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar. c. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/myLib.jar/Paper.class. d. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/myLib.jar. e. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac - cp/foo/myLib.jar/Paper Book.java. f. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d/foo/myLib.jar Book.java g. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -classpath /foo/myLib.jar Book.java
  9. 9. Questão Resolvida Um desenvolvedor está criand a classe Book, que necessita de acessar a classe Paper. A classe Paper encontra-se em um arquivo JAR chamado myLib.jar. Quais 3 dos abaixo que usados independentemente permitirão ao desenvolvedor usar a classe Paper ao compilar a classe Book? (Escolha três opções.) a. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar. b. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar. c. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/myLib.jar/Paper.class. d. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/myLib.jar. e. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac - cp/foo/myLib.jar/Paper Book.java. f. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d/foo/myLib.jar Book.java g. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -classpath /foo/myLib.jar Book.java Correto Correto Correto
  10. 10. Questão Dado: 1. package com.apps; 2. 3. public class MyApp { 4. public static void main(String[] args) {} 5. } And MyApp exists in the /programs/com/apps directory. Assume the CLASSPATH environment variable is set to "." (current directory). Quais 2 comandos java irão executar MyApp? (Escolha 2 opções) A. java MyApp if run from the /programs directory B. java com.apps.MyApp if run from the /programs directory C. java -classpath /programs com.apps.MyApp if run from any directory D. java -classpath . MyApp if run from the /programs/com/apps directory E. java -classpath /programs/com/apps:. MyApp if run from the /programs directory F. java com.apps.MyApp if run from the /programs/com/apps directory
  11. 11. Questão Resolvida Dado: 1. package com.apps; 2. 3. public class MyApp { 4. public static void main(String[] args) {} 5. } And MyApp exists in the /programs/com/apps directory. Assume the CLASSPATH environment variable is set to "." (current directory). Quais 2 comandos java irão executar MyApp? (Escolha 2 opções) A. java MyApp if run from the /programs directory B. java com.apps.MyApp if run from the /programs directory C. java -classpath /programs com.apps.MyApp if run from any directory D. java -classpath . MyApp if run from the /programs/com/apps directory E. java -classpath /programs/com/apps:. MyApp if run from the /programs directory F. java com.apps.MyApp if run from the /programs/com/apps directory Correto Correto
  12. 12. Questão 1. import java.util.*; 2. public class MapTest 3. { 4. public static void main(String[] args) 5. { 6. Object obj = new LinkedHashMap(); 7. if(obj instanceof Collection) 8. System.out.print("For "); 9. if(obj instanceof Map) 10. System.out.print("A Few "); 11. if(obj instanceof LinkedList) 12. System.out.print("Dollars More "); 13. if(obj instanceof HashMap) 14. System.out.print("Good Men"); 15. } 16. } O que irá acontecer quando vocÊ tentar compilar e rodar o código acima? a. It will print - A Few Good Men b. It will print - For A Few Good Men c. It will print - For A Few Dollars More Good Men d. It will print - For A Few Dollars More e. It will print - For A Few f. It will print - A Few Dollars More
  13. 13. Questão Resolvida 1. import java.util.*; 2. public class MapTest 3. { 4. public static void main(String[] args) 5. { 6. Object obj = new LinkedHashMap(); 7. if(obj instanceof Collection) 8. System.out.print("For "); 9. if(obj instanceof Map) 10. System.out.print("A Few "); 11. if(obj instanceof LinkedList) 12. System.out.print("Dollars More "); 13. if(obj instanceof HashMap) 14. System.out.print("Good Men"); 15. } 16. } O que irá acontecer quando vocÊ tentar compilar e rodar o código acima? a. It will print - A Few Good Men b. It will print - For A Few Good Men c. It will print - For A Few Dollars More Good Men d. It will print - For A Few Dollars More e. It will print - For A Few f. It will print - A Few Dollars More Correto
  14. 14. Questão Se o código abaixo exibe: count = 3, qual das declarações abaixo é false? (Assumindo que os métodos equals() e hashCode() foram sobrescritos corretamente). 1. int count = 1; 2. if(a.equals(b)) 3. count++; 4. if(c.equals(d)) 5. count++; 6. if(a.hashCode() == b.hashCode()) 7. count++; 8. if(c.hashCode() == d.hashCode()) 9. count++; 10. System.out.println("count = " + count); a. a.equals(b) and !(c.equals(d)) b. a.equals(b) or c.equals(d) but not both c. a.hashCode()==b.hashCode() and c.hashCode() == d.hashCode() d. a.equals(b) and c.equals(d)
  15. 15. Questão Resolvida Se o código abaixo exibe: count = 3, qual das declarações abaixo é false? (Assumindo que os métodos equals() e hashCode() foram sobrescritos corretamente). 1. int count = 1; 2. if(a.equals(b)) 3. count++; 4. if(c.equals(d)) 5. count++; 6. if(a.hashCode() == b.hashCode()) 7. count++; 8. if(c.hashCode() == d.hashCode()) 9. count++; 10. System.out.println("count = " + count); a. a.equals(b) and !(c.equals(d)) b. a.equals(b) or c.equals(d) but not both c. a.hashCode()==b.hashCode() and c.hashCode() == d.hashCode() d. a.equals(b) and c.equals(d) Correto
  16. 16. Questão O que irá acontecer ao tentar compilar e executar o código abaixo? 1. public final class EqualsDemo 2. { 3. private String str; 4. 5. public EqualsDemo(String s) 6. { 7. str = s; 8. } 9. 10. public boolean equals(EqualsDemo obj) 11. { 12. if(!(obj instanceof EqualsDemo)) 13. return false; 14. EqualsDemo ed = (EqualsDemo)obj; 15. return (str == ed.str || (str != null && str.equals(ed.str))); 16. } 17. 18. public static void main(String[] args) 19. { 20. EqualsDemo demo1 = new EqualsDemo("Java"); 21. EqualsDemo demo2 = new EqualsDemo("java"); 22. System.out.println(demo1.equals(demo2)); 23. } 24. } A. It will print - true B. It will print - false C. Compilation error D. Exception
  17. 17. Questão Resolvida O que irá acontecer ao tentar compilar e executar o código abaixo? 1. public final class EqualsDemo 2. { 3. private String str; 4. 5. public EqualsDemo(String s) 6. { 7. str = s; 8. } 9. 10. public boolean equals(EqualsDemo obj) 11. { 12. if(!(obj instanceof EqualsDemo)) 13. return false; 14. EqualsDemo ed = (EqualsDemo)obj; 15. return (str == ed.str || (str != null && str.equals(ed.str))); 16. } 17. 18. public static void main(String[] args) 19. { 20. EqualsDemo demo1 = new EqualsDemo("Java"); 21. EqualsDemo demo2 = new EqualsDemo("java"); 22. System.out.println(demo1.equals(demo2)); 23. } 24. } A. It will print - true B. It will print - false C. Compilation error D. Exception Correto
  18. 18. Questão Como você implementaria do método hashCode() corretamente para a classe abaixo? (Assumindo que essa classe implemnta o método equals() corretamente e use a implementação mais apropriada) public class HashDemo { private Integer arr[]; //other methods not shown } A. public int hashCode() { int hash = 7; if(arr == null) hash = 31 * hash + 0; else for(int i=0; i<arr.length; i++) { hash = 31 * hash +(arr[i] == null? 0 : arr[i].hashCode()); } return hash; } B. public int hashCode() { int hash = 7; if(arr == null) hash = 31 * hash + 0; else for(int i=0; i<arr.length; i++) { hash = 31 * hash + arr[i].hashCode(); } return hash; } C. public int hashCode() { int hash = 7; if(arr == null) hash = 31 * hash + 0; else hash = 31 * hash + arr.hashCode(); return hash; } D. The array should not be involved in the calculation of the hashCode() method.
  19. 19. Questão Ressolvida Como você implementaria do método hashCode() corretamente para a classe abaixo? (Assumindo que essa classe implemnta o método equals() corretamente e use a implementação mais apropriada) public class HashDemo { private Integer arr[]; //other methods not shown } A. public int hashCode() { int hash = 7; if(arr == null) hash = 31 * hash + 0; else for(int i=0; i<arr.length; i++) { hash = 31 * hash +(arr[i] == null? 0 : arr[i].hashCode()); } return hash; } B. public int hashCode() { int hash = 7; if(arr == null) hash = 31 * hash + 0; else for(int i=0; i<arr.length; i++) { hash = 31 * hash + arr[i].hashCode(); } return hash; } C. public int hashCode() { int hash = 7; if(arr == null) hash = 31 * hash + 0; else hash = 31 * hash + arr.hashCode(); return hash; } D. The array should not be involved in the calculation of the hashCode() method. Correto
  20. 20. Questão Dado: 2. public class foo { 3. void m1() { } 4. protected void m2 { } 5. } 6. class bar extends foo { 7. // insert code here 8. } Quais fragmentos de código, inseridos independentemente na linha 7 irão compilar? (Escolha 4) A. public void m1() { } B. protected void m1() { } C. private void m1() { } D. void m2() { } E. public void m2() { } F. protected void m2() { } G. private void m2() { }
  21. 21. Questão Resolvida Dado: 2. public class foo { 3. void m1() { } 4. protected void m2 { } 5. } 6. class bar extends foo { 7. // insert code here 8. } Quais fragmentos de código, inseridos independentemente na linha 7 irão compilar? (Escolha 4) A. public void m1() { } B. protected void m1() { } C. private void m1() { } D. void m2() { } E. public void m2() { } F. protected void m2() { } G. private void m2() { } Correto Correto Correto Correto

×