CIS 260 Midterm Review

This review doesn't necessarily have all the topics covered on the midterm, but it does cover most of what will be on the exam. If you know this material, then you should be set.

Show all answers     Hide all answers

    Data types

  1. In cases where floats and doubles get initialized by Java, they are initialized to ________ Answer 0.0
  2. In cases where bytes, shorts, ints, and longs get initialized by Java, they are initialized to ________ Answer 0
  3. In cases where booleans get initialized by Java, they are initialized to ________ Answer false
  4. In cases where reference fields get initialized by Java, they are initialized to ________ Answer null
  5. The size in bytes of an int data type in Java is _____ byte(s). Answer 4
  6. The size in bytes of a byte data type in Java is _____ byte(s). Answer 1
  7. The size in bytes of a float data type in Java is _____ byte(s). Answer 4
  8. The size in bytes of a long data type in Java is _____ byte(s). Answer 8
  9. The size in bytes of a short data type in Java is _____ byte(s). Answer 2
  10. The size in bytes of a char data type in Java is _____ byte(s). Answer 2
  11. The size in bytes of a double data type in Java is _____ byte(s). Answer 8
  12. Describe the difference between a reference variable and a non-reference variable. Answer A reference varialble holds a reference to an object, whereas a non-reference variable holds tha value of a primitive (built-in) data type.
  13. Explain what autoboxing is. Answer Autoboxing is the abilty Java has to turn primitive data types into objects of their wrapper classes. Unboxing is the reverse procedure. Example: int --> Integer
  14. Strings

  15. The main difference between a String and a StringBuffer is that a String is ________. Answer immutable
  16. The main difference between a StringBuffer and a StringBuilder is that a StringBuffer is ________. Answer thread safe
  17. A StringTokenizer is used to ... Answer break a String into tokens (words).
  18. The String method that returns an uppercase version of a String is ________ Answer toUpperCase().
  19. The String method that returns a lowercase version of a String is ________ Answer toLowerCase().
  20. The String method that returns a character at a specific position in the String is ________ Answer charAt(int).
  21. Explain the difference between the String methods equals and compareTo. Answer If s1 and s2 are Strings, then s1.equals(s2) returns a boolean indicating whether the text inside Strings s1 and s2 are equal or not. On the other hand, s1.compareTo(s2) returns an integer value. The returned value is less than 0 if s1 comes before s2 lexicographically, greater than 0 if s1 comes after s2 lexicographically, and 0 if s1 and s2 contain the same text.
  22. Why bother using the String methods equals, equalsIgnoreCase, compareTo, and compareToIgnoreCase? Answer Because the relational operators don't work for comparing objects, and Strings are objects in Java.
  23. Why should you override the toString() method in the classes that you write? Answer Because the you will be able to easily display the data from the objects created from that class by using System.out.print or System.out.println. You will also be able to automatically concatenate the formatted data from toString() to any Strings you wish using the concatenation operator (+).
  24. Arrays

  25. Write the code to declare an array of five floats named rate. Answer float[] rate = new float[5];
  26. Write one line of code that declares an array of five floats named rate and initializes all of the elements to 7.35. Answer float[] rate = {7.35, 7.35, 7.35, 7.35, 7.35};
  27. Write one line of code that declares an array of two Strings named logo and initializes the elements to CIS and 260. Answer String[] logo = { "CIS", "260" };
  28. Write a Java function named product that accepts an array of floats and returns the product of all the elements multiplied. Answer float product(float[] ar) { float prod = 1.0; for (int i=0; i<ar.length; i++) { prod *= ar[i]; } return prod; }
  29. Write a Java function named sum that accepts a 2-D array of floats and returns the sum of all the elements added together. Answer float sum(float[][] ar) { float tot = 0.0; for (int i=0; i<ar.length; i++) { for (int j=0; j<ar[i].length; j++) { tot += ar[i][j]; } } return tot; }
  30. Object-oriented programming

  31. The code to call an object's superclass default constructor is ________. Answer super();
  32. The code to call an object's superclass toString() method is ________. Answer super.toString();
  33. The private access modifier restricts access to ________ Answer methods within the same class.
  34. The public access modifier restricts access to ________ Answer methods from any class. No classes are restricted.
  35. The protected access modifier restricts access to ________ Answer methods in its own class, subclasses, and in the same package.
  36. Default access in Java restricts access to ________ Answer methods within any class in the same directory (package).
  37. You can write a valid class without writing any methods for that class. Answer True, but it will still inherit methods from its base class.
  38. You can write a valid class without writing the code to create any fields/variables. Answer True, but it will still inherit fields/variables from its base class.
  39. If there is just one abstract method in a class, then the class must be declared abstract. Answer True
  40. Abstract classes may contain non-abstract methods. Answer True. A purely abstract class is usually declared as an interface instead of a class.
  41. An abstract class may be instantiated. Answer False. An abstract class cannot be instantiated. If you extend the abstract class, however, and override all of its inherited abstract methods in the derived class, then you may instantiate the derived class (assuming it has no additional abstract methods of its own).
  42. You can instantiate a class that extends an abstract class if you implement all of the inherited abstract methods (and don't create new abstract methods). Answer True. An abstract class cannot be instantiated, but if you extend the abstract class and override all of its inherited abstract methods in the derived class, then you may instantiate the derived class (assuming it has no additional abstract methods of its own).
  43. An abstract class may not be extended unless you override all of its abstract methods. Answer False, but the derived class that extends the abstract class must also be declared abstract if it does not override all of its inherited abstract methods.
  44. Class (static) fields are initialized by default. Answer True
  45. Instance fields are initialized by default. Answer True
  46. Local variables are initialized by default. Answer False
  47. Each object of a class gets its own copy of every class (static) field. Answer False. All objects of a class must share the class (static) fields.
  48. Each object of a class gets its own copy of every instance field. Answer True. That is their purpose.
  49. You can use static fields and static methods of a class before any objects have been created from that class. Answer True. That's why you can use the static Math.pow(double, double) method without having to create a Math object first.
  50. All classes have default constructors because Java will automatically create one if the programmer doesn't. Answer False. Java will create a default constructor, but only if NO constructors have been written for a class.
  51. The first statement in a constructor is always a call to a superclass constructor. Answer True. If you do not write one, Java will automatically create a call to the superclass default constructor (which will fail to compile if the superclass doesn't have a constructor).
  52. The keyword final used with a variable means that the variable is a constant. Answer True
  53. The keyword final used with a method means that the method is the last method in the class. Answer False. It means that the method cannot be overridden by any subclasses.
  54. The keyword final used with a class means that the class contains only constants. Answer False. It means that the class cannot be subclassed.
  55. How many classes can you extend when writing a new class? Answer One. You can not extend more than one class at a time, and you must extend one. If you do not specify a class to extend, then the class will automatically extend the Object class.
  56. How many interfaces can you implement when writing a new class? Answer As many as you want. It could be none, or it could be many. If there is more than one, then just use a comma separated list of interfaces.
  57. Can you write a class which does not extend any other class? Answer No, becsause Java will make your class automatically extend the Object class if you do not specify a class to extend.
  58. Can you write a class which does not implement any interfaces? Answer Yes.
  59. Declare an abstract, publicly accessible, instance method named getAge which takes no arguments and returns an int. Answer abstract public int getAge();
  60. Write a default constructor for a class named Item which does nothing. Answer public Item() { }
  61. Explain the reason for using a WindowAdapter instead of a WindowListener. Answer A WindowListener requires that you override seven abstract methods. Usually, you are only interested in overriding one or two. The WindowAdapter has already overridden all seven WindowListener methods, so by extending it, you only have to override the methods you are interested in changing.
  62. Write the signature for the method you must override to implement an ActionListener interface. Answer public void actionPerformed(ActionEvent e)
  63. Explain the term instantiation. Answer Instantiation is a fancy word for creating an object from a class.
  64. Explain the term overloading. Answer Overloading a method is when you write a method with the same name as another method, but with a different argument list, and possibly a different return data type.
  65. Explain the term overriding. Answer Overriding a method is writing a method with the same signature as an inherited method to replace the inherited method.
  66. Explain the term composition. Answer Composition is when classes are put together using other classes. You see this whenever a class includes a reference variable as one of its instance or static class fields.
  67. Explain the term polymorphism. Answer Polymorphism is the ability to treat subclasses like they are objects of their superclass type.
  68. Miscellaneous

  69. Importing java.awt.* will import both java.awt.Graphics and java.awt.event.ActionListener. Answer False. java.awt.event.ActionListener is at another level and will not be imported.
  70. Importing individual Java classes (eg. import javax.swing.JLabel) is more efficient then importing everything in the same package using a wildcard (i.e. import javax.swing.*). Answer False. Java is clever enough not to include the unused pieces in the compiled class.
  71. You can write a program using no import statements and still use Swing components found in javax.swing.*. Answer True, but you will have to fully qualify the names of the classes you are using. Example: javax.swing.JLabel lblHello = new javax.swing.JLabel("Hello");
  72. Assertions are useful for checking input data for errors. Answer False. Assertions are for checking for unexpected errors that should never happen. One problem with using them to check user input is that an assertion error would terminate program execution.
  73. Assertions are the primary reason why Java programs run so slow. Answer False on a couple of fronts. First, Java assertions are disabled by default and should not usually be running in most applications. Second, Java programs actually run very fast these days - unlike their speed ten years ago.
  74. The package statement must be the first non-blank, non-comment line in a Java source file. Answer True
  75. All unreferenced objects will be garbage collected by a call to System.gc(). Answer This question will not be asked in quite this form. In general, it will be the case that unreferenced objects are garbage collected when System.gc() is called, but with garbage collection there are no guarantees.
  76. All unreferenced objects will have their finalize() methods called when you call System.gc(). Answer Once again, this question will not be asked in quite this form. In general, it will be the case that unreferenced objects will have their finalize() methods called when System.gc() is called, but with garbage collection there are no guarantees. The garbage collection may be delayed and the finalize() method may never be called.
  77. Write a single line comment that contains the text: CIS 260 Answer // CIS 260
  78. Write a multi-line comment that contains the text: Java II Programming Answer /* Java II Programming */
  79. Write a Javadoc style comment that contains the text: Sun Answer /** Sun */
  80. Given a value of 5.755 in variable rate, write the code that will display the rate on the console screen in a field 7 characters wide with 1 digit following the decimal. Answer System.out.printf("%7.1f", rate);
  81. Practical coding

  82. Write a class named Item that has the following requirements:
    - a private static double constant named rate initialized to 4.3
    - a public instance integer named n
    - a private instance String named name
    - a default constructor
    - a constructor that has parameters for the instance variables
    - a copy constructor constructor
    - accessor methods for the private non-constant variables
    - mutator methods for the private non-constant variables
    Answer
    class Item {
        private static final double rate = 4.3;
        public int n;
        private String name;
        public Item(int n, String nm) { this.n = n; setName(nm); }
        public Item(Item itm) { this(itm.n, itm.name); }
        public Item() { this(0, ""); }
        public String getName() { return name; }
        public void setName(String nm) { name = nm; }
    }