Multiple Choice(without choices):
For questions 1-4, assume x and y are String variables with x = "Hello" and y = null.
- The result of (x = = y) is
- The result of x.length( ) + y.length( ) is
- If the operation y = "Hello"; is performed, then the result of (x = = y
- If the operation y = x; is performed, then the result of (x = = y) is
For questions 5-8, consider a class that stores 2 int values. These values can be assigned int values with the messages set1(x) and set2(x) where x is an int, and these values can be accessed through get1( ) and get2( ). Assume that y and z are two objects of this class. The following instructions are executed:
y.set1(5);
y.set2(6);
z.set1(3);
z.set2(y.get1( ));
y = z;
5. The statement z.get2( ); will
6. The statement y.get2( ); will
7.If the instruction z.set2(y.get1( )); is executed, which of the following is true?
- (y = = z) is still true
8. If the instructions z.set2(5); and y.set1(10); are performed, which of the following is true?
10.Consider the following swap method. If String x = "Hello" and String y = "Goodbye", then swap(x, y); results in which of the following?
public void swap(String a, String b)
{
String temp;
temp = a;
a = b;
b = temp;
}
For questions 10 – 11, use the following class definition:
public class StaticExample
{
private static int x;
public StaticExample (int y)
{
x = y;
}
public int incr( )
{
x++;
return x;
}
}
10. What is the value of z after the third statement executes below?
StaticExample a = new StaticExample(5);
StaticExample b = new StaticExample(12);
int z = a.incr( );
11. If there are 4 objects of type StaticExample, how many different instances of x are there?
12. An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?
13. In order to implement Comparable in a class, what method(s) must be defined in that class?
14. Which of the following method headers would properly define the method needed to make this class Comparable?
True/False Questions:
Several variables can reference the same object.
The = = operator performs differently depending on whether the variables being compared are primitives types or objects.
If first and last are both String variables, then first.equals(last); does the same thing as (first = = last).
If String x = null; then x.length( ); returns the int 0.
Assume that the class Bird has a static method fly( ). If b is a Bird, then to invoke fly, you could do Bird.fly( );.
If an exception is thrown and is not caught anywhere in the program, then the program terminates.
Any class can implement an interface, but no classes can implement more than a single interface.
All objects implement Comparable.
An object uses the “this” reserved word to refer to itself.
Any variable can take on the value null to indicate that it has no value.
Assuming name is a String variable, the test if (name != null && name.length() < 10) will cause a NullPointerException if name is null.
The statements String a = “hi”; String b = a + “”; cause a and b to be alises of each other.
Variables should never be static.
If the Bar class has a static method named foo which takes no parameters, then it may be called with the statement. Bar.foo();
When a program attempts to divide by 0, a NullPointerException occurs.
In an interface, the programmer has the choice of providing a method body or not.
A class that implements an interface must implement every method in the interface.