Mid-Term study guide chapter 1 Multiple Choice Questions



Download 325.47 Kb.
Page3/4
Date31.01.2017
Size325.47 Kb.
#13210
1   2   3   4

True/False Questions:

1) Java methods can return only primitive types (int, double, boolean, etc).

Answer: False. Explanation: Java methods can also return objects, such as a String.

2) Formal parameters are those that appear in the method call and actual parameters are those that appear in the

method header.

Answer: False. Explanation: The question has the two definitions reversed. Formal parameters are those that appear

in the method header, actual parameters are the parameters in the method call (those being passed to the method).

3) All Java classes must contain a main method which is the first method executed when the Java class is called upon.

Answer: False. Explanation: Only the driver program requires a main method. The driver program is the one that is

first executed in any Java program (except for Applets), but it may call upon other classes as needed, and these other

classes do not need main methods.

4) Java methods can return more than one item if they are modified with the reserved word continue, as in public

continue int foo( ) { … }

Answer: False. Explanation: All Java methods return a single item, whether it is a primitive data type an object, or void.

The reserved word continue is used to exit the remainder of a loop and test the condition again.

5) The following method header definition will result in a syntax error: public void aMethod( );

Answer: True. Explanation: The reason for the syntax error is because it ends with a “;” symbol. It instead needs to be

followed by { } with 0 or more instructions inside of the brackets. An abstract method will end with a “;” but this

header does not define an abstract method.

6) A method defined in a class can access the class’ instance data without needing to pass them as parameters or

declare them as local variables.

Answer: True. Explanation: The instance data are globally available to all of the class’ methods and therefore the

methods do not need to receive them as parameters or declare them locally. If variables of the same name as instance

data were declared locally inside a method then the instance data would be “hidden” in that method because the

references would be to the local variables.

7) The interface of a class is based on those data instances and methods that are declared public.

Answer: True. Explanation: The interface is how an outside agent interacts with the object. Interaction is only

available through those items declared to be public in the class’ definition.

8) Defining formal parameters requires including each parameters type.

Answer: True. Explanation: In order for the compiler to check to see if a method call is correct, the compiler needs to

know the types for the parameters being passed. Therefore, all formal parameters (those defined in the method header)

must include their type. This is one element that makes Java a Strongly Typed language.

9) Every class definition must include a constructor.

Answer: False. Explanation: Java allows classes to be defined without constructors, however, there is a default

constructor that is used in such a case.

10) While multiple objects of the same class can exist, there is only one version of the class.

Answer: True. Explanation: A class is an abstraction, that is, it exists as a definition, but not as a physical instance.

Physical instances are created when an object is instantiated using new. Therefore, there can be many objects of type

String, but only one String class.

11) A class may contain methods but not variable declarations.

Answer: False. A class may contain both methods and variable declarations.

12) A constructor is a method that gets called automatically whenever an object is created, for example with the new

operator.

Answer: True. The constructor is used to initialize an object and it gets called whenever a new object is created from a

particular class.

13) A constructor must have the same name as its class.

Answer: True. A constructor is required to have the same name as the class.

14) A constructor must always return an int.

Answer: False. In fact, a constructor cannot return anything. It has no return value, not even void. When a constructor

is written, no return value should be listed.

15) A class’s instance data are the variables declared in the main method.

Answer: False. A class’s instance data are declared in the class but not inside any method. Variables declared in a

method are local to that method and can only be used in that method. Furthermore, every class need not have a

main method.

16) The methods in a class should always be made public so that those outside the class can use them

Answer: False. Methods that need to be available for use outside a class should be made public, but methods that will

only be used inside the class should be made private.

17) The body of a method may be empty.

Answer: True. The method body may have 0 or more instructions, so it could have 0. An empty method body is simply

{ } with no statements in between.

18) The return statement must be followed a single variable that contains the value to be returned.

Answer: False. The return statement may be following by any expression whose type is the same as the declared return

type in the method header. For example, return x*y+6; is a valid return statement. The statement return; is also

valid for a method that does not return anything (void).

19) The number and types of the actual parameters must match the number and types of the formal parameters.

