Finals Quiz - Java 2: True/False
Finals Quiz - Java 2: True/False
True/False
Indicate whether the statement is true or false.
____ 3. If a member of a class is a private method, you can access it outside the class.
____ 4. Members of a class are usually classified into three categories: public, private, and static.
____ 8. A class and its members can be described graphically using Unified Modeling Language (UML) notation.
myObject.setX(10);
myObject.print();
____ 11. The default constructor executes when an object is instantiated and initialized using an existing object.
____ 12. When no object of the class type is instantiated, static data members of the class fail to exist.
____ 13. The abstract data type specifies the logical properties and the implementation details.
Multiple Choice
Identify the choice that best completes the statement or answers the question.
(i)
public class Employee
{
private String name;
private double salary;
private int id;
public Employee()
{
name = "";
salary = 0.0;
id = 0;
}
public Rectangle()
{
length = 0;
width = 0;
}
Which of the following statements correctly instantiates the Rectangle object myRectangle?
public Cylinder ()
{
baseRadius = 0;
height = 0;
}
public Circle()
{
radius = 0.0;
}
public Circle(double r)
{
radius = r;
}
Which of the following statements are valid in Java? (Assume that console is Scanner object initialized
to the standard input device.)
(i)
r = console.nextDouble();
myCircle.area = 3.14 * r * r;
System.out.println(myCircle.area);
(ii)
r = console.nextDouble();
myCircle.set(r);
System.out.println(myCircle.area());
public Rectangle()
{
length = 0;
width = 0;
}
What are the values of the instance variables of newRect after the following statement execute?
newRect.makeCopy(tempRect);
____ 9. Suppose that Book is class with two instance variables-name of type String and numOfBooks of type
int. Which of the following would be appropriate syntax for the heading of a copy constructor for the class
called Book?
a. public Book(String n, int num)
b. public Book()
c. public Book(Book b)
d. public copyBook(String n, int num)
public Secret()
{
x = 0;
z = 1;
}
public Secret(int a)
{
x = a;
}
public Secret(int a, int b)
{
x = a;
y = b;
}
____ 10. How many constructors are present in the class definition in the accompanying figure?
a. zero c. two
b. one d. three
____ 11. ADTs illustrate the principle of ____.
a. information hiding c. encapsulation
b. privacy d. overloading
____ 12. Inheritance is an example of what type of relationship?
a. is-a c. was-a
b. has-a d. had-a
____ 13. Any new class you create from an existing class is called a(n) ____.
a. base class c. derived class
b. superclass d. extended class
____ 14. Suppose there are three classes named Shape, Circle, and Square. What is the most likely relationship
between them?
a. Square is a superclass, and Shape and Circle are subclasses of Square.
b. Shape is a superclass, and Circle and Square are subclasses of Shape.
c. Shape, Circle, and Square are all sibling classes.
d. These three classes cannot be related.
____ 15. What is the correct syntax for defining a new class Parakeet based on the superclass Bird?
a. class Parakeet isa Bird{ }
b. class Bird defines Parakeet{ }
c. class Bird hasa Parakeet{ }
d. class Parakeet extends Bird{ }
____ 16. What type of inheritance does Java support?
a. single inheritance c. multiple inheritance
b. double inheritance d. Java does not support inheritance.
____ 17. If class Dog has a subclass Retriever, which of the following is true?
a. Because of single inheritance, Dog can have no other subclasses.
b. Because of single inheritance, Retriever can extend no other class except Dog.
c. The relationship between these classes implies that Dog “is-a” Retriever.
d. The relationship between these classes implies that Retriever “has-a” Dog.
a. zero c. three
b. two d. four
____ 19. Consider the following class definitions.
(i)
public void print()
{
System.out.print(x + " " + y);
}
(ii)
a. Only (i)
b. Only (ii)
c. Both (i) and (ii)
d. None of these
____ 20. Consider the following class definitions.
(i)
public void set(int a, int b)
{
super.set(a);
y = b;
}
(ii)
a. Only (i)
b. Only (ii)
c. Both (i) and (ii)
d. None of these
Suppose a class Car and its subclass Honda both have a method called speed as part of the class
definition. rentalH refers to an object of the type Honda and the following statements are found in the
code:
rentalH.cost();
super.speed();
____ 21. In the scenario described in the accompanying figure, what will the first statement do?
a. The cost method in Honda will be called.
b. The cost method in Car will be called.
c. Nothing will be called since the code will not compile as a result of multiple definitions of
speed.
d. Overloading will be used to determine which cost method to use.
____ 22. Which operator is used to determine if an object is of a particular class type?
a. The operator new c. The instanceof operator
b. The dot (.) operator d. The + operator