You are on page 1of 5

BITP 3113 Exercise

Find the expected output from these code segments:

1/5
BITP 3113 Exercise

class Base {
public static void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public static void show() {
System.out.println("Derived::show() called");
}
}

class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}

2/5
BITP 3113 Exercise

class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
}

class Parent extends Grandparent {


public void Print() {
System.out.println("Parent's Print()");
}
}

class Child extends Parent {


public void Print() {
super.super.Print();
System.out.println("Child's Print()");
}
}

public class Main {


public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}

class Base {
public void foo() { System.out.println("Base"); }
}

class Derived extends Base {


private void foo() { System.out.println("Derived"); }
}

public class Main {


public static void main(String args[]) {
Base b = new Derived();
b.foo();
}
}

3/5
BITP 3113 Exercise

class Base {
public void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public void show() {
System.out.println("Derived::show() called");
}
}

public class Main {


public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}

class Base {
final public void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public void show() {
System.out.println("Derived::show() called");
}
}

class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}

4/5
BITP 3113 Exercise

(this is a modified UML class diagram from our Lab 2 session)

Shirt
- shirtCode : String
- description : String
- size : char S=small M=medium L=large
- color : char B=blue R=red G=green Y=yellow
- unitPrice : double
- quantityInStock : int
- replenishStock : boolean
+ Shirt(shirtCode:String, quantityInStock:int, unitPrice:double)
+ calculateTotalStockValue():double
+ displayDetails():void

1. Create the following two Shirt objects:


“Kenzo”,100,55.00
“Paco Rabane”,80,70.00
2. Add size and color to both objects (use setter methods)
3. Calculate the current total stock value for both T shirts.
4. Modify the quantity in stock for Kenzo T-shirt to become 25.
5. Calculate the current total stock value for both T shirts.

Indicate TRUE or FALSE


1 Instance variable names may only contain letters and digits.
2 A class always has a constructor.
3 int is the name of a class available in the package java.lang.
4 Java bytecode is executed directly by the hardware.
5 A default constructor sets all Boolean fields to false.
6 A class can belong to more than one package.
7 A class cannot be extended from more than a single super class.
8 A class may not have more than three constructors.
9 The return type of a constructor is void.
10 The initialization, condition and increment part of a for loop are optional.

5/5

You might also like