Answer: True. The names of the corresponding actual and formal parameters may be different but the number and

types of the parameters must match.

20) The different versions of an overloaded method are differentiated by their signatures.

Answer: True. A method’s signature include the number, types, and order of its parameters. With overloaded methods,

the name is the same, but the the methods must differ in their parameters.

21) If a method takes a double as a parameter, you could pass it an int as the actual parameter.

Answer: True. Since converting from an int to a double is a widening conversion, it is done automatically, so there

would be no error.

22) A method defined without a return statement will cause a compile error.

Answer: False. If a method is declared to return void then it doesn’t need a return statement. However, if a method is

declared to return a type other than void, then it must have a return statement.

23) The println method on System.out is overloaded.

Answer: True. The println method includes versions that take a String, int, double, char, and boolean.

24) Method decomposition is the process of creating overloaded versions of a method that do the same thing, but

operate on different data types.

Answer: False. Method decomposition is breaking a complicated method into simpler methods to create a more

understandable design.

25) An object may be made up of other objects.

Answer: True. An aggregate object is an object that has other objects as instance data.


Chapter 5: Enhancing Classes

Multiple Choice:

For questions 1-4, assume x and y are String variables with x = "Hello" and y = null.

1) The result of (x = = y) is

a) true


b) false

c) a syntax error

d) a run-time error

e) x being set to the value null

Answer: b. Explanation: x is a String instantiated to the value "Hello" and y is a String that has not yet been

instantiated, so they are not the same String. (x = = y) is a condition, testing to see if x and y are the same String (that

is, x and y reference the same item in memory), which they don't, so the result is false.
2) The result of x.length( ) + y.length( ) is

a) 0


b) 5

c) 6


d) 10

e) a thrown exception

Answer: e. Explanation: The statement y.length( ) results in a thrown NullPointException because it is not possible to

pass a message to an object that is not currently instantiated (equal to null).


3) If the operation y = "Hello"; is performed, then the result of (x = = y) is

a) true


b) false

c) x and y becoming aliases

d) x being set to the value null

e) a run-time error

Answer: b. Explanation: While x and y now store the same value, they are not the same String, that is, x and y

reference different objects in memory, so the result of the condition (x = = y) is false.


4) If the operation y = x; is performed, then the result of (x = = y) is

a) true


b) false

c) x being set to the value null while y retains the value "Hello"

d) y being set to the value null while x retains the value "Hello"

e) x being set to y, which it is already since y = x; was already performed

Answer: a. Explanation: When y = x; was performed, the two variables are now aliases, that is, they reference the

same thing in memory. So, (x = = y) is now true.

For questions 5-8, consider a class that stores 2 int values. These values can be assigned int values with the messages

set1(x) and set2(x) where x is an int, and these values can be accessed through get1( ) and get2( ). Assume that y and z

are two objects of this class. The following instructions are executed:

y.set1(5);

y.set2(6);

z.set1(3);

z.set2(y.get1( ));

y = z;
5) The statement z.get2( ); will

a) return 5

b) return 6

c) return 3

d) return 0

e) cause a run-time error

Answer: a. Explanation: The statement y.get1( ) returns the value 5, and therefore the statement z.set2(y.get1( )) sets

the second value of z to be 5, so that z.get2( ) returns 5.
6) The statement y.get2( ); will

a) return 5

b) return 6

c) return 3

d) return 0

e) cause a run-time error

Answer: a. Explanation: The statement y = z; causes y and z to be aliases where y.get2( ); returns the same value as

z.get2( );. Since z.set2(y.get1( )); was performed previously, z and y's second value is 5.


7) If the instruction z.set2(y.get1( )); is executed, which of the following is true?

a) (y = = z) is still true

b) (y.get1( ) = = z.get1( )) but (y.get2( ) != z.get2( ))

c) (y.get1( ) = = z.get1( )) and (y.get2( ) = = z.get2( )) but (y != z)

d) (y.get1( ) = = z.get2( )) and (y.get2( ) = = z.get1( )) but (y != z)

e) the statement causes a run-time error

Answer: a. Explanation: Since y=z; was performed previously, y and z are aliases, so that a change to one results in a

