Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
1ECE 39595 Java Lab, Spring 2021, Exam 1
The answer sheet is a separate sheet and can be edited, and therefore annotated with your answers.
Not counting the answer sheet, this exam has 7 pages and 40 questions each worth 2.5 points.
You may begin the exam whenever it becomes available. I will give a 10 minute warning at the end of that the
exam answer sheet needs to have already been uploaded to Brightspace.
Answering questions. Most questions are something like \Q28. If the statement causes a compile time or
runtime error, answer “Err”. For the remainder of the program execution, assume the statement that gave an error
is not longer part of the program. If the statement has no output and does not cause an error, answer “Ok”. If the
statement does not cause an error and has output, give the output. If a statement has newlines, you do not need to
include those in your answer.
If you are not in zoom with video turned on you may recieve a 0 on the exam. I will be recording the exam.
Programs are given without import statements for brevity. Assume all needed imports are present.
2This page has questions 1 – 14.
public class Base {
public int k = 1;
public void f1(int i) {
System.out.println("B.f1(int)");
}
public void f2( ) {
System.out.println("B.f2");
}
public void f3( ) {
System.out.println("B.f3");
}
public void f4( ) {
System.out.print("B.f4, ");
f5( );
}
public void f5( ) {
System.out.print("B.f5, ");
f6( );
}
private void f6( ) {
System.out.println("B.f6");
}
}
public class Derived extends Base {
public int k = 20;
public void f1( ) {
System.out.println("D.f1");
}
public void f2( ) {
System.out.println("D.f2");
}
public void f4( ) {
System.out.print("D.f4, ");
f6( );
}
private void f6( ) {
System.out.println("D.f6");
}
}
public class Main {
public static void main(String[ ] args) {
Base b = new Base( );
b.f1(5); // Q1
b.f1( ); // Q2
b.f2( ); // Q3
b.f5( ); // Q4
b.f6( ); // Q5
b = new Derived( );
b.f1(5); // Q6
b.f1( ); // Q7
b.f2( ); // Q8
b.f3( ); // Q9
b.f4( ); // Q10
System.out.println("k: "+b.k); // Q11
Derived d = new Derived( );
d.f1( ); // Q12
d.f3( ); // Q13
d.f5( ); // Q14
}
}
3This page has questions 15 – 19, below. There are three files in this program. Main.java is in the
directory working. The directory pack1 is a subdirectory of working, and files C1.java and C2.java are
members of the pack1 directory. The drawing below illustrates this.
For Q15 and Q16, answer ”Ok” if the lines compile, and “Err” otherwise.
working
pack1 Main.java
C1.java C2.java
// start of C1.java
package pack1;
public class C1 {
public void f1( ) {
System.out.println("C1.f1");
}
void f2( ) {
System.out.println("C1.f2");
}
}
// start of C2.java
package pack1;
public class C2 {
void f3( ) {
C1 c1 = new C1( );
c1.f1( ); // Q15
c1.f2( ); // Q16
}
}
// start of Main.java
import pack1.C1;
public class Main {
public static void main(String[ ] args) {
C1 c1 = new C1( );
C2 c2 = new C2( ); // S3
pack1.C2 c2 = new pack1.C2( ); // S4
c1.f1( ); // Q17
c1.f2( ); // Q18
}
}
Q19. Which of S3 and S4 is legal? Give one or both. Hint: at least one is legal.
4This program has questions 20 – 22.
public class Int {
public int v;
}
public class Main {
private void bar(Int ii, int i) {
ii.v = 100;
i = 100;
}
private void foo(int ii, float ff) {
System.out.println("if");
}
private void foo(int ii, short ss) {
System.out.println("is");
}
private void foo(float ff, double dd) {
System.out.println("fd");
}
private void foo(double dd, float ff) {
System.out.println("df");
}
public static void main(String[ ] args) {
short s = 1;
int i = 2;
float f = 1.0f;
double d = 2.0;
Main m = new Main( );
m.foo(s, s); // Q20
m.foo(f,f); // Q21
Int box = new Int( );
box.v = 0;
m.bar(box, i);
System.out.println(" "+box.v + ", "+i); // Q22
}
}
5This program has questions 23 – 27.
public class B {
public B( ) {
System.out.println("B");
}
}
public class D1 extends B {
public D1( ) {
System.out.println("D1");
}
}
public class D2 extends B {
public D2( ) {
System.out.println("D2");
}
}
public class Main {
B b = new B( );
D1 d1 = new D1( ) // Q23
D2 d2 = new D2( );
b = d1; // Q24
d1 = b; // Q25
b = new D1( );
d2 = (D2) b; // Q26
d1 = new D1( );
d2 = new D2( );
d1 = (D1) d2; // Q27
}
6This page has questions 28 – 35.
public interface I {
int j = 0;
public abstract void f1( );
public abstract void f3( );
}
public abstract class A {
int i = 0;
public abstract void f1( );
public void f2( ) {
System.out.println("A.f2");
}
}
public class D extends A implements I {
public void f1( ) { // S1
System.out.println("D.f1");
}
public void f3( ) {
System.out.println("D.f3");
}
}
public abstract class F extends A { }
public class Main {
public static void main(String[ ] args) {
I i = new I( ); // Q28
A a = new A( ); // Q29
A ad = new D( ); // Q30
I id = new D( ); // Q31
F ff = new F( ); // Q32
System.out.println("I.j: "+I.j); // Q33
D.j = 20; // Q34
}
}
Q35. Does the implementation of f1 at statement S1 provide a concrete implementation for the abstract class f1
declared in:
(a) interface I
(b) abstract class A
(c) both (a) and (b)
(d) neither of (a) or (b)
7This program is used to answer questions 35 – 40.
public class E1 extends Exception {
public int i1;
public E1(int i) {i1 = i;}
public String toString( ) {
return "E1 "+i1;
}
}
public class E2 extends Exception {
public int i2;
public E2(int i) {i2 = i;}
public String toString( ) {
return "E2 "+i2;
}
}
public class E3 {
public int i3;
public E3(int i) {i3 = i;}
public String toString( ) {
return "E3 "+i3;
}
}
public class Main {
public void foo(int i) throws E1, E2 {
if (i == 0) return;
if (i == 1) throw new E1(1);
if (i == 2) throw new E2(2);
}
public static void main(String[ ] args) {
Main m = new Main( );
for (int i = 0; i < 3; i++) { // S1
try {
m.foo(i);
}
catch (E1 e) {System.out.print(e+" ");}
catch (E2 e) {System.out.print(e+" ");}
finally {System.out.println(" "+i);}
}
try { // S2
for (int j = 0; j < 3; j++) {
m.foo(j);
}
}
catch (E1 e) {System.out.print(e+" ");}
catch (E2 e) {System.out.print(e+" ");}
finally {System.out.println("finally");}
try { // S3
m.foo(1);
}
catch (Exception e) {System.out.println(e);}
}
}
Q36: Is E3 a legally defined exception? Answer “yes” if it is, “no” otherwise.
Q37: What is the output from iteration i=0 of the i loop at S1?
Q38: What is the output from iteration i=2 of the i loop at S1?
Q39: What is printed by the execution of the entire try-catch statement at S2?
Q40: What is printed by the execution of the entire try-catch clause at S3?