Para Certificacion Java
Para Certificacion Java
The sample test is modeled on the Sun Certification for JavaTM 6 Programmer exam. The test has 50 questions
and needs to be executed in 2 hours. The real exam may be a little tougher than this. You need to score 35
correct answers out of 60 to clear the real exam in 180 minutes. Please let us know at ngabrani At hotmail dot
com if you find any issues with the test. The site also offers another mock exam and questions by topic.
1. Which declaration of the main method below would allow a class to be started as a standalone program.
Select the one correct answer.
A. public static int main(char args[])
B. public static void main(String args[])
C. public static void MAIN(String args[])
D. public static void main(String args)
E. public static void main(char args[])
2. What all gets printed when the following code is compiled and run? Select the three correct answers.
A. i=0 j=0
B. i=0 j=1
C. i=0 j=2
D. i=1 j=0
E. i=1 j=1
F. i=1 j=2
G. i=2 j=0
H. i=2 j=1
I. i=2 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.
if(args.length > 1)
System.out.println(i);
if(args.length > 0)
System.out.println(i - 1);
else
System.out.println(i - 2);
}
}
A. test
B. test -1
C. 0
D. 1
E. 2
4. In Java technology what expression can be used to represent number of elements in an array named arr ?
5. How would the number 5 be represented in hex using up-to four characters.
6. Which of the following is a Java keyword. Select the four correct answers.
A. extern
B. synchronized
C. volatile
D. friend
E. friendly
F. transient
G. this
H. then
7. Is the following statement true or false. The constructor of a class must not have a return type.
A. true
B. false
8. What is the number of bytes used by Java primitive long. Select the one correct answer.
A. The number of bytes is compiler dependent.
B. 2
C. 4
D. 8
E. 64
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.
10. Which of the following is correct? Select the two correct answers.
A. The native keyword indicates that the method is implemented in another language like C/C++.
B. The only statements that can appear before an import statement in a Java file are comments.
C. The method definitions inside interfaces are public and abstract. They cannot be private or
protected.
D. A class constructor may have public or protected keyword before them, nothing else.
11. What is the result of evaluating the expression 14 ^ 23. Select the one correct answer.
A. 25
B. 37
C. 6
D. 31
E. 17
F. 9
G. 24
12. Which of the following are true. Select the one correct answers.
A. && operator is used for short-circuited logical AND.
B. ~ operator is the bit-wise XOR operator.
C. | operator is used to perform bitwise OR and also short-circuited logical OR.
D. The unsigned right shift operator in Java is >>.
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.
14. Which of the following is true. Select the two correct answers.
A. A class that is abstract may not be instantiated.
B. 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++.
C. A static variable indicates there is only one copy of that variable.
D. A method defined as private indicates that it is accessible to all other classes in the same
package.
15. What all gets printed when the following program is compiled and run. Select the two correct answers.
A. 0
B. 1
C. 2
D. 3
16. What all gets printed when the following program is compiled and run. Select the one correct answer.
A. 0
B. 1
C. 2
D. The program does not compile because of statement "i=++i;"
17. What all gets printed when the following gets compiled and run. Select the three correct answers.
1.
2. public class test {
3. public static void main(String args[]) {
4. int i=1, j=1;
5. try {
6. i++;
7. j--;
8. if(i/j > 1)
9. i++;
10. }
11. catch(ArithmeticException e) {
12. System.out.println(0);
13. }
14. catch(ArrayIndexOutOfBoundsException e) {
15. System.out.println(1);
16. }
17. catch(Exception e) {
18. System.out.println(2);
19. }
20. finally {
21. System.out.println(3);
22. }
23. System.out.println(4);
24. }
25. }
26.
27.
B. 0
C. 1
D. 2
E. 3
F. 4
18. What all gets printed when the following gets compiled and run. Select the two correct answers.
A. 0
B. 1
C. 2
D. 3
E. 4
19. What all gets printed when the following gets compiled and run. Select the two correct answers.
A. 1
B. 2
C. 3
D. 4
20. What all gets printed when the following gets compiled and run. Select the two correct answers.
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);
}
}
A. 1
B. 2
C. 3
D. 4
21. Which of the following are legal array declarations. Select the three correct answers.
A. int i[5][];
B. int i[][];
C. int []i[];
D. int i[5][5];
E. int[][] a;
22. What is the range of values that can be specified for an int. Select the one correct answer.
A. The range of values is compiler dependent.
B. -231 to 231 - 1
C. -231-1 to 231
D. -215 to 215 - 1
E. -215-1 to 215
23. 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.
24. What gets printed when the following code is compiled and run. Select the one correct answer.
A. 0
B. 1
C. 2
D. -1
25. Which of these is a legal definition of a method named m assuming it throws IOException, and returns
void. Also assume that the method does not take any arguments. Select the one correct answer.
A. void m() throws IOException{}
B. void m() throw IOException{}
C. void m(void) throws IOException{}
D. m() throws IOException{}
E. void m() {} throws IOException
26. Which of the following are legal identifier names in Java. Select the two correct answers.
A. %abcd
B. $abcd
C. 1abcd
D. package
E. _a_long_name
27. At what stage in the following method does the object initially referenced by s becomes available for
garbage collection. Select the one correct answer.
int i = 0, j = 1;
System.out.println(i);
}
}
A. 4
B. 5
C. 6
D. 6.1
E. 9
40. Is the following statement true or false. As the toString method is defined in the Object class,
System.out.println can be used to print any object.
A. true
B. false
41. Which of these classes defined in java.io and used for file-handling are abstract. Select the two correct
answers.
A. InputStream
B. PrintStream
C. Reader
D. FileInputStream
E. FileWriter
42. Name the collection interface used to represent collections that maintain unique elements.
43. What is the result of compiling and running the following program.
str1.concat(str2);
System.out.println(str1);
}
}
A. abc
B. def
C. abcabc
D. abcdef
E. defabc
F. abcdefdef
44. Select the one correct answer. The number of characters in an object of a class String is given by
A. The member variable called size
B. The member variable called length
C. The method size() returns the number of characters.
D. The method length() returns the number of characters.
45. Select the one correct answer. Which method defined in Integer class can be used to convert an Integer
object to primitive int type.
A. valueOf
B. intValue
C. getInt
D. getInteger
46. Name the return type of method hashCode() defined in Object class, which is used to get the unique
hash value of an Object.
47. Which of the following are correct. Select the one correct answer.
A. An import statement, if defined, must always be the first non-comment statement of the file.
B. private members are accessible to all classes in the same package.
C. An abstract class can be declared as final.
D. Local variables cannot be declared as static.
48. Name the keyword that makes a variable belong to a class, rather than being defined for each instance of
the class. Select the one correct answer.
A. static
B. final
C. abstract
D. native
E. volatile
F. transient
49. Which of these are core interfaces in the collection framework. Select the one correct answer.
A. Tree
B. Stack
C. Queue
D. Array
E. LinkedList
F. Map
50. Which of these statements are true. Select the two correct answers.
A. For each try block there must be at least one catch block defined.
B. A try block may be followed by any number of finally blocks.
C. A try block must be followed by at least one finally or catch block.
D. If both catch and finally blocks are defined, catch block must precede the finally block.
1. b
2. b, c, f
3. d. Note that the program gets one command line argument - 2. args.length will get set to 1. So the
condition if(args.length > 1) will fail, and the second check if(args.length > 0) will return true.
4. arr.length
5. Any of these is correct - 0x5, 0x05, 0X05, 0X5
6. b, c, f, g
7. a
8. d
9. "am"
10. a, c. Please note that b is not correct. A package statement may appear before an import statement. A
class constructor may be declared private also. Hence d is incorrect.
11. a
12. a
13. protected
14. a, c
15. b, c
16. c
17. a, d, e
18. d, e
19. a, c
20. b, c
21. b, c, e
22. b
23. d
24. a
25. a
26. b, e . The option c is incorrect because a Java identifier name cannot begin with a digit.
27. d
28. c
29. c, d
30. a, c
31. b, d
32. b, c
33. c
34. c
35. c
36. b, c
37. f
38. long
39. e
40. a
41. a, c
42. Set
43. a
44. d
45. b
46. int
47. d
48. a
49. f
50. c, d
--------------------*******************************************************************
1. Which of the following are Java keywords? Select the three correct answers.
A. external
B. implement
C. throw
D. void
E. integer
F. private
G. synchronize
H. unsigned
2. Which of the following are legal definitions of the main method that can be used to execute a class.
Select the one correct answer.
A. public void main(String args)
B. public static int main(String args[])
C. public static void main(String args[])
D. static public void MAIN(String args[])
E. public static void main(string args[])
F. public static void main(String *args)
3. Which of these are legal array declarations or definitions? Select the two correct answers.
A. int[] []x[];
B. int *x;
C. int x[5];
D. int[] x = {1,2,3};
4. Name the collection interface used to represent a sequence of numbers in a fixed order.
5. The class Hashtable is used to implement which collection interface. Select the one correct answer.
A. Table
B. List
C. Set
D. SortedSet
E. Map
6. What gets printed when the following program is compiled and run? Select the one correct answer.
class test {
int i;
do {
i++;
System.out.println(i);
}
A. The program does not compile as i is not initialized.
B. The program compiles but does not run.
C. The program compiles and runs but does not print anything.
D. The program prints 0.
E. The program prints 1.
7. What gets printed when the following program is compiled and run? Select the one correct answer.
1.
2.
3.
4. class xyz {
5.
6. static int i;
7.
8. public static void main(String args[]) {
9.
10.
11.
12. while (i < 0) {
13.
14. i--;
15.
16. }
17.
18. System.out.println(i);
19.
20. }
21.
22. }
23.
24.
25.
B. The program does not compile as i is not initialized.
C. The program compiles but does not run.
D. The program compiles and runs but does not print anything.
E. The program prints 0.
F. The program prints 1.
8. What gets printed when the following program is compiled and run? Select the one correct answer.
class xyz {
int i,j,k;
for (i = 0; i < 3; i++) {
System.out.println(i);
A. 0
B. 1
C. 2
D. 3
E. 4
9. Using up to four characters what is the Java representation of the number 23 in hex?
10. What gets printed when the following program is compiled and run? Select the one correct answer.
class test {
int i;
if(check == true)
i=1;
else
i=2;
if(i=2) i=i+2;
else i = i + 4;
System.out.println(i);
A. 3
B. 4
C. 5
D. 6
E. The program does not compile because of the statement if(i=2)
11. Select the one correct answer. The smallest number that can be represented using short primitive type in
Java is -
A. 0
B. -127
C. -128
D. -16384
E. -32768
F. The smallest number is compiler dependent.
12. Given the following declarations, which of the assignments given in the options below would compile.
Select the two correct answers.
1.
2.
3.
4. int i = 5;
5.
6. boolean t = true;
7.
8. float f = 2.3F;
9.
10. double d = 2.3;
11.
12.
13.
B. t = (boolean) i;
C. f = d;
D. d = i;
E. i = 5;
F. f = 2.8;
13. What gets printed when the following program is compiled and run. Select the one correct answer.
1.
2.
3.
4. public class incr {
5.
6. public static void main(String args[]) {
7.
8. int i , j;
9.
10. i = j = 3;
11.
12. int n = 2 * ++i;
13.
14. int m = 2 * j++;
15.
16. System.out.println(i + " " + j + " " + n + " " + m);
17.
18. }
19.
20. }
21.
22.
23.
B. 4486
C. 4488
D. 4466
E. 4386
F. 4388
G. 4468
14. Given two non-negative integers a and b and a String str, what is the number of characters in the
expression str.substring(a,b) . Select the one correct answer.
A. a + b
B. a - b
C. b - a - 1
D. b - a + 1
E. b - a
F. b
15. What is the result of compiling and running the following program. Select the one correct
answer.
1.
2.
3.
4. class test {
5.
6. public static void main(String args[]) {
7.
8. char ch;
9.
10. String test2 = "abcd";
11.
12. String test = new String("abcd");
13.
14. if(test.equals(test2)) {
15.
16. if(test == test2)
17.
18. ch = test.charAt(0);
19.
20. else
21.
22. ch = test.charAt(1);
23.
24. }
25.
26. else {
27.
28. if(test == test2)
29.
30. ch = test.charAt(2);
31.
32. else
33.
34. ch = test.charAt(3);
35.
36. }
37.
38. System.out.println(ch);
39.
40. }
41.
42. }
43.
44.
45.
B. 'a'
C. 'b'
D. 'c'
E. 'd'
16. What is the result of compiling and running the following program. Select the one correct
answer.
1. class test {
3. int i,j=0;
4. for(i=10;i<0;i--) { j++; }
5. switch(j) {
6. case (0) :
1. j=j+1;
7. case(1):
1. j=j+2;
2. break;
8. case (2) :
1. j=j+3;
2. break;
9. case (10) :
1. j=j+10;
2. break;
10. default :
1. break;
11. }
12. System.out.println(j);
13. }
2. }
3.
B. 0
C. 1
D. 2
E. 3
F. 10
G. 20
17. What is the number displayed when the following program is compiled and run.
class test {
System.out.println(test1.xyz(100));
if(num == 1) return 1;
}
}
18. Which of the following statements are true. Select the one correct answer.
A. Arrays in Java are essentially objects.
B. It is not possible to assign one array to another. Individual elements of array can however be
assigned.
C. Array elements are indexed from 1 to size of array.
D. If a method tries to access an array element beyond its range, a compile warning is generated.
19. Which expression can be used to access the last element of an array. Select the one correct answer.
A. array[array.length()]
B. array[array.length() - 1]
C. array[array.length]
D. array[array.length - 1]
20. What is the result of compiling and running the following program. Select the one correct answer.
class test {
call_array(arr[0], arr);
arr[i] = 6;
i = 5;
A. 1,2
B. 5,2
C. 1,6
D. 5,6
21. Which of the following statements are correct. Select the one correct answer.
A. Each Java file must have exactly one package statement to specify where the class is stored.
B. If a Java file has both import and package statement, the import statement must come before
package statement.
C. A Java file has at least one class defined.
D. If a Java file has a package statement, it must be the first statement (except comments).
22. What happens when the following program is compiled and then the command "java check it out" is
executed. Select the one correct answer.
class check {
System.out.println(args[args.length-2]);
23. What all gets printed when the following code is compiled and run. Select the three correct answers.
class test {
try {
}
catch(ArrayIndexOutOfBoundsException e1) {
System.out.println("1");
catch(Exception e2) {
System.out.println("2");
finally {
System.out.println(3);
System.out.println("4");
A. 1
B. 2
C. 3
D. 4
24. A program needs to store the name, salary, and age of employees in years. Which of the following data
types should be used to create the Employee class. Select the three correct answers.
A. char
B. boolean
C. Boolean
D. String
E. int
F. double
25. To make a variable defined in a class accessible only to methods defined in the classes in same package,
which of the following keyword should be used. Select the one correct answer.
A. By using the keyword package before the variable.
B. By using the keyword private before the variable.
C. By using the keyword protected before the variable.
D. By using the keyword public before the variable.
E. The variable should not be preceded by any of the above mentioned keywords.
26. In implementing two classes Employee and Manager, such that each Manager is an Employee, what
should be the relationship between these classes. Select the one correct answer.
A. Employee should be the base class of Manager class.
B. Manager should be the base class of Employee class.
C. Manager class should include the Employee class as a data member.
D. Employee class should include Manager class as a data member.
E. The Manager and Employee should not have any relationship.
27. Select the one most appropriate answer. What is the purpose of method parseInt defined in Integer class.
A. The method converts an integer to a String.
B. The method is used to convert String to an integer, assuming that the String represents an integer.
C. The method is used to convert String to Integer class, assuming that the String represents an
integer.
D. The method converts the Integer object to a String.
28. What should be done to invoke the run() method on a thread for an object derived from the Thread class.
Select the one correct answer.
A. The run() method should be directly invoked on the Object.
B. The start() method should be directly invoked on the Object.
C. The init() method should be directly invoked on the Object.
D. The creation of the object using the new operator would create a new thread and invoke its run()
method.
1. c, d, f
2. c. The main method must be static and return void. Hence a and b are incorrect. It must take an array of
String as argument. Hence e and f are incorrect. As Java is case sensitive, d is incorrect.
3. a, d
4. List
5. e. The collection interface Map has two implementation HashMap and Hashtable.
6. a. Local variables are not initialized by default. They must be initialized before they are used.
7. d. The variable i gets initialized to zero. The while loop does not get executed.
8. c. During various iterations of three loops, the only time i, j and k have same values are when all of them
are set to 2.
9. 0x17 or 0X17.
10. e. The statement "i=2" evaluates to 2. The expression within the if block must evaluate to a boolean.
11. e. The range of short primitive type is -32768 to 32767.
12. c,d. Java does not allow casts between boolean values and any numeric types. Hence a is incorrect.
Assigning double to a float requires an explicit cast. Hence b and e are incorrect.
13. a
14. e
15. b. Both Strings test and test2 contain "abcd" . They are however located at different memory addresses.
Hence test == test2 returns false, and test.equals(test2) returns true.
16. d. The for loop does not get executed even once as the condition (i < 0) fails in the first iteration. In the
switch statement, the statement j = j +1; gets executed, setting j to 1. As there is no break after this case,
the next statement also gets executed setting j to 3.
17. 5050. The recursive function xyz essentially sums up numbers 1 to num. This evaluates to (num * (num
+ 1))/2.
18. a. Java supports assignment of one array to another. Hence b is incorrect. Array elements are indexed
from 0. Hence c is incorrect. A method that accesses array elements out of its range does not generate a
compilation error. Hence d is incorrect.
19. d. array.length gives the number of elements in the array. As indexes in Java start from 0, d is the correct
answer.
20. c. In the invocation of call_array, the first element is invoked using call-by-value, and the second using
call-by-reference.
21. d. import statement, package statement and class definitions are all optional in a file. Hence a and c are
incorrect. If both import and package statements are present in a file, then package statement must
appear before the import statement. Hence b is incorrect.
22. e. The args array consists of two elements "it" and "out". args.length is set to two.
23. a,c,d. The exception ArrayIndexOutOfBoundsException is generated as the main method tries to access
i[2]. Hence 1 gets printed. After this finally block gets excuted, before the program exits.
24. d,e,f
25. e. A data member that does not have public/protected/private is accessible to all methods in the same
package.
26. a. The Manager and Employee share as "is a" relationship - A Manager is an Employee. This is captured
by making Employee the base class of Manager.
27. b. The method int parseInt(Sting s) returns the integer value corresponding to input String, assuming that
the input string represents an integer in base 10.
28. b. The start() method invokes the run() method when the thread is ready to execute.
29. d