change to the other. The statement z.set2(y.get1( )); is essentially the same as z.set2(z.get1( )); or y.set2(y.get1( )); and

in any case, it sets the second value to equal the first for the object which is referenced by both y and z.
8) If the instructions z.set2(5); and y.set1(10); are performed, which of the following is true?

a) (y = = z) is still true

b) (y.get1( ) = = z.get1( )) but (y.get2( ) != z.get2( ))

c) (y.get1( ) = = z.get1( )) and (y.get2( ) = = z.get2( )) but (y != z)

d) (y.get1( ) = = z.get2( )) and (y.get2( ) = = z.get1( )) but (y != z)

e) this statement causes a run-time error

Answer: a. Explanation: Since y=z; was peformed previously, y and z are aliases meaning that they refer to the same

object. The statement z.set2(5); causes a change to the object's second value while y.set1(10); causes a change to the

object's first value but neither change the fact that y and z are aliases.
9) Consider the following swap method. If String x = "Hello" and String y = "Goodbye", then swap(x, y); results

in which of the following?

public void swap(String a, String b)

{

String temp;



temp = a;

a = b;


b = temp;

}

a) x is now "Goodbye" and y is now "Hello"



b) x is now "Goodbye" and y is still "Goodbye", but (x != y)

c) x is still "Hello" and y is now "Hello", but (x != y)

d) x and y are now aliases

e) x and y remain unchanged


Answer: e. Explanation: When x and y are passed to swap, a and x become aliases and b and y become aliases. The

statement temp = a sets temp to be an alias of a and x. The statement a = b sets a to be an alias of b and y, but does not

alter x or temp. Finally, b = temp sets b to be an alias of temp and y, but does not alter y. Therefore, x and y remain the

same.
10) Which of the following methods is a static method? The class in which the method is defined is given in

parentheses following the method name.

a) equals (String)

b) toUpperCase (String)

c) sqrt (Math)

d) format (DecimalFormat)

e) paint (Applet)

Answer: c. Explanation: The Math class defines all of its methods to be static. Invoking Math methods is done by

using Math rather than a variable of type Math. The other methods above are not static.


11) Static methods cannot

a) reference instance data

b) reference non-static instance data

c) reference other objects

d) invoke other static methods

e) invoke non-static methods

Answer: b. Explanation: A static method is a method that is part of the class itself, not an instantiated object, and

therefore the static method is shared among all instantiated objects of the class. Since the static method is shared, it

cannot access non-static instance data because all non-static instance data are specific to instantiated objects. A static

method can access static instance data because, like the method, the instance data is shared among all objects of the

class. A static method can also access parameters passed to it.
For questions 12 – 13, use the following class definition:

public class StaticExample

{

private static int x;



public StaticExample (int y)

{

x = y;



}

public int incr( )

{

x++;


return x;

}

}


12) What is the value of z after the third statement executes below?

StaticExample a = new StaticExample(5);

StaticExample b = new StaticExample(12);

int z = a.incr( );

a) 5

b) 6


c) 12

d) 13


e) none, the code is syntactically invalid because a and b are attempting to share an instance data

Answer: d. Explanation: Since instance data x is shared between a and b, it is first initialized to 5, it is then changed to

12 when b is instantiated, and then it is incremented to 13 when a.incr( ) is performed. So, incr returns the value 13.
13) If there are 4 objects of type StaticExample, how many different instances of x are there?

a) 0


b) 1

c) 3


d) 4

e) There is no way to know since any of the objects might share x, but they do not necessarily share x

Answer: b. Explanation: Because x is a static instance data, it is shared among all objects of the StaticExample class,

and therefore, since at least one object exists, there is exactly one instance of x.


14) An object that refers to part of itself within its own methods can use which of the following reserved words to

denote this relationship?

a) inner

b) i


c) private

d) this


e) static

Answer: d. Explanation: The reserved word this is used so that an object can refer to itself. For instance, if an object

has an instance data x, then this.x refers to the object’s value x. While this is not necessary, it can be useful if a local

variable or parameter is named the same as an instance data. The reserved word this is also used to refer to the class as

