Preview

09 - Creating and Storing Objects

 1. In the following example, the __________ asks for an object to be constructed.
public class StringDemo1
{
  public static void main ( String[] args )
  {
    String str ;

    str = new String( "I love testandtrack" );
  }
}

  class operator

  args operator

  new operator

  public operator

 2. The following expression (when the program is run) creates a new _____ object by following the description contained in the _____ class.
new String( "I love testandtrack" )

  sub

  string

  super

  instance

 3. Refer to the code in question 1. Is the following statement true or false?
Before the program runs, there is already an object in existence. 
The new String object is created before the program runs. 

  FALSE

  TRUE

 4. Again, referring to the code in question 1, the declaration "String str;" creates a reference variable, but does not create a String object.

  TRUE

  FALSE

 5. The following statement _________________________________.
str = new String( "I love testandtrack" ); 

  creates an object and puts a reference to that object in str.

  creates a class and puts a reference to that class in the object

  creates an object and puts the class itself in str, as an indrect reference

  creates a class and puts a reference to that class in the object

 6. The syntax for creating new objects is as follows. The constructor name ___________________.
ClassName variableName = new ConstructorName(parameters);

  must never be the same as the class name

  is the same as the class name

  is the same as the reference variable name

  must never be the same as any variable inside the class

 7. Which statement would we use to create an object from ExampleClass?

  ExampleClass exampleClass = new ExampleClass();

  new ExampleClass() = exampleClass

  ExampleClass exampleClass = ExampleClass();

  ExampleClass exampleClass = new ExampleClass;

 8. Which of the following statements is true of this code? (refer to the 'Objects Example' code, with reference to the code above it.
public class ObjectTest {

  private String exampleVariableOne;

  // constructor of the class
  public ObjectTest(String exampleVariableOne) {
    this.exampleVariableOne = exampleVariableOne;
  }

  public void print(){
    System.out.println(exampleVariableOne);
  }
}

-------Objects Example----------

public class ObjectsExample {

  public static void main(String[] args) {
    ObjectTest objectTest = new ObjectTest("Hello World!");
    objectTest.print();
  }
}

  It creates a new object of the test class. The constructor of the ObjectTest class takes in no parameters

  It creates a new object of the test class. The constructor of the ObjectTest class takes in one String parameter

  It creates a new class of the test object. The constructor of the ObjectTest class is void

  All of the listed answers are valid

 9. Constructors are said to be ____________ when there are multiple constructors with the same name but a different signature.

 10. ____________ allow values to be passed to the?constructor to establish the initial state of?the object.

 11. The keyword ___ is a special value used to indicate that a reference is not associated with any object

  null

  args

  Nref

  new

 12. The memory associated with a variable of a reference type holds an object reference value or, if there is no object, _____.

  args

  new

  null

  Nref

 13. Which statement in the Main class will cause an error?
public class Main {
  public static void main (String[] args){
    ExampleClass exampleClassOne = new ExampleClass();
    System.out.println(exampleClassOne.varOne);
    System.out.println(exampleClassOne.varTwo);
    System.out.println(exampleClassOne.getString());
    System.out.println(exampleClassOne.getBool());
  }
}

public class ExampleClass {
  public int varOne = 10;
  private String varTwo = "Hello World";
  private boolean varThree = false;
  public String getString(){
    return varTwo;
  }
  public boolean getBool(){
    return varThree;
  }
}

  System.out.println(exampleClassOne.varTwo);

  System.out.println(exampleClassOne.getString());

  System.out.println(exampleClassOne.varOne);

  System.out.println(exampleClassOne.getBool());

 14. Refer to the code below and decide which of the following lines is invoking an object's method.
public class StringTester
{

  public static void main ( String[] args )
  {
    String str1;   // str1 is a variable that refers to an object, 
                   // but the object does not exist yet.
    int    len;    // len is a primitive variable of type int

    str1 = new String("Random Jottings"); 
                                                           
    len  = str1.length();  

    System.out.println("The string is " + len + " characters long");
  }
}

  str1 = new String("Random Jottings");

   public static void main ( String[] args )

   int len;

  len = str1.length();

 15. Refer to the code in the previous question. Which line is creating an object of type String?

  len = str1.length();

  str1 = new String("Random Jottings");

   public static void main ( String[] args )

   int len;

 16. An object consists of both variables (state information) and ________(small programs). Both of these are called members of the object.

 17. To access the members of an object in Java, we use ______________.

  dot notation

  floating point notation

  standard notation

  string notation

 18. To invoke the length() method of the object named str1 what would you type?

  str1.length=str1

  length.str1

  str1.length();

  length()=str1

 19. To store the value computed by the length method in a variable, what would you type?

  len = length.str1

  str1.length=len;

  len = length(str1);

  len = str1.length();

 20. A ____is like a cookie cutter that can be used many times to make many cookies. There is only one cookie cutter, but can be used to make many cookies.

 21. Fill in the blanks for the following statement.
In Java, a characteristic of a class that is not shared
by its objects is called ______. There is only one class 
definition for a given class, so when a program is running, 
if something is ______ then there is only one of it.

  public

  private

  static

  void

 22. A program can execute a static method without first creating an object! All other methods (those that are not static) exist only when they are part of an object.

  TRUE

  FALSE

 23. Explore the code below. How many objects are there here?
String prompt1 = "Press Enter to Continue."
String prompt2 = "Press Enter to Continue."
String prompt3 = "Press Enter to Continue."
String prompt4 = "Press Enter to Continue."

 24. What is the output of the following program?
public class StringTester
{

  public static void main ( String[] args )
  {
    String str1;   // str1 is a variable that may refer to an object. 
                   // The object does not exist unit the "new" is executed.
    int    len;    // len is a primitive variable of type int

    str1 = new String("Random Jottings");  // create an object of type String
                                                           
    len  = str1.length();  // invoke the object's method length()

    System.out.println("The string is " + len + " characters long");
  }
}

  The string is len characters long

  The string is 15 characters long

  println(Random Jottings)

  Random Jottings

 25. In the above code the variable _____ is used to refer to this object. In other words, ____ gives the object a name.

 26. What happens when you compile the below class?
class A
{
    int i;
 
    static
    {
        System.out.println(i);
    }
}

  It compiles correctly but has no output.

  Syntax error (Line 7). You cannot use the key word 'static' on its own

  Compile time error (Line 7). You can?t refer a non static field inside a static initialization block.

  Compile time error (line 1). 'A' is a reserved word in Java.

 27. The output of the following program will be: 3,2,1 Null
public class A
{
    static
    {
        System.out.println(1);
    }
 
    static
    {
        System.out.println(2);
    }
 
    static
    {
        System.out.println(3);
    }
 
    public static void main(String[] args)
    {
        A a;
    }
}

  FALSE

  TRUE

 28. What is the output of this code?
class A
{
    static int i;
 
    static
    {
        System.out.println(1);
 
        i = 100;
    }
}
 
public class StaticInitializationBlock
{
    static
    {
        System.out.println(2);
    }
 
    public static void main(String[] args)
    {
        System.out.println(3);
 
        System.out.println(A.i);
    }
}

  100,1

  1,2,3,100

  2,3,1,100

  100,2,3,1

 29. What is the output of this code? (note: Parameter values are passed by value in the calling of a method -a copy of the value is created in the method, and the original value is not affected by the method call.)
 public class testmeth
                       {
                           static int i = 1;
                           public static void main(String args[])
                            {
                                 System.out.println(i+? , ?);
                                 m(i);
                                 System.out.println(i);
                            }
                            public void m(int i)
                            {
                               i += 2;                              
                            }
                       }

  1,

  1 , 3

  1,1

   3 , 1