SDSU CS 596 Java Programming
Fall Semester, 1998
Past Exams
Course Web Site
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 13-Oct-98

Contents of Past Exams


Below are some exams I have given in the past. I make these exams public for two reasons. First, some students have access to my old exams already. Providing these exams removes some of the advantage these students have over those that do not have access to old exams. Second, it does give people an idea of what types of questions I have asked in exams in the past.

However, this is the first time that CS596 Java Programming has been offered. The exams given here are from a different course: CS535. That course covered different material and had a different focus that CS596. This means that some of the questions below cover material that we have not covered. You will not be responsible for material not covered in the lectures, notes or reading assignments from the text. In addition, the number of people taking this course (about 120) makes essay-like questions less desirable. The size of the class may alter my exam style.

CS 535 Fall 1997 (Java)

Midterm Exam

1a. Explain the difference between static and dynamic access of methods.
b. Give one situation where a reference to a non-static method will be resolved statically in Java.

2. Define overloading a method.

3a. List the four types of access levels that a field of a class can have in Java.
b. Explain two of the access levels. That is given a field "foo" in class Bar with access level of type X, where can field "foo" be accessed? Where is field "foo" hidden from view?

4. Explain the differences between super and this in Java.

5. Show how to declare and create a triangular array in Java. That is first row of the array will be one column long, the second row will be two columns long, the k'th row will be k columns long.

6. Class Child is a subclass of class Parent. Class Parent has a method called foo. If class Child is to override the method foo, what restrictions does the Parent's foo method place on the Child's foo method in Java?

7. Explain how the finally block operates in a try-catch.

8. A blank final field is a field that is declared final, but not assigned a value in its declaration. Where can the field be assigned a value in Java?

9. What will be the output of the following Java program? Be careful!

class Parent
   {
   static 
      { 
      System.out.println( "Parent static" ); 
      }
   }
class Child extends Parent
   {
      {
      System.out.println( "Child non-static" );
      }
      
   public Child()
      {
      System.out.println( "In child constructor" );
      }
      
   static 
      {
      System.out.println( "Child static" );
      }
      
   public static void main( String[] arguments )
      {
      new Child();
      new Child(); //Yes I mean to repeat it here
      }
   }

Final Exam

1a. List the different access levels for fields and methods in Java.
b. Explain the differences between the access levels in Java? That is given a class A with a method B where can the method B be accessed and where can it not be accessed if it has access level X. (Answer for X equal to each access level listed in a.)

2a. How is an exception raised in Java?
b. What are the different types of exceptions in Java?
c. How is the exception handler found in Java? Give a separate answer for each type of exception.

3a. What is polymorphism?
b. Explain how to achieve polymorphism in Java.

4a. What is an overloaded method in Java?
b. What is an overridden method in Java?
c. Give an example when you can not overload a method in Java.
d Give an example when you can not override a method in Java.

5a. What is a NaN?
b. If A and B are fields that have been assigned a NaN ( A = NaN, B = NaN) what are the results of the following comparisons?

A < B
A == B
A > B

6. We have two classes A and B. What relationship between A and B do the following phrases indicate. Please indicate the role of A and B in the relationship. For example: A is the parent of B.
a. A is-kind-of B
b. A is-analogous-to B
c. A has-a B
d. A depends-upon B

7. True or False . A Java class A has a final field F. All objects created from class A must have the same value for F.

True or False . An access of a non-static method is always resolved dynamically.

True or False . If a child class constructor does not explicitly call it's parent's constructor, then the parents constructor is not called when creating a child object.

8. a . What is a scenario?
b. What do we use scenarios for in object-oriented design?

9. Identify the classes in the following description of a refrigerator. List at least one responsibility for each class.

A refrigerator has a motor, a temperature sensor, a light and a door. The motor turns on and off primarily as prescribed by the temperature sensor. However, the motor stops when the door is opened. The motor restarts when the door is closed if the temperature is too high. The light is turned on when the door opens and is turned off when the door is closed.

CS 535 Fall 1996 (Java)

Midterm

1) a) We have a class A that has a protected field P. Explain where the protected field P is accessible and where it is not accessible.
b) We have a class A that has a field N, with no explicit access level given. Explain where the field N is accessible and where it is not accessible.

2) a) What is a NaN?
b) Under what conditions will a NaN be generated in Java?

3) What is a constructor? When are they called?
b) What is the role of a finalize method? When is it called?

4) What are the differences between an abstract class and an interface in Java?

5) a) What makes an exception check or unchecked?
b) How can a Java program treat an unchecked exception differently than a checked exception?

6) Let a child and parent class both have a method, public void foo(), with the same signature. The rules for determining which foo () is executed at run time can be summarized as "overridden methods are resolved dynamically". Explain why hidden fields can not be resolved dynamically.

7) What is super? Give an example of when you would use “super”.

8) We have two classes A and B. What relationship between A and B do the following phrases indicate. Please indicate the role of A and B in the relationship. For example: A is the parent of B.
a) Is-kind-of
b) Is-analogous-to
c) Has-a

9) True or False . A Java class A has a final field F. All objects created from class A must have the same value for F.

True or False . An access of a non-static method is always resolved dynamically.

True or False . The average number of fields per class should be less than 6.

True or False . If a child class constructor does not explicitly call it's parent's constructor, then the parents constructor is not called when creating a child object.

10) What is the result of compiling and if they compile executing the following programs?
a)
class  Test
   {
   public static void A()
      {
      int  array[] = new int[5];
      try  
         {
         System.out.println( "Start Try"  );
         array[  10  ]  =  1;
         }
      catch (  Exception error  )
         {
         System.out.println( "Exception " );
         throw error;
         }
      finally
         {
         System.out.println(  "Final Block"  );
         return;
         }
      }
      
   public  static  void  main( String  args[]  )
      {
      try {  A();  }
      catch  (ArrayIndexOutOfBoundsException error ) 
         {
         System.out.println(  "In Catch"  );
         }
      System.out.println(  "After try in main"  );
      }
   }
