______represents an entity in the real world that can be distinctly identified.

A. A class

B. An object

C. A method

D. A data field

8.2 ______is a construct that defines objects of the same type.

A. A class

B. An object

C. A method

D. A data field

8.3 An object is an instance of a ______.

A. program

B. class

C. method

D. data

8.4 The keyword ______is required to declare a class.

A. public

B. private

C. class

D. All of the above.

Section 8.4 Constructing Objects Using Constructors

8.5 ______is invoked to create an object.

A. A constructor

B. The main method

C. A method with a return type

D. A method with the void return type

8.6 Which of the following statements are true?

A. A default constructor is provided automatically if no constructors are explicitly declared in the class.

B. At least one constructor must always be defined explicitly.

C. Every class has a default constructor.

D. The default constructor is a no-arg constructor.

8.7 Which of the following statements are true?

A. Multiple constructors can be defined in a class.

B. Constructors do not have a return type, not even void.

C. Constructors must have the same name as the class itself.

D. Constructors are invoked using the new operator when an object is created.

8.8 Analyze the following code:
publicclassTest{
public static void main(String[] args) {
A a = new A();
a.print();
}
}
classA{
String s;
A(String newS) {
s = newS;
}
void print() {
System.out.println(s);
}
}

A. The program has a compilation error because class A is not a public class.

B. The program has a compilation error because class A does not have a no-arg constructor.

C. The program compiles and runs fine and prints nothing.

D. The program would compile and run if you change A a = new A() to A a = new A("5").

IN TERM 2

======

variety

======

TASK:

Can you identify from the followings Which are synonymous?

instance

class

data member

method

this

object

constructor

user-defined type

declaration

TASK II:

What is the difference between System.out.println() and System.out.print()?

TASK III:

Can we have more than one class per file in Java? If there are any restrictions, what are they?

TASK IV:

Write the output generated by the following code segments.

1. for (int i=0; i<5; i++) {

switch (i) {

case 0:

System.out.println("m");

break;

case 1:

case 2:

System.out.println("o");

break;

case 3:

System.out.println("d");

case 4:

System.out.println("!");

break;

}

}

Define the following terms using 1-3 sentences or a formula

(6 pts each)

:

(a)

Inheritance:

inheritance (in Java) is when a class extends another class, and

consequently includes all the visible methods and variables from the other class as

part of its own interface.

(b)

Polymorphism:

the ability to create a variable, object, function, operator, or object

that has more than one form; also defined as the ability for values of different data

types to be handled using a uniform interface.

(c)

Superclass:

the parent of a subclass. Provides functionality, in the form of methods

and variables, that are inherited by a subclass.

(d)

Subclass:

a class which extends a superclass. Inherits functionality, in the form of

methods and variables, from the superclass.

(e)

Abstract Class

: a class which declares an abstract method – a method that lacks an

implementation (body). Abstract classes cannot be implemented directly. They must

be first extended by subclasses, which provide implementations of the abstract

methods.

What kind of class cannot be instantiated as an

object

? Why?

Abstract classes cannot be instantiated because they may have one or more methods that

lack an implementation.

b.

What is the highest-level superclass in the Java language? What does this mean?

java.lang.Object. This means that all instances of all classes inherit the methods of

Object, and can be stored in an Object reference.

c.

What is a

constructor

and how is it used in Java?

A constructor is a method that is used to create a new instance of a class. The constructor

of FooClass is invoked by "new FooClass(...args...)"

d.

Explain

the primary mechanism for code re-use in object-oriented programming?

Inheritance provides code re-use because it allows all specializations of a class to share a

single implementation of methods which are defined in the superclass.

e.

What is an

interface

in Java, and how is it used?

An interface is a list of methods that a class which implements the interface must

implement. Classes may implement multiple interfaces. This is useful because it allows

an object to be stored in a variable whose type is any of the interfaces it implements,

which is typically used for message passing and event handling (callbacks)

======

MULTIPLE CHOICES Qs

======

Which of the following best de

#

nes a

data type

? Circle the

single best

answer.

(i)

A set of values.

(ii)

A set of operations.

(iii)

A sequence of 0s and 1s.

(iv)

A set of values and operations on those values.

======

PROGRAMS

======

TASK:

Create a 'Foo' class with two data members; 'x' and 'y', a default constaructor, a constructor that takes in two parameters, a method totalXY which returns the sum of data members x and y, and a main that creates two 'Foo' instances, then prints the total of calling each of their totalXY methods(use a loop).