Hab jetzt den real Java Test bestanden.
Hier die Fragen, wo ich Fehler hatte. Und besonders schwierige Fragen. Manche waren sehr einfach. Man muß allerdings über 80% und einen weiteren OO-Test bestehen für den gelben Gürtel. :
A. What happens when you try to compile and run this program?
public class ClassOne {
//...
public String toString(){
return "It's me, an instance of classOne";
}
//...
public static void main(String[] args) {
String test;
ClassOne classOne;
test = classOne.toString();
//...
}
}
1. The JVM creates a new object automatically
2. A NullPointerException occurs (dachte ich)
3. The method is not called and the program continues.
4. The method is called and the program continues.
5. You have the compiler error "The local variable classOne may not have been initialized" on the statement test = classOne.toString(); (das war richtig).
Ja gut. Irgendwie klar. Kompiliert nicht.
B. Which modifiers may appear in a field declaration?
1. public (richtig)
2. protected (richtig)
3. package (falsch)
4. private (richtig)
5. static (richtig)
6. final (richtig)
Ich hab blöderweise package angeklickt. Der leere modifier wird manchmal package-private genannt. Das war die Assoziation. Eine komische und falsche Assoziation.
C.
What will the following program print ?
public class Test
{
public static void main ( String [] args )
{
boolean bTwo = true;
boolean bFour = false;
if ( bTwo || validate1 () )
{
System.out.println ( "3" );
}
if ( bFour && validate2 () )
{
System.out.println ( "4" );
}
}
public static boolean validate1 ()
{
System.out.println ( "1" );
return false;
}
public static boolean validate2 ()
{
System.out.println ( "2" );
return true;
}
}
1)
1
3
2
2)
1
3
3)
4
4)
3
hatte ich richtig. Natürlich Option 4.
D)
This code fragment compiles.
String [] color = {"red", "green", "blue"};
color = new String [5];
color = {"red", "green", "blue", "yellow", "white"};
hatte ich mehr zufällig richtig. Die letzte Zeile ist ein Fehler. Man kann Arrays so nur in Zeilen benutzen, in denen man die Array-Variable auch deklariert. kurios.
E)
Study the following code and decide which of the objects referred by the variables are eligible for Garbage Collection after gc[3]=null; has been executed and before the program terminates.
class GarbageTest {
public static void main(String args[]) {
GarbageTest gc[] = new GarbageTest[5];
for (int i = 0; i < 5; i++) {
gc = new GarbageTest();
}
gc[3] = gc[1];
gc[4] = gc[1];
gc[3] = null;
// more code here
}
}
Ich Idiot dachte, dass g[3], g[1] und g[4] eligible für gc sind. Das ist natürlich Unsinn, da sie noch im Array gehalten werden !!!
F. Which result is displayed on the console (when this code fragment is in a healthy main method, within a class) ?
Double number = new Double(15);
switch (number) {
case 0: System.out.println("The value is 0");
case 15: System.out.println("The value is 15");
default: System.out.println("The value is neither 0 nor 15");
}
a) The value is 15
b)The value is 0
c) The value is neither 0 nor 15
d) The value is 0
The value is 15
The value is neither 0 nor 15
e)
The value is 15
The value is neither 0 nor 15
(ich)
f)Compilation fails (das war richtig)
Für Java 5 hatte ich recht. Offenbar gilt Java5 nur, wenn es explizit angegeben ist. No Fair.
---
g) Suppose you have a method called operate which returns an int and contains this code:
try {
result = numerator / denominator;
return result;
} catch (ArithmeticException ae) {
return 0;
} finally {
result = 3;
return result;
}
What will be the return value of the method operate if the value of numerator is 1 and the value of denominator is 0?
3 war richtig.
H)
What is the output of the following code fragment ?
// list points to a new ArrayList
list.add(new Integer(1));
list.add(new Integer(2));
...
list.add(new Integer(5));
// now lists contains 1, 2, 3, 4 and 5.
for (int index = 0; index < list.size(); index++) {
System.out.println (index + 1 + " : " + list.get(index));
}
a)
01 : 1
11 : 2
21 : 3
31 : 4
41 : 5
b)
1 : 1
2 : 2
3 : 3
4 : 4
5 : 5
c) An error will occur, the operands aren't the same.
d) The list.get(index) will throw an IndexOutOfBound exception at some point.
b war richtig. Nicht leicht.
I)
Which of these code fragments are correctly formed and compile without errors ?
1) for (int i = 0,j = 1;i < j;i++,j++){
System.out.println("i:"+i+" j:"+j);
}
2) for (int i = 0,int j = 1;i < j;i++,j++){
System.out.println("i:"+i+" j:"+j);
}
3)
int i = 0;
for (int i = 0,j = 1;i < j;i++,j++){
System.out.println("i:"+i+" j:"+j);
}
4)
int i;
for (int j = 1;i < j;i++,j++){
System.out.println("i:"+i+" j:"+j);
}
5)
int i = 0;
for (int j = 1;i < j;i++,j++){
System.out.println("i:"+i+" j:"+j);
}
6)
for (int i = 0,j = 1;i < j;i++,j++)
System.out.println("i:"+i+" j:"+j);
Alles ausser 2) kompiliert. Hart. Hatte ich richtig.