0% found this document useful (0 votes)
201 views31 pages

Java 8 (Guía 2) - Examen

The code defines a FeedingSchedule class with a main method. It initializes a boolean variable keepGoing to true. No other code is shown, so the output would be empty as the program does not print anything or terminate the loop.

Uploaded by

Andrey Cordero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views31 pages

Java 8 (Guía 2) - Examen

The code defines a FeedingSchedule class with a main method. It initializes a boolean variable keepGoing to true. No other code is shown, so the output would be empty as the program does not print anything or terminate the loop.

Uploaded by

Andrey Cordero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

1. What is the output of the following code?

LocalDate date = LocalDate.of(2018,Month.APRIL,40);


System.out.println(date.getYear()+” “+date.getMonth()+” “+date.geeDayofMonth());

a) Another Date
b) The code does not compile
c) A runtime exception is throw
d) 2018 APRIL 4
e) 2018 APRIL 30
f) 2018 MAY 10

La respuesta correcta es la opción C.


2. What is the output of the following program?

01: public class Dog{


02: public String name;
03: public void parseName(){
04: system.out.print(“1”);
05: try{
06: System.out.print(“2”);
07: Int x = Integer.parseInt(name);
08: System.out.print(“3”);
09: }catch(NumberFormatException e){
10: System.out.print(“4”);
11: }
12: }
13: public static void main (String [] args){
14: Dog leroy = new Dog();
15: Leroy.name = “Leroy”;
16: Leroy.parseName();
17: System.out.print(“5”);
18: }}

a) 124
b) 1235
c) An uncaught exception is throw
d) The code does not compile
e) 1234
f) 12
g) 1245
3. What modifiers are assumed for all interface variables?(choose all that apply)

¿Qué modificadores se asumen para todas las variables de interfaz? (Elija todas las que
correspondan)

a) protected
b) private
c) public
d) abstract
e) final
f) static

Se supone que las variables de interfaz son públicas estáticas finales. A y B son incorrectas porque
las variables de la interfaz deben ser públicas: las interfaces son implementadas por las clases, no
heredadas por las interfaces. D es incorrecta porque las variables nunca pueden ser abstractas.
4. what is printed besides the stack trace caused by the NullPointException from line 16?

¿Qué se imprime además del seguimiento de la pila causado por la excepción


NullPointException de la línea 16?

01: public class DoSomething{


02: public void go(){
03: System.out.print(“A”);
04: try{
05: stop();
06: }catch (ArithmeticException e){
07: System.out.print(“B”);
08: }finally{
09: System.out.print(“C”);
10: }
11: System.out.print(“D”);
12: }
13: public void stop(){
14: System.out.print(“E”);
15: Object x= null;
16: x.toString();
17: System.out.print(“F”);
18: }
19: public static void main(String[] args){
20: new DoSomething().go();
21: }
22: }

a) AECD
b) AEC
c) AE
d) AEBCD
e) No output appears other than the stack trace
5. what is the result of the following?

List<String> one = new ArrayList<String>();


one.add(“abc”);
List<String> two = new ArrayList<>();
two.add(“abc”);
if(one==two)
System.out.println(“A”);
else if (one.equals(two))
System.out.println(“B”);
else
System.out.println(“C”);

a) A
b) The code does not compile
c) B
d) An exception is thrown
e) C
6. what is the result of the following class?

01: import java.util.function.*;


02:
03: public class Panda{
04: int age;
05: public static void main(String[] args){
06: Panda p1 = new Panda();
07: p1.age = 1;
08: check (p1, p -> p.age < 5);
09: }
10: private static void check(Panda panda, Predicate<Panda> pred){
11: String result = pred.test(panda) ? ”match” : ”not match”;
12: System.out.print(result);
13: }}

a) match
b) Compiler error on line 11.
c) not match
d) Compiler error on line 10.
e) Compiler error on line 8.
f) A runtime exception is thrown.
7. what is the output of the following code snipper?

13: System.out.print(“a”);
14: try {
15: System.out.print(“b”);
16: throw new IllegalArgumentException();
17: } catch (Runtime exception e) {
18: System.out.print(“c”);
19: } finally {
20: System.out.print(“d”);
21: }
22: System.out.print(“e”);

a) The code does not compile.


b) abde
c) abcde
d) An caughtexception is thrown.
e) abe
f) abce
8. what is the output of the following code?

01: interface Nocturnal {


02: default boolean isBlind() { return true; }
03: }
04: public class Owl implements Nocturnal {
05: public boolean isBlind() { return false; }
06: public static void main (String[] args) {
07: Nocturnal nocturnal = (Nocturnal) new Owl();
08: System.out.println(nocturnal.isBlind());
09: }
10: }

a) The code will not compile because of line 7.


b) true
c) The code will not compile because of line 8.
d) The code will not compile because of line 5.
e) The code will not compile because of line 2.
f) false
9. what is the output of the following code?

