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]
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]
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
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!
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
class Hello {
public static void main (String[] anyargs ) {
System.out.println("I\'m saying hello!");
}
}
a) What does this program output ? [1 Mark]
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]
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
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");
}
}
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]
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]