0% found this document useful (0 votes)
54 views

Finals Quiz - Java 2: True/False

The document contains a quiz on Java classes with true/false and multiple choice questions. Some key points: 1. The questions cover topics like class components, modifiers, access levels, constructors, instantiation, and relationships between classes. 2. Multiple choice questions test understanding of valid class definitions, constructor names, object instantiation syntax, and legal method calls. 3. True/false questions assess knowledge of class member access rules, static vs instance methods, and default constructor behavior.

Uploaded by

abigail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Finals Quiz - Java 2: True/False

The document contains a quiz on Java classes with true/false and multiple choice questions. Some key points: 1. The questions cover topics like class components, modifiers, access levels, constructors, instantiation, and relationships between classes. 2. Multiple choice questions test understanding of valid class definitions, constructor names, object instantiation syntax, and legal method calls. 3. True/false questions assess knowledge of class member access rules, static vs instance methods, and default constructor behavior.

Uploaded by

abigail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

FINALS QUIZ – JAVA 2

True/False
Indicate whether the statement is true or false.

____ 1. The components of a class are called fields.

____ 2. Modifiers are used to alter the behavior of the class.

____ 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.

____ 5. The methods of a class must be public.

____ 6. The non-static methods of a class are called instance methods.

____ 7. Constructors are called like any other method.

____ 8. A class and its members can be described graphically using Unified Modeling Language (UML) notation.

____ 9. Given the declaration

public class MyClass


{
private int x;

public void print()


{
System.out.println("x = " + x);
}

private void setX(int y)


{
x = y;
}
}

MyClass myObject = new MyClass();

The following statement is legal.

myObject.setX(10);

____ 10. Given the declaration

public class MyClass


{
private int x;
public void print()
{
System.out.println("x = " + x);
}

private void setX(int y)


{
x = y;
}
}

MyClass myObject = new MyClass();

The following statement is legal.

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.

____ 14. A subclass cannot directly access public members of a superclass.

____ 15. Composition is a “has-a” relationship.

Multiple Choice
Identify the choice that best completes the statement or answers the question.

____ 1. Class members consist of all of the following EXCEPT ____.


a. named constants c. pre-defined methods
b. variable declarations d. methods

____ 2. Which of the following class definitions is correct in Java?

(i)
public class Employee
{
private String name;
private double salary;
private int id;

public Employee()
{
name = "";
salary = 0.0;
id = 0;
}

public Employee(String n, double s, int i)


{
name = n;
salary = s;
id = i;
}

public void print()


{
System.out.println(name + " " + id + " " + salary);
}
}
(ii)
public class Employee
{
private String name;
private double salary;
private int id;

public void Employee()


{
name = "";
salary = 0.0;
id = 0;
}

public void Employee(String n, double s, int i)


{
name = n;
salary = s;
id = i;
}

public void print()


{
System.out.println(name + " " + id + " " + salary);
}
}

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. Neither is correct
____ 3. Constructors have the same name as the ____.
a. member methods c. class
b. data members d. package

____ 4. Consider the following class definition.

public class Rectangle


{
private double length;
private double width;

public Rectangle()
{
length = 0;
width = 0;
}

public Rectangle(double l, double w)


{
length = l;
width = w;
}

public void set(double l, double w)


{
length = l;
width = w;
}

public void print()


{
System.out.println(length + " " + width);
}

public double area()


{
return length * width;
}

public double perimeter()


{
return 2 * length + 2 * width;
}
}

Which of the following statements correctly instantiates the Rectangle object myRectangle?

(i) myRectangle = new Rectangle(12.5, 6);


(ii) Rectangle myRectangle = new Rectangle(12.5, 6);
(iii) class Rectangle myRectangle = new Rectangle(12.5, 6);

a. Only (i) c. Only (iii)


b. Only (ii) d. Both (ii) and (iii)

____ 5. Consider the following class definition.

public class Cylinder


{
private double baseRadius;
private double height;

public Cylinder ()
{
baseRadius = 0;
height = 0;
}

public Cylinder (double l, double h)


{
baseRadius = l;
height = h;
}

public void set(double r, double h)


{
baseRadius = r;
height = h;
}

public String toString()


{
return (baseRadius + " " + height);
}

public double SurfaceArea()


{
return 2 * 3.14 * baseRadius * height;
}

public double volume()


{
return 3.14 * baseRadius * baseRadius * height;
}
}

