Practice Exam - Classes, Random, and Inheritance
Name______________________________
True/False
____ 1. If obj is an object, an instance of some class, then System.out.println(obj); is a statement
equivalent to System.out.println(obj.toString()); .
____
2. In order to construct an object of a class in client code, the programmer needs to know the names of each
private instance variable in the class.
____
3. The keyword public indicates that the class is accessible to all potential clients.
____
4. In a class, private instance variables model an object's behaviors.
____
5. A class encapsulates state and behavior.
____
6. Constructors do not have return types, but all other methods do.
____
7. A class can only have one constructor. Multiple constructors are not allowed.
____
8. In order to use an object of a class in client code, the programmer needs to know how each public and private
method has been implemented.
____
9. The first line of code in any subclass constructor, either explicitly or implicitly, will always be a call to one of
its superclass's constructors.
____ 10. Inheritance is used when the relationship between two classes is a "IS-A” relationship.
Multiple Choice
____ 11. What is a method?
a.
b.
c.
d.
e.
a tool that helps organize a program
a tool that complicates a program
a tool that makes a program longer
a tool that makes a program harder to read
a tool that makes a program more disorganized
____ 12. What is output by the code below?
public class CS{
public void one(){
System.out.print("one");
}
public void two(){
System.out.print("two");
one();
}
}
//code in the main of another class
CS test = new CS();
test.one();
test.two();
test.two();
a. onetwo
b. onetwotwo
c. twoone
d. onetwoonetwo
e. onetwoonetwoone
____ 13. What is output by the code below?
public class Alligator{
public void speak(){
System.out.println("grrrr");
}
public void sayName(){
System.out.println("gatuh");
speak();
}
}
//code in the main of another class
Alligator all = new Alligator();
all.sayName();
all.speak();
all.sayName();
all.speak();
a.
grrrr
gatuh
grrrr
gatuh
b.
gatuh
grrrr
grrrr
gatuh
grrrr
grrrr
c.
sayName
speak
sayName
speak
d.
sayName
speak
speak
sayName
speak
speak
e.
gatuh
grrrr
Consider the class and statements that follow.
public class Dog{
public void barkOnce(){
System.out.print("woof");
}
public void barkTwice(){
barkOnce();
barkOnce();
}
}
____ 14. Given the declaration below, which of the following are valid statements in a main method?
Dog doggy = new Dog();
I.
doggy.barkOnce();
II. doggy.barkTwice();
III. barkOnce();
a. I only
b. II only
c. III only
d. I and II only e. I, II and III
only
____ 15. Given the code below, what is the output?
Dog doggy = new Dog();
doggy.barkOnce();
doggy.barkTwice();
a. woof
b. woof
c. woof
woof
woof
woof
____ 16. Which of the following would be a legal Java identifier?
a.
b.
c.
d.
e.
ive Got It
iveGotIt!
i’veGotIt
ive-Got-It
iveGotIt
d. woofwoofwoof
e. woofwoof
____ 17. Consider the following statement:
System.out.print("1 big bad wolf\t8 the 3 little pigs\n”);
System.out.print(“4 dinner \"tonight\"");
This statement will output _____ lines of text?
a. 1
b. 2
c. 3
d. 4
e. 5
____ 18. Consider the following statement(s):
System.out.print("I can program in Java");
System.out.println(" because I'm a student at");
System.out.println("the number one high school SAHS!");
What will be the output?
a. I can program in Java because I'm a student at
b.
c.
d.
e.
the number one high school SAHS!
I can program in Java
because I'm a student at
the number one high school SAHS!
I can program in Java
because I'm a student at the number one high school SAHS!
I can program in Java
I can program in Java because I'm a student at the number one high school
SAHS!
(outputs on one line)
____ 19. Consider the class and statements that follow.
public class Dog{
public void bark(){
System.out.println("woof");
}
public void howl(){
System.out.println("arrooo");
bark();
}
}
Given the code below, what is the output?
Dog doggy = new Dog();
doggy.bark();
doggy.howl();
a.
woof
arrooo
b.
woof
woof
arrooo
c.
arrooo
woof
arrooo
d.
woof
arrooo
arrooo
e.
woof
arrooo
woof
c.
2
d.
3
e.
4
____ 20. What is output by the code below?
int x = 8;
x = x % 3;
System.out.print(x);
a.
____
21.
22.
23.
1
6.0
b.
7.0
c.
8.0
d.
9.0
e.
27.0
c.
17
d.
17.0
e.
27.0
c.
21
d.
15
e.
27
What is output by the code below?
int x = 9,
int y = 8;
int z = x + y;
double a = z;
System.out.print(a);
a.
____
b.
What is output by the code below?
System.out.println( Math.floor(6.7) );
a.
____
0
8
b.
9
What is output by the code below?
int x = 9;
x = x + 3 * 2;
System.out.print(x);
a.
24
b.
14
____
24.
What is output by the code below?
System.out.println( Math.pow( Math.sqrt(81),2) );
a.
____
25.
26.
27.
28.
29.
81.0
d.
91.0
e.
27
0
b.
7
c.
0.7
d.
27
e.
Runtime error.
0
b.
1
c.
0.0
d.
1.0
e.
2
0
b.
1
c.
2
d.
3
e.
5
c.
2
d.
0
e.
3
c.
2
d.
0
e.
3
2
d.
0
e.
3
c.
2
d.
0
e.
3
c.
9.0
d.
9.6
e.
9
What is output by the code below?
System.out.print(4 % 3);
a.
____
c.
What is output by the code below?
int x = 15;
x = x % 5;
System.out.print(x);
a.
____
18.0
What is output by the code below?
double x = 4/3;
System.out.print(x);
a.
____
b.
What is output by the code below?
System.out.println( 7 / 0);
a.
____
9.0
.5
b.
1
What is output by the code below?
System.out.print(3 / 4);
a.
____
30.
.5
b.
1
What is output by the code below?
System.out.print( (double)2 / 2 );
a.
____
31.
1
b.
1.0
c.
What is output by the code below?
System.out.print( (double)1 / 2 );
a.
____
32.
0.5
b.
1
What is output by the code below?
System.out.print( (int)9.6 );
a.
____
33.
10
b.
6
How would you create this range:
(int)(Math.random()
a.
b. (int)(Math.random()
(int)(Math.random()
c.
d. (int)(Math.random()
[99..100]
*
*
*
*
0) + 99
1) + 99
2) + 99
99) + 2
____
34.
How would you create this range of random numbers:
(int)(Math.random() * 45)
a.
b. (int)(Math.random() * 45) + 1
(int)(Math.random() * 44)
c.
d. (int)(Math.random() * 46)
[0..45]
____
35.
How would you create this range of random numbers:
(int)(Math.random() * 45)
a.
b. (int)(Math.random() * 45) + 1
(int)(Math.random() * 44) + 1
c.
d. (int)(Math.random() * 46) + 1
[1..45]
____
36.
How would you create this range of random numbers:
(int)(Math.random() * 91) - 45
a.
b. (int)(Math.random() * 45) - 45
(int)(Math.random() * 45) + 45
c.
d. (int)(Math.random() * 90) - 45
[-45..45]
____
37.
a.
b.
c.
d.
____
38.
(int)(Math.random()
(int)(Math.random()
(int)(Math.random()
(int)(Math.random()
* 100) - 100
* 99) - 100
* 101) - 100
*0) - 100
[27..56]
How would you create this range:
a.
b.
c.
d.
____
[-100..0]
How would you create this range:
(int)(Math.random()
(int)(Math.random()
(int)(Math.random()
(int)(Math.random()
*
*
*
*
27)
29)
56)
30)
+
+
+
56
27
27
27
39. How can you distinguish a constructor of a class from the methods of a class?
a. Constructors have the keyword public.
b. Constructors have no return type.
c. Constructors have the same name as the class.
d. Choices B and C
Given the following incomplete class:
public class Binky
{
//private instance variables not shown
public Binky() {...}
public Binky(String name) {...}
public Binky(String name, int age) {...}
public String getName() {...}
public void setName(String name) {...}
public int getAge() {...}
public void setAge(int age) {...}
public String toString() {...}
//other methods not shown
}
____
40. Which one of the following statements would create a new Binky object using the one parameter constructor?
a. Binky baby = Binky("Sally");
b. Binky baby = new Binky("Sally");
c. Binky = new Binky("Sally");
d. baby = Binky("Sally");
e. none of these
____
41. Which of the following statements would instantiate a new Binky object with a given name (Linus) and age(0)?
a. Binky baby = new Binky();
d. Binky baby = new Binky("Linus",0);
b. Binky baby = new Binky();
e. none of these
baby.setName("Linus");
baby.setAge(0);
c. Binky baby = new Binky("Linus");
baby.setAge(0);
____
42. Given that baby has been instantiated as a Binky object, which of the following statements would be an appropriate call to the
getName method?
a. Binky.getName();
b. baby.getName();
c. baby.getName("Lucy");
d. System.out.println(baby.getName());
e. baby = getName();
____
43. Given that baby has been instantiated as a Binky object, which of the following statements would be an appropriate call to the
setName method?
a. Binky.setName("Lucy");
b. baby.setName();
c. baby.setName("Lucy");
d. System.out.println(baby.setName("Lucy"));
e. baby = setName("Lucy");
____
____
44. Assume that babyBoy and babyGirl have been instantiated as Binky objects. Which of the following code segments would calculate
the total of their ages?
a.
int ageTotal = babyBoy.getAge();
ageTotal += babyGirl.getAge();
b.
int ageTotal;
ageTotal = babyBoy.getAge() + babyGirl.getAge();
c.
int ageTotal = babyBoy + babyGirl;
d.
e.
A and B
none of the above
45. The process of creating a new object is called:
a. encapsulation
b. class
c.
instantiation
d.
new
e.
initialization
____
46. When creating instance variables for a class, which of the following keywords is necessary to insure information hiding?
a. new
b. static
c. void
d. public
e. private
____
47. Variables found in a method heading are called
a. arguments
b. formal paramters c.
constructors
d.
initial values
____
48. The purpose of a constructor is to allocate memory for the object AND
a. call other class methods.
b. test all of the other class methods.
c. output a message that the memory has been allocated.
d. initialize the private instance variables.
e. none of these.
____
49. The concept of packaging together the state and behaviors of an object is known as
a. object
b. information
c. encapsulation
d. constructing an
overloading
hiding
object
e.
none of these
e.
none of these
____
50. Private methods and private instance variables
a. can be accessed by client code and methods of the same class.
b. can only be accessed by methods of the same class.
c. are not necessary to maintain information hiding.
d. are never used in Object Oriented Programming.
e. none of these
____
51. The concept that the “client code” does not need to know about the inner workings of class object in order to use the
operations for the object is known as
a.
method
overloading
b.
information
hiding
c.
encapsulation
d.
constructing an
object
e.
none of these
____
52. When is a constructor method called?
a. At the beginning of each main method.
b. Whenever a class name is used.
c. When a new object is instantiated.
d. Whenever a class is implemented.
e. Constructor methods cannot be called.
____
53. Which characteristic of an object says that at any moment its instance variables have particular values?
a. state
b. behavior
c. identity
d. instantiation
e. none of these
____
54. What are the only type of methods that can be used with the keyword new?
a. accessors
b. modifiers
c. constructors
d. methods
e.
none of these
____
55. When creating a method for a class, which of the following keywords is necessary to allow client code to use the method?
a. new
b. static
c. void
d. public
e. private
____
56. Having multiple constructors in a class is an example of
a. information
b. encapsulation
c. instantiation
hiding
____
57. What distinguishes a default constructor from other class constructors?
a. A default constructor has no parameters.
b. A default constructor is a void method.
c. A default constructor has a return type of int.
d. A default constructor has one parameter.
e. A class cannot have a default constructor.
d.
method
overloading
e.
none of these
____
58. Values passed to a method when it is invoked are called: (i.e. bob.hop(6); )
a. actual
b. formal
c. constructors
d. initial values
parameters or
parameters
arguments
____
59. An object is
a. the same as a class.
b. an instance of a class
c. a primitive type variable.
d. a method that accesses class attributes.
e. can only be used as a private instance variables.
____
60. What is the name of a class method that indicates how to initialize a new object?
a. initializer
b. constructor
c. modifier
d. accessor
____
61. Public methods
a. can be accessed by client code and methods of the same class.
b. can only be accessed by methods of the same class.
c. are not necessary to maintain information hiding.
d. are never used in Object Oriented Programming.
e. none of these
____
62. Class methods that change an object's state are called:
a. accessors
b. constructors
c. modifiers
____
d.
methods
e.
none of these
e.
none of these
e.
void methods
63. Consider the following classes.
public class Boo{
public void clue(){
System.out.println("Boo's clue.");}
}
public class Moo extends Boo{
public void clue(){
System.out.println("Moo's clue.");}
}
What will be the output of the following code segment?
Moo blue = new Moo();
blue.clue();
a.
b.
c.
d.
Boo’s clue.
Moo’s clue.
There will be a compiler error because the compiler cannot tell which clue method is being called.
There will be no output.
Consider the following inheritance hierarchy.
____
64. Which of the following statements is true?
a. UrRobot is a subclass of Robot
b. Robot is a superclass of BetterRobot
c. BetterRobot is a superclass of Object
d. UrRobot is a superclass of Object
____
65. If bob has been declared and instantiated as a Robot object, which of the following method calls are legal?
I.
if(bob.frontIsClear())
II.
bob.turnLeft();
III.
bob.turnRight();
a. I only
b. II only
c. III only
d. I and II
e. I and III
____
66. Which of the following statements is true?
I.
II.
III.
a.
____
____
Robot is a sublcass of UrRobot and a superclass of BetterRobot.
UrRobot is a superclass of Robot and a subclass of Object.
BetterRobot is a superclass of Robot and UrRobot and a subclass of Object.
I only
b.
II only
c.
III only
d.
I and II
e.
I and III
67. Parameters found in a method heading are called
a. value parameters.
b. reference parameters.
c.
formal parameters.
d.
actual parameters.
68. Parameters found in the call of a method are called
a. value parameters.
b. reference parameters.
c.
formal parameters.
d.
actual parameters.
h.
i.
j.
k.
l.
m.
accessor methods
modifier methods
new
instantiation
public
private
Matching
a.
b.
c.
d.
e.
f.
g.
class
object
state
behaviors
instance variables
parameters
constructor
____
69. Properties of an object. Corresponds to private instance variables
____
70. When this visibility qualifier is applied to a class member, the class member is accessible to client code.
____
71. Contains instructions to initialize an object. Has the same name as the class and no return type.
____
72. Variables or values found in the method heading or the method call. Used to transfer information between methods.
____
73. Keyword that must be used when instantiating a new object.
____
74. The process of creating a new object.
____
75. When this visibility qualifier is applied to a class member, the class member is not accessible to client code.
____
76. A class method that can change the state of an object.
____
77. Actions that an object can perform. Corresponds to public methods
____
78. Each object of a class gets its own copy of these variables.
____
79. A class method that does not change the state of an object.
____
80. An instance of a class
____
81. A programmer defined data type. A blueprint for constructing objects
TEST - Classes, Random, Graphics
Answer Section
TRUE/FALSE
1.
2.
3.
4.
5.
6.
7.
ANS: T
PTS: 1
ANS: F
PTS: 1
ANS: T
PTS: 1
REF: p. 139
ANS: F
PTS: 1
ANS: T
PTS: 1
REF: p. 132
ANS: T
PTS: 1
REF: p. 140
ANS: F
A class template can include more than one constructor, provided each has a unique parameter list.
PTS:
8. ANS:
9. ANS:
10. ANS:
1
F
T
T
REF:
PTS:
PTS:
PTS:
p. 142
1
1
1
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
MULTIPLE CHOICE
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
A
E
B
D
D
E
B
A
E
C
A
D
D
C
E
D
A
B
D
B
A
E
C
D
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
B
A
C
D
D
B
D
D
C
D
C
E
B
D
C
B
B
C
A
C
D
D
A
A
B
B
A
C
B
B
D
D
C
D
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
C
L
G
F
J
K
M
I
D
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
1
1
1
1
1
1
1
1
1
MATCHING
69.
70.
71.
72.
73.
74.
75.
76.
77.
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
ANS:
REF: p. 131
REF: p. 132
REF: p. 142
REF: p. 151
REF: p. 138
REF: p. 135
78.
79.
80.
81.
ANS:
ANS:
ANS:
ANS:
E
H
B
A
PTS:
PTS:
PTS:
PTS:
1
1
1
1
© Copyright 2026 Paperzz