PIC20A Midterm 2004 Winter
Instructions
- The exam consists of 10 pages(including this page). You are responsible
for checking the number of pages
-
Please read and answer all the questions carefully.
The point value for each question is indicated. The total number of points available is 50.
If you have trouble with a question, leave it and try another.
- Unless otherwise stated, you may assume when code is given that the code compiles and
is correct. When you are asked to write code, you are expected to write code in good style and
with proper indentation.
- Good luck!
| Last Name:_________________ |
First Name:________________ |
| Student ID:_________________ |
|
Question
|
Q1(14pts)
|
Q2(10pts)
|
Q3(5pts)
|
Q4(11pts)
|
Q5(10pts)
|
Total
|
Scores
|
|
|
|
|
|
|
Q1)(14pts)
a)
What is the output of java WhileTest?
public class WhileTest {
public static void main(String[] args) {
int x=2, y=5;
while( x*y < 20) {
System.out.println(y/x);
x++;
}
}
}
Ans:
b)
What is the output of java ForTest?
public class ForTest {
public static void main(String[] args) {
for(int i=0, j=5; j>=3; i+=2,j--) {
for(int k=0; k<j; k++) {
System.out.print(i);
}
System.out.println();
}
}
}
Ans:
c)
What is the output of java StringTest?
public class StringTest {
public static void main(String[] args) {
String s="string string";
System.out.println("1: " + s.lastIndexOf('i'));
System.out.println("2: " + s.charAt(3));
System.out.println("3: " + s.indexOf("ing"));
System.out.println("4: " + s.indexOf("IN", 4));
}
}
Ans:
d)
What is the output of java StaticTest?
public class StaticTest {
public static int a=1;
public static int b=2;
public int c;
public String s;
public StaticTest(int c, String s) {
this.c = c;
this.s = s;
a++;
b+=c;
}
public static void main(String[] args) {
StaticTest t1 = new StaticTest(5, "ab");
StaticTest t2 = new StaticTest(1, "hi");
t1.a+=3;
t2.b=t2.b+t2.c;
System.out.println("1: " + StaticTest.a);
System.out.println("2: " + StaticTest.b);
StaticTest t3 = new StaticTest(2, "pic");
System.out.println("3: " + t1.a);
System.out.println("4: " + t1.b);
System.out.println("5: " + t1.c);
System.out.println("6: " + t1.s);
}
}
Ans:
e)
What is the output of java SubTest?
Super.java
public class Super {
public int x, y;
public Super(int x, int y) {
this.x = x;
this.y = x + y;
}
Super(int x) {
this(x,x);
}
Super(int x, int y, int z) {
this(x + 10, y+z);
}
public String toString() {
return x + ", " + y;
}
}
Sub.java
public class Sub extends Super {
public int a, b, c;
public Sub(int a, int b, int c) {
super(a,b,c);
this.a = a;
this.b = b;
this.c = c;
}
public Sub(int a, int b) {
this(a,b,0);
}
public Sub(int a) {
super(a);
this.a = a;
b = a;
c = a;
}
public String toString() {
return super.toString() + ", " + a + ", " + b + ", " + c;
}
}
SubTest.java
public class SubTest {
public static void main(String[] args) {
Sub s1 = new Sub(2);
System.out.print("1: ");
System.out.println(s1);
Sub s2 = new Sub(0,1);
System.out.print("2: ");
System.out.println(s2);
Sub s3 = new Sub(1,1,1);
System.out.print("3: ");
System.out.println(s3);
}
}
Ans:
f)
What is the output of java Test?
public class Test {
public double x;
public Test(double x) {
this.x = x;
}
public void multiply(int a) {
x*=a;
}
public void increase(Test t) {
t.x = t.x + x;
}
public void newTest(Test t) {
t = new Test(3.14);
}
public static void main(String[] args) {
Test t1 = new Test(2.2);
Test t2 = new Test(1.1);
t1.multiply(2);
t1.increase(t2);
System.out.print("1: ");
System.out.println(t1.x);
System.out.print("2: ");
System.out.println(t2.x);
t1.newTest(t2);
System.out.print("3: ");
System.out.println(t1.x);
System.out.print("4: ");
System.out.println(t2.x);
}
}
ans:
Q2) (10 pts)
Find out the compiling errors of the following programs. Circle the errors and fix them. Write your corrections
next to the errors you circle. There may be more than one errors in each program.
a)
public class Error1 {
public void method(int a) {
a = 3;
return;
}
public int method() {
System.out.println("Hi");
return;
}
public int method2() {
return 3.5/2;
}
public double method2(int a) {
return a++;
}
}
b)
public class Error2 {
public static void main(String[] args) {
int a=1;
if(a=1) {
System.out.println("a is 1");
} else {
System.out.println("a is not 1");
}
float f = 2.3;
if(f > 2.1) {
System.out.println("f is greater than 2.1");
}
}
}
c)
ClassA.java
public abstract class ClassA {
public abstract int method();
public int method(int a) {
return a + method();
}
}
ClassB.java
public class ClassB extends ClassA {
public int method(int a) {
return 2*a;
}
public int method(int a, int b) {
return a + b;
}
}
Q3(5pts)
Read the following program and answer part (a) and part (b)
ClassA.java
package test;
public class Class1 {
private int privateMethod() {
return 1;
}
protected int protectedMethod() {
return 2;
}
public int publicMethod() {
return 3;
}
}
a) For the following program:
ClassB.java
package test;
public class Class2 extends Class1 {
public void method() {
Class1 x = new Class1();
????????????;
}
}
Which of the following statements can be ???????????? ? Circle the correct answer(s).
privateMethod()
x.privateMethod()
publicMethod()
x.publicMethod()
protectedMethod()
x.protectedMethod()
b) For the following program:
Class3.java
import test.*;
public class Class3 {
public static void main(String[] args) {
Class2 y = new Class2();
???????????;
}
}
Which of the following statements can be ???????????? ? Circle the correct answer(s).
y.method()
y.privateMethod()
y.protectedMethod()
y.publicMethod()
Q4) (11pts)
Write a method int[] cut(int[] array, int max). The method returns a new array with all the numbers strictly bigger than max removed
from the original array. e.g. if array = {1,3,5,6,1,2,8}, max = 5, then the cut(array, 5) returns {1,3,5,1,2}
(6, 8 are strictly bigger than 5 and are removed). The method returns an array of length 0 if all the numbers are strictly bigger than max.
Fill in your codes
public class IntArray {
public static int[] cut(int[] array, int max) {
// your codes
}
}
Q5) (10 pts)
Write a method count(String s) to count the number of occurences of "ABC" in string s.
example, when s = "###ABC%%%ABC*******ABC%%%", then count(s) returns 3.
When s="123ABCabcABCdef", then count(s) returns 2.
Fill in the codes:
public class CountABC {
public static int count(String s) {
// your codes
}
}