download instant at

Chapter 2 – Object Oriented Concepts in Java

Multiple Choice

  1. An object is:
  2. A variable that stores values such as numbers and characters.
  3. A basic part of a java program defined by a class.
  4. A definition of variables and methods that relate to each other.
  5. Any thing that is used in a program.

Ans: B

  1. The term encapsulation refers to:
  2. The practice of having each object protect and manage its own information.
  3. The idea that the details of how something works doesn’t matter to the user.
  4. The fact that each object variable in java is really a reference to another memory location containing the instance of that object.
  5. The practice of putting classes together in packages to keep them organized.

Ans: A

  1. The term abstraction refers to:
  2. The practice of having each object protect and manage its own information.
  3. The idea that the details of how something works doesn’t matter to the user.
  4. The fact that each object variable in java is really a reference to another memory location containing the instance of the object.
  5. The process of extending other classes in order to make more detailed objects that share some of the same features.

Ans: B

4. Which of the following best is an example of encapsulation?

a.A small group of programmers work together, learning everything about all the code so that they can all work on any part of the system.

b.A programmer working alone to write all the code for a large system so that others do not have to understand his implementations

c.A programmer writes a piece of code to be integrated into a larger system. The piece of code has everything public so that anyone can manipulate its private data.

d.An object is written where the public methods interface with a larger system without the larger system needing to know about the private implementation of data.

Ans: D

5. When you create a new instance of an object you are:

  1. Declaring a reference variable that could eventually point to object data.
  2. Declaring methods and variables that can be used by classes in code
  3. Creating memory space for variables and creating the object to be used.
  4. Creating a null reference for an object.

Ans: C

  1. When creating a string object how can you call the constructor to initialize a string?
  2. You can call the constructor using the new operator.
  3. You can use a string literal to assign a value to the string object.
  4. You can use any method of the class to call the constructor
  5. Both A and B

Ans: D

  1. The import statement:
  1. Copies code from the other files into the current one in order for them to use the classes and libraries contained there.
  2. Provides a link to the other files so that the compiler can find the classes and libraries contained there.
  3. Moves the code from the library class into the current object in order to be recognized and used.
  4. Calls the constructor for the package specified in order to make the package available for the current program.

Ans: B

  1. The methods and data that will be stored by the class are called its:
  1. Members
  2. Blueprint
  3. Outline
  4. Object

Ans: A

Consider the following method for questions 9 – 10

public void quandary(int x){

number = x * 5;

}

Assume that the method compiles as written.

  1. Which of the following is true about the variable number?
  1. It would need to be imported from another object
  2. It would need to be declared on a scope level above the method in order to be used in this manner.
  3. It needs to be declared public in order to be used in this manner.
  4. It needs to be declared private in order to be used in this manner.

Ans: B

  1. What would be the ramifications of changing the method quandary to private instead of public?
  1. We would not be able to use the number variable without declaring it inside of the method.
  2. We would not be able to have a void return type.
  3. We would not be able to use the quandary method outside of this class.
  4. We would not be able to use the number variable outside of this class.

Ans: C

  1. Which of the following would be an example of method overloading?
  1. A restaurant class is extended by several classes that inherit some similarities, and add some new unique properties as well.
  2. A customer object gets created many times, each one with different instance data.
  3. Each waiter is responsible for his/her own bill and customers
  4. A restaurant object that had two pay methods – one for cash and one for credit.

Ans: D

  1. Consider the following variable declarations – which of the following would cause a null reference if the variable were attempted to be used?
  1. int x;
  2. String x;
  3. String x = new String();
  4. String x = new String(“hi”);

Ans: B

13. Which of the following variables would be best implemented as a static variable?

I. A variable used to count the number of times an object is instanciated.

II. A variable to represent the price (fare) of a single ride within a subway object.

  1. A count of the number of people riding that particular subway train.
  1. I only
  2. II only
  3. III only
  4. I and II only

Ans: D

14. Which of the following would give you the maximum integer value that could be used?

  1. Max_Int
  2. Integer.Max_Int
  3. Integer x = new Integer(0);

x.Max_Int;

d. Both B and C

Ans: D

  1. Which of the following is not a visibility modifier?
  1. Public
  2. Private
  3. Static
  4. Protected

Ans: C

  1. Which of the following visibility modifiers depend upon inheritance structures?
  1. Public
  2. Private
  3. Static
  4. Protected