01: class Arthropod{


02: public void printName (double input) { system.out.print(“Arthropod”); }
03: }
04: public class Spider extends Arthropod{
05: public void printName (int Input) { System.out.print (“spider”); }
06: public static void main (String[] args){
07: Spider spider = new Spider();
08: spider.printName(4);
09: spider.printName(9.0);
10: }
11: }
a) ArthropodSpider
b) ArthropodArthropod
c) SpiderArthropod
d) The code will not compile because line 9.
e) The code will not compile because line 5.
f) SpiderSpider
10. what is the output of the following code snnipet?

03: int x = 1, y = 15;


04: while x < 10
05: y--;
06: x++;
07: System.out.println(x+”,”+y);

a) the following code contains an infinite loop and does not terminate.
b) 11, 5
c) The code will not compile because of line 4.
d) 10, 6
e) The code will not compile because of line 3.
f) 10, 5
11. which exception will the following throw?

Object obj = new Integer(3);


String str = (String) obj;
System.out.println(“str”);

a) ClassCastException
b) NumberFormatException
c) ArrayIndexOutOfBoundsException
d) IllegalArgumentException
e) None of the above
12. what is the result of the following code snippet?

03: int m = 9, n = 1, x = 0;
04: while (m > n) {
05: m--;
06: n += 2;
07: x += m + n;
08: }
09: System.out.println(x);

a) 50
b) 23
c) 13
d) The code will not compile because of line 7.
e) 36
f) 11
13. what is the output of the following code snippet?

03: int x = 4;
04: long y = x * 4 – x++;
05: if (y < 10) System.out.println(“Too Low”);
06: else System.out.println(“Just Right”);
07: else System.out.println(“Too High”);

a) Just Right
b) Too High
c) The code will not compile because of line 7.
d) The code will not compile because of line 6.
e) Compiles but throws a NullPointerException.
f) Too Low
14. what is the result of the following program?

01: public class MathFunctions {


02: public static void addToInt(int x, int amountToAdd) {
03: x = x + amountToAdd;
04: }
05: public static void main(String[] args) {
06: int a = 15;
07: int b = 10;
08: MathFunctions.addToInt (a, b);
09: System.out.println(a);}}

a) None of the obve.


b) Compiler error on line 8.
c) Compiler error on line 3.
d) 25
e) 15
f) 10
15. what is the result of the following statements?

06: List <String> list = new ArrayList<String>();


07: list.add(“one”);
08: list.add(“two”);
09: list.add(7);
10: for(String s : list) System.out.print(s);

a) Compiler error on line 9.


b) onetwo
c) Compiler error on line 10.
d) onetwo7.
e) onetwo followed by an exception
16. which of the following methods compile? (Choose all that apply)

a) public int methodF() {return;}


b) public void methodD() {}
c) public void methodA() {return;}
d) public int methodD() {return 9;}
e) public int methodG() {return null;}
f) public int methodE() {return 9.0:}
g) public void methodB() {return null;}
17. what modifiers are implicitly applied to all interface methods? (choose all that apply) o you
like?

¿Qué modificadores se aplican implícitamente a todos los métodos de la interfaz? (elija todos
los que correspondan) o le gusten?

a) abstract
b) void
c) static
d) default
e) protected
f) public

Todos los métodos de la interfaz son implícitamente públicos, por lo que la opción f es correcta. La
opción E es incorrecta. Los métodos de la interfaz pueden declararse como estáticos o default,
pero nunca se añaden implícitamente. La B es incorrecta-void no es un modificador. A es
incorrecta, antes de Java 8 todos los métodos de la interfaz se asumían como abstractos.

Java 8 incluye métodos por default y estáticos para la interfaz, no puedes asumir que el
compilador aplicará implícitamente el modificador abstract a todos los métodos.
18. what is the result of the following code?

07: StringBuilder sb = new StringBuilder();


08: sb.append(“aaa”).insert(1,”bb”),insert(4,”ccc”);
09: System.out.println(sb);

a) abbaaccc
b) bbaacca
c) bbaaaccc
d) The code does not compile.
e) An exception is thrown.
f) abbaccca
19. what is the result of the following code?

String s1 = “Java”;
String s2 = “Java”;
StringBuilder sb1 = new StringBuilder();
Sb1.append(“Ja”).append(“va”);
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(sb.toString() == s1);
System.out.println(sb.toString().equals(s1));

a) true is printed out exactly once.


b) true is printed out exactly three times.
c) true is printed out exactly twice.
d) true is printed out exactly four times.
e) The code does not compile.
20. what is the output of the following program?

01: public class FeedingSchedule {


02: public static void main String[] args) {
03: booblean keepGoing = true;
04: int count = 0;
05: int x = 3;
06: while(count++ < 3){
07: int y = (1 + 2 * count) % 3;
08: switch(y){
09: default:
10: case 0: x -= 1; break;
11: case 1: x += 5;
12: }
13: }
14: System.out.println(x);
15: ]}

