Comprehension | Computer Science homework help

  

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).

Differences 

1) Abstract class is a class which contains one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods. 

 

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)

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. (2 pts) 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) 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.

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.

7. What will happen when you attempt to compile and run the following code. Classes are

saved in separate files.

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

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

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

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?

 

4. ( 6 points)What is displayed on the console when the following program is run?

public class Test {

public static void main(String[] args) {

try {

int[] a = new int[10];

a[10] = 1;

System.out.println(“Welcome to HTML”);

}

catch (Exception ex) {

System.out.println(“There is an exception”);

}

finally {

System.out.println(“The finally clause is executed”);

}

}

}

What is the output?

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?

Class ArrayList<E>

Resizable-array implementation of the List interface. Implements all optional list operations,

and permits all elements, including null. In addition to implementing the List interface, this

class provides methods to manipulate the size of the array that is used internally to store the

list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

Method Summary

Appends the specified element to the end of this list.

Inserts the specified element at the specified position in this list.

Appends all of the elements in the specified Collection to the end of this list,

in the order that they are returned by the specified Collection’s Iterator.

Inserts all of the elements in the specified Collection into this list, starting at

the specified position.

Removes all of the elements from this list.

Returns a shallow copy of this ArrayList instance.

Returns true if this list contains the specified element.

Increases the capacity of this ArrayList instance, if necessary, to ensure that it

can hold at least the number of elements specified by the minimum capacity

argument.

Returns the element at the specified position in this list.

Searches for the first occurence of the given argument, testing for equality

using the equals method.

Returns the index of the last occurrence of the specified object in this list.

Removes the element at the specified position in this list.

Removes a single instance of the specified element from this list, if it is

present (optional operation).

protected

void

Removes from this List all of the elements whose index is between

fromIndex, inclusive and toIndex, exclusive.

Replaces the element at the specified position in this list with the specified

element.

Returns the number of elements in this list.

Returns an array containing all of the elements in this list in the correct order.

<T> T[]

Returns an array containing all of the elements in this list in the correct order;

the runtime type of the returned array is that of the specified array.

Trims the capacity of this ArrayList instance to be the list’s current size.