APOLLO COMPUTER EDUCATION LTD.
JAVA PROGRAMING LANGUAGE TEST
25 x 4 =100 TIMINGS: 1:00 hrs
1) Which four options describe the correct default values for array elements of the types
indicated?
int -> 0
String -> "null"
Dog -> null
char -> '\u0000'
float -> 0.0f
boolean -> true
A
1, 2, 3, 4
.
B. 1, 3, 4, 5
C
2, 4, 5, 6
.
D
3, 4, 5, 6
.
2) Which will legally declare, construct, and initialize an array?
A
int [] myList = {"1", "2", "3"};
.
B. int [] myList = (5, 8, 2);
C
int myList [] [] = {4,9,7,0};
.
D
int myList [] = {4, 3, 7};
.
3)Which three are legal array declarations?
int [] myScores [];
char [] myChars;
1
int [6] myScores;
Dog myDogs [];
Dog myDogs [7];
A
1, 2, 4
.
B. 2, 4, 5
C
2, 3, 4
.
D
All are correct.
.
4)Which one of the following will declare an array and initialize it with five numbers?
A
Array a = new Array(5);
.
B. int [] a = {23,22,21,20,19};
C
int a [] = new int[5];
.
D
int [5] array;
.
5) Which is the valid declarations within an interface definition?
A
public double methoda();
.
B. public final double methoda();
C
static void methoda(double d1);
.
D
protected void methoda(double d1);
.
6) Which one is a valid declaration of a boolean?
A
boolean b1 = 0;
.
B. boolean b2 = 'false';
2
C
boolean b3 = false;
.
D
boolean b4 = Boolean.false();
.
E. boolean b5 = no;
7) Which is a valid declarations of a String?
A
String s1 = null;
.
B. String s2 = 'null';
C
String s3 = (String) 'abc';
.
D
String s4 = (String) '\ufeed';
.
8) What is the numerical range of a char?
A
-128 to 127
.
B. -(215) to (215) – 1
C
0 to 32767
.
D
0 to 65535
.
9) Which of the following are legal lines of code?
int w = (int)888.8;
byte x = (byte)1000L;
long y = (byte)100;
byte z = (byte)100L;
A
1 and 2
.
B. 2 and 3
C
3 and 4
.
3
D
All statements are correct.
.
10)What will be the output of the program?
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
A
Finally
.
B. Compilation fails.
C
The code runs with no output.
.
D
An exception is thrown at runtime.
.
11)What will be the output of the program?
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
A
4
B. Exception
C
Compilation fails.
.
D
Arithmetic Exception
.
12) Which of the following line of code is suitable to start a thread ?
class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}
}
A
Thread t = new Thread(X);
.
B. Thread t = new Thread(X); t.start();
C
X run = new X(); Thread t = new Thread(run); t.start();
.
D
Thread t = new Thread(); x.run();
.
13) Suppose that you would like to create an instance of a new Map that has an iteration
order that is the same as the iteration order of an existing instance of a Map. Which
concrete implementation of the Map interface should be used for the new instance?
A
TreeMap
.
B. HashMap
C
LinkedHashMap
.
D
The answer depends on the implementation of the existing instance.
.
5
14) You need to store elements in a collection that guarantees that no duplicates are stored
and all elements can be accessed in natural order. Which interface provides that
capability?
A
java.util.Map
.
B. java.util.Set
C
java.util.List
.
D
java.util.Collection
.
15) Which interface provides the capability to store objects using a key-value pair?
A
Java.util.Map
.
B. Java.util.Set
C
Java.util.List
.
D
Java.util.Collection
.
16) Which is valid declaration of a float?
A
float f = 1F;
.
B. float f = 1.0;
C
float f = "1";
.
D
float f = 1.0d;
.
17) What will be the output of the program?
public class Test
{
public static void main (String args[])
{
String str = NULL;
System.out.println(str);
}}
A
NULL
.
6
B. Compile Error
C
Code runs but no output
.
D
Runtime Exception
.
18) What will be the output of the program?
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() )
{
System.out.print( it.next() + " " );
}
A
one two three four
.
B. four three two one
C
four one three two
.
D
one two three four one
.
19) You want subclasses in any package to have access to members of a superclass. Which
is the most restrictive access that accomplishes this objective?
A
Public
.
B.Private
C.Protected
D
Transient
.
7
20) Which two are acceptable types for x?
switch(x)
default:
System.out.println("Hello");
1. byte
2. long
3. char
4. float
5. Short
6. Long
A
1 and 3
.
B.2 and 4
C.3 and 5
D
4 and 6
.
21) Which statement is true?
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
8
}
A
Compilation fails.
.
B."odd" will always be output.
C."even" will always be output.
D
"odd" will be output for odd values of x, and "even" for even values.
.
22) Which statement is true?
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}
A
There is a syntax error on line 1.
.
B.There are syntax errors on lines 1 and 6.
C.There are syntax errors on lines 1, 6, and 8.
D
There is a syntax error on line 6.
.
23) What will be the output of the program?
int i = 1, j = 10;
do
{
if(i > j)
{
9
break;
}
j--;
} while (++i < 5);
System.out.println("i = " + i + " and j = " + j);
A
i = 6 and j = 5
.
B.i = 5 and j = 5
C.i = 6 and j = 4
D
i = 5 and j = 6
.
24) What will be the output of the program?
int x = l, y = 6;
while (y--)
{
x++;
}
System.out.println("x = " + x +" y = " + y);
A
x=6y=0
.
B.x = 7 y = 0
C.x = 6 y = -1
D
Compilation fails.
.
25) What will be the output of the program?
for(int i = 0; i < 3; i++)
{
switch(i)
{
case 0: break;
case 1: System.out.print("one ");
case 2: System.out.print("two ");
case 3: System.out.print("three ");
}
}
10
System.out.println("done");
A
done
.
B.one two done
C.one two three done
D
one two three two three done
.
11