Announcements‎ > ‎

Sample Midterm Questions

posted Feb 9, 2011, 6:14 AM by Sukhjit Singh   [ updated Feb 9, 2011, 6:15 AM ]

1. [10  points]

 

(a) Write a class "Telephone" to describe Telephone. Assume that the characteristics of telephone s we wish to model are:

 

the number of buttons on the telephone

the color (a string such as "green").

the serial number (a string).

Wired or Wireless.

All these characteristics should be set in the constructor, and "get" methods should be provided for each of them. In addition, a method must

be provided to change the color. You must choose your own method names, parameter lists, and return types.

 

(b) Write statements (that might go in a main( ) method) to do these tasks:

 

i) create a Telephone object that has 12 button, is red and has serial number 5K959111.

ii) paint the Telephone object created in (i) so that it is now blue.

 

2. [10 points]

   a. Given this code fragment:
int sum = 0 ;
for (int i = 1 ; i <= 5 ; i++)
sum += i ;
 
What is the value of sum, after the code is executed?

 

b. Given the code fragment:

   int c;

   c = 4.8 + 2;

What is value of c?       
 

c. What are the data types of the following constants?

 

3.14f

           true

3. [10 points]

 Give an example of overloading a static function.

4. [10 points]
 
   a. Declare a variable name and assign it a value of John.
 

   b. What are the values of the following two expressions?  In each line, assume that:

 

     double x = 10.0;

     double y = 5.0;

     double n = 2.0;

     double m = 1.0;

 

     a. x + n * y – (x + n) * y

     b. (m / n + m) % n

5. Write a function to find even numbers between 0 and 100, using while loop. (Note: 0 is an even number).

6. When overriding constructors what rules if any you must follow?
 

7. In Java, is it OK for a class to have two methods with the same name? If yes, explain when you would have two methods with the same name in a class.

 

8. What is the output of the following Java program?

 

         class Acura 
         {
              Acura (String s) 
              { 
                       System.out.println("Acura " + s); 
              }
     }
     class Honda 
     {
                 Honda (String s) 
                 { 
                          System.out.println("Honda " + s); 
                 }
         }
         public class Cars 
         {
                 public static void main(String args[]) 
                 {
                          Acura integra = new Acura ("Integra");
                          Honda civic = new Honda ("Civic");
                          Honda accord = new Honda ("Accord");
                 }
  }

 

 

 

Comments