b) 
class  Test
   {
   String message = "Global";
   public static void A( String message)
      {
      message = message + "End";
      }
      
   public  static  void  main( String  args[]  )
      {
      String message = "Main";
      A( message );
      System.out.println( message );
      }
   }

c)
class Parent
   {
   public Parent()   
      { 
      System.out.println( "Parent" ); 
      }
   public Parent( double value )   
      { 
      System.out.println( "Parent value "  + value );  
      }
   }
class  Child extends Parent
   {
   public Child( String message )
      {
      System.out.println( "Child " + message );
      }
   
   public Child( double value )
      {
      System.out.println( "Child value " + value );
      }
   public  static  void  main( String  args[]  )
      {
      Child A = new Child( 5.5 );
      Parent B = new Child();
      }
   }

CS 535 Fall 1995 - (C++)

MidtermExam

1) List the different ways to implement implicit type conversion in a class.

2) If a class has a pointer as a data member, what items should be part of the class to prevent various pointer problems.

3) a) True or False. A copy constructor does not have initialization phase.
b) Give two different types of items that must be initialized in the initialization phase of a constructor.

4) Explain the object-oriented meaning of the following terms.
a) client-server
b) responsibilities
c) scenario

5) a) How do the variables A and B differ?

char  *const  A  =  "Hi"; 
const  char*  B  =  "Hi";

b) Explain the problems with the following uses of C and D

const  char*  C  =  "hi mom";   
C[3] = 'a';
char  *const  D  =  "hi mom"; 
D = "hi dad";

6) We have two entities A and B. What relationship between A and B do the following phrases indicate. Please indicate the role of A and B in the relationship. For example: A is the parent of B.
a) A is a B
b) A is part of B
c) A uses B
d) A is kind of B

What is the output of the following programs?

7)
#include <iostream.h>
void functionA( int X, double Y) { cout << "First function\n";};
void functionA( char X, float Y ) { cout << "Second function\n";};
main() {
   char Me = 'a';
   int You = 5;
   functionA( Me, You );
}
8)
#include <iostream.h>
class Container {
   public:
      int value;
      Container( int amount ) { value = amount;  
                  cout << "Value " << value << endl; };
      ~Container() { cout << " You just killed: " << value << endl; };
};
class ExamQuestion {
   public :      
      Container data;
      ExamQuestion(int A) : data(A) { cout << "New Object\n";};
      ExamQuestion( const ExamQuestion& X ) : data(X.data.value + 10)
                     { cout << "Special\n"; };
};
void TrickyPart(ExamQuestion why)
{
   ExamQuestion PartB = why;   
   cout << "After PartB\n";
}
   
void main()
{   
   ExamQuestion Answer(1);   
   cout << "Call TrickyPart\n";
   TrickyPart(Answer);      
   cout << "end" << endl;
}

What is the result of compiling and running (if they compile) the following programs.

9) 
class Test {
   private :
      float data;
   public :
      void setData( int& value) { data = value;};
      static float getData()   { return data;};
      Test( int value ) : data( value ) {};
};
#include <iostream.h>
void main()
{
   Test me = 10.5;
   cout << me.getData() << endl;
}
10) 
int A = 10;
float functionB( int A, char B = 5, float C ) {
   return ::A + B + C;
}
#include <iostream.h>
main() {
   int A = 2;
   float X = 11.1;
   cout << functionB( A, X );
}

Final Exam


1) A data member can be declared public, private, or protected. A base class can be inherited via public, private, or protected inheritance. Explain the effects of private and protected inheritance on the access the derived class has of the public and protected data members of the base class.

2) A base class has a data member called "Foo". The derived class also implements a data member of the same name "Foo". Explain how a function member of the derived class can access the base's data member "Foo".

3) What problem does multiple inheritance cause? How does C++ solve the problem?

4) What are the major steps in the Wirfs-Brock design process?

5) What is the difference between a superclass and a subsystem?

6) a) What is an abstract class?
b) How do you indicate an abstract class in C++?

7) Given classes A and B, what is the most likely relationship between A and B indicated by the phrases:
a) A is a kind of B
b) A uses B
c) A is similar to B
d) A depends on B
e) A has knowledge of B

8) What is the output of the following program?

#include <iostream.h>
class ClassA {
   public:
      virtual void Print() {   cout << "Class A\n"; }
};
class ClassZ :  public ClassA  {   
   public:
      void Print() {   cout << "Class Z\n";}
};
main()
{
   ClassZ* Zpointer = new ClassZ;
   ClassA* Apointer  = new ClassA;
   ClassA   Avariable;
   
   Zpointer->Print();
   Apointer->Print();
   Apointer = Zpointer;
   Avariable = *Zpointer;
   Apointer->Print();
   Avariable.Print();
}

9) What is the output of the following program?

#include <iostream.h>
class A  { 
   public : 
      A(int a = 0)    {cout << a << "A\n" ;}
};
class B :  public A    { 
   public : 
      B(int b = 0) : A(b - 1)   {cout << b <<"B\n" ;}
}; 
class C :  public A { 
   public :  
      B DataMember; 
      C(int c = 0) : A(c-1)    {cout << c << "C\n" ;}  
};
class Test :   public C, public B, public A  {
   public: 
      C State;
      Test() : State(10),  C(50),  B(40),  A(30) { cout << "Test\n"; }; 
};
main () {
   Test me;
   cout << "Done" << endl;
};


Copyright © 1998 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

visitors since 13-Oct-98