Multiple Choice Questions: 1. The behavior of an object is defined by xld14276

VIEWS: 304 PAGES: 22

More Info
									Multiple
Choice
Questions:




      1.
      The
behavior
of
an
object
is
defined
by
the
object's



a)
    instance
data

b)
    constructor

c)
    visibility
modifiers


d)
    methods

e)
    all
of
the
above












Answer:

d.

Explanation:

The
methods
dictate
how
the
object
reacts
when
it
is

passed
messages.

Each
message
is
implemented
as
a
method,
and
the
method
is
the

code
that
executes
when
the
message
is
passed.

The
constructor
is
one
of
these

methods
but
all
of
the
methods
combine
dictate
the
behavior.

The
visibility

modifiers
do
impact
the
object's
performance
indirectly.













     2.
    Which
of
the
following
reserved
words
in
Java
is
used
to
create




     an
instance
of
a
class?



a.
   class

b.
   
public

c.
   public
or
private,
either
could
be
used

d.
   import

e.
   new













Answer:

e.

Explanation:

The
reserved
word
“new”
is
used
to
instantiate
an
object,

that
is,
to
create
an
instance
of
a
class.

The
statement
new
is
followed
by
the
name

of
the
class.

This
calls
the
class'
constructor.

Example:

Car
x
=
new
Car(
);
will

create
a
new
instance
of
a
Car
and
set
the
variable
x
to
it.











3.
    In
order
to
preserve
encapsulation
of
an
object,
we
would
do
all
of
the

following
except
for
which

 one?



a)
    Make
the
instance
data
private

b)

   Define
the
methods
in
the
class
to
access
and
manipulate
the








    


      
        instance
data

c)

   Make
the
methods
of
the
class
public

d)

   Make
the
class
final

e)
    
All
of
the
above
preserve
encapsulation













Answer:

d.

Explanation:

Encapsulation
means
that
the
class
contains
both
the
data

and
the
methods
needed
to
manipulate
the
data.

In
order
to
preserve
encapsulation

properly,
the
instance
data
should
not
be
directly
accessible
from
outside
of
the

classes,
so
the
instance
data
are
made
private
and
methods
are
defined
to
access
and

manipulate
the
instance
data.

Further,
the
methods
to
access
and
manipulate
the

instance
data
are
made
public
so
that
other
classes
can
use
the
object.

The
reserved

word
“final”
is
usedto
control
inheritance
and
has
nothing
to
do
with
encapsulation.













4.


   A
variable
whose
scope
is
restricted
to
the
method
where
it
was



       declared
is
known
as
a(n)



a)
     parameter

b)
     global
variable

c)
     local
variable

d)
     public
instance
data

e)
     private
instance
data














Answer:

c.

Explanation:

Local
variables
are
those
that
are
“local”
to
the
method
in

which
they
have
been
declared,
that
is,
they
are
accessible
only
inside
that
method.


Global
variables
are
those
that
are
accessible
from
anywhere,
while
parameters
are

the
variables
passed
into
a
method.

Instance
data
can
be
thought
of
as
global

variables
for
an
entire
object.










5.
   A
class'
constructor
usually
defines



a)
   how
an
object
is
initialized

b)
   how
an
object
is
interfaced

c)
   the
number
of
instance
data
in
the
class

d)
   the
number
of
methods
in
the
class

e)
   if
the
instance
data
are
accessible
outside
of
the
object
directly












Answer:

a.

Explanation:

The
constructor
should
be
used
to
“construct”
the
object,

that
is,
to
set
up
the
initial
values
of
the
instance
data.

This
is
not
essential,
but
is

typically
done.

The
interface
of
an
object
is
dictated
by
the
visibility
modifiers
used

on
the
instance
data
and
methods.












6.
   Instance
data
for
a
Java
class




a)
   are
limited
to
primitive
types
(e.g.,
int,
double,
char)

b)
   are
limited
to
Strings

c)
   are
limited
to
objects(e.g.,
Strings,
classes
defined
by
other

 
     


     programmers)

d)
   may
be
primitive
types
or
objects,
but
objects
must
be
defined
to
be



     private

e)
   may
be
primitive
types
or
objects













Answer:

e.

Explanation:

The
instance
data
are
the
entities
that
make
up
the
class

and
may
be
any
type
available
whether
primitive
or
object,
and
may
be
public
or

private.

By
using
objects
as
instance
data,
it
permits
the
class
to
be
built
upon
other

classes.

This
relationship
where
a
class
has
instance
data
that
are
other
classes
is

known
as
a
has‐a
relationship.













The
Coin
class,
as
defined
in
Chapter
4,
consists
of
a
constructor,
and
methods
flip,

isHeads
and
toString.





The
method
isHeads
returns
true
if
the
last
flip
was
a
Heads,
and
false
if
the
last
flip

was
a
Tails.





The
toString
method
returns
a
String
equal
to
“Heads”
or
“Tails”
depending
on
the

result
of
the
last
flip.




7.
     A
set
of
code
has
already
instantiated
c
to
be
a
Coin
and
has
input
a
String

guess,
from
the
user
asking
whether
the
user
guesses
that
a
coin
flip
will
result
in

“Heads”
or
“Tails”.

Which
of
the
following
sets
of
code
will
perform
the
coin
flip
and

see
if
the
user's
guess
was
right
or
wrong?



