Expert Answers

Part I: Comprehension of java programming concepts (Total 16):

Answer the following questions in a paragraph using full sentences.

  1. Define the term inheritance in java and give an example.(4 points).

2. List two differences and one similarity between an interface and an abstract class (6
Points).

3. Describe the Java Collections Framework. Explain the differences between the Set, List and Map interfaces referring to the elements/objects that they store. (6 points)

CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 3 of 15
Part II Fill in the blanks (Total 20 points):
Complete each sentence or statement with the missing word(s)
Type in the missing word(s) in the given box, , to complete each statement.
1. (2 pts)The constructor of a derived class can access the constructor of its base class by using the java reserved word(keyword)

2. ___________binding is when the meaning of a method invocation is not bound to it until the program is run.

3. (2 pts) Adding the _______modifier to a method heading prevents derived classes from overriding the method.

4. (2 pts) A(n) specifies headings for methods that must be defined by an __________ class.
5. ( 2 pts) In Java, every class is a descendant of the _______ class
6. ( 2 pts) Object-oriented programming allows you to derive new classes from existing classes. This is called __________.
7. (2 pts) ______ may be used to convert an object of one class type to another within the
inheritance hierarchy.
8. (1 point each) A(n)________ class can be extended with the condition of providing
implementations for all of the methods of the class.
9. (2 pts) A(n)container is a kind of component that can have other components added to it.
10. ( 2 pts) You can refer to any member of the current object from within an instance method or a constructor by using the java _______ keyword .

Part III: Multiple Choice Questions(Total 28 pts)
Identify the letter of the choice that best completes the statement or answers the question.
Type the letter “X” in the given box, , to indicate the selection. (2 points each).
1.When comparing two objects to see if they have the same contents, one should
a. use the = operator
b. use the == operator
c. define an equals method in the class and use it
d. use the equals method to see if they have the same variable name

2. The term overloaded refers to
a. an error where more than one method matches a method call
b. two or more methods in the same class that have the same name
c. method calls where there are two many actual parameters
d. two or more methods in different classes that have the same name

3. Suppose Child is a derived class of Parent and doStuff() is a private method in the class
Parent. Which of the following is true?
a. doStuff() may be used in the definition of methods in Child.
b. doStuff() may not be used in the definition of methods in Child.
c. In some, but not all cases, doStuff() may be used in the definition of methods in Child..
d. none of the above

4. Which statement best describes overriding a method?
a. Methods in a base class and derived class have the same name but different visibility
modifiers.
b. Methods in a base class and derived class have the same name and have the same
number and types of parameters.
c. Methods in a base class and derived class have the same name but have different return
types.
d. Methods in a base class and derived class have the same name but different number or
types of parameters.
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu

5. Assume that Sub1 and Sub2 are both subclasses of class Super.
Given the declarations:
Super super = new Super();
Sub1 sub1 = new Sub1();
Sub2 sub2 = new Sub2();
Which statement best describes the result of attempting to compile and execute the following
statement:
super = sub1;
a. Compiles and definitely legal at runtime
b. Does not compile
c. Compiles and may be illegal at runtime

6. For the following code:
class Super
{ int index = 5;
public void printVal()
{ System.out.println( “Super” );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( “Sub” );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Super();
System.out.print( sup.index + “,” );
sup.printVal();
}
}
What will be printed to standard output?
a. The code will not compile.
b. The code compiles and “5, Super” is printed to standard output.
c. The code compiles and “5, Sub” is printed to standard output.
d. The code compiles and “2, Super” is printed to standard output.
e. The code compiles and “2, Sub” is printed to standard output.
f. The code compiles, but throws an exception.

CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 6 of 15
7. What will happen when you attempt to compile and run the following code. Classes are
saved in separate files.
public class Base{
private void amethod(int iBase){
System.out.println(“Base.amethod”);
}
}
public class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}
public void amethod(int iOver){
System.out.println(“Over.amethod”);
}
}
a. Compile time error complaining that Base.amethod is private
b. Runtime error complaining that Base.amethod is private
c. Output of “Base.amethod”
d. Output of “Over.amethod”

