Java Type System and Object Model QUIZ QUIZ Subtype relation 1. - - PDF document

java type system and object model
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Java Type System and Object Model

QUIZ

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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 ?

slide-13
SLIDE 13

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 ?

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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