CS 161 Introduction to Programming, Java

Final Exam

version 1
Name:
GMU ID:
Read and respond to each of the following questions.

  1. Q - Consider the following class definition:
          public class ChocolateChipCookie {
            public static String owner;
            public static final int numChips;
            public int width;
          }
    
    Which of the following correctly completes the sentence? Each ChocolateChipCookie instance
    1. has its own individual owner field, its own individual numChips field, and its own individual width field.
    2. has its own individual owner field and its own individual width field, but not its own individual numChips field.
    3. has its own individual numChips field and its own individual width field, but not its own individual owner field.
    4. has its own individual owner field, but not its own individual numChips field or its own individual width field.
    5. has its own individual width field, but not its own individual owner field or its own individual numChips field.
    6. has its own individual numChips field, but not its own individual owner field or its own individual width field.

    [1 point]

    A - e - The owner and numChips, being marked static, are shared by all ChoclateChipCookie instances; only width is a field that each instance will have its own of. The fact that numChips is marked final has no bearing: it only means that the number of chips a ChocolateChipCookie has never changes once set.


  1. Q - Suppose that takeFinal is a method of a class named Student. The takeFinal method takes no arguments. Write an example of calling the method takeFinal for an instance of Student named aStudent.

    [2 points]

    A - I was looking for something like:

     aStudent.takeFinal(); 

  1. Q - What happens when an instance of an object is no longer referenced by any variable, array entry, or other collection of objects in Java?

    [1 point]

    A - The instance is available to be garbage collected.


  1. Q - Name two characteristics of Java that are leading to its growing popularity over other programming languages.

    [2 points]

    A - I was looking for things such as garbage collection, type safety, object oriented, portable, platform independence, etc.


  1. Q - What is the term for what a Java compiler produces?

    [1 point]

    A - bytecode.


  1. Q - What is the name for the part of the Java platform in which an application or applet runs?

    [1 point]

    A - Java Virtual Machine (JVM)


  1. Q - True or false:    All objects of the same class have the same fields (though not necessarily the same field values.)

    [1 point]

    A - True.


  1. Q - True or false:    All objects of the same class share the same fields (if you change the field associated with one object all objects will see the same change.)

    [1 point]

    A - False.


  1. Q - Give the declaration for a variable called amount of type double. The variable should be initialized to 20 in the declaration.

    [3 points]

    A -

    double amount = 20.;

  1. Q - Write a Java assignment statement that will set the value of the variable grade to the value of the variable finalExam multiplied by the value of the variable curve. The variables are all of type float.

    [3 points]

    A -

    float grade = finalExam * curve;
    pecifying the type of grade is optional since the question says they are all of type float.
  1. Q - What is the output produced by the following code?
    int code = 1;
    code++;
    if (code > 0) 
      System.out.print("Hello ");
    else
      System.out.print("Good-bye ");
    
    1. Hello
    2. Good-bye
    3. Hello Good-bye
    4. NullPointerException
    5. This code won't compile, therefore won't produce any output

    [1 point]

    A - a: Hello


  1. Q - True or false:    In a Java class named Student
    public Student (int amount) { ... } 
    and
    private Student() { ... } 
    (where ... is some additional code) are valid constructors that can both exist?

    [1 point]

    A - True: an example of constructor overloading.


  1. Q - Consider the following fragment of code:
    int field1 = 100; 
    int field2 = 200; 
    field2 = field1;
    field1 = filed2;       // line 4
    
    When the code has executed through the line with the comment "line 4", what are the values of field1 and field2?
    1. field1 is 100 and field2 is 200
    2. field1 is 200 and field2 is 100
    3. field1 is 100 and field2 is 100
    4. field1 is 200 and field2 is 200
    5. none of the above

    [1 point]

    A - c


  1. Q - Consider the following declaration:
    public static final int SIZE = 100;
    Which of the modifiers makes SIZE a class variable?
    1. public
    2. static
    3. int
    4. final
    5. none of the modifers used make SIZE a class variable.

    [1 point]

    A - b Marking a variable static means it is a class variable


  1. Q - In Java, which of the following loop statements may have their bodies executed zero times?
    1. while-statement
    2. do-while-statement
    3. for-statement
    4. all of a, b, and c
    5. a and b, but not c
    6. a and c, but not b
    7. b and c, but not a

    [1 point]

    A - f


  1. Q - What is the name of the Java primitive type, variables of which can contain 16 bit Unicode?

    [1 point]

    A - char


  1. Q - Given a Java class MyExample, which has an instance variable called myData. How many copies of myData are stored in memory?
    1. One
    2. The number of times the value of myData is changed.
    3. The total depends on the number of MyExample objects that have been instantiated.
    4. 3
    5. Zero

    [1 point]

    A - c


  1. Q - Given a Java class MyExample, which has an class variable called classData. How many copies of classData are stored in memory?
    1. One
    2. The number of times the value of myData is changed.
    3. The total depends on the number of MyExample objects that have been instantiated.
    4. 3
    5. Zero

    [1 point]

    A - a


  1. Q - Which answer correctly completes the sentence: Java provides wrapper classes for
    1. all primitive types and the String type.
    2. all numeric types.
    3. all primitive types except boolean.
    4. all primitive types.
    5. every class in Java is a wrapper class.

    [1 point]

    A - d


  1. Q - To what object oriented programming concept does the Java keyword extends apply?

    [1 point]

    A - inheritance was what I was looking for.


  1. Q - To what object oriented programming concept does the Java keyword private apply?

    [1 point]

    A - encapsulation was what I was looking for, as in keeping implementation details hidden.


  1. Q - True or False:    System is a primitive data type in Java as shown by the use of System.out.println to output information.

    [1 point]

    A - False: System is simply one of the classes provided by the Java API


  1. Q - What is the phrase that describes the kind of programming used for most current application development, particularly programs that use a Graphical User Interface.

    [2 points]

    A - Event Driven Programming.


  1. Q - Write a code fragment that illustrates the way Exceptions are handled in Java

    [3 points]

    A - I was looking for a skeleton of a try ... catch block; i.e.,

    try {
     ...
    }
    catch (SomeException e) {
     ...
    }
    
    although I would have also accepted
    public ... SomeMethodOrConstructor() throws Exception { ...
    

  1. Q - Write a code fragment that illustrates a branching construct in Java

    [3 points]

    A - I was looking for the skeleton of an if, if .. else, or switch statment: one of the three branching constructs in Java, e.g.:

    if (i == 0) {
      ...
    }
    
    or
    if (result != false) {
     ...
    }
    else {
     ...
    }
    
    or
    switch (value) {
    case 1:
     ...
      break
     ...
    }
    

  1. Q - In a few words, what is a Wrapper class (do not just give examples)

    [2 points]

    A - I was looking for "encapsulates primitive data types" or words to that effect. I'd also accept "provides convenience methods for primitive data types": some mention of their relationship to primitive data


  1. Q - True or False:    In Java array indices start at 0.

    [1 point]

    A - True; Java array indices begin with 0.


  1. Q - Write a code fragment that declares, instantiates, initializes, and prints an array of 5 integers with values 20, 40, 60, 80, and 100 using a loop.

    [5 points]

    A - One point each for array declaration; instantiation; initialization; a for, while or do .. while loop; and recognition that array indices start at 0; e.g.,

        int intarray[] = { 20, 40, 60, 80, 100};
        for (int i = 0; i < 5; i++ ) {
           System.out.println(intarray[i]);
        }
    
    or
        int anotherarray[] = new int[5];
        for (int i = 0; i < 5; i++ ) {
           anotherarray[i] = (i+1) * 20;
           System.out.println(anotherarray[i]);
        }
    

  1. Q - With respect to the Java classes that make up an application, which of the following is most correct.
    1. All of the methods should be marked static
    2. All the methods must be defined in a single class
    3. There is at least one class with a method named main
    4. All of the fields must be public
    5. all of a, b, c, and d
    6. a and b only.
    7. b and d only

    [1 point]

    A - c


  1. Q - True or False:    A Java class can not contain both class and instance methods.

    [1 point]

    A - False.


  1. Q - Write a Java code fragment that uses a branching construct to write out the name of a day of the week depending on the value of an int field named day having a value 1 - 7.

    [4 points]

    A - I was looking for the use of either an if .. else .. or a switch statement; some output, and some use of logic to decide which string to print.

      String dayName = null;
      switch (day) {
      case 1:
        dayName = "Sunday";
        break;  
      case 2:
        dayName = "Monday";
        break;  
      case 3:
        dayName = "Tuesday";
        break;  
      case 4:
        dayName = "Wednesday";
        break;  
      case 5:
        dayName = "Thursday";
        break;  
      case 6:
        dayName = "Friday";
        break;  
      case 7:
        dayName = "Saturday";
        break;  
      }
      System.out.println(dayName);
    
    Is one way.
  1. Q - True or False:    With respect to arrays in Java, only arrays of object types, like String, not arrays of primitives have a field named length that can be used to determine how many elements the array.

    [1 point]

    A - False


  1. Q - In Java, new classes can be defined by extending existing classes. This is an example of:
    1. encapsulation
    2. interface
    3. polymorphism
    4. inheritance
    5. a, c, and d

    [1 point]

    A - d


  1. Q - Write a code fragment that shows the proper syntax for declaring a variable of the class Integer and initializing its value to 5.

    [3 points]

    A - The proper syntax is shown below:

    Integer myWrappedData = new Integer(5);
    

  1. Q - Write a code fragment that demonstrates declaring a field named currentAccount for a class named BankAccount, calling the no argument constructor for the class and assigning the resulting reference to the currentAccount field.

    [4 points]

    A -

     BankAccount currentAccount = new BankAccount();
    

  1. Q - If a class is named Student, which of the following is a valid signature for a constructor for this class?
    1. public Student()
    2. public void Student( String name )
    3. private Student()
    4. public Student( String name )
    5. All of a, b, c, d
    6. a, b, and d
    7. a, c, and d
    8. only a and d

    [1 point]

    A - g Constructors take 0 or more arguments, and don't have any return type. While rare, a constructor marked private is permissible; in fact I mentioned private constructors a couple of times in class.


  1. Q - True or false:    To allow Java's automatic garbage collection mechanism reclaim the memory used by an object, one can assign the keyword null to an object reference.

    [1 point]

    A - True.


  1. Q - Consider the following Java class definition:
     public class Species {
       private String name;
       public void setName(String newName) {
         name = newName;
       }
       public String toString() {
         return new String(name);
       }
    
    If the following lines of Java code are in another class:
         Species klingon = new Species();
         klingon.setName("Klingon");
         Species romulan = klingon;
         romulan.setName("Romulan");
         System.out.println(klingon);
         System.out.println(romulan);
    
    What will be output by trying to run this code?

    [2 points]

    A -

    Romulan
    Romulan
    

  1. Q - Identify three Java keywords associated with branching or looping control flow constructs.

    [3 points]

    A - Gee, how about looking at some of the examples in this test? for, while, do, if, else, switch, case, break, default


  1. Q - What happens if you try to access an invalid index in an array in Java?

    [1 point]

    A - IndexOutOfBoundsException or one of one of its subclasses, e.g., ArrayOutofBounds exception. I'd accept just "throws an exception".


  1. Q - In the context of programming languages, what are reserved words

    [1 point]

    A - The vocabulary


  1. Q - In a few words, in computer programming an expression is

    [2 points]

    A - " An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value." The key concept was "something that gets evaluated to a single value".


  1. Q - What kinds of error messages alert you to the fact that your program contains an error?

    [1 point]

    A - Syntax error or run-time error. I'd also accept Exceptions.


  1. Q - Suppose Employee is a class with a method named getBossName that takes no arguments and that returns a value of type String. Suppose also that dilbert is an object/instance of the type Employee. Write a code fragment that assigns dilbert's boss' name to a variable named bossName.

    [3 points]

    A -

    String bossName = dilbert.getBossName();

  1. Q - What kinds of programming errors don't alert you to the fact that they exist; for example, they produce "reasonable looking", but still incorrect, output?

    [1 point]

    A - Logic errors.


  1. Q - Which statement is not true:
    1. A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind.
    2. An object is a software bundle of variables (fields) and related methods.
    3. Classes are created from the template that an instance defines.
    4. none of a, b, or c above are true
    5. all of a, b, or c above are true

    [1 point]

    A - c


  1. Q - True or false:    Since arrays are objects in Java they inherit all the characteristics of java.lang.Object

    [1 point]

    A - True


  1. Q - True or false:    In Java an array's length can change at any time.

    [1 point]

    A - False


  1. Q - True or false - In Java, if the programmer does not define any constructor for a class there is no way to instantiate objects of that type.

    [1 point]

    A - False: If you don't define one, Java will create a default, no-arg constructor.


  1. Q - True or false - There are three categories of variables in Java: class variables, instance variables, and local variables

    [1 point]

    A - True