CS111 C++ Practice Quiz #1

The Basics of Classes




  1. In Java, String is a class. Name two methods which a String object can access its stored characters.

  2. Examine the following class.

    public class Counter{
      private int count;

      public Counter() {
        count = 0;
        }

      public void increment() {
        count++;
        }
      public int getValue() {
        return count ;
      }

    Add a method reset() that resets the count back to zero.
  3. Consider a class Time that represents a point in time, such as 0 A.M. or 3:30 P.M. Identify instance variables that can be used for implementing the Time class.
  4. Examine the following class.

    public class Student{
      private String name;

      public Student(String first, String last) {
        name = last + ", " + first;
        }

      }

    Instantiate a Student object containing the name "Lang, Fritz".
  5. Using the Student class, construct an implementation for a Student constructor so that after the call Student s1 = new Student(); the name instance variable of s1 is "Unknown".
  6. What happens if no constructor is provided for the Student class?
  7. Name two static variables of the system class.
  8. Name a static constant of the Math class.


Answers
  1. substring() and charAt() methods.
  2. public void reset() {
      count=0
      }
  3. int hours; //BETWEEN 1 AND 12
    int minutes; //BETWEEN 0 AND 59
    boolean pm // TRUE FOR P.M., FALSE FOR A.M.
  4. Student fritz = new Student("Fritz", "Lang");
  5. public Student() {
      name ="Unknown";
      }
  6. A constructor is generated that initializes member variables to zero.
  7. System.in and System.out
  8. Math.PI