a) 13
b) 6
c) 5
d) 4
e) 7
f) The code will not compile because the line 7.
21. What is the output of the following application?

1:public class CompareValues {


2: public static void main(String[] args) {
3: int x=0;
4: while (x++ < 10) {}
5: String message = x > 10 ? "Greater than":false;
6: System.out.println(message+","+x);
7: }
8: }

a) The code will not compile because of line 5.


b)false,10
c) Greater than,11
d) Greater than,10
e) The code will not compile because of line 4.
f)false,11
22.Which are true of the following code? (Choose all that apply)

1:public class Rope {


2: public static void swing() {
3: System.out.print("swing");
4: }
5: public void climb() {
6: System.out.print("climb");
7: }
8: public static void play() {
9: swing();
10: climb();
11: }
12: public static void main(String[] args) {
13: Rope rope = new Rope();
14: rope.play();
15: Rope rope2 = null;
16: rope2.play();
17: }
18: }

a)There is exactly one compiler error in the code.


b)There are exactly two compiler errors in the code.
c)The code compiles as is.
d)If the lines with compiler errors are removed, the output is swing swing
e) If the lines with compiler errors are removed, the output is climb climb
23. Which statements are true for both abstract classes and interfaces? (Choose all that apply)

a)Neither can be instantiated directly (Ninguno de los dos se puede instanciar directamente).

b)Both can contain public static final variables (Ambos pueden contener variables finales
estáticas públicas).

c)All methods within them are assumed to be abstract (Se asume que todos los métodos dentro de
ellos son abstractos).

d)Both can contain default methods (Ambos pueden contener métodos default).

e)Both can be extended using the extend keyword (Ambos se pueden extender usando la
palabra clave extend).

f)Both inherit java.lang.Object (Ambos heredan de java.lang.Object).

g)Both can contain static methods (Ambos pueden contener métodos estaticos).

C es incorrecto, porque una clase abstracta puede contener métodos concretos. Desde Java 8, las
interfaces también pueden contener métodos concretos en forma de métodos estáticos o por
default.

Aunque se supone que todas las variables de las interfaces son públicas estáticas finales, las clases
abstractas también pueden contenerlas, por lo que B es correcta.

Tanto las clases abstractas como las interfaces pueden extenderse con la palabra clave extends,
por lo que E es correcto.

Sólo las interfaces pueden contener métodos por default, por lo que D es incorrecta.

Tanto las clases abstractas como las interfaces pueden contener métodos estáticos, por lo que G
es correcta.

Tanto las clases abstractas como las interfaces requieren una subclase concreta para ser
instanciadas, por lo que A es correcta.

Una interfaz por sí misma no hereda java.lang.Object. F es incorrecto.


24.Which of the following lambda expressions can fill line blank? (Choose all that apply)

List<String> list = new ArrayList<>();


list.removeIf();

a) s->s.isEmpty()
b) String s->s.isEmpty()
c) s->{s.isEmpty()}
d) s->{return s.isEmpty();}
e) s->{ s.isEmpty();}
f) (String s)-> s.isEmpty()

removeIf() espera una interfaz Predicate.


En la opción A, la interfaz de Predicado toma una lista de parámetros de un parámetro utilizando
el tipo especificado.
C y E son incorrectos ya que no utilizan la palabra clave return. El return es necesario dentro de los
corchetes para los cuerpos lambda.

A la opción B le faltan los paréntesis alrededor de la lista de parámetros. Los paréntesis son
opcionales para un solo parámetro con un tipo inferido.
25. What is the output of the following code snippet

3: java.util.List<Integer> list = new java.util.ArrayList<Integer>();


4: list.add(10);
5: list.add(14);
6: for(int x : list) {
7: System.out.print(x + ", ");
8: break;
9: }

a)The code will not compile because of line 8.


b)The code contains an infinite loop and does not terminate.
c) The code will not compile because of line 7.
d)10,14
e)10,
f)10,14,
Which of the following exceptions are thrown by the JVM? (Choose all that apply)

a. ArrayIndexOutOfBoundsException
b. ExceptionInInitializerError
c. java.io.IOException
d. NullPointerException
e. NumberFormatException

What is the result of the following program?

1: public class Squares {

2: public static long square(int x) {

3: long y=x*(long)x;

4: x=-1;

5: return y;

6: }

7: public static void main(String[] args) {

8: int value = 9;

9: long result = square(value);

10: System.out.println(value);

11: }}

a. -1
b. 9
c. 81
d. Compiler error on line 9.
e. Compiler error on a different line.
What is the result of the following code?

int[] array = {6,9,8};


List<Integer> list = new ArrayList<>();

list.add(array[0]);

list.add(array[2]);

list.set(1, array[1]);

list.remove(0);

System.out.println(list);

a. [8]
b. [9]
c. Something like [Ljava.lang.String;@160bc7c0
d. An exception is thrown.
e. The code does not compile.

You might also like