Part 1: Multiple Choice
Circle the Correct Answer
The next 2 questions refer to the following (incomplete) Point class.
public class Point
{ " /*** fields ***/
private int xCoord; // the current x coordinate private int yCoord; // the
current y
coordinate
// METHODS
// default constructor: initialize the point to 0,0 public Point( ) { ... }
// another constructor: initialize the point to x,y public Point(int x, int
y) { ... }
// set the x coordinate to the given value - public void
setX(int x) { ... }
// set the y coordinate to the given value public void setY(int y) { ... }
// return the x coordinate public int getX( ) { ... }
// return the y coordinate public int getY( ) { ... }
// move the point horizontally d units public void moveHorizontal(int d) {
... }
// move the point vertically d units public void moveVertical(int d) { ... }
}
Commentary for problem 1
Segments I uses the first constructor but with incorrect follow-on
methods (xCoord and yCoord do not exist). Segment II uses the first
constructor with appropriate follow-on methods. Segment III uses the second
constructor properly.
¢
Assume
that P is a Point variable in a method that is not in the Point class. Which
of the following code segments correctly sets P to represent the point
(5,5)?
Segment I
Segment II
Segment III
P = new Point ( );
P = new Point ( );
P = new Point(5,5);
P.xCoord = 5;
P.setX(5);
P.yCoord = 5;
P.setY(5);
A. I only
B. II only
C. III only
D. I and II
E. II and III
ANSWER
Commentary for problem 2
Observe the methods defined above
¢
Assume
that P is a Point variable that represents the point (x, y) in a method that
is not
in the Point class.
Which of the following code segments correctly changes P to represent
the point
(y, x) ?
A. P.getX( ) = P.getY( ) ;
P. getY( ) = P.getX ( ) ;
B. P.setX (P. getY () ) ;
P. setY (P.getX ( ) ) ;
C. P.moveHorizontal(P.getY());
P.moveVertical(P.getX( );
D. int tmp = P.xCoord;
P.xCoord = P.yCoord;
P.yCoord = tmp;
E. int tmp = P.getX( );
ANSWER
P.setX(P.getY( ));
P . setY ( tmp) ;
Commentary for problem 3
¢
Which
of the following best describes the purpose of a method's pre- and
postconditions?
A. They provide information to the programmer about what the method is
intended to do.
ANSWER
B. They provide information to the programmer about how the method is
implemented.
C. They provide information to the compiler that permits it to generate
better code.
D.
They provide information to the compiler that makes type checking easier.
E. They permit the method to be in a different file than the code that calls
the method.
¢
Assume
that x is an initialized int variable.
The code segment
if (x > 5) x *= 2;
if (x > 10) x = 0;
is equivalent to which of the following code segments?
A. x = 0 ;
does not account for x < 5, etc
B. if (x > 5) x = 0;
what about x = 10?
C. if (x > 5) x *= 2;
what about x = 10?
D. if (x > 5) x = 0;
should be x = 0 – eliminated the
answer from consideration
else x *= 2;
E. if (x > 5) x *= 2;
ANSWER
else if (x > 10) x = 0;
¢
Consider
the following code segment:
public void mystery( int j, int k )
{
if (j !=k) mystery(j+1, k);
}
Which of the following best characterizes the conditions under which the
call mystery ( x, y )
leads
to an infinite recursion?
A. All conditions
B. No conditions
C. x < y
D. x > y ANSWER
E. x==y
¢
Consider
the following code segment:
int x = 0;
boolean y = true;
if (y && (x != 0) && (2/x == 0)) System.out.println("success");
else System.out.println("failure");
Which of the following statements about this code segment is true?
A. There will be an error when the code is compiled because the first &&
operator is applied
to a non-boolean expression.
B. There will be an error when the code is compiled because a boolean
variable (y) and an
int variable (x) appear in the same if -statement condition.
C. There will be an error when the code is executed because of an attempt to
divide by zero.
D. The code will compile and execute without error; the output will be
"success."
E. The code will compile and execute without error; the output will be
"failure." ANSWER
¢
Consider the following code
segment:
if (n > 0) n = -n;
if (n < 0) n = 0 ;
This segment is equivalent to which of the following?
A. n = 0
Answer
B. if (n > 0) n = 0;
C. if( n < 0 ) n = 0 ;
D. if (n > 0) n = -n; else n = 0 ;
E. if( n < 0 ) n = 0 ; else n= -n ;
Note: n can be 0, < 0, or > 0.
If n is 0, then the if statements do not apply (they are false), so n
remains 0.
If n > 0 then it is set to –n by the first if statement, then it is set to 0
by the second if statement.
If n < 0 then the first statement does not apply and it is set to 0 by the
second statement.
¢
What will
be printed by the following?
public
class Lists
{
public static void main (String [] args)
{
printStars(4);
}
public static void printStars(int k)
{
if (k>0)
{
for (int j = 1;
j<=k;
j++)
{
System.out.print("*") ;
System.out.println();
printStars(k - 1);
}
}
}
}
A. ****
***
**
* |
C. ***
**
* |
E. *
*
*
* |
B. *
**
***
**** |
D. *
**
*** |
F. *
Answer
*
*
*
* |
¢
If
addition had higher precedence than multiplication, then the value of the
expression
2*3+4*5
would be which of the following?
A. 14
B. 26
C. 50
D. 70
Answer
E. 120
¢
Given
the following class. What is printed?
public
class
Lists
{
public
static
void main (String
[] args)
{
int
A[] = new
int[5];
for (int
i = 1; i<5; i++)
A[i] = i;
//precondition: A.length > 0
int
x= 0;
for
(int
k = 1; k < A.length;
k++)
{
if
(A[k] <A[x]) x=k;
}
System.out.println(x);
}
}
A. It returns the value of the smallest element of A.
B. It returns the value of the largest element of A.
C. It returns the index of the smallest element of A. Answer
D. It returns the index of the largest element of A.
E. It is not possible to determine what the code segment does
without knowing how A is
initialized.
Given the following class Superclass and the derived class Subclass. What is
printed?
public class Superclass
{
public void printMethod()
{
System.out.println("Printed in Superclass.");
}
}
public class Subclass extends Superclass
{
public void
printMethod()
{
super.printMethod();
System.out.println("Printed in Subclass");
-
}
public static void
main(String[] args)
{
Subclass s = new Subclass();
s.printMethod();
}
}
A. Printed in Superclass. on one line followed by Printed in Subclass on the
next line.
Answer
B. Printed in Subclass on one line followed by Printed in Superclass on the
next line.
C. Nothing will be printed, incorrect syntax
D. Printed in Superclass. followed by Printed in Subclass all on the same
line
E. Printed in Subclass followed by Printed in Superclass. all on the same
line
Part 2: String Class
Fill In
The Blanks
¢
Strings
are objects of the ________________________ class.
String
¢
The
operator used for concatenation of strings is ___________________________
+
¢
If a
reference to an object is no longer needed then the memory it used is
reclaimed by the
Java
______________________________
garbage collector
¢
A
reference variable set to ___________________ means that it does not refer
to an object.
null
¢
A String
is a _______________ data type and a char is a __________________ data type
reference, primitive
¢
The
________________ operator will check the address of where the object is
being stored.
==
¢
If a
reference to an object is no longer being used (over written by another, for
example), the
original
reference is referred to as ___________________
garbage
¢
The
package in which the String class resides is imported for you automatically.
_____True
_____False
True
¢
Different
reference variables that refer to the same object are
called___________________.
aliases
¢
You
cannot modify a String object. We therefore say that they are
____________________.
immutable
Part 3: Inheritance
Fill In
The Blanks
¢
______________________
allows you to define a new class based on a class that already
exists.
Inheritance
¢
he class
that is used as a basis for defining a new class is called a
___________________
superclass
or base class
¢
The new
class based on the superclass is called a ___________________
subclass or
child class
or derived class
¢
The
keyword __________________ used in a class invokes the constructor of the
parent
class
of the class.
super
¢
A derived
class can ___________________ a method from its base class by defining a
replacement
method with the same signature.
override
¢
The
________________ access specifier means that the class member can be
accessed
directly
from outside the class
public
¢
The
________________ access specifier means that class members cannot be
accessed
directly from outside the class
private
¢
The
________________ access specifier is used with inheritance. This means that
the class
member can
be used in the class in which it is defined and in any subclass of that
class.
protected
¢
A (An)
_____________________ class is a class for which no bodies for the methods
are
defined.
interface
¢
A (An)
_____________________ class is a class for which all methods have been
defined.
concrete
¢
A (An)
_____________________ class is a class that contains both defined and
undefined
methods.
abstract
¢
A class
(not the one with a main method) that contains an object of another class is
said to
have a “has
a” relationship with the other class. A class using inheritance is said to
have a
(an)
_______________ relationship with the class it inherits from.
is a
¢
A derived
class can _______________ a method from its base class by defining a
replacement
method with the same signature.
override
¢
The class
from which all classes in Java inherit, either implicitly or explicitly is
the
_____________ class.
Object
¢
Two
keyword that indicate one class inherits from another
are _________________ and
_________________. For example public class A _____________ B
extends,
implements
¢
A
derived class is typically smaller then the base class.
True_____ False_____
False