Java Type System and Object Model QUIZ
QUIZ Subtype relation 1. Yes 2. No Comparable<String> is a subtype of Object ? Comparable<String> is a subtype of String ? String is a subtype of Comparable<String> ? boolean is a subtype of Object ? boolean[][] is a subtype of Object ?
QUIZ Subtype relation 1. Yes 2. No Comparable<String> is a subtype of Object ? Comparable<String> is a subtype of String ? String is a subtype of Comparable<String> ? boolean is a subtype of Object ? boolean[][] is a subtype of Object ?
QUIZ Subtype relation 1. Yes 2. No Comparable<String> is a subtype of Object ? Comparable<String> is a subtype of String ? String is a subtype of Comparable<String> ? boolean is a subtype of Object ? boolean[][] is a subtype of Object ?
QUIZ Subtype relation 1. Yes 2. No Comparable<String> is a subtype of Object ? Comparable<String> is a subtype of String ? String is a subtype of Comparable<String> ? boolean is a subtype of Object ? boolean[][] is a subtype of Object ?
QUIZ Subtype relation 1. Yes 2. No Comparable<String> is a subtype of Object ? Comparable<String> is a subtype of String ? String is a subtype of Comparable<String> ? boolean is a subtype of Object ? boolean[][] is a subtype of Object ?
QUIZ Subtype relation 1. Yes 2. No Comparable<String> is a subtype of Object ? Comparable<String> is a subtype of String ? String is a subtype of Comparable<String> ? boolean is a subtype of Object ? boolean[][] is a subtype of Object ?
public void test(Employee e) { QUIZ A e.setBonus(10); B ((Manager)e).setBonus(10); Static / dynamic type } Which – if any – errors arise? … test(new Driver()); … No error Compiler Exception error on runtime 1. A,B 2. A B 3. A B 4. B A 5. A,B 6. A B 7. B A 8. B A 9. A,B 10. I don’t know
COMPILER: error public void test(Employee e) { QUIZ No setBonus method in A e.setBonus(10); Employee B ((Manager)e).setBonus(10); Static / dynamic type } COMPILER: ok Which – if any – errors arise? RUNTIME: ClassCastException … test(new Driver()); … No error Compiler Exception error on runtime 1. A,B 2. A B 3. A B 4. B A 5. A,B 6. A B 7. B A 8. B A 9. A,B 10. I don’t know
QUIZ Employee e = new Manager(); System.out.println( (e instanceof Comparable) + Type inquiry (e instanceof Manager) + e.getClass().getName() ); What is written ? 1. true true Employee 2. true true Manager 3. true false Employee 4. true false Manager 5. false true Employee 6. false true Manager 7. false false Employee 8. false false Manager 9. I don’t know
QUIZ Employee e = new Manager(); System.out.println( (e instanceof Comparable) + Type inquiry (e instanceof Manager) + e.getClass().getName() ); What is written ? 1. true true Employee 2. true true Manager 3. true false Employee 4. true false Manager 5. false true Employee 6. false true Manager 7. false false Employee 8. false false Manager 9. I don’t know
QUIZ int n1 = 5/2; double n2 = 5/2; Primitive types double n3 = 5/(double)2; System.out.println( n1 +” ”+ n2 +” ”+ n3 ); What is written ? 1. 2 2 2 2. 2.0 2.0 2.0 3. 2.5 2.5 2.5 4. 2 2.5 2.5 5. 2 2.0 2.5 6. 2 2 2.5 7. 2.0 2.0 2.5 8. 2.0 2.5 2.5 9. I don’t know
QUIZ n1 2 int n1 = 5/2; 2.0 n2 double n2 = 5/2; Primitive types double n3 = 5/(double)2; n3 2.5 System.out.println( n1 +” ”+ n2 +” ”+ n3 ); What is written ? 1. 2 2 2 2. 2.0 2.0 2.0 3. 2.5 2.5 2.5 4. 2 2.5 2.5 5. 2 2.0 2.5 6. 2 2 2.5 7. 2.0 2.0 2.5 8. 2.0 2.5 2.5 9. I don’t know
QUIZ Generic type /* @param basket: some kind of container holding elements * @param value: a specific element (value != null) * @return true: if value occurs in basket * false: otherwise */ public <E, F extends Iterable<E>> boolean search(F basket, E value) { for (E elem : basket) if (value.equals(elem)) return true; return false; } Is method correct? 1. No, compiler error 2. No, it compiles, but it is not correct 3. Yes, it compiles and it is correct 4. I don’t know
QUIZ Generic type /* @param basket: some kind of container holding elements * @param value: a specific element (value != null) * @return true: if value occurs in basket * false: otherwise */ public <E, F extends Iterable<E>> boolean search(F basket, E value) { for (E elem : basket) if (value.equals(elem)) return true; return false; } Is method correct? 1. No, compiler error 2. No, it compiles, but it is not correct 3. Yes, it compiles and it is correct 4. I don’t know
QUIZ Generic type/wildcard public static void sort(List<E> a) { ... } How would you expect the Collections.sort(…) method to be defined – what should replace ? 1. <E> 2. <E extends Comparable<E>> 3. <E super Comparable<E>> 4. <E extends Comparable<? extends E>> 5. <E extends Comparable<? super E>> 6. <E super Comparable<? extends E>> 7. <E super Comparable<? super E>> 8. I don’t know
QUIZ Generic type/wildcard public static void sort(List<E> a) { ... } How would you expect the Collections.sort(…) method to be defined – what should replace ? 1. <E> 2. <E extends Comparable<E>> 3. <E super Comparable<E>> 4. <E extends Comparable<? extends E>> 5. <E extends Comparable<? super E>> 6. <E super Comparable<? extends E>> 7. <E super Comparable<? super E>> 8. I don’t know
Recommend
More recommend