Docsity

Quiz 5: Completing a Java Point Class and Array Index Out of Bounds Exception, Exercises of Computer Science

Toggle sidebar
bg1
Signature _____________________ CSE 11 Name ________________________
Quiz 5
cs11f ____ Fall 2009 Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no calculators.
Given the following partial class definition for Point, fill in the blanks to complete the class definition:
Assume you are given the array definition below and the array is properly initialized:
public class Point
{
private int x;
private int y;
public Point( int x, int y )
{
setX( x );
setY( y );
}
public int getX()
{
____________________________;
}
public int getY()
{
____________________________;
}
public void setX( int x )
{
___________________________;
}
public void setY( int y )
{
___________________________;
}
}
Point[] points = new Point[10];
// Assume array points is properly and fully initialized here
Without using any extra variables other than the variable points, write the single Java statement to change
the first Point's x value to be the same value as the last Point's y value. Do not use any magic numbers or
integer constants other than 0 or -1.
____________________________________________________________________________________
If we tried to access points[55], what Runtime exception would be thrown?
____________________________________________________________________________________
pf2

Partial preview of the text

Download Quiz 5: Completing a Java Point Class and Array Index Out of Bounds Exception and more Exercises Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 5

cs11f ____ Fall 2009 Student ID ____________________

This quiz is to be taken by yourself with closed books, closed notes, no calculators.

Given the following partial class definition for Point, fill in the blanks to complete the class definition:

Assume you are given the array definition below and the array is properly initialized:

public class Point { private int x; private int y;

public Point( int x, int y ) { setX( x ); setY( y ); }

public int getX() { ____________________________; }

public int getY() { ____________________________; }

public void setX( int x ) { ___________________________; }

public void setY( int y ) { ___________________________; } }

Point[] points = new Point[10];

// Assume array points is properly and fully initialized here

Without using any extra variables other than the variable points , write the single Java statement to change

the first Point's x value to be the same value as the last Point's y value. Do not use any magic numbers or

integer constants other than 0 or -1.

____________________________________________________________________________________

If we tried to access points[55], what Runtime exception would be thrown?

____________________________________________________________________________________

Given the following class definitions for class Foo, class Fubar, and class FubarTest:

Given the following class definition:

public class Quiz { private int q5 = 5; }

Write the equivalent class definition explicitly showing everything the Java compiler implicitly inserts by

default. Underline the parts the Java compiler will automatically include as it compiles into bytecode.

What question would you like to see on the Final Exam?

public class Foo { public Foo() { System.out.println( "Foo ctor #1" ); }

public Foo( int x, int y ) { System.out.println( "Foo ctor #2" ); }

public String toString() { System.out.println( "Foo.toString" ); return "Foo.toString"; } }

public class Fubar extends Foo { public Fubar( int x, int y ) { this( x, y, -99 ); System.out.println( "Fubar ctor #1" ); }

public Fubar( int x, int y, int z ) { super( x, y ); System.out.println( "Fubar ctor #2" ); }

public String toString() { System.out.println( "Fubar.toString" ); return super.toString() + " + " + "Fubar.toString"; } }

public class FubarTest { public static void main( String[] args ) { Foo ref = new Fubar( 5, 10 );

System.out.println( "-----" );

System.out.println( ref.toString() ); } }

What is the output when we run FubarTest as in

java FubarTest