Ans: D

17. Which of the following would best be implemented by extending ArrayList?

  1. An object is being written to maintain information about a school. As a part of this object you will need to store a list of available classrooms, a list of teachers, and a variety of other data.
  2. An object is being written to maintain student’s grades throughout the year. It will need to have basic information about each student and a list of their grades.
  3. A grocery list item is being created to store a person’s grocery list. It will contain a series of Items (another class) that will need to be stored and retrieved.
  4. A company wishes to create a person object to store all the information about anyone who either works for them or is a client. Information will include name, address, phone number, emergency contact, etc.
  5. None of the above examples are a good extension of ArrayList.

Ans: C

  1. The super keyword is used for:
  2. Calling the constructor of the parent object of the current class.
  3. Calling the constructor of the child object of the current class.
  4. Making a reference to a method or variable of the parent object of the current class.
  5. Both A and C

Ans: D

  1. Which of the following is a true statement about overloaded methods and overridden methods?
  2. An overloaded method cannot be overridden.
  3. Overridden methods will never appear in the same class.
  4. Overloaded methods will never appear in the same class.
  5. Overridden methods cannot be overloaded.

Ans: B

  1. The basic class that is a parent of all other classes by default is:
  2. Class
  3. Java.lang
  4. String
  5. Object

Ans: D

  1. Use the following definitions to answer the question:

public abstract class FirstClass

public class SecondClass extends FirstClass

Which of the following lines of code would not be allowed? (assume both FirstClass and SecondClass have defaut constructors)

  1. FirstClass myClass = new FirstClass();
  2. FirstClass myClass = new SecondClass();
  3. SecondClass myClass = new SecondClass();
  1. I only
  2. II only
  3. III only
  4. I and II

Ans: B

  1. Which method is not defined in the Object class?
  2. toString
  3. equals
  4. compareTo
  5. There are no methods defined in the Object class

Ans: C

  1. What is polymorphism?
  2. The practice where classes can extend multiple other classes through the use of the extends keyword in code.
  3. The ability for java programs to be reused because objects are created to store information in generic situations.
  4. The ability for java to choose the appropriate overridden method for the type of object that was instantiated, not necessarily the type of the reference.
  5. The ability for java to choose the appropriate type in order to cast the variable from one type to another.

Ans: C

  1. The statement that is used to prevent an exception from terminating a program is:
  2. Try
  3. If
  4. While
  5. Exception

Ans: A

  1. What are the two primary types of Throwable objects?
  2. Exception and Error
  3. Thrown and caught
  4. Runtime and compile time
  5. Arithmetic and Linkage

Ans: A

Use the Marine Biology Simulation Case Studyin order to answer the following questions

26. Using the Quick reference for the DarterFish class, how many of Fish’s methods were overridden by Darter fish?

a. two

b. three

c. four

d. five

e. six

Ans: B

27. From a method in DarterFish which of the following method calls would not be allowed? (you may assume appropriate parameters are specified in each case)

a. super.generateChild(..)

b. act()

c. move()

d. super.initialize(..)

e. There are no methods of Fish that cannot be called from DarterFish

Ans: D

28. Which of the following methods are a part of SlowFish to satisfy the inheritance requirements of extending Fish?

a. SlowFish

b. nextLocation

c. move

d. no methods needed to be implemented by SlowFish to satisfy the

requirements

Ans: D

29. Which of the following would not be an acceptable method to retrieve the neighbor to the north of Location loc? (assume loc has be correctly initialized and is within the range of the environment env)

I. loc.NORTH

II. env.getNeighbor(loc, Direction.NORTH)

III. new Location(loc.row() -1, loc.col());

a. I only

b. II only

c. III only

d. I and III only

e. I and II only

Ans: D

30. A new type of fish, AsciiFish, has been introduced into the case study. What would be an appropriate beginning for a constructor for AsciiFish that inherits Fish and creates a blue fish facing in a random direction.

I. public AsciiFish(Environment env, Location loc){

super(env, loc, Direction.West, Color.blue);

II. public AcsiiFish(Location loc, Environment env){

super(loc, env, env.randomDirection(), Color.blue);

III public AsciiFish(Environment env, Location loc){

myColor = Color.blue;

super(env, loc, env.randomDirection());

a. I only

b. II only

c. I and II only

d. II and III only

e. I, II, and III

Ans: A

download instant at