Anda di halaman 1dari 33

Certificacin Java

1. Which declaration of the main method below would allow a class to be started as a standaloneprogram. Select the one correct answer. 1.public static int main(char args[]) 2.public static void main(String args[]) 3.public static void MAIN(String args[]) 4.public static void main(String args) 5.public static void main(char args[])

1. Incorrecta por devolver int y tener como parmetro un array de carcter. 3. Incorrecta por nombrarla mal el procedimiento. MAIN es diferente a main, ya que Java es caseSensitive. 4. El parmetro debe ser un array y no un String. 5. El parmetro debe ser un array de String y no de char. 2. Correcta.
Apunte: El mtodo main siempre ser esttico. El array de String no tiene por qu llamarse args. public static void main (String cadenas[]) sera vlido.

2. What all gets printed when the following code is compiled and run? Select the three correct answers.

1 2 3 4 5 6 7

public class xyz { public static void main (String[] args){ for(int i = 0; i < 2; i++) { for(int j = 2; j>= 0; j--) { if(i == j) break; System.out.println("i=" + i + " j="+j); }

8
9 10 } }

1. i=0 j=0 2. i=0 j=1 3. i=0 j=2 4. i=1 j=0 5. i=1 j=1 6. i=1 j=2 7. i=2 j=0 8. i=2 j=1 9. i=2 j=2 Aqu solamente hace falta seguir la traza de la ejecucin. Para i= 0, j toma los valores 2, y 1, en 0 se sale del bucle ms interno. Para i= 1, j toma el valor 2, en 1 se sale del bucle ms interno.

Las correctas seran las opciones 3 (i=0, j=2), 2 (i=0, j=1), y 6 (i=1, j=2).

3. What gets printed when the following code is compiled and run with the following command: java test 2 Select the one correct answer.

public class test { public static void main(String args[]) {


1

2
3 4 5 6 7 8 9 10

Integer intObj=Integer.valueOf(args[args.length-1]); int i = intObj.intValue(); if(args.length > 1) System.out.println(i); if(args.length > 0) System.out.println(i - 1); else

11
12

System.out.println(i - 2);
} }

1. test 2. test -1 3. 0 4. 1 5. 2 Con el comando java test 2, estamos ejecutando la clase test y pasndole como argumento el nmero 2. Como pasamos un nico argumento, el array args[] debe tener longitud 1. En intObj (un Integer) guardamos el valor de args[args.length-1], que ser args[0], lo cual es vlido y sera 2. Ese valor lo vuelve a hacer int y lo guarda en la variable i. Como la longitud de args es >0, imprime (i-1) = 1. La opcin correcta es la 4.

4. In Java technology what expression can be used to represent number of elements in an array named arr ? arr.length

6. Which of the following is a Java keyword. Select the four correct answers. 1. extern 2. synchronized 3. volatile 4. friend 5. friendly 6. transient 7. this 8. then Los correctos son: 2(synchronized), 3(volatile), 6(transient), 7(this) Incorrectos: 1(extern, creo que se usa en c, c#), 4(friend, creo que es c++), 5(friendly, inventada), 8(then,visualbasic)

7. Is the following statement true or false? The constructor of a class must not have a return type. 1. true 2. false La correcta es 1. Los constructores no pueden tener un tipo de retorno.
8. What is the number of bytes used by Java primitive long. Select the one correct answer. 1. The number of bytes is compiler dependent. 2. 2 3. 4 4. 8 5. 64 Nos dicen el nmero de bytes no bits. El long tiene un tamao de 64 bits = 8 bytes. La correcta es la opcin 4.

9. What is returned when the method substring(2,4) is invoked on the string example? Include the answer in quotes as the result is of type String. Al principio dije ampl pero no, eso es el substring de sql server La respuesta correcta es am. Substring(2,4) significa a partir de la posicin 2 hasta la 4.
10. Which of the following is correct? Select the two correct answers. 1. The native keyword indicates that the method is implemented in another language like C/C++ 2. The only statements that can appear before an import statement in a Java file are comments. 3. The method definitions inside interfaces are public and abstract. They cannot be private or protected. 4. A class constructor may have public or protected keyword before them, nothing else. Las correctas son la 1 y la 3. La nmero 2 es incorrecta porque antes de los imports tambin puede aparecer la declaracin de paquete. La nmero 4 es incorrecta porque un constructor puede ser tambin privado.

13. Name the access modifier which when used with a method, makes it available to all the classes in the same package and to all the subclasses of the class. Protected.

14. Which of the following is true. Select the two correct answers. 1. A class that is abstract may not be instantiated. 2. The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++. 3. A static variable indicates there is only one copy of that variable. 4. A method defined as private indicates that it is accessible to all other classes in the same package. Las correctas son 1 y 3. La palabra clave que indica que el cuerpo de un metodo se encuentra en otro lugar es abstract y la palabra clave que indica que el cdigo no es Java, es native. Un mtodo definido como privado slo puede utilizarse en la misma clase donde se define.

15. What all gets printed when the following gets compiled and run. Select the two correct answers. a. 1 b. 2 c. 3 d. 4 Las opciones correctas son a) y c). Es decir que tanto el operador == como el equals considera que las cadenas son iguales.

