OOP Theory 3 Part 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Exercises

Ex.1) What attributes do all Software objects have?


a. Software objects have RAM, ROM, and processors.
b. Software objects have identity, state, and behavior.
c. Software objects have variables and storage.
d. Software objects are made of computer components.

Ex.2) When you run a Java application by typing java someClass what is
the first method that starts?
a. The main() method of someClass. b. The run() method someClass.
c. The someClass method. d. The applet method.

Ex.3) What is a class?


a. A class is a section of computer memory containing objects.
b. A class is a section of the hard disk reserved for object oriented
programs.

Object Oriented Programming in Java 1


Exercises

c. A class is the part of an object that contains the variables.


d. A class is a description of a kind of object.

Ex.4) What is another name for creating an object?


a. instantiation b. insubordination c. initialization d. inheritance

Ex.5) What are the static variables and methods of a class?


a. Variables and methods that form the foundation of each object of that
class.
b. Variables and methods that belong to all objects in the computer system.
c. Variables and methods that belong only the objects of that class.
d. Variables and methods that are part of the class definition, but not of its
objects.

Object Oriented Programming in Java 2


Exercises

Ex.6) Which of the following invokes the method length() of the object str
and stores the result in val?
a. val = str.length() b. val = length.str()
c. val = length().str d. val = length( str );

Ex.7) How many objects of a given class may be constructed in an


application?
a. Only one per constructor. b. As many as the application asks for.
c. Only one per class. d. One object per variable.

Ex.8) Which of the following is correct?


a. String alpha("Hello Quiz!");
b. . String = "Hello Quiz!";
c. String alpha = new "Hello Quiz!"
d. String alpha = "Hello Quiz!"

Object Oriented Programming in Java 3


Exercises

Ex.9) Examine the following declarations:


int area;
String name;
Which of the following is true?
a. area is a primitive variable, and name is a reference variable.
b. area is a reference variable, and name is a primitive variable.
c. both are primitive variables d. both are reference variables

Ex.10) Examine the following section of code:


int area;
String name;
How many objects have been created?
a. None---there is one object reference variable, but no objects yet.
b. One---there is one object reference variable so there must be one
object.

Object Oriented Programming in Java 4


Exercises

c. Two---one for each variable.


d. Two---one for each type.

Ex.11) Examine the following section of code:


String strA;
String strB = new String("Cheese");
How many objects have been created?
a. zero b. one c. two d. three

Ex.12) What is written to the monitor by the following section of code:


String strA = new String("Roasted ");
String strB = new String("Acorns ");
strA = strB;
System.out.print ( strA );
System.out.println( strB );

Object Oriented Programming in Java 5


Exercises

a. Roasted Acorns b. Acorns Roasted


c. Roasted Roasted d. Acorns Acorns

Ex.13) What is written to the monitor by the following section of code:


String strA = new String("Roasted ");
String strB = new String("Acorns ");
strA = strB;
if ( strA == strB )
system.out.println("Two copies of a reference");
else
system.out.println("Two different references.");
a. Two copies of a reference. b. Two different references.
c. Two copies of a reference. Two different references.
d. Roasted Acorn references.

Object Oriented Programming in Java 6


Exercises

Ex.14) Examine the following section of code:


String strA = new String("Roasted ");
String strB = new String("Acorns ");
strA = strB;
How many objects have been created? After the last statement has
executed, how many objects are now accessible (don't count garbage)?
a. created: 0 now accessible: 0 b. created: 2 now accessible: 1
c. created: 1 now accessible: 1 d. created: 2 now accessible: 2

Ex.15) Examine the following section of code:


String strA = new String("Roasted ");
strA = new String("Toasted ");
strA = new String("Fried ");
strA = new String("Baked ");
strA = new String("Beans ");

Object Oriented Programming in Java 7


Exercises

How many objects (total) are created? After the last statement has
executed, how many objects are now accessible (don't count garbage)?
a. This section of code is incorrect.
b. created: 5 now accessible: 5
c. created: 1 now accessible: 1
d. created: 5 now accessible: 1

Ex.16) Examine the following section of code:


String strA = new String("Roasted ");
String strB = new String("Toasted ");
String strC = new String("Fried ");
String strD = new String("Baked ");
String strE = new String("Beans ");
How many objects (total) are created? After the last statement has

Object Oriented Programming in Java 8


Exercises

executed, how many objects are now accessible (don't count garbage)?
a. This section of code is incorrect.
b. created: 5 now accessible: 5
c. created: 1 now accessible: 1
d. created: 5 now accessible: 1

Ex.17) Examine the following section of code:


String strA = new String("Roasted ");
String strB = strA;
String strC = strA;
String strD = strA;
String strE = strA;
How many objects (total) are created? After the last statement has
executed, how many objects are now accessible (don't count garbage)?

Object Oriented Programming in Java 9


Exercises

a. This section of code is incorrect.


b. created: 5 now accessible: 5
c. created: 1 now accessible: 1
d. created: 5 now accessible: 1

Ex.18) Objects of type String are immutable which means that once you
create them you cannot change them.

Create a class called MyPoint which is also immutable and which has the
following instance variables and methods:

- x coordinate
- y coordinate
- print method to output the point

Object Oriented Programming in Java 10


More on Strings

A string variable/object is immutable which means that once a string is


created, it cannot be changed. Each time a string is modified, a new string
is created. The following example illustrates:

String str=“Yes”;
Yes
str

Yes
str=“No”; str

No

Object Oriented Programming in Java 11


More on Strings 2
Java also provides a StringBuffer class which is not immutable and
can be more efficient for code that does a lot of string manipulation. A
string buffer implements a mutable sequence of characters. A string buffer
is like a String, but can be modified.

StringBuffer sb=new StringBuffer(“LLM");


sb.setCharAt(0,‘K'); //”KLM”

You couldn’t do this with a string object. There is also another useful
class called StrignTokenizer which can be used to break strings into
smaller strings:
BufferedReader br=new BufferedReader (new
FileReader("test.txt"));

String line=br.readLine();
StringTokenizer st=new StringTokenizer(line);

Procedural Programming in Java 12


More on Strings 3
while(st.hasMoreTokens())
System.out.println(st.nextToken());
The previous code fragment would open a file (test.txt), read the first line
and using a StringTokenizer object, the line would be broken into
several smaller strings.
The default delimiter is a space but you can specify any other character as
a delimiter (or even specify multiple delimiter characters in a string):
StringTokenizer st=new StringTokenizer(line,“ ,.\n”);

This class can be useful when retrieving data from delimited text files. It
relieves the programmer from having to do the extraction process
manually. For more information on the JDK documentation.

Procedural Programming in Java 13

You might also like