Вы находитесь на странице: 1из 5

UNIVERSITY OF WARWICK

First Year Examinations: April 2008

Programming for Computer Scientists

Time allowed: 2 hours.


Attempt ALL the questions.
All questions carry 25 marks each.
Total marks 125

Most answers require a single statement or a short piece of Java code to be written. Do not
give complete programs or supply irrelevant declarations or input–output statements unless
explicitly asked for. Number questions clearly but do NOT start each answer on a new page.

Read the instructions on the answer book carefully and make sure that the particulars required
are entered on each answer book.

1. Primitive types

(a) Java has six primitive numeric data types. Why is this the case, and why does Java
not make do with a single primitive numeric data type? [4]

(b) Give suitable primitive data types for representing the following real-world
values:
(i) Days of the week;
(ii) The state of a light switch;
(iii) A bank account balance;
(iv) The Unicode character ‘\u0041’;
(v) PI with 6 significant digits of accuracy. [5]

(c) Numeric type conversion and type casting provide the programmer with extra
flexibility in terms of moving data from one data type to another.

(i) Give examples of both in which there is no loss of numeric


accuracy. [4]
(ii) Provide an example of type casting in which one of the original
values is truncated in some way. [1]
(iii) How can you check that there will be no loss of precision when
performing a type cast? Under what circumstances will the Java
compiler issue a loss of precision error, and how may such errors be
prevented? [3]

(d) Write a program that reads a positive integer between 0 and 1000 inclusive, and
prints the sum of the digits in that integer. For example, if the integer input is 932,
the program should return the number 14. [8]

-1- Continued
2. Control statements

(a) Using nested for loops write a program that produces the following output: [2]

0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4

(b) Modify your program so that it produced the following output:

0
0 1
0 1 2
0 1 2 3

Your program should continue to use nested for loops. [2]

(c) Modify your program for the final time so that it produces the output:

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

Your program should continue to use nested for loops; you may also find it
useful to develop a subsidiary method which, given an integer parameter x, prints
x spaces. [6]

(d) Using the following code as an example, describe the semantics of Java’s
switch statement: [5]

1. int i = 1;
2. switch (i)
3. {
4. case 0 : System.out.print(“ zero ”);
5. case 1 : System.out.print(“ one ”);
6. case 2 : System.out.print(“ two ”);
7. case 3 : System.out.print(“ three ”);
8. default : System.out.print(“ done ”);

-2- Continued
9. }

(e) Explain how the behaviour of the code in (d) can be modified if the break
statement is added to the code: (i) at the end of line 5; (ii) at the end of line 7; (iii)
at the end of line 8. [3]

(f) What difference does it make to the code in (d) if the default statement is
moved to directly below line 3? [2]

(g) Loop constructs in any programming language are subject to non-termination.


Give an example of a loop which does not terminate. Then describe a way in
which you, as a programmer, can ensure that you avoid infinite loops. Hint: think
about how you can formally reason about programs with loops. [5]

3. Arrays

(a) Given two equal sized arrays, a and b, write a method called equalContent that
prints true when the contents of a are also found in b, and prints false otherwise.
E.g.

EqualContent([1,2,3,4], [4,1,3,2]) returns true, whereas


EqualContent([1,2,3,4], [4,5,3,2]) returns false. [7]

(b) In mathematics, the Sieve of Eratosthenes is a simple, ancient algorithm for


finding all prime numbers (those that divide only by themselves and 1) up to a
specified integer. The algorithm works as follows:

(i) First write down a list of numbers from 2 to the specific integer
you have in mind. Call this list A. E.g. A = [2, 3, 4, 5, 6, …, 998,
999, 1000].

(ii) Next, write the number 2 (the first prime number) in another list for
primes found; call this list B. E.g. B = [2].

(iii) Then mark off 2, and all multiples of 2, from list A. E.g. A = [2, 3,
4, 5, 6, …, 998, 999, 1000].

(iv) The first remaining number in the list is prime; add this to list B.
E.g. B = [2, 3].

(v) Mark off this number, and all multiples of this number, from list A.
The crossing-off of multiples can be started at the square of the
number, as lower multiples have already been crossed out in
previous steps.

(vi) Repeat steps (iv) and (v) until no more numbers are left in A. Note
that once you reach a number greater than the square root of the
highest number in list A, all the numbers remaining in list A are

-3- Continued
prime.
Write a Java program that implements the Sieve of Eratosthenes. [12]

(c) Parameter passing with arrays in Java is different from parameter passing
using primitive data types. Explain why this is the case, and give one
example of a typical programming mistake that misunderstandings in this
area might cause. [4]

(d) Describe what we mean when we say that arrays are monomorphic. Is it
possible to express polymorphic properties in a single array? [2]

4. Classes

Computer models of climate systems are very useful for producing climate projections
into the future. Computer climate models are used daily by the Met Office to gain
physical insight into major features of the climate system, and to produce climate
projections for a range of assumptions about emissions of carbon dioxide and other
greenhouse gases.

Typically, these climate models capture large areas of the atmosphere as adjoining 3-D
cubes of data. Each 3-D cube, which we term a sub-region, stores many data elements.
A change in temperature or pressure for a large atmospheric area, is therefore
represented as the application of a mathematical function to each of the sub-regions in
the larger atmospheric area as a whole.

Figure 1: Model representation of a large atmospheric area with


one highlighted sub-region.

(a) Design a simple class which represents an atmospheric sub-region, which stores
four values: day temperature, night temperature, sea-level pressure and carbon
dioxide concentration.

Ensure that your class is well encapsulated and that there is a means for printing

-4- Continued
and updating the data that each instance of this class represents. [10]
(b) Now write a class for the large atmospheric area, such as that seen in Figure 1.
This should represent a 3-D collection of sub-regions of dimension X*Y*Z.

Ensure that the class is well encapsulated and that each sub-region has some
arbitrary initial values. You should also ensure that you have a way of printing
the data for the large atmospheric area as a whole. [11]

(c) Add a new method to your class from part (b) that increases the day-time
temperature in each sub-region by 1o. [4]

5. Modifiers, classes and hierarchies

(a) “When declaring data and methods as public or private, or static and
non-static the data and the methods that access the data must correspond.” Explain
what is meant by this statement, illustrating your answer with some suitable
examples. [6]

(b) What does dynamic binding mean in the context of Java programming. [6]

(c) Explain the terms encapsulation, inheritance and polymorphism. Illustrate each of
your answers with an example. [6]

(d) Describe and illustrate the difference between a Java abstract class and a Java
interface. Give examples of each (being sure to get the syntax as correct as
possible). You should also illustrate how each is used in Java programming. [7]

-5- End
Continued

Вам также может понравиться