public class test { public static void main(String args[]) {


1

2
3 4 5 6

String s1 = "abc"; String s2 = "abc"; if(s1 == s2) System.out.println(1); else System.out.println(2); if(s1.equals(s2)) System.out.println(3); else System.out.println(4); } }

7
8 9 10 11 12 13 14

16. What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 17. What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 18. What is an abstract class? Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated. 19. What is the difference between an Interface and an Abstract class? An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

20. What all gets printed when the following gets compiled and run. Select the two correct answers. a. 1 b. 2 c. 3 d. 4 En esta ocasin las opciones correctas son b) al no ser iguales con el operador == y c) al ser iguales con el operador Ver aclaracin String literals http://www.coderanch.com/t/410967/java/java/String-abc-String-new-String
public class test {
1
2 3 4 5 6 7 8 9 10 11

public static void main(String args[]) { String s1 = "abc"; String s2 = new String("abc");

if(s1 == s2) System.out.println(1); else System.out.println(2);

12
13 14 15 16

if(s1.equals(s2)) System.out.println(3); else

21. How can you ensure that the memory allocated by an object is freed. Select the one correct answer. A.By invoking the free method on the object. B.By calling system.gc() method. C.By setting all references to the object to new values (say null). D.Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object. La correcta es la 4. No se puede forzar la ejecucin del recolector de basura.

22. Which of the following are keywords in Java. Select the two correct answers. A. friend B. NULL C. implement D. synchronized E. Throws

F. R: d, e

23. Which of these are legal identifiers. Select the three correct answers. A.number_1 B.number_a

C.$1234
D.-volatile

R: a, b, c

24. Which of these are not legal identifiers. Select the four correct answers. A. 1alpha B. _abcd C. xy+abc D. transient E. account-num F. very_long_name G.a, c, d, e

25. Which of the following are Java keywords. Select the four correct answers.

A.super
B.String C.void D.synchronize E.Instanceof F.a, b, c, e.

26. How many bytes are used to represent the primitive data type int in Java. Select the one correct answer. A. 2 B. 4 C. 8 D. 1 E. The number of bytes to represent an int is compiler dependent.

27.What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 28. What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

29. What is an abstract class? Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated. 30. What is the difference between an Interface and an Abstract class? An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

30. How many bytes are used to represent the primitive data type int in Java. Select the one correct answer. a) 2 b) 4 c) 8 d) 1 e) The number of bytes to represent an int is compiler dependent. El tipo int es de 32 bits, son 4 bytes. Opcin b) Para acordarnos byte(1 byte), short (2 bytes), int (4 bytes), long (8 bytes). 31. What is the legal range of values for a variable declared as a byte. Select the one correct answer. a) 0 to 256 b) 0 to 255 c) -128 to 127 d) -128 to 128 e) -127 to 128 f) -215 to 215 1 Un byte como la misma palabra indica ocupa un byte, 8 bits . El rango va desde -2(elevado a 7) a 2 (elevado a 7) 1, es decir de -128 a 127, opcin c)Correcta. 32. The width in bits of double primitive type in Java is . Select the one correct answer. a) The width of double is platform dependent b) 64 c) 128 d) 8 e) 4 Un double ocupa 8 bytes, 64 bits, opcin b) Correcta

