Sun Certified Java Programmer
Practice Exam ( 21 - 30 Questions)
Question 21 What will happen if you compile/run the following code?
1: public class Q21
2: {
3: int maxElements;
4:
5: void Q21()
6: {
7: maxElements = 100;
8: System.out.println(maxElements);
9: }
10:
11: Q21(int i)
12: {
13: maxElements = i;
14: System.out.println(maxElements);
15: }
16:
17: public static void main(String[] args)
18: {
19: Q21 a = new Q21();
20: Q21 b = new Q21(999);
21: }
22: }
A) Prints 100 and 999.
B) Prints 999 and 100.
C) Compilation error at line 3, variable maxElements was not initialized.
D) Compillation error at line 19.
Answer
Question 22 What will happen if run the following code?
1: Boolean[] b1 = new Boolean[10];
2:
3: boolean[] b2 = new boolean[10];
4:
6: System.out.println("The value of b1[1] = " +b1[1]);
7: System.out.println("The value of b2[1] = " +b2[1]);
A) Prints "The value of b1[1] = false"
"The value of b2[1] = false".
B) Prints "The value of b1[1] = null"
"The value of b2[1] = null".
C) Prints "The value of b1[1] = null"
"The value of b2[1] = false".
D) Prints "The value of b1[1] = false"
"The value of b2[1] = null".
Answer
Question 23 Which of the following are valid array declarations/definitions?
1: int iArray1[10];
2: int iArray2[];
3: int iArray3[] = new int[10];
4: int iArray4[10] = new int[10];
5: int []iArray5 = new int[10];
6: int iArray6[] = new int[];
7: int iArray7[] = null;
A) 1.
B) 2.
C) 3.
D) 4.
E) 5.
F) 6.
G) 7.
Answer
Question 24 What is the output for the following lines of code?
1: System.out.println(" " +2 + 3);
2: System.out.println(2 + 3);
3: System.out.println(2 + 3 +"");
4: System.out.println(2 + "" +3);
A) Compilation error at line 3
B) Prints 23, 5, 5 and 23.
C) Prints 5, 5, 5 and 23.
D) Prints 23, 5, 23 and 23.
Answer
Question 25 The following declaration(as a member variable) is legal.
static final transient int maxElements = 100;
A) True.
B) False.
Answer
Question 26 What will happen if you compile/run the following lines of code?
1: int[] iArray = new int[10];
2:
3: iArray.length = 15;
4:
5: System.out.println(iArray.length);
A) Prints 10.
B) Prints 15.
C) Compilation error, you can't change the length of an array.
D) Runtime exception at line 3.
Answer
Question 27 What will happen if you compile/run the folowing lines of code?
1: Vector a = new Vector();
2:
3: a.addElement(10);
4:
5: System.out.println(a.elementAt(0));
A) Prints 10.
B) Prints 11.
C) Compilation error at line 3.
D) Prints some garbage.
Answer
Question 28 What will happen if you invoke the following method?
1: public void check()
2: {
3: System.out.println(Math.min(-0.0,+0.0));
4: System.out.println(Math.max(-0.0,+0.0));
5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
6: }
A) prints -0.0, +0.0 and false.
B) prints -0.0, +0.0 and true.
C) prints 0.0, 0.0 and false.
D) prints 0.0, 0.0 and true.
Answer
Question 29 What will happen if you compile/run this code?
1: int i = 012;
2: int j = 034;
3: int k = 056;
4: int l = 078;
5:
6: System.out.println(i);
7: System.out.println(j);
8: System.out.println(k);
A) Prints 12,34 and 56.
B) Prints 24,68 and 112.
C) Prints 10, 28 and 46.
D) Compilation error.
Answer
Question 30 When executed the following line of code will print
System.out.println(-1 * Double.NEGATIVE_INFINITY);
A) -Infinity
B) Infinity
C) NaN
D) -NaN
Answer
Question No 21 D. Constructors should not return any value. Java won't allow to indicate with void.
In this case void Q21() is an ordinary method which has the same name of the Class.
Question No 22 C. By default objects will be initialized to null and primitives to their corresponding default vaulues.
The same rule applies to array of objects and primitves.
Back to Question 22
Question No 23 B,C,E and G. You can't specify the array dimension in type specification(left hand side),
so A and D are invalid. In line 6 the array dimension is missing(right hand side) so F is invalid.
You can intialize an array with null. so G is valid.
Back to Question 23
Question No 24 B.
Back to Question 24
Question No 25 A.
Back to Question 25
Question No 26 C. Once array is created then it is not possible to change the length of the array.
Back to Question 26
Question No 27 C. You can't add primitives to Vector. Here 10 is int type primitive.
Back to Question 27
Question No 28 B. The order of floating/double values is
-Infinity --> Negative Numbers/Fractions --> -0.0 --> +0.0 --> Positive Numbers/Fractions --> Infinity.
Back to Question 28
Question No 29 D. Here integers are assinged by octal values. Octal numbers will contain digits from 0 to 7.
8 is illegal digit for an octal value, so you get compilation error.
Back to Question 29
Question No 30 B. Compile and see the result.
Back to Question 30