| Term Exam 1 | : Monday February 14th, 2000 |
| Instructor | : Osmar Zaïane |
| Section | : B1 |
| Version | : A |
After I write my program with an editor, I use a(n) compiler before I execute my program. That particular tool points out syntax or compile-time errors in my program.
My Java application runs inside a(n) Java virtual
machine or interpreter.
When my Java program runs inside a web browser, it is called a(n) applet.
Objects in the same class have something in common: a set of messages called a protocol.
Thestate of an object is what makes it different from other objects in the same class.
The import statement is used to access classes in a package.
To see the top element of a stack without creating a side effect, I should send to the stack the message peek().
When a Java application is launched, the main method is invoked. When a Java applet is launched, the web browser creates an applet object and sends the init() message to the object.
The following are possible answers: (Applet, Compiler, Compile-time, Import, Init, Interpreter, Java Virtual Machine, Main, Messages, Paint, Parameter, Peek, Pop, Program, Protocol, Push, Run, State, Syntax)
Which part of the previous phrase is a reference to "HELLO"?
"Hello".toUpperCase()
|
import java.util.*; public class myTest { /* Reads 3 numbers and displays their sum and average */ public static void main (String args[]) { /* program statements go here */ Integer firstNum, secondNum, thirdNum; int sum; System.out.println(new Date()); firstNum = Keyboard.in.readInteger(); secondNum = Keyboard.in.readInteger(); thirdNum = Keyboard.in.readInteger(); sum = firstNum.intValue() + secondNum.intValue() + thirdNum.intValue(); System.out.print("Sum of the 3 numbers = "); System.out.println(sum); System.out.print("Average of the 3 numbers = "); System.out.println(sum/3f); } } |
Another possible solution could be:
|
import java.util.*; public class myTest { /* Reads 3 numbers and displays their sum and average */ public static void main (String args[]) { /* program statements go here */ int firstNum, secondNum, thirdNum, sum; System.out.println(new Date()); firstNum = Keyboard.in.readInteger().intValue(); secondNum = Keyboard.in.readInteger().intValue(); thirdNum = Keyboard.in.readInteger().intValue(); sum = firstNum + secondNum + thirdNum; System.out.print("Sum of the 3 numbers = "); System.out.println(sum); System.out.print("Average of the 3 numbers = "); System.out.println(sum/3f); } } |
|
public int sumThree(Integer number1, Integer number2, Integer number3) { return(number1.intValue() + number2.intValue() + number3.intValue()); } |
|
public static void main (String args[]) { /* program statements go here */ myTest test; test = new myTest(); Integer firstNum, secondNum, thirdNum; int sum; System.out.println(new Date()); firstNum = Keyboard.in.readInteger(); secondNum = Keyboard.in.readInteger(); thirdNum = Keyboard.in.readInteger(); sum = test.sumThree(firstNum, secondNum, thirdNum); System.out.print("Sum of the 3 numbers = "); System.out.println(sum); System.out.print("Average of the 3 numbers = "); System.out.println(sum/3f); } |
public class myQuestion {
/* instance variables */
private int r;
private int n;
private Stack s;
public myQuestion(int x, int y) {
this.r = x;
this.n = y;
this.s = new Stack();
}
public void a1 (String ms, int mn) {
this.r = this.r + mn;
this.n = this.n + 1;
this.s.push(ms);
}
public double disc() {
double value;
value = this.r * 0.25;
return value;
}
public display() {
String t;
System.out.println(this.r);
System.out.println(this.n);
t = this.s.pop();
System.out.println(t);
t = this.s.pop();
System.out.println(t);
t = this.s.pop();
System.out.println(t);
}
}
|
a ) [12 points] What is the output of the program segment:
myQuestion q;
q=new myQuestion(0,0);
q.a1("potato", 9);
q.a1("avocado", 4);
q.a1("tomato",7);
q.display();
System.out.println(q.disc());
20
3
tomato
avocado
potato
5
b) [8 points] What is the output of the program segment:
myQuestion q;
q=new myQuestion(20,2);
q.a1("bread", 5);
q.a1("milk",7);
q.a1("pop",8);
q.display();
System.out.println(q.disc());
40
5
pop
milk
bread
10