33. Which of these are valid declarations for the main method? Select the one correct answer. A. public void main(); B. public static void main(String args[]); C. static public void main(String);

D. public static void main(String );


E. public static int main(String args[]); Which of the following are valid declarations for the main

34. Which of these assignments are valid. Select the four correct answers. A. short s = 28; B. float f = 2.3; C. double d = 2.3; D. int I = '1'; E. byte b = 12;

35. What is difference to extend abstract class and non abstract class? There's no difference, except that abstract classes may have abstract methods. Abstract methods are methods without implementations and these must be implemented by your subclass (unless you make your subclass abstract too). Since youre a class have no abstract methods, there is no different what so ever from a subclass-perspective. (The only difference is that a may no longer be instantiated.)

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead. Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example.

It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract. For example, abstract class X implements Y { // implements all but one method of Y}class XX extends X { // implements the remaining method in Y} In this case, class X must be abstract because it does not fully implement Y, but class XX does, in fact, implement Y.

NOTA:

una clase abstracta puede implementar a otras clases. Adems se puede heredar de ella. Una clase puede heredar o implementar a otras. O bien puede heredar e implementar a la vez. Una interfaz no puede ser instanciada.

Tipo byte short int long

Tamao 1Byte (8 bits) 2 Bytes (16 bits) 4 Bytes (32 bits) 8 Bytes (64 bits)

Tipo float double

Tamao 4 Byte (32 bits) 8 Bytes (64 bits)

1) byte El tipo de dato byte puede representar datos enteros que se encuentren en el rango de -128 a +127. El tamao de un dato de tipo byte es de 8 bits. 2) short El tipo de dato short puede representar datos enteros que se encuentren en el rango de -32768 y +32767. El tamao de un dato de tipo short es de 16 bits. 3) int El tipo de dato int puede representar datos enteros que se encuentren en el rango de -2147483648 y +2147483647. El tamao de un dato de tipo int es de 32 bits. 4) long El tipo de dato int puede representar datos enteros que se encuentren en el rango de -9223372036854775808 y +9223372036854775807. El tamao de un dato de tipo int es de 64 bits. Para indicar de manera explicita que el dato es un long , se agrega una L o l al final del valor de la variable.

TIPOS DE DATOS FLOTANTES


1) float El tipo de dato float puede representar datos en coma flotante que se encuentren en el rango de 1.40239846e45f y 3.40282347e+38f. El tamao de un dato de tipo short es de 32 bits. Para indicar de manera explicita que el dato es un float , se agrega una F o f al final del valor de la variable. 2) double El tipo de dato double puede representar datos en coma flotante que se encuentren en el rango de 4.94065645841246544e324d y 1.7976931348623157e+308d. El tamao de un dato de tipo short es de 64 bits. Para indicar de manera explicita que el dato es un double , se agrega una D o d al final del valor de la variable. TIPO DE DATO BOOLEAN El tipo de dato boolean puede representar dos valores logicos : true(verdadero) ofalse(falso).

TIPO DE DATO CHAR El tipo de dato char se usa para representar caracteres(codigo Unicode). Un caracter es representado internamente por un entero.

Java list of keywords:

abstract

double

int

strictfp

**

boolean

else

interface

super

break

extends

long

switch

byte

final

native

synchronized

case

finally

new

this

catch

float

package

throw

char

for

private

throws

class

goto *

protected

transient

const *

if

public

try

continue

implements

return

void

default

import

short

volatile

do

instanceof

static

while

Anda mungkin juga menyukai