If there are 4 objects of type StaticExample, how many different instances of x are there?
a) 0b) 1
c) 3
d) 4
e) There is no way to know since any of the objects might share x, but they do not necessarily share x
Use the following class definition:
public class StaticExample
{
private static int x;
public StaticExample (int y)
{
x = y;
}
public int incr( )
{
x++;
return x;
}
}
Answer: b.
Explanation: Because x is a static instance data, it is shared among all objects of the StaticExample class, and therefore, since at least one object exists, there is exactly one instance of x.
Enhancing Classes
- A listener is an object that
- JOptionPane is a class that provides GUI
- In order to have some code throw an exception, you would use which of the following reserved words?
- An exception can produce a “call stack trace” which lists
- A Java program can handle an exception in several different ways. Which of the following is not a way that a Java program could handle an exception?
- Assume that you are defining a class and you want to implement an ActionListener. You state addActionListener(this); in your class’ constructor. What does this mean?
- If s is a String, and s = “no”; is performed, then s
- Which of the following pieces of logic could be used in the method that implements Comparable? Assume that the method is passed Object a, which is really a ChessPiece. Also assume that ChessPiece has a method called returnType which returns the type of the given piece. Only one of these answers has correct logic.
- Which of the following method headers would properly define the method needed to make this class Comparable?
- In order to implement Comparable in a class, what method(s) must be defined in that class?
- Which of the following interfaces would be used to implement a class that represents a group (or collection) of objects?
- An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?
- What is the value of z after the third statement executes below?
- Static methods cannot
- Which of the following methods is a static method? The class in which the method is defined is given in parentheses following the method name.
- Consider the following swap method. If String x = "Hello" and String y = "Goodbye", then swap(x, y); results in which of the following?
- If the instructions z.set2(5); and y.set1(10); are performed, which of the following is true?
- If the instruction z.set2(y.get1( )); is executed, which of the following is true?
- The statement y.get2( ); will
- The statement z.get2( ); will
- If the operation y = x; is performed, then the result of (x = = y) is
- If the operation y = "Hello"; is performed, then the result of (x = = y) is
- The result of x.length( ) + y.length( ) is
- The result of (x = = y) is