Suppose that you have the following declaration.

Cylinder cyl = new Cylinder(1.5, 10);

Which of the following sets of statements are valid in Java?


(i)
cyl.surfaceArea();
cyl.volume();
cyl.print();
(ii)
print(cyl.surfaceArea);
print(cyl.volume());

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these
____ 6. Consider the following statements.

public class Circle


{
private double radius;

public Circle()
{
radius = 0.0;
}

public Circle(double r)
{
radius = r;
}

public void set(double r)


{
radius = r;
}

public void print()


{
System.out.println(radius + " " + area + " "
+ circumference);
}

public double area()


{
return 3.14 * radius * radius;
}

public double circumference()


{
return 2 * 3.14 * radius;
}

Circle myCircle = new Circle();


double 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());

a. Only (i) c. Both (i) and (ii)


b. Only (ii) d. None of these
____ 7. Consider the following statements.

public class Rectangle


{
private double length;
private double width;

public Rectangle()
{
length = 0;
width = 0;
}

public Rectangle(double l, double w)


{
length = l;
width = w;
}

public void set(double l, double w)


{
length = l;
width = w;
}

public void print()


{
System.out.println("Length = " + length
+ "; Width = " + width + "\n" +
+ " Area = " + area()
+ "; Perimeter = " + perimeter());
}

public double area()


{
return length * width;
}

public void perimeter()


{
return 2 * length + 2 * width;
}

public void makeCopy(Rectangle otherRect)


{
length = otherRect.length;
width = otherRect.width
}
}

Rectangle tempRect = new Rectangle(14, 10);


Rectangle newRect = new Rectangle(9, 5);

What are the values of the instance variables of newRect after the following statement execute?

newRect.makeCopy(tempRect);

a. length = 14; width = 10


b. length = 9; width = 5
c. length = 14; width = 5
d. None of these

____ 8. What is the default definition of the method toString?


a. There is no default definition; you must define the method yourself.
b. It creates a string that is the name of the object’s class, followed by the hash code of the
object.
c. It creates a string that is the name of the program.
d. It creates a string that is the name of the package, followed by the name of the class,
followed by the hash of the object.

____ 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 class Secret


{
private int x;
private static int y;
public static int count;
public int z;

public Secret()
{
x = 0;
z = 1;
}

public Secret(int a)
{
x = a;
}
public Secret(int a, int b)
{
x = a;
y = b;
}

public String toString()


{
return ("x = " + x + ", y = " + y + ",
count = " + count);
}

public static void incrementY()


{
y++;
}
}

____ 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.

____ 18. Consider the following class definitions.

public class BClass


{
private int x;
private double y;

public void print() { }


}

public class DClass extends BClass


{
private int a;
private int b;

public void print() { }


}

Suppose that you have the following statement.

DClass dObject = new DClass();

How many instance variables does dObject have?

a. zero c. three
b. two d. four
____ 19. Consider the following class definitions.

public class BClass


{
private int x;

public void set(int a)


{
x = a;
}

public void print()


{
System.out.print(x);
}
}

public class DClass extends BClass


{
private int y;

public void set(int a, int b)


{
//Postcondition: x = a; y = b;
}
public void print(){ }
}

Which of the following correctly redefines the method print of DClass?

(i)
public void print()
{
System.out.print(x + " " + y);
}

(ii)

public void print()


{
super.print();
System.out.print(" " + y);
}

a. Only (i)
b. Only (ii)
c. Both (i) and (ii)
d. None of these
____ 20. Consider the following class definitions.

public class BClass


{
private int x;

public void set(int a) { x = a; }

public void print(){ }


}

public class DClass extends BClass


{
private int y;

public void set(int a, int b)


{
//Postcondition: x = a; y = b;
}
public void print(){ }
}
Which of the following is the correct definition of the method set of the class DClass?

(i)
public void set(int a, int b)
{
super.set(a);
y = b;
}

(ii)

public void set(int a, int b)


{
x = a;
y = b;
}

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

____ 23. An abstract class can contain ____.


a. only abstract methods c. abstract and non-abstract methods
b. only non-abstract methods d. nothing
____ 24. An abstract method ____.
a. is any method in the abstract class
b. cannot be inherited
c. has no body
d. is found in a subclass and overrides methods in a superclass using the reserved word
abstract
____ 25. Composition is also called ____.
a. inheritance c. overloading
b. aggregation d. overriding

You might also like