8. Which of the following statements is true?
a. An interface can only contain method(s) and not variables
b. Interfaces cannot have constructors
c. A class may extend only one other class and implement only one interface
d. A class accesses an interface via the keyword uses
Ans b

9. An abstract class
a. is a class which cannot be instantiated
b. is a class which has no methods
c. is a class which has only abstract methods
d. is a class which has only overridden methods
CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu

10) Which of the following statements is false?
a. If a class has any abstract methods it must be declared abstract itself.
b. All methods in an abstract class must be declared as abstract
c. When applied to a class, the final modifier means it cannot be sub-classed
d. abstract and interface are Java keywords

11. A catch-block
a. is a special method used in exception handling
b. allows the user to specify what will happen next in the program
c. is not allowed in the same method as a try-block
d. contains code that is executed when an exception occurs

12. Given the following code
import java.io.*;
public class Ppvg{
public static void main(String argv[]){
Ppvg p = new Ppvg();
p.fliton();
}
public int fliton(){
try{
FileInputStream din = new FileInputStream(“Ppvg.java”);
din.read();
}catch(IOException ioe){
System.out.println(“flytwick”);
return 99;
}finally{
System.out.println(“fliton”);
}
return -1;
}
}
Assuming the file Ppvg.java is available to be read correctly which of the following
statements is true if you try to compile and run the program?
a. The program will run and output only “flytwick”
b. The program will run and output only “fliton”
c. The program will run and output both “fliton” and “flytwick”
d. An error will occur at compile time because the method fliton attempts to return two
values

CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
13. What is the default layout manager for a JPanel?
a. FlowLayout
b. BorderLayout
c. GirdLayout
d. No default layout manager

14.Suppose you want to display a text in multiple lines, which component should you use?
a. JButton
b. JLabel
c. JTextField
d. JTextArea

CMIS 242 2009-10, Spring, Session 1 Nafia Gungordu
Page 9 of 15
Part IV: Comprehension of java programming code (Total 36pts. ):
Please indicate what is displayed on the console(standard output) when the following program/code is
executed? Please note that all the given codes can be compiled and executed.
1. (5 Points) Consider the following classes saved separately:
public class Base
{
private int num = 10;
public void print()
{
System.out.println(num);
}
public void setNum(int j)
{
num = j;
}
}
public class Derived extends Base
{
public void setNum(int j)
{
super.setNum(j + 10);
}
}
public class BaseDriver{
public static void main(String[] args)
{
Derived d = new Derived();
d.setNum(20);
d.print();
}
}
What is the output?

2. (5 points)Consider the following classes saved in separate files:
public class Base
{
public void print(Base b)
{
System.out.println(“Base”);
}
}
public class Derived extends Base
{
public void print(Derived b)
{
System.out.println(“Derived”);
}
}
public class BaseDriver{
public static void main(String[] args)
{
Base d1 = new Base();
Derived d2 = new Derived();
d1.print(d1);
d1.print(d2);
}
}
What is the output?

3. (6 points)Consider the code below:
void myMethod()
{ try
{
fragile();
}
catch( NullPointerException npex )
{
System.out.println( “NullPointerException thrown ” );
}
catch( Exception ex )
{
System.out.println( “Exception thrown ” );
}
finally
{
System.out.println( “Done with exceptions ” );
}
System.out.println( “myMethod is done” );
}
What is printed to standard output if fragile() throws an Exception called IllegalArgumentException?

5. (6 pts)Design a java interface called Priority that includes two methods: incrementPriority (void method)and getPriority(returns an integer). The interface should define a way to establish numeric priority among a set of objects.

6. ( 8 points)What is the output of the following program? Please view/use the “References”
section at the end of the exam.
import java.util.*;
public class Exercise{
public static void main(String[] arg){
ArrayList<String> list = new ArrayList<String>();
list.add(0,”One”);
list.add(1,”Two”);
list.add(2,”Three”);
list.add(3,”Four”);
list.add(4,”Five”);
System.out.println(list);
list.remove( 3 );
System.out.println(list);
System.out.println(list.get(2));
list.set(1,”Hello”);
System.out.println(list);
}
}
What is the output?