Java Type System and Object Model QUIZ QUIZ Subtype relation 1. - - PDF document
Java Type System and Object Model QUIZ QUIZ Subtype relation 1. - - PDF document
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
QUIZ
- 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?
Subtype relation
QUIZ
- 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?
Subtype relation
QUIZ
- 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?
Subtype relation
QUIZ
- 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?
Subtype relation
QUIZ
- 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?
Subtype relation
QUIZ
- 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?
Subtype relation
public void test(Employee e) { e.setBonus(10); ((Manager)e).setBonus(10); } … test(new Driver()); …
QUIZ
No error Compiler error Exception
- n 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 Which – if any – errors arise? Static / dynamic type A B
QUIZ
No error Compiler error Exception
- n 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: ok RUNTIME: ClassCastException COMPILER: error No setBonus method in Employee
Which – if any – errors arise? Static / dynamic type public void test(Employee e) { e.setBonus(10); ((Manager)e).setBonus(10); } … test(new Driver()); … A B
QUIZ
Employee e = new Manager(); System.out.println( (e instanceof Comparable) + (e instanceof Manager) + e.getClass().getName() ); 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 What is written ? Type inquiry
QUIZ
Employee e = new Manager(); System.out.println( (e instanceof Comparable) + (e instanceof Manager) + e.getClass().getName() ); 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 What is written ? Type inquiry
QUIZ
int n1 = 5/2; double n2 = 5/2; double n3 = 5/(double)2; System.out.println( n1 +” ”+ n2 +” ”+ n3 );
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 Primitive types What is written ?
QUIZ
int n1 = 5/2; double n2 = 5/2; double n3 = 5/(double)2; System.out.println( n1 +” ”+ n2 +” ”+ n3 );
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 2 n1 2.0 n2 2.5 n3 Primitive types What is written ?
QUIZ
/* @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 Generic type
QUIZ
/* @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 Generic type
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
public static void sort(List<E> a) { ... } Generic type/wildcard
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
public static void sort(List<E> a) { ... } Generic type/wildcard