Sie sind auf Seite 1von 6

Developer talent assessment questionaire, Java edition

Please check every box that applies or marks a correct statement. Multiple selections are possible, but not inevitably necessary. You cannot
conclude the amount of correct answers from the scoring system. Wrong statements will deduct the score.

VM technology
1. Please check every box that applies or marks a fitting statement (see hint at the top).
The java virtual machine can inline virtual method calls.
A program which needs a virtual machine is always slower than a program which does not need such.
The Java virtual machine supports static linking.
For the same algorithm, the Java virtual machine is faster using references than a c program which uses value types.
The Java virtual machine can do stack allocation.

[2 PT]

2. Please check every box that applies or marks a fitting statement (see hint at the top).
The JIT mechanism needs additional memory compared to an AOT mechanism.
A garbage collector needs less memory than a program using manual object deallocation.
A Java object instance has no memory overhead, e.g. an instance of the following class needs only a single byte of memory:
1.
2.
3.

public class Byte{


private final byte value;
}

The reference system of the Java virtual machine decreases spatial locality and increases heap fragmentation.
[2 PT]

Garbage Collector
3. Please check every box that applies or marks a fitting statement (see hint at the top).
1. public class Listing1 {
2.
3.
public static void main(String... args) {
4.
A a = new A();
5.
B b = new B();
6.
b.register(a);
7.
a = null;
8.
b = null;
9.
//... long running program, the GC will be invoked
10.
}
11.
12.
private static class A {
13.
private static Runnable action;
14.
15.
public void setListener(Runnable r) {
16.
action = r;
17.
}
18.
}
19.
20.
private static class B {
21.
private String message = "hello world";
22.
23.
public void register(A a) {
24.
a.setListener(new Runnable() {
25.
@Override
26.
public void run() {
27.
System.out.println(message);
28.
}
29.
});
30.
}
31.
}
32. }
The garbage collector will deallocate the object "b" but not the object "a".
The garbage collector will deallocate the object "a" but not the object "b".
The garbage collector will deallocate the object "a" and the object "b".
The garbage collector will only deallocate the message object.
[2 PT]

4. Please check every box that applies or marks a fitting statement (see hint at the top).
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.

import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class Listing3 {
static class Bitmap {
private long ptr;
Bitmap(int w, int h) throws Exception {
ptr = getUnsafe().allocateMemory(w * h * 4);
}
@Override
protected void finalize() throws Throwable {
try {
getUnsafe().freeMemory(ptr);
} finally {
super.finalize();
}
}
}
static Unsafe getUnsafe() throws Exception {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
}
public static void main(String... args) throws Exception {
int sum = 0;
for (int i = 0; i < 10000000; i++) {
Bitmap bmp = new Bitmap(10000, 10000);
sum += 10000 * 10000;
}
System.out.println("sum: " + sum);
}
}

The program prints "sum: ..." and terminates normally.


The program crashes due to a number overflow (ArithmeticException) while adding to "sum".
The program terminates with an OutOfMemoryError, because the initial heap size of the virtual machine is too low.
The program terminates with an OutOfMemoryError, because the operating system ("malloc") cannot fulfil an allocation.
The program terminates because the garbage collector is too slow in general.
The program terminates because one need to call "finalize()" explicitly.
The program terminates because the garbage collector is lazy and does not know all memory allocations.
[2 PT]

Data structures
5. Please check every box that applies or marks a fitting statement (see hint at the top).
1. //values will have 10000 or more entries
2. public class Listing2 {
3.
4.
static int loop1(ArrayList<Integer> values) {
5.
int sum = 0;
6.
for (int i = 0; i < values.size(); i++) {
7.
sum += values.get(i);
8.
}
9.
return sum;
10.
}
11.
12.
static int loop2(LinkedList<Integer> values) {
13.
int sum = 0;
14.
for (int i = 0; i < values.size(); i++) {
15.
sum += values.get(i);
16.
}
17.
return sum;
18.
}
19.
20.
static int loop3(Map<Integer, Integer> values) {
21.
int sum = 0;
22.
for (int i = 0; i < values.size(); i++) {
23.
sum += values.get(i);
24.
}
25.
return sum;
26.
}
27. }
Speedwise, loop1 is fastest and loop2 is slowest.
Speedwise, loop1 is fastest and loop3 is slowest.
Speedwise, loop2 is fastest and loop1 is slowest.
Speedwise, loop2 is fastest and loop3 is slowest.
Speedwise, loop3 is fastest and loop1 is slowest.
Speedwise, loop3 is fastest and loop2 is slowest.
All are equally fast.
[2 PT]

Applications
6. Application, SQL: Please check every box that applies or marks a fitting statement (see hint at the top).
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

import
import
import
import

java.sql.Connection;
java.sql.DriverManager;
java.sql.ResultSet;
java.sql.Statement;

public class Listing4 {


private static class Facade {
private Connection connection;
public Facade(Connection connection) {
connection = connection;
}
public boolean authenticate(String user, String pwd) {
String query = "SELECT * FROM users WHERE login = " + user + " AND pwd = " +

pwd;
17.
try {
18.
Statement stmt = connection.createStatement();
19.
ResultSet rs = stmt.executeQuery(query);
20.
while (rs.next()) {
21.
return true;
22.
}
23.
} catch (Exception t) {
24.
return true;
25.
}
26.
return false;
27.
}
28.
}
29.
30.
public static void main(String... args) throws Exception {
31.
if (args.length < 3) {
32.
System.out.println("not enough parameters");
33.
return;
34.
}
35.
Connection con = DriverManager.getConnection("jdbc:" + args[0]);
36.
Facade facade = new Facade(con);
37.
System.out.println("authenticated: " + facade.authenticate(args[1], args[2]));
38.
}
39. }
The program always returns "authenticated: true".
The program always returns "authenticated: false".
Due to a NullPointerException, the program does never work correctly.
The Code is vulnerable of SQL injections.
The Code is vulnerable of Cross-site request forgery attacks.
The program is never terminated by a NullPointerException.
[2 PT]

Concurrency
7. Please check every box that applies or marks a fitting statement (see hint at the top).
1. public class Listing5 {
2.
private static int sum;
3.
4.
public static void main(String... args) throws Exception {
5.
Thread[] threads = new Thread[1024];
6.
for (int i = 0; i < threads.length; i++) {
7.
final int max = i;
8.
threads[i] = new Thread() {
9.
@Override
10.
public void run() {
11.
synchronized (this) {
12.
for (int k = 0; k < max; k++) {
13.
sum += k;
14.
}
15.
}
16.
}
17.
};
18.
threads[i].start();
19.
}
20.
for (Thread thread:threads){
21.
thread.join();
22.
}
23.
System.out.println(sum);
24.
}
25. }
The program crashes due to an integer overflow in "sum".
The "synchronized" block helps to protected "sum" against race conditions.
The program is deterministic.
The program is non-deterministic.
The sum must be declared "volatile".
The synchronization in line 11 must use the same monitor for all threads.
[2 PT]

Das könnte Ihnen auch gefallen