Monday, December 16, 2013

Possible test questions

Untitled Document

Multiple Choice(without choices):

For questions 1-4, assume x and y are String variables with x = "Hello" and y = null.

  1. The result of (x = = y) is
  2. The result of x.length( ) + y.length( ) is 
  3. If the operation y = "Hello"; is performed, then the result of (x = = y
  4. 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?

    1. (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.

Chap 5 Answers

Untitled Document

Chapter 5: Enhancing Classes
Solutions


Multiple Choice
Solutions

True/False
Solutions

  • a
  • c
  • a
  • b
  • e
  • c
  • e
  • d
  • e
  • d

 

  • T
  • T
  • F
  • F
  • T
  • T
  • F
  • T
  • T
  • T

 

Short Answer Solutions

    • What is output by the following code?

String s1 = “hello “;
String s2 = s1;
s2 = s2 + “there “;
System.out.println(s1);
System.out.println(s2);

hello
hello there

    • Discuss how Java passes parameters to a method. Is this technique the same for primitive types and objects? Explain.

Java passes all parameters by value.  This means that the current value of the actual parameter is copied into the formal parameter in the method header.  This technique is consistent between primitive types and objects because object references rather than objects themselves are passed.  When an object (actually, an object reference) is passed, the current value of the reference is copied into the corresponding formal parameter in the method header.

  • 5.3          Explain why a static method cannot refer to an instance variable.

A static method is invoked through a class rather than through an object of the class.  No object of the class needs to be instantiated in order to invoke a static method.  If no object is instantiated, no instance variable exists.  Hence, a static method cannot refer to an instance variable.

    • Can a class implement two interfaces that each contain the same method sig­nature? Explain.

Yes, a class can implement two interfaces each of which contains the same method signature.  The class which implements an interface provides method implementations for each of the abstract methods defined in the interface.  In satisfying the requirements for a method of one interface, it simultaneously satisfies the requirements for a method with the same signature in another interface.

    • Create an interface called Visible that includes two methods: makeVisible and makeInvisible. Both methods should take no parameters and should return a boolean result. Describe how a class might implement this interface.

public interface Visible
{
public boolean makeVisible ();
public boolean makeInvisible ();
}
A class implementing Visible would begin with
public class Icon implements Visible
and would contain at least two methods (makeVisible and makeInvisible), which take no parameters and return a boolean value indicating the success of the method.

  • 5.6          Create an interface called VCR with methods that represent what a video cassette recorder does (play, stop, etc.). Define the method signatures any way you want. Describe how a class might implement this interface.

 

public interface VCR
{
public void play ();
public void pause ();
public void stop ();
public void rewind ();
public void fastforward ();
}
A class implementing VCR would include “implements VCR” in the class header and would implement each of the five methods from the VCR interface. The methods would behave as indicted by their name. For example, fastforward causes the VCR to start fast-forwarding. This keeps going until the stop method is called or another method such as play or rewind is called.

5.7          Given the Num and ParameterTester classes listed earlier in the chapter, what is the result of executing the following lines of code?

ParameterTester myTester = new ParameterTester();
int anInteger = 27;
Num aNum = new Num(38);
Num anotherNum = new Num(49);
myTester.changeValues(anInteger, aNum, anotherNum);
System.out.println(“anInteger: “ + anInteger);
System.out.println(“aNum: “ + aNum);
System.out.println(“anotherNum: “ + anotherNum);

 

Before changing the values:
f1      f2      f3
27      38      49

After changing the values:
f1      f2      f3
999     888     777

anInteger: 27
aNum: 888
anotherNum: 49

 

 

AP-Style Multiple Choice
Solutions

  1. D
  2. B
  3. B
  4. A
  5. E
  6. A

 

 

AP-Style Free Response Solution

5.1
a.
implements Comparable

b.
public String getFirst()
{
return first;
}
public String getLast()
{
return last;
}

public int compareTo (Object otherName)
{
if (this.last.compareTo(((Name)otherName).getLast()) == 0)
return this.first.compareTo(((Name)otherName).getFirst());
else
return this.last.compareTo(((Name)otherName).getLast());
}

 

c.
public String toString ()
{
return first + " " + last;
}