Program Design Diagnostic Test

28th October 1999

I have split up the questions in the same order as we had them. Questions are in yellow, answers in white where I know them or have notes from Miryam & Ziba. Please check all the answers yourselves as as some are pure guesswork! If anyone has taken notes of the answers, please send me e-mail and I will update the answers here as soon as I can. Thanks for your support. R2

Q1

Fill in the missing parts of the Fibonaci Class below. (Answers in White) [7 Marks]
	class Fibonaci{
		public static void main( String[] args )
    		{
			int i, fib=1, fiblast = 1, fiblastbutone = 1;
			System.out.print("1, 1, ");
			for (i=2; i<13; i++)
  			{
  				fib = fiblast + fiblastbutone;
  				fiblastbutone = fiblast;
  				fiblast = fib;
    				System.out.print(fib + ",  ");
  			}
			System.out.println("\n");
    		}
	}
	
What unusual output would you see if the upper parameter integer in the 'for' loop (ie: 13) is changed to 90 ? [1 Mark]
The series would suddenly flip to negative numbers and then continue in reverse. This is due to the limit of +/-32,767 for the INT data type used for the 'fib' variable.


Q4

Look at the following Java declaration for Class Circle:
	class Circle{
		private int the_radius;
		public circle(){}
		public circle(int rad)
		{
			the_radius = rad;
		}
	}
	
a) What are the "public circle (... elements )" called ? [1 Mark]
Constructor Methods - ie: they MAKE circles

b) If the statement "Circle yourcirc = new Circle();" is executed, what would be the value of yourcirc.the_raduis ? [1 Mark]
Zero - int type is initialised to zero


Q7

a) What kind of method in Java doesn't return a value ? [1 Mark]
void method

b) What output would the following Java code produce ? [2 Marks]

		int counter = 4
		do
		{ 	System.out.print(" " + counter );
			counter = counter - 3;
		}
		while ( counter > 0 );
		System.out.println(" and That\'s it!");
	
4 1 and that's it!


Q10

One way to declare and initialise an array in Java is to list all its elements, which both initialises the array and fixes its length. eg:
		String[] names = { "Ding", "Dong" };
	
After this declaration:

a) What are the values of names[1] and names.length ? [2 Marks]
"Dong" & 2 - Arrays are indexed from Zero

b) What is the result of including names[9] = "Andme"; ? [1 Mark]
Error Message - Array Subscript too large


Q2

Examin the following Java Program:
		class Hello {
			public static void main (String[] anyargs ) {
				System.out.println("I\'m saying hello!");
			}
		}
	
a) What does this program output ? [1 Mark]
I'm saying hello! - Note: escape character not printed

b) Give the Java command that compiles this program ? [1 Mark]
javac Hello.java - Note: Case must match Class Description

c) Give the Java command that runs this compiled program ? [1 Mark]
java hello - Note: Case no longer important

d) What two syntax alternatives can you use to add comments to a Java Program ? [2 Marks]

  1. // Comment to end of line
  2. /* Block Comment */
  3. /** JavaDoc Comment */


Q5

If the first line of a program was modified to: class Circle extends Shape

a) What would Shape be known as in relation to Circle ? [1 Mark]
Super Class or Parent Class

b) If you can't directly create objects of type Shape, what kind of class is it ? [1 Mark]
Abstract Class - Contains at least one Abstract Method


Q6

What output would the following Java Code produce ? [3 Marks]
		class BooleanExpressions {
			public static void main (String[] args) {
				int a=7, int b=5, int c =8, int d=2;
				boolean p=true, q=false;
				if (!q) System.out.println("q is false");
				if (!(d == c)) System.out.println("c is not equal to d");
				if (a > b || c < b && q)
					System.out.println("that\'s one question");
				else
					System.out.println("now that\'s another question");
			}
		}
	
  1. q is false
  2. c is not equal to d
  3. now that's another question
Note: the conditional in the if brackets must equate to either True (to do what comes next) or False (to jump what comes next). When "q" has a value of FALSE, then "!q" (Not Q) has a value of True. The symbol "||" means "OR", the symbol "&&" means "AND".


Q8

The following Java code fragment is a small part of the declaration of the Java Class Lib Object class:
		public abstract class Java Lib object extends object {
			public static final float THE-VALUE;
			public font getSomething ();
		
	
a) How would you create objects that can use the getSomething() method ? [1 Mark]
Extend class

b) Would a copy of THE-VALUE be supplied to each such object ? [1 Mark]
No! - THE-VALUE is a static variable, only available to the class [(c) Harjit Sidhu]