a whole, for instance, if the class is going to implement an interface class rather than import an implementation of an

interface class.


15) Which of the following interfaces would be used to implement a class that represents a group (or collection) of

objects?


a) Iterator

b) Speaker

c) Comparable

d) MouseListener

e) KeyListener

Answer: a. Explanation: Iterator is an abstract class allowing the user to extend a given class that implements Iterator

by using the features defined there. These features include being able to store a group of objects and iterate (step)

through them.


16) In order to implement Comparable in a class, what method(s) must be defined in that class?

a) equals

b) compares

c) both lessThan and greaterThan

d) compareTo

e) both compares and equals

Answer: d. Explanation: The Comparable class requires the definition of a compareTo method that will compare two

objects and determine if one is equal to the other, or if one is less than or greater than the other and respond with a

negative int, 0 or a positive int. Since compareTo responds with 0 if the two objects are equal, there is no need to also

define an equals method.


For questions 17-18, consider a class called ChessPiece. This class has two instance data, String type and int player.

The variable type will store “King”, “Queen”, “Bishop”, etc and the int player will store 0 or 1 depending on whose

piece it is. We wish to implement Comparable for the ChessPiece class. Assume that, the current ChessPiece is

compared to a ChessPiece passed as a parameter. Pieces are ordered as follows: “Pawn” is a lesser piece to a “Knight”

and a “Bishop”, “Knight” and “Bishop” are equivalent for this example, both are lesser pieces to a “Rook” which is a

lesser piece to a “Queen” which is a lesser piece to a “King.”


17) Which of the following method headers would properly define the method needed to make this class

Comparable?

a) public boolean comparable(Object cp)

b) public int comparable(Object cp)

c) public int compareTo(Object cp)

d) public int compareTo( )

e) public boolean compareTo(Object cp)

Answer: c. Explanation: To implement Comparable, you must implement a method called compareTo which returns

an int. Further, since this class will compare this ChessPiece to another, we would except the other ChessPiece to be

passed in as a parameter (although compareTo is defined to accept an Object, not a ChessPiece).


18) Which of the following pieces of logic could be used in the method that implements Comparable? Assume

that the method is passed Object a, which is really a ChessPiece. Also assume that ChessPiece has a method called

returnType which returns the type of the given piece. Only one of these answers has correct logic.

a) if (this.type < a.returnType( )) return –1;

b) if (this.type = = a.returnType( )) return 0;

c) if (this.type.equals(a.returnType( )) return 0;

d) if (a.returnType( ).equals(“King”)) return -1;

e) if (a.returnType( ).equals(“Pawn”)) return 1;

Answer: c. Explanation: If the type of this piece and of a are the same type, then they are considered equal and the

method should return 0 to indicate this. Note that this does not cover the case where this piece is a “Knight” and a is a

“Bishop”, so additional code would be required for the “equal to” case. The answer in b is not correct because it

compares two Strings to see if they are the same String, not the same value. The logic in d and e are incorrect because

neither of these takes into account what the current piece is, only what the parameter’s type is. In d, if a is a “King”, it

will be greater than this piece if this piece is not a “King”, but will be equal if this piece is a “King” and similarly in e, it

does not consider if this piece is a “Pawn” or not. Finally, a would give a syntax error because two Strings cannot be

compared using the “<” operator.


19) If s is a String, and s = “no”; is performed, then s

a) stores the String “no”

b) references the memory location where “no” is stored

c) stores the characters ‘n’, ‘o’

d) stores an int value that represents the two characters

e) stores the character ‘n’ and a reference to the memory location where the next character, ‘o’ is stored

Answer: b. Explanation: Strings are objects and all objects in Java are referenced by the variable declared to be an

object. That is, the variable represents the memory location where the object is stored. So, s does not directly store

“no” or ‘n’, ‘o’, but instead stores a memory location where “no” is stored.
20) Assume that you are defining a class and you want to implement an ActionListener. You state

addActionListener(this); in your class’ constructor. What does this mean?

a) The class must import another class which implements ActionListener

b) The class must define the method actionPerformed

c) The class must define the method ActionListener

d) The class must define an inner class called ActionListener

