Problem set 3

Assigned: Feb. 17.
Due: Mar. 3
.

Problem 1

Consider the following pseudo-code.
int I;                      // Global variable

void foo(int X)
{ X = X+I;
  X = X+I;
}
  
main() {
  I=10;
  foo(I)
  print(I);
}

What does this program print, if parameters are passed (A) pass by value; (B) pass by reference; (C) pass by copy-restore?

Problem 2

Consider the following toy Java program in hwk3.java
// File hwk3.java

class A {  
  public int Data;
  public int f(int I) { return(I+1); } }


class B extends A {
   public double f(double I) { return(I*2.0); } }

class C extends B {
    public double Data;
    public int f(int I) { return(I+10); }
    public String f(String s) { return("Echo " + s); } }

public class hwk3 {
public static void main(String args[]) {
  A W = new A();
  W.Data = 2;
  C X = new C();
  X.Data = 5;
  B Y = X;
  System.out.println(W.f(W.Data));
  System.out.println(Y.f(W.Data));
  System.out.println(Y.f(X.Data));
  System.out.println(X.f("hello"));
//  System.out.println(Y.f("hello"));
} }