a)
     c.flip(
);


       if(c.isHeads(
).equals(guess))
System.out.println("User
is
correct");



b)
     if(c.flip(
).equals(guess))
System.out.println("User
is
correct");



c)
     if(c.isHeads(
).equals(guess))
System.out.println("User
is
correct");



d)
     c.flip(
);


       if(c.toString(
).equals(guess))
System.out.println("User
is
correct");



e)
     c.flip(
).toString(
);


       if(c.equals(guess))
System.out.println("User
is
correct");















Answer:

d.

Explanation:

c.flip(
)
must
be
performed
first
to
get
a
Coin
flip.

Next,

the
user's
guess
is
compared
against
the
value
of
the
Coin's
flip.

The
Coin
c
stores

the
result
as
an
int
and
the
user's
guess
is
a
String.

Using
getFace(
)
returns
the
int

value
but
using
the
toString
method
will
return
the
String
“Heads”
or
“Tails”.

So,
the

code
compares
c.toString(
)
with
the
user's
guess
using
the
String
method
equals.


















The
Die
class
from
chapter
4
has
two
constructors
defined
as
follows.
Assume

MIN_FACES
is
an
int
equal
to
4.



public
Die(
)

 
      
     
     

{
     
        
      
     
     


      numFaces
=
6;
        
     
     
       


      faceValue
=
1;

}
     
        
      
     
     
     



public
Die(int
faces)

{




      if
(faces
<
MIN_FACES)



      
        numFaces
=
6;


      else



      
        numFaces
=
faces;


      faceValue
=
1;

      
     
     

}





8.
    The
instruction
Die
d
=
new
Die(10);
results
in



a)
    The
Die
d
having
numFaces
=
6
and
faceValue
=
1

b)
    The
Die
d
having
numFaces
=
10
and
faceValue
=
1

c)
    The
Die
d
having
numFaces
=
10
and
faceValue
=
10

d)
    The
Die
d
having
numFaces
=
6
and
faceValue
=
10

e)
    A
syntax
error










Answer:

b.

Explanation:

Since
an
int
parameter
is
passed
to
the
constructor,
the

second
constructor
is
executed,
which
sets
numFaces
=
10
(since
numFaces
>=

MIN_FACES)
and
faceValue
=
1.













The
Die
class
from
chapter
4
has
two
constructors
defined
as
follows.
Assume

MIN_FACES
is
an
int
equal
to
4.



public
Die(
)

 
      
       
    public
Die(int
faces)

{
     
        
      
       
    
      {


      numFaces
=
6;
          
    
      
       if(faces
<
MIN_FACES)

    


      
        faceValue
=
1;

    
      
       
       numFaces
=
6;


      
        
      
       
    
      
       else


}
     
        
      
       
    
      
       
       numFaces
=
faces;


      
        
      
       
    
      
       faceValue
=
1;


      
        
      
       
    
      }





9.
    The
instruction
Die
d
=
new
Die(10,
0);
results
in



a)
    The
Die
d
having
numFaces
=
6
and
faceValue
=
1

b)
    The
Die
d
having
numFaces
=
10
and
faceValue
=
1

c)
    The
Die
d
having
numFaces
=
10
and
faceValue
=
10

d)
    The
Die
d
having
numFaces
=
6
and
faceValue
=
10

e)
    A
syntax
error












Answer:

e.

Explanation:

The
Die
class
has
two
constructors,
one
that
receives
no

parameters
and
one
that
receives
a
single
int
parameter.

The
instruction
above
calls

the
Die
constructor
with
2
int
parameters.

Since
no
constructor
matches
this

number
of
parameters
exists,
a
syntax
error
occurs.












public
class
Swapper


      {
     private
int
x;


      
      private
String
y;


      
      public
int
z;


      
      


      
      public
Swapper(int
a,
String
b,
int
c)


      
      {
      x
=
a;


      
      
       y
=
b;


      
      
       z
=
c;


      
      }




      
      public
String
swap(
)


      
      {
      int
temp
=
x;




      
      
       x
=
z;


      
      
       z
=
temp;


      
      
       return
y;


      
      }


      
      public
String
toString(
)


      
      {


      
      
       if
(x
<
z)



      
      
       return
y;


      
      
       else



      
      
       return
""
+
x
+
z;


      
      }


      }






10.
 If
the
instruction

 
          
    
        
       
 
     
       


      
      
       
        
     
    Swapper
s
=
new
Swapper(0,
"hello",
0);


      


      is
executed
followed
by



      
      
       
        
     
    s.toString(
);



      what
value
is
returned
from
s.toString(
)?



a)
    "hello"

b)
    “helllo00”

c)
    “00”

d)
    “0”

e)
    0







Answer:

c.

Explanation:

The
toString
method
compares
x
and
z,
and
if
x
<
y
it

returns
the
String
y.

In
this
case,
x
==
z,
so
the
else
clause
is
executed,
and
the
String

of
""
+
x
+
z
is
returned.

This
is
the
String
"00".















11.
   Consider
a
method
defined
with
the
header:





      
     public
void
doublefoo(double
x)


      Which
of
the
following
method
calls
is
legal?



a)
    doublefoo(0);

b)
    doublefoo(0.555);

c)
    doublefoo(0.1
+
0.2);

d)
    doublefoo(0.1,
0.2);

e)
    all
of
the
above
are
legal
except
for
d













Answer:

e.

Explanation:

In
the
case
of
a,
the
value
0
(an
int)
is
widened
to
a
double.


In
the
case
of
c,
the
addition
is
performed
yielding
0.3
and
then
doublefoo
is
called.


The
parameter
list
in
d
is
illegal
since
it
contains
two
double
parameters
instead
of

1.






								
To top