e) The class must define the method actionPerformed in an inner class named ActionListener

Answer: b. Explanation: Since the ActionListener being implemented is being added in this class (that is what the

“this” refers to in addActionListener), then this class must define the abstract methods of the ActionListener class.

There is only one abstract method, actionPerformed. The answer in a is not true, if the class that implements

ActionListener were being imported (assume that the variable al is an instance of this imported class), then the proper

statement would be addActionListener(al);.


21) A Java program can handle an exception in several different ways. Which of the following is not a way that a

Java program could handle an exception?

a) ignore the exception

b) handle the exception where it arose using try and catch statements

c) propagate the exception to another method where it can be handled

d) throw the exception to a pre-defined Exception class to be handled

e) all of the above are ways that a Java program could handle an exception

Answer: d. Explanation: A thrown exception is either caught by the current code if the code is contained inside a try

statement and the appropriate catch statement is implemented, or else it is propagated to the method that invoked the

method that caused the exception and caught there in an appropriate catch statement, or else it continues to be

propagated through the methods in the opposite order that those methods were invoked. This process stops however

once the main method is reached. If not caught there, the exception causes termination of the program (this would be

answer a, the exception was ignored). However, an exception is not thrown to an Exception class.
22) An exception can produce a “call stack trace” which lists

a) the active methods in the order that they were invoked

b) the active methods in the opposite order that they were invoked

TB 52 Lewis/Loftus/Cocking: Chapter 5 Test Bank

c) the values of all instance data of the object where the exception was raised

d) the values of all instance data of the object where the exception was raised and all local variables and

parameters of the method where the exception was raised

e) the name of the exception thrown

Answer: b. Explanation: The call stack trace provides the names of the methods as stored on the run-time stack. The

method names are removed from the stack in the opposite order that they were placed, that is, the earliest method was

placed there first, the next method second, and so forth so that the most recently invoked method is the last item on the

stack, so it is the first one removed. The stack trace then displays all active methods in the opposite order that they were

called (most recent first).

23) In order to have some code throw an exception, you would use which of the following reserved words?

a) throw

b) throws

c) try

d) Throwable



e) goto

Answer: a. Explanation: The reserved word throw is used to throw an exception when the exception is detected, as in:

if (score < 0) throw new IllegalTestScoreException(“Input score ” + score + “ is negative”);
24) JOptionPane is a class that provides GUI

a) dialog boxes

b) buttons

c) output fields

d) panels and frames

e) all of the above

Answer: a. Explanation: the JOptionPane class contains three formats of dialog boxes, an input dialog box that

prompts the user and inputs String text, a message dialog box that outputs a message, and a confirm box that prompts

the user and accepts a Yes or No answer.
25) A listener is an object that

a) implements any type of interface

b) is used to accept any form of input

c) is an inner class to a class that has abstract methods

d) waits for some action from the user

e) uses the InputStreamReader class

Answer: d. Explanation: The listener “listens” for a user action such as a mouse motion, a key entry or an activation of

a GUI object (like a button) and then responds appropriately. Listeners allow us to write programs that interact with the

user whenever the user performs an operation as opposed to merely seeking input from the user at pre-specified times.


Directory: cms -> lib4 -> va01000195 -> centricity -> domain
domain -> Pages 816-820 Truman’s domestic policies after the war—Truman was tasked with reconversion to a peacetime economy and introducing his domestic agenda
domain -> Pioneers of Sports & Entertainment Industries Matching Exercise
domain -> Unit 3: Introduction to Sports & Entertainment Business Principles
domain -> Apush period two key concepts review (1607-1754) This review refers to some examples we did not go over in class – so don’t stress about those!
domain -> The Sound of the Sea Henry Wadsworth Longfellow
domain -> Apush period one key concepts review (1491-1607) This review refers to some examples we did not go over in class – so don’t stress about those!
domain -> The Space Program Notes nasa
domain -> And Then There Were None Name: By Agatha Christie Guided Reading Questions Chapters 1, 2, & 3

Download 325.47 Kb.

Share with your friends:
1   2   3   4




The database is protected by copyright ©ininet.org 2024
send message

    Main page