q5 - Programming Fundamentals for CIS Majors (CIS-3023)...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Programming Fundamentals for CIS Majors (CIS-3023) Quiz 5: 2110l July 2010 Interfaces and Abstract Classes in lava 1. Abstract classes are declared using the keyword, whereas interfaces in java are declared using the keyword. 2. Can a method be declared as abstractfinal? Give a simple example; else, explain why not. 3. Which of the following statements are true about abstract classes? (a) The abstract class is a class with one or more abstract methods that have to be implemented by sub-classes. (b) Abstract classes cannot contain any concrete methods [implementations]. (c) Just like Interfaces, abstract classes cannot have instance variables. ((1) Abstract classes can contain private properties (data/ members). The true statements are: (a) (b) (c) (d) 4. Which of the following statements are true about interfaces? (a) The lava Interface can contain only method declarations and public static final constants and doesn't contain their implementation. (b) Classes which implement an interface must provide method definitions for all the methods present. (c) All variables in an interface are by default "public staticfinal” (i.e., constant, class variables). (d) All the properties [data/ members) in an interface are public. (e) Interfaces support multiple inheritance unlike classes. The true statements are: (a)_ (b) _ (c) __ (d) __ (e) _ 5. To add a new method to an interface, you MUST implement that method in ALL ofthe classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass. The above statement is: True or False (circle the correct choice) 6. Consider the following statements: (a) Interfaces cannot be instantiated. (b) Abstract classes cannot be instantiated. (c) Classes that implement interfaces cannot be instantiated. ((1) Classes that are derived from abstract classes can be instantiated. Which of the above are true? (a) (b) (c) (d) 7. Find and correct the error in the following code fragment. Do not change the any members or method declarations. public class Geometry{ privaze int id; priva:e int :ype; static int geoCoun:er=O; public Geometry() { m } public abstract In:ersects() { m } 8. Find and correct the error in the following interface. public interface Pet{ void play(){ System.out.println(“This animal can play!”); } void bark(); 9. Which method definitions should a class provide to implement the Comparable interface : i. compareTo() ii. compare[) the Comparator interface : i. compareTo() ii. compare[) [Check mark the correct answer) 10. Can a class implement both Comparable and Comparator interfaces? If yes, how? If not, explain Why. 11. Change the following Employee class and make it implement the Comparable interface. Our goal is to sort employees by their empid in default ascending order. class Employee { int empid; string nare; double salary; private Static int numberOfEmp=O; public static void addEmp(){numberOf3mp++;} public Employee(){ this.empid=0; this.name=“"; this.salary=0.0; } public Employee(int eid, string ename, double esal){ :his.empid=eid; :his.name=ename; :his.salary=esal; addEmp(); 12. (a) Now add a new class called EmployeeAgeDescComparator that compares Employees by their name in non-ascending order using the Comparator interface. (b) Then, complete the following sort method in the display driver [class TestEmployee). @JPfithemHmuymimmea. import java.util.*; public class TestEmployee{ public static void main(String args){ Emp e = new Emp(); Sys:em.ou:.prin:ln(e.empid+e.name+e.salary); Emp e1: new ?mp(’0’,"Gates",5500.50); Sys:em.ou:.prin:ln(el.empidnel.namenel.salary); Emp e2= new ?mp('07,"Jane",9999.99); Sys:em.ou:.prin:in(e2.empidneZ.namene2.salary); Empl] emps = {e,e‘_,e2}; Arrays . sort ( for(Object o:emps) System.out.println( ((Employee)o).name ); ...
View Full Document

This note was uploaded on 05/27/2011 for the course CIS 3023 taught by Professor Dobbins during the Summer '08 term at University of Florida.

Page1 / 4

q5 - Programming Fundamentals for CIS Majors (CIS-3023)...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online