Lebanese University
Faculty of Sciences – section 5
I2211 – Object Oriented Programming
Output exercises
Question 1.
What is the exact output of the following program?
public abstract class A { public class B extends A implements
private int a; MyInterface{
public static int count; private int b;
public A(){ public B(){
count++; System.out.println("B");
System.out.println("A"); b += 2;
} }
@Override public B(int b){ this.b = b; }
public String toString(){
return "a: " + a; public void methodX(int k){
} b *= 7;
} }
public class C extends B { @Override
public C(){ public String toString(){
super(4); return "b: " + b;
System.out.println("C"); }
} @Override
public void methodX(double k){ public void test(){ b -= 1; }
System.out.println("count:" + count); }
}
}
public interface MyInterface { public class OutputQuestion {
void test(); public static void main(String[] args) {
} B b1 = new B(6);
C c = new C();
A a = new B();
c.methodX(10);
c.methodX(10.0);
b1.test();
c.test();
((MyInterface)a).test();
System.out.println(a);
System.out.println(b1);
System.out.println(c);
}
}
Page 1/2
Lebanese University
Faculty of Sciences – section 5
Question 2.
Given the following java project composed of several java classes and one interface. The statements of the
main method are numbered from 1 to 12. What is the exact output of each statement in the main? Write
the statement number followed by its output.
public interface Playable { public class Xbox2 extends Xbox {
public String howToPlay();
} }
public class VideoGame { public class Nintendo extends VideoGame {
public String toString() { public String howToPlay() {
return "A game"; return "mobile";
} }
public String howToPlay() { }
return "Press on buttons";
}
}
public class Xbox extends VideoGame implements Playable {
public Xbox(){System.out.println("xbox created"); }
public String howToPlay() {return "In front of TV";}
}
public class Test {
public static void main(String[] args) {
1. Nintendo n1 = new Nintendo();
2. System.out.println(n1);
3. System.out.println(n1.howToPlay());
4. VideoGame v2 = new Xbox();
5. System.out.println(v2.howToPlay());
6. Xbox2 v3 =new Xbox2();
7. System.out.println(v3.howToPlay());
8. Object[] A = new Object[3];
9. A[0]=n1;
10. A[1]=new Xbox();
11. A[2]=v3;
for (int i = 0; i < A.length; i++) {
if (A[i] instanceof Xbox) {
12. System.out.println(((Xbox) A[i]).howToPlay());
}
}
}
}
Page 2/2