énoncé

Transcription

énoncé
Université Paris Diderot
Programmation Orientée Ob jet
L2, EIDD et M1 linguistiques
Année 2015-2016
◦
TD n
12
Révisions
1
Exceptions
Exercice 1
1
2
3
4
5
6
7
8
9
10
11
c l a s s E extends E x c e p t i o n {}
public c l a s s Test {
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
try { throw new E ( ) ;
} catch ( E x c e p t i o n e ) {
}
}
Exercice 2
1
2
3
4
5
6
7
8
9
10
11
System . out . p r i n t l n ( " a " ) ;
} catch (E e ) {
System . out . p r i n t l n ( "b" ) ;
}
Le code suivant est-il correct ? Si oui, qu'ache-t-il ?
c l a s s E extends E x c e p t i o n {}
public c l a s s Test {
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
try { throw new E ( ) ;
} catch (E e ) {
}
}
Exercice 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Le code suivant est-il correct ? Si oui, qu'ache-t-il ?
System . out . p r i n t l n ( " a " ) ;
} catch ( E x c e p t i o n e ) {
System . out . p r i n t l n ( "b" ) ;
}
Qu'ache le code suivant ?
c l a s s E extends E x c e p t i o n {}
public c l a s s Test {
public s t a t i c void f ( ) throws E x c e p t i o n {
try { throw new E ( ) ;
} catch ( E x c e p t i o n e ) {
}
System . out . p r i n t l n ( " a " ) ;
} finally {
System . out . p r i n t l n ( "b" ) ;
}
public s t a t i c void g ( ) throws E x c e p t i o n {
try { throw new E x c e p t i o n ( ) ;
} catch (E e ) {
System . out . p r i n t l n ( " c " ) ;
} finally {
System . out . p r i n t l n ( "d" ) ;
1
17
18
19
20
21
22
23
24
25
26
27
28
29
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
try { f ( ) ;
} catch ( E x c e p t i o n e ) {
}
System . out . p r i n t l n ( " e " ) ;
try { g ( ) ;
} catch ( E x c e p t i o n e ) {
System . out . p r i n t l n ( " f " ) ;
}
}
}
2
Héritage
Exercice 4
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
}
}
Qu'ache le programme suivant ?
class A {
public s t a t i c S t r i n g f ( ) {
return "A" ;
}
}
c l a s s B extends A {
public s t a t i c S t r i n g f ( ) {
return "B" ;
}
}
public c l a s s Exo4 {
public s t a t i c void myf ( Object o ) {
}
System . out . p r i n t l n ( " Object " ) ;
public s t a t i c void myf (A a ) {
}
System . out . p r i n t l n ( "A" ) ;
System . out . p r i n t l n ( a . f ( ) ) ;
public s t a t i c void myf (B b ) {
}
System . out . p r i n t l n ( "B" ) ;
System . out . p r i n t l n ( b . f ( ) ) ;
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
Object o = new A( ) ;
Object o2 = new B( ) ;
A a = new A( ) ;
A a2 = new B( ) ;
}
}
myf ( o ) ;
myf ( o2 ) ;
myf ( a ) ;
myf ( a2 ) ;
Et si les fonctions f() dans les classes A et B ne sont pas static ?
Exercice 5 Le programme suivant compile-t-il ?
2
1
2
3
4
c l a s s C { public void g ( ) {} }
c l a s s D extends C {
public s t a t i c void f ( ) { super . g ( ) ; }
}
3
Classes abstraites, interfaces
Exercice 6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
interface I {
int i = 1 ;
}
interface J extends I {}
public c l a s s A implements J{
s t a t i c void f ( ) {
}
}
}
f () ;
System . out . p r i n t l n ( i ) ;
Le code suivant est-il correct ? Si oui, qu'ache-t-il ?
interface I {
void f ( ) ;
}
abstract c l a s s A implements I {}
c l a s s B extends A {
public void f ( ) {
}
System . out . p r i n t l n ( " a " ) ;
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
A a = new B( ) ;
}
}
Exercice 8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
i = 2;
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
Exercice 7
1
2
3
4
5
6
7
8
9
10
11
12
13
Le code suivant est-il correct ? Si oui, qu'ache-t-il ?
a . f () ;
Le code suivant est-il correct ? Si oui, qu'ache-t-il ?
interface I {
public void f ( ) ;
}
abstract c l a s s A implements I {
abstract void g ( ) ;
}
c l a s s B extends A{
s t a t i c void g ( ) {
}
System . out . p r i n t l n ( " a " ) ;
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
}
}
g () ;
3
4
Classes internes
Exercice 9
1
2
3
4
5
6
7
Le code suivant est-il correct ?
class A {
class B {
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
B b = new B( ) ;
}
}
Exercice 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
}
System . out . p r i n t l n ( " a " ) ;
class B {
void g ( ) {
}
}
f () ;
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
B b = ( new A( ) ) . new B( ) ;
}
}
b . g () ;
Le code suivant est-il correct ?
public c l a s s A {
void f ( ) {
}
System . out . p r i n t l n ( " a " ) ;
static class B {
void g ( ) {
}
}
Exercice 12
1
2
3
4
5
6
7
8
9
10
11
12
13
Le code suivant est-il correct ? Si oui, qu'ache-t-il ?
public c l a s s A {
void f ( ) {
Exercice 11
1
2
3
4
5
6
7
8
9
10
}
}
f () ;
Le code suivant est-il correct ? Si oui, qu'ache-t-il ?
public c l a s s C {
s t a t i c int n = 0 ;
static class D {
void g ( ) {
}
}
n++;
System . out . p r i n t l n ( n ) ;
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
D d = new D( ) ;
}
}
d . g () ;
4

Documents pareils