the solutions

Download Report

Transcript the solutions

Previous Exam
1.0
Question 1 - a
Is the following statement true or false?
Briefly explain your answer.
A && B is the same as B && A for any
Boolean expressions A and B.
False. If either expression has a side effect,
you cannot swap them.
Question 1 - b
Write a program that reads in a series of
floating-point numbers and prints out the
minimum, maximum and average values.
Assume use of the ConsoleReader class.
Question 1 – b
public class MinMaxAve
{ public static void main(String[] args)
{ ConsoleReader console = new
ConsoleReader(System.in);
double low = 0;
double high = 0;
double sum = 0;
int count = 0;
boolean done = false;
Question 1 – b
String input = console.readLine();
if (input == null)
done = true;
else
{ low = Double.parseDouble(input);
high = low;
sum = sum + low;
count++;
}
Question 1 - b
while (!done)
{ input = console.readLine();
if (input == null)
done = true;
else
{ double next = Double.parseDouble(input);
sum = sum + next;
count++;
if (next > high)
high = next;
else if(next < low)
low = next;
}
}
Question 1 - b
System.out.println("The maximum value is: " +
high);
System.out.println("The minimum value is: "
+ low);
if (count > 0)
System.out.println("The average value
is: " + sum / count);
}
}
Question 1 - c
Write a method that computes the
alternating sum of all elements in an integer
array. For example, if the method is
invoked with the array containing :
314259263
then it returns
3 + 1 – 4 + 2 – 5 + 9 – 2 + 6 – 3 = 7.
Question 1 - c
public int altSum(int[] a)
{ int r = 0;
for (int i = 0; i < a.length ; i++ )
{ if (i % 2 == 0)
r = r + a[i];
else
r = r - a[i];
}
return r;
}
}
Question 2 - a
Write a simple Bank Account class that
rejects negative amounts in the deposit and
withdraw methods and rejects withdrawals
that would result in an overdraft.
Question 2 - a
public class BankAccount
{ private double balance;
public BankAccount()
{ balance = 0;
}
public BankAccount(double initialBalance)
{ if (initialBalance >= 0)
balance = initialBalance;
}
Question 2 - a
public void deposit(double amount)
{ if (amount >= 0)
balance = balance + amount;
}
public void withdraw(double amount)
{ if (amount <= balance)
balance = balance - amount;
}
public double getBalance()
{ return balance;
}
}
Question 2 - b
 What is wrong with the following loop?
Explain 2 ways of fixing the error.
int[] v = new int [10];
for (int i = 1; i <= 10; i++)
v[i] = i*i;
Question 2 - b
 There is a bounds error. The index of the for loop
begins at 1 (thereby skipping the first element of
the array) and ends with 10 (which is one past the
end of the array and a fatal error).
 One remedy is to adjust the bounds for i:
for (int i = 0; i < 10; i++)
v[i] = (i + 1) * (i + 1);
 Another remedy is to adjust the array index:
for (int i = 1; i <= 10; i++)
v[i - 1] = i * i;
Question 2 - c
 For each of the following, say if it is true or false.
–
–
–
–
–
Array subscripts must be integers
Arrays cannot contain Strings as elements
Arrays cannot use Strings as subscripts
Parallel arrays must have equal length
Two-dimensional arrays always have the same number
of rows and columns
– Two parallel arrays can be replaced by one twodimensional array
– Elements of different columns in a two-dimensional
array can have different types
Question 2 - c
 true (or an expression which evaluates to an
integer)
 false, for example main(String[] args)
 true
 true (in order to be parallel)
 false
 false (the array elements might be of different
types)
 false
Question 3 - a
Explain the difference between class and
instance methods/members. How can this
be expressed in your code?
Question 3 - a
instance method need object reference
– reference.method(parameters)
class methods belong to class, no need for
object
– ClassName.method(parameters)
class methods via static
Question 3 - b
Can class methods/members be used in
instance methods? Explain why (not).
Yes, since each object has access to the
class from which it is instantiated.
Question 3 - c
Can class instance methods/members be
used in class methods? Explain why (not).
No, a class does not have access to any of
its objects.
Question 3 - d
In software engineering, solutions to
common design problems are often called
design patterns. One of them is called
Singleton and it describes how one can
make sure that a certain class can have only
one instance during a program's life-cycle.
How would you implement such a
Singleton class in Java?
Question 3 - d
public class Singleton
{
private static Singleton instance;
protected Singleton()
{
…
}
public static Singleton giveInstance()
{
if(instance == null)
instance = new Singleton();
return instance;
}
}
Question 3 - e
How would you check that you can indeed
have only one instance?
 reference1 == reference2)
Question 4 - a
 public static final double PI = 3.14;
accessible from everywhere
belonging to class level
constant
type double
Question 4 - a
 protected final void printString(char a)
accessible from subclasses
method cannot be overridden
no return value
takes char as an argument
Question 4 - a
private BankAccount()
constructor for BackAccount class
default constructor
only to be used inside this class
Question 4 - b
Describe the mechanism of error/exception
handling in java.
exceptions – errors
try – catch – finally
throw
Question 4 - c
Why would one use error handling in a
constructor? What will happen with the
(partial) object?
to state that something went wrong during
object creation
object will be garbage collected
Question 4 - d
Explain the differences between call-byreference and call-by-value parameter
passing?
call-by-reference: pointer to memory is
passed
call-by-value: actual contents of memory is
given - value
Question 4 - d
Which systems are used in Java?
objects by reference
primitive types by value
Question 5
1. import java.util.*;
2.
3. public class Run
4. {
5.
6.
Vector elements;
7.
8.
public Run(int in)
9.
{
10.
int t = a;
11.
elements = new Vector();
12.
for(int t = 0; t <in; t++)
13.
{
14.
if(t%2==0)
15.
elements.addElement(new ElementA("a"));
16.
else
17.
elements.addElement(new ElementB(t));
18.
19.
}
20.
}
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
public String toString()
{
String res = ";
int s = elements.size();
for(int i=0; i<s; i++)
{
res += elements.elementAt(i);
res += "\n";
}
return res;
}
public Vector giveElements()
{
return elements;
}
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.}
public static void main(String[] argv)
{
Element a = new Element();
Run r = new Run(4);
System.out.println(r);
ElementA b = new Element(5);
Vector el = r.giveElements();
System.out.println((ElementA) el.elementAt(1));
}
1.// abstract class Element
2.
3.public abstract class Element
4.{
5.
6.
7.
public Element()
8.
{
9.
System.out.println("Element created");
10.
}
11.
12.
protected abstract String printContents();
13.
14.
15.
protected String printType()
16.
{
17.
return "Element "
18.
}
19.
20.
public String toString()
21.
{
22.
printType();
23.
}
24.
25.}
1.// public class ElementA
2.
3.public class ElementA extend Element
4.{
5.
String el;
6.
7.
public ElementA()
8.
{
9.
System.out.println("ElementA created");
10.
el = "nothing";
11.
}
12.
13.
public ElementA(String input)
14.
{
15.
System.out.println("ElementA created with input");
16.
el = input;
17.
}
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.}
/* protected constructor */
protected ElementA(a)
{
System.out.println("ElementA created with int");
el = Integer.toString(a);
}
protected String printContents()
{
return el;
}
protected String printType()
{
return "ElementA inherits from "
}
super.printType();
public String toString()
{
return printType() +
"and contains " + printContents();
1./ public class ElementB
2.
3.
4.
5.public class ElementB extends ElementA
6.{
7.
8.
Vector in;
9.
10.
public ElementB()
11.
{
12.
System.out.println("ElementB created");
13.
in = new Vector();
14.
}
15.
16.
17.
public ElementB(String el)
18.
{
19
System.out.println("ElementB created with input");
20.
super(el);
21.
22.
}
23.
24.
public ElementB(int a)
25.
{
26.
super(a);
27.
System.out.print("ElementB created with int");
28.
in = new Vector();
29.
final int el = 2;
30.
System.out.println("and el equals to " +
this.el);
31.
el++;
32.
for(int i=0;i<a; i++)
33.
{
34.
in.addElement(new Integer(i + el));
35.
}
36.
}
37.
38.
protected String printContents()
39.
{
40.
return el + " " + in.toString();
41.
}
42.
43.
protected String printType()
44.
{
45.
return "ElementB inherits from " +
super.printType();
46.
}
47.
48.
public String toString()
49.
{
50.
return printType() +
51.
"and contains " + printContents();
52.
}
53.
Question 5 - b
 Predict the output of this program.
Element created
ElementA created with input
Element created
ElementA created with int
ElementB created with intand el equals to 1
Element created
ElementA created with input
Element created
ElementA created with int
ElementB created with intand el equals to 3
ElementA inherits from Element and contains a
ElementB inherits from ElementA inherits from Element and
contains 1 [2]
ElementA inherits from Element and contains a
ElementB inherits from ElementA inherits from Element and
contains 3 [2, 3, 4]