JMU CS349 - Developing Multimedia
Gallery Help Policies Solutions Study-Aids Syllabus Tools
Java Review Questions


  1. In Java, what are the implications of having one class extend another class?
  2. In Java, what are the implications of having a class implement an interface?
  3. In Java, what are the implications of having an interface extend another interface?
  4. In Java, what are the implications of having a class implement more than one interface?
  5. Given the following Animation class:
        public class Animation
        {
    
            public void play()
            {
                System.out.println("I don't know how!");
            }
        }
        

    and the following application:

        public class Bad
        {
            public static void main(String[] arguments)
            {
                int  i;
    
                if ((arguments.length > 0) || (arguments != null)) 
                {
                    System.out.println(arguments[0]);
                }
    
                i = 1;
                j = 2;
    
    	    Animation.play();
            }
        }
        
    1. Assuming the Animation class is correct, identify all of the syntactic defects (i.e., the defects that will be detected at compile-time) in the Bad class.
    2. Identify the faults that could cause a failure at run-time) in the Bad class.
  6. Given the following Pair class:
        public class Pair
        {
    	public double    x, y;
        }
        

    Which lines in the following class will compile and which lines won't?

        public class WereYouPayingAttention
        {
    	private final int         i = 0;
            private final boolean[]   b = new boolean[2];
            private final Pair        p = new Pair();
    
            public void change()
            {
    	    i    = 5;
                b[0] = true;
                p.x  = 2.0;
                p    = new Pair();
                p    = null;
                b    = new boolean[2];
                b    = new boolean[3];
            }
        }
        
  7. Given the following Security and Stock classes:
        public abstract class Security 
        {
            protected double price;
            private   int    volume;
    
            public Security()
            {
                price = 100.0;
                volume = 5000;
            }
    
    
            public abstract void print();
        }
        
        public class Stock extends Security
        {
            private String exchange;
    
    
            public Stock()
            {
                super();
                price = 200.0;
                exchange = new String("NASDAQ");
            }
    
    
            public void print()
            {
                System.out.println(price+"   "+exchange);
            }
        }
        

    What will be printed by the following code snippet?

        public class Question
        {
            public static void main(String[] args)
            {
                Stock s;
    
    
                s = new Stock();
                s.print();
            }
        }
        
  8. Suppose we have the following classes:
    public class Chirp {...}
    public class ExpandedChirp extends Chirp {...}

    and the following methods in another class:

    public void add(Chirp ch) {...}
    public void add(ExpandedChirp ch) {...}
    1. What code is executed when add() is passed a Chirp object?
    2. What code is executed when add() is passed an ExpandedChirp object that is declared to be an ExpandedChirp>
    3. What code is executed when add() is passed an ExpandedChirp object that is declared to be a Chirp?
  9. Given the following RunningTime and Movie classes:
        public class RunningTime
        {
            public double hours, minutes;
      
            public RunningTime(double hrs, double mins)
            {
                hours = hrs;
                minutes = mins;
            }
        }
      
      
        public class Movie 
        {
            public RunningTime rt;
      
            public Movie(RunningTime length)
            {
                rt = length;
            }
        }
        

    What will be printed by the following code snippet?

        public class Theater
        {
            public static void main(String[] args)
            {
                Movie          matrix;
                RunningTime    rtMatrix;
      
      
                rtMatrix = new RunningTime(2.0, 10.0);
                matrix = new Movie(rtMatrix);
      
                rtMatrix.hours = 5.0;
      
                System.out.println(matrix.rt.hours+"hrs "+
                                   matrix.rt.minutes+"mins");
      
            }
        }
        
  10. Given the following Clock class:
    public abstract class Clock
    {
        protected int      hours, minutes;
    
    
        public Clock()
        {
    	this(0,0);
        }
    
        public Clock(int hours, int minutes)
        {
    	this.hours = hours;
    	this.minutes = minutes;
        }
    
    
        public abstract String format();
    
    
        public void print()
        {
    	System.out.println(format());
        }
    }
      

    the following TerseClock class:

    public class TerseClock extends Clock
    {
        public TerseClock(int hours, int minutes)
        {
    	super(hours, minutes);
        }
    
    
        public String format()
        {
    	return "" + hours + ":" + minutes;
        }
    }
      

    and the following VerboseClock class:

    public class VerboseClock extends Clock
    {
        public VerboseClock(int hours, int minutes)
        {
    	super(hours, minutes);
        }
    
        public String format()
        {
    	return "The time is " + minutes +" after " + hours;
        }
    }
      

    either identify and fix all of the errors in the following ClockDriver class or show what will be printed (assuming it is executed properly):

    public class ClockDriver
    {
        public static void main(String[] args)
        {
    	Clock         c;
    
    
    	c = new TerseClock(10,15);
    	c.print();
    
    	c = new VerboseClock(12,30);
    	c.print();
        }
    }
      
  11. Given the following Taxable interface, and Book, Clothes, Food, and TaxBasedOnPrice classes:
    public interface Taxable
    {
        public abstract double getTax();
    }
      
    public class Book implements Taxable
    {
        private String          title;
    
        public Book(String title)
        {
    	this.title = title;
        }
    
        public double getTax()
        {
    	return 5.00;
        }
    
        public String toString()
        {
    	return title + " Tax: " + getTax();
        }
    }
      
    public class Clothes extends TaxBasedOnPrice
    {
        private int       size;
        private String    color, style;
    
        public Clothes(String style, String color, int size, double price)
        {
    	super(price, 0.10);
    	this.style = style;
    	this.color = color;
    	this.size  = size;
        }
        
    
        public String toString()
        {
    	return "Size " + size + " " + style + " in " + color + 
    	       " Price: " + getPrice() + "  Tax: " + getTax();
        }
    }
      
    public class Food extends  TaxBasedOnPrice
    {
        private String      description;
    
    
        public Food(String description, double price)
        {
    	super(price, 0.05);
    	this.description = description;
        }
    
       
    
        public String toString()
        {
    	return description + " Price: " + getPrice() + 
    	       "  Tax: " + getTax();
        }
    }
      
    public abstract class TaxBasedOnPrice implements Taxable
    {
        private double      price, taxRate;
    
    
        public TaxBasedOnPrice(double price, double taxRate)
        {
    	this.price   = price;
    	this.taxRate = taxRate;
        }
    
        public double getPrice()
        {
    	return price;
        }
    
        public double getTax()
        {
    	return price * taxRate;
        }
     
    }
      

    what will be printed by the following driver?

    public class TaxDriver
    {
        public static void main(String[] args)
        {
    	Book            book;
    	Clothes         clothes;
    	Food            food;
    	Taxable[]       purchases;
    
    	purchases = new Taxable[3];
    
    	book    = new Book("Multimedia Software");
    	clothes = new Clothes("Shirt", "Yellow", 8, 12.00);
    	food    = new Food("Hot Dog",2.00);
    
    	purchases[0] = book;
    	purchases[1] = clothes;
    	purchases[2] = food;
    
    	printMessage(food);
    	printMessage(purchases[0]);
    	printMessage(clothes);
    	printMessage(purchases[1]);
    
    	System.out.println(book.getTax());
    	System.out.println(clothes.getTax());
    	System.out.println(food.getTax());
    
    	for (int i=0; i<purchases.length; i++) 
    	    System.out.println(purchases[i].getTax());
    
            printAll(purchases);
        }
        
    
        public static void printMessage(Food food)
        {
    	System.out.println("I'm food!");
        }
        
    
        public static void printMessage(Taxable taxable)
        {
    	System.out.println("I'm taxable!");
        }
        
    
    
        public static void printAll(Taxable[] items)
        {
    	for (int i=0; i<items.length; i++) 
    	    System.out.println(items[i].getTax());
    
    	for (int i=0; i<items.length; i++) 
    	    System.out.println(items[i].toString());
    
        }
    }
      
  12. Suppose the file LetterGrade contains the following:
    public enum LetterGrade
    {
           F, D, DPLUS, CMINUS, C, CPLUS, BMINUS, B, BPLUS, AMINUS, A;       
    }
        
      
    1. How would you declare a variable of type LetterGrade named eng299?
    2. How would you assign the LetterGrade corresponding to an "A-" to eng299?
    3. How would you compare two LetterGrade objects names eng299 and cs239?
  13. Given the following interfaces and classes:
    public interface Sized
    {
        public int getSize ();
    }
    
    public class Business implements Sized
    {
        public String name;
        public int squareFeet;
    
        public Business ( String name , int squareFeet )
        {	
            this.name = name;
            this.squareFeet = squareFeet;
        }
    
        public int getSize ()
        {
            return squareFeet;
        }
    
        public String toString ()
        {
            return name + " (" + squareFeet + " sq. feet )";
        }
    }
    
    public abstract class Residence implements Sized
    {
        private int squareFeet;
    
        public Residence ( int squareFeet )
        {
            this.squareFeet = squareFeet;
        }
    
        public abstract int payment ();
    
        public int getSize()
        {
            return squareFeet;
        }
    
        public String toString()
        {
            return " Square feet : " + getSize () + " Payment : " + payment ();
        }
    }
    
    public class Apartment extends Residence
    {
        private int rent;
    
        public Apartment (int rent ,int squareFeet)
        {
            super(squareFeet);
            this.rent = rent ;
        }
    
        public int payment()
        {
            return rent;
        }
    }
    
    public class Condo extends Residence
    {
        private int mortgage;
    
        public Condo(int mortgage, int squareFeet)
        {
            super(squareFeet);
            this.mortgage = mortgage ;
        }
    
        public int payment ()
        {
            return mortgage ;
        }
    }
    

    and the following declarations and instantiations:

        Sized[]     stuff ;
        Residence[] homes ;
        Condo[]     development ;
    
        stuff = new Sized[3];
        homes = new Residence[3];
        development = new Condo[3];
    

    indicate whether each of the following statements will: not compile (N), compile but generate an exception at runtime (C+X), or compile and run without generating an exception (C+R).

    stuff[0] = new Business("Martins", 12000);
    stuff[1] = new Residence(2200);
    stuff[2] = new Condo(900, 2200);
    stuff[0] = homes[0];
    homes[0] = stuff[0];
    stuff[0] = development[0];
    homes[0] = new Business("Kroger", 10000);
    homes[1] = new Residence(2200);
    homes[2] = new Apartment(600, 1200);
    development[0] = new Condo(600, 1200);
    stuff[0].name = "Food Lion";
    development[0].squareFeet = 200;
    int p = homes[0].payment();
    int q = development[0].payment();
    int m = development[0].mortgage;
    int s = stuff[0].getSize();
    int t = homes[0].getSize();
    int u = development[0].getSize();
    
  14. Given the interfaces and classes from the previous question, write statements that accomplish each of the following.
    1. Declare variables named downtown and region as ArrayLists of Business objects.
    2. Instantiate downtown.
    3. Declare a variable named hburg as a HashMap in which the keys are String objects and downtown could be a value.
    4. Instantiate hburg.
    5. Add downtown to hburg using the key "Downtown".
    6. Declare a variable named iter as an Iterator of Business objects.
    7. Get the value in hburg with the key "Downtown" and assign it to the variable named region.
    8. Get an Iterator of Business objects from region and assign it to iter.
    9. Declare an integer variable named sum and initialize it to 0.
    10. Use iter to write a loop that adds up the total square footage of all of the Business objects from region. The result must be stored in sum. (Multiple statements.)
  15. The following method must return true when the parameter age is in the closed interval \([13, 19]\) and must return false otherwise. Implement this method in one statement, without using an if statement, a ternary operator, loop, or method call.
      public static boolean isTeenager(int age)
      {
      }
      

Copyright 2021