Section 7 Quiz
(Answer
all questions in this section)
1. What is encapsulation? Mark for Review
(1) Points
A
keyword that allows or restricts access to data and methods.
A
programming philosophy that promotes simpler, more efficient coding by using
exiting code for new applications.
A
structure that categorizes and organizes relationships among ideas, concepts of
things with the most general at the top and the most specific at the bottom.
A
programming philosophy that promotes protecting data and hiding implementation
in order to preserve the integrity of data and methods. (*)
2. Consider creating a class Square that
extends the Rectangle class provided below. Knowing that a square always has
the same width and length, which of the following best represents a constructor
for the Square class?
Mark for Review
(1) Points
(*)
3. Which of the following demonstrates the
correct way to create an applet Battlefield?
Mark for Review
(1) Points
public
class Applet extends Battlefield{...}
public
class Battlefield extends Applet{...} (*)
public
class Battlefield(Applet){...}
public
Applet Battlefield{...}
4. What is the Java keyword final used for
in a program? Mark for Review
(1) Points
It
permits access to the class variables and methods from anywhere.
It
restricts a class from being extendable and restricts methods from being
overridden. (*)
It
permits redefining methods of a parent class inside the child class, with the
same name, parameters, and return type.
There is
no such keyword in Java.
It
terminates the program.
5. Which of the following are true about
abstract methods? Mark for Review
(1) Points
(Choose
all correct answers)
They may
contain implementation.
They
must be overloaded.
They
must be overridden in a non-abstract subclass. (*)
They
must be declared in an abstract class. (*)
They
cannot have a method body. (*)
6. Which of
the following would be most beneficial for this scenario?
Joe is a college student who has a tendency to lose his
books. Replacing them is getting costly. In an attempt to get organized, Joe
wants to create a program that will store his textbooks in one group of books,
but he wants to make each book type the subject of the book (i.e. MathBook is a
book). How could he store these different subject books into a single array?
Mark for Review
(1) Points
By
overriding the methods of Book.
Using
polymorphism. (*)
This is
not possible. Joe must find another way to collect the books.
By
ignoring the subject type and initializing all the book as objects of type
Book.
7. Consider
public class YourClass{
public YourClass(int i)
{/*code*/}
// more code...}
To instantiate YourClass, what would you write? Mark for Review
(1) Points
YourClass
y = new YourClass();
YourClass
y = new YourClass(3); (*)
YourClass
y = YourClass(3);
YourClass
y = YourClass();
None of
the above.
8. The
following code creates an object of type Animal:
Animal a;
Mark for Review
(1) Points
True
False
(*)
9. The basic
unit of encapsulation in Java is the primitive data type. True or false? Mark for Review
(1) Points
True
False
(*)
10. You can
create static class methods inside any Java class. True or false? Mark for Review
(1) Points
True (*)
False
11. Any
instance of the same class can assign a new value to a static variable. True or
false? Mark for Review
(1) Points
True (*)
False
12. Which of
the following access modifiers doesn't work with a static variable? Mark for Review
(1) Points
private
default
protected
public
friendly
(*)
13. Which of
the following shows the correct way to initialize a method DolphinTalk that
takes in 2 integers, dol1 and dol2, and returns the greater int between the
two? Mark for Review
(1) Points
int DolphinTalk(dol1,
dol2){ if(dol1 > dol2) return dol1; else return dol2;}
int
DolphinTalk(int,int){ if(dol1 > dol2) return dol1; else return dol2;}
int
DolphinTalk(int dol1,int dol2){ if(dol1 > dol2) return dol1; else return
dol2;} (*)
int
DolphinTalk, int dol1,int dol2 { if(dol1 > dol2) return dol1; else return
dol2;}
All of
the above
14. Which of
the following could be a reason to return an object? Mark for Review
(1) Points
Because
you wish to be able to use that object inside of the method.
It has
faster performance than returning a primitive type.
The
method makes changes to the object and you wish to continue to use the updated
object outside of the method. (*)
None of
the above. It is not possible to return an object.
15. Which of
the following specifies accessibility to variables, methods, and classes? Mark for Review
(1) Points
Overload
constructors
Methods
Parameters
Access
modifiers (*)
1. A final
static variable can change at runtime. True or false? Mark for Review
(1) Points
True
False
(*)
2. Static
classes can have different access specifiers than the parent class. True or
false? Mark for Review
(1) Points
True (*)
False
3. Static
classes can exist as inner classes. True or false? Mark for Review
(1) Points
True (*)
False
4. Which of
the following correctly describes an "is-a" relationship? Mark for Review
(1) Points
It
restricts access to a specified segment of code.
A
helpful term used to conceptualize the relationships among nodes or leaves in
an inheritance hierarchy. (*)
A
programming philosophy that promotes protecting data and hiding implementation
in order to preserve the integrity of data and methods.
A
programming philosophy that promotes simpler, more efficient coding by using
exiting code for new applications.
5. Why are
hierarchies useful for inheritance?
Mark for Review
(1) Points
They are
used to organize the relationship between a superclass and its subclasses. (*)
They
restrict a superclass to only have one subclass.
They
keep track of where you are in your program.
They
organize constructors and methods in a simplified fashion.
6. Which of
the following correctly describes the use of the keyword super? Mark for Review
(1) Points
A
keyword that signals the end of a program.
A
keyword that allows subclasses to access methods, data, and constructors from
their parent class. (*)
A
keyword that restricts access to only inside the same class.
A
keyword that allows access from anywhere.
7. What is
Polymorphism? Mark for Review
(1) Points
A way of
redefining methods with the same return type and parameters.
A way to
create multiple methods with the same name but different parameters.
A class
that cannot be initiated.
The
concept that a variable or reference can hold multiple types of objects. (*)
8. What is
the Java keyword final used for in a program?
Mark for Review
(1) Points
It
permits redefining methods of a parent class inside the child class, with the
same name, parameters, and return type.
It
permits access to the class variables and methods from anywhere.
There is
no such keyword in Java.
It
restricts a class from being extendable and restricts methods from being
overridden. (*)
It
terminates the program.
9. Identify
the correct way to declare an abstract class.
Mark for Review
(1) Points
public
class abstract ClassName(...)
public
abstract class ClassName{...} (*)
public
abstract ClassName(...)
abstract
public class ClassName{...}
10. If the
return type from a method is boolean then 2.5 is a valid return value. True or
false? Mark for Review
(1) Points
True
False
(*)
11. The basic unit of encapsulation in Java is
the primitive data type. True or false?
Mark for Review
(1) Points
True
False
(*)
12. Consider:
public class MyClass{
public MyClass()
{/*code*/}
// more code...}
To instantiate MyClass, what would you write? Mark for Review
(1) Points
MyClass
m = new MyClass;
MyClass
m = MyClass();
MyClass
m = MyClass;
MyClass
m = new MyClass(); (*)
13. Which of the following can be used as a
parameter? Mark for Review
(1) Points
(Choose
all correct answers)
Strings
(*)
Integers
(*)
Objects
(*)
Constructors
Arrays
(*)
14. Which segment of code represents a correct
way to define a variable argument method?
Mark for Review
(1) Points
Integer
easyArray ... (int elems) {//code}
String
easyArray(String ... elems) {//code} (*)
String
... easyArray(String elems) {//code}
String
easyArray(... String elems) {//code}
15. Identify the error(s) in the class below.
Choose all that apply
Mark for Review
(1) Points
Two
methods cannot have the same name.
Final
cannot be used as an access modifier.
Private
cannot be used as an access modifier.
No
method named min is defined. (*)
The
parameters must be the same for all methods with the same name.
1. Which of
the following would be most beneficial for this scenario?
Joe is a college student who has a tendency to lose his
books. Replacing them is getting costly. In an attempt to get organized, Joe
wants to create a program that will store his textbooks in one group of books,
but he wants to make each book type the subject of the book (i.e. MathBook is a
book). How could he store these different subject books into a single array?
Mark for Review
(1) Points
By
ignoring the subject type and initializing all the book as objects of type
Book.
Using
polymorphism. (*)
By
overriding the methods of Book.
This is
not possible. Joe must find another way to collect the books.
2. Abstract
classes cannot implement interfaces. True or false? Mark for Review
(1) Points
True
False
(*)
3. If Oak
extends Tree, it is possible to declare an object such that
Tree grandfatherT = new Oak();
True or false?
Mark for Review
(1) Points
True (*)
False
4. Which segment
of code correctly defines a method that contains two objects of class Tree as
parameters? Mark for Review
(1) Points
void
bloom(Tree pine, Tree oak) {//code here } (*)
Tree
bloom (pine, oak) {//code here }
void
bloom, Tree pine, Tree oak {//code here }
None of
the above, objects cannot be passed as parameters.
5. Consider
the following:
There is a method A that calls method B. Method B is a
variable argument method.
With this, which of the following are true? Mark for Review
(1) Points
(Choose
all correct answers)
Method A
can invoke method B twice, each time with a different number of arguments. (*)
A
compliler error will result since method B does not know how large an array to
create when it is invoked by method A.
When
invoked, method B creates an array to store some or all of the arguments passed
to it from method A. (*)
All of
the above.
6. Which of
the following is a possible way to overload constructors? Mark for Review
(1) Points
(*)
7. Identify
the driver class that correctly initializes employees Jane and Brandon. The
Employee class is below.
public class Employee {
private String name;
private int age;
private double salary;
public Employee(String n, int a, double s) {
name = n;
age = a;
salary = s;
}
//methods for this class would go here
} Mark for
Review
(1) Points
public
class driver_class {
public static void main(String[] args) {
Employee Jane = new Employee("Jane", 48,
35.00);
Employee Brandon = new Employee("Brandon", 36,
20.00);
}
} (*)
public
class driver_class {
public static void main(String[] args) {
Employee("Jane", 48, 35.00);
Employee("Brandon", 36, 20.00);
}
}
public
class driver_class {
public Employee{
Jane = new Employee("Jane", 48, 35.00);
Brandon = new Employee("Brandon", 36, 20.00);
}
}
public
class Employee {
public class driver-class{
Employee Jane = new Employee();
Employee Brandon = new Employee();
}
}
8. What is
wrong with the following class declaration?
class Account{
private int number;
private String name;
public Account;
} Mark for
Review
(1) Points
There is
nothing wrong.
The
constructor method has no definition. (*)
Classes
cannot include mixed data types.
Classes
cannot include strings.
9. The basic
unit of encapsulation in Java is the primitive data type. True or false? Mark for Review
(1) Points
True
False
(*)
10. Static methods can return any object type.
True or false? Mark for Review
(1) Points
True (*)
False
11. Static
classes can extend their parent class. True or false? Mark for Review
(1) Points
True (*)
False
12. Non-final
static class variables should be private to prevent changes from other classes.
True or false? Mark for Review
(1) Points
True (*)
False
13. What is
encapsulation? Mark for Review
(1) Points
A
programming philosophy that promotes simpler, more efficient coding by using
exiting code for new applications.
A
programming philosophy that promotes protecting data and hiding implementation
in order to preserve the integrity of data and methods. (*)
A
structure that categorizes and organizes relationships among ideas, concepts of
things with the most general at the top and the most specific at the bottom.
A
keyword that allows or restricts access to data and methods.
14. If a
variable in a superclass is private, could it be directly accessed or modified
by a subclass? Why or why not?
Mark for Review
(1) Points
No.
Nothing inherited by the super class can be changed in the subclass.
Yes. Any
variable passed through inheritance can be changed, but private methods cannot.
Yes. A
subclass inherits full access to all contents of its super class.
No. A
private variable can only be modified by the same class with which it is
declared regardless of its inheritance. (*)
15. Which of
the following demonstrates the correct way to create an applet Battlefield? Mark for Review
(1) Points
public
class Applet extends Battlefield{...}
public
class Battlefield(Applet){...}
public
Applet Battlefield{...}
public
class Battlefield extends Applet{...} (*)
1. What
keyword is used to inherit a superclass?
Mark for Review
(1) Points
extends
(*)
public
this
new
2. It is
possible to extend a class that already exists in Java, such as the Applet
class. True or false? Mark for Review
(1) Points
True (*)
False
3. Why are
hierarchies useful for inheritance?
Mark for Review
(1) Points
They are
used to organize the relationship between a superclass and its subclasses. (*)
They
organize constructors and methods in a simplified fashion.
They
keep track of where you are in your program.
They
restrict a superclass to only have one subclass.
4. Is there
a difference between overriding a method and overloading a method? Mark for Review
(1) Points
Yes.
Overriding is done in the subclass and allows for redefining a method inherited
from the superclass and overloading is done within a class and allows for
multiple methods with the same name. (*)
No, they
are the same.
Yes.
Overriding is done within a single class and overloading is done through a
series of superclasses and their subclasses.
Yes.
Overriding allows for the creation of an array of different object types and
overloading restricts an array to only contain the same object types.
5. It is
possible to inherit from an abstract class. True or false? Mark for Review
(1) Points
True (*)
False
6. If Sandal
extends Shoe, it is possible to declare an object such that
Sandal s = new Shoe();
Mark for Review
(1) Points
True
False
(*)
7. Static
classes are designed as thread safe class instances. True or false? Mark for Review
(1) Points
True
False
(*)
8. Static
classes can extend any class in their class path. True or false? Mark for Review
(1) Points
True (*)
False
9. Static
classes can extend their parent class. True or false? Mark for Review
(1) Points
True (*)
False
10. Which
segment of code correctly defines a method that contains two objects of class
Tree as parameters? Mark for Review
(1) Points
void
bloom(Tree pine, Tree oak) {//code here } (*)
Tree
bloom (pine, oak) {//code here }
void
bloom, Tree pine, Tree oak {//code here }
None of
the above, objects cannot be passed as parameters.
11. Which of
the following is the correct way to code a method with a return type an object
Automobile? Mark for Review
(1) Points
Automobile
upgrade(String carA){
carA="Turbo";
return carA;
}
Automobile
upgrade(Automobile carA){
carA.setTurbo("yes");
return carA;
} (*)
String
upgrade(String carA){
carA="Turbo";
return carA;
}
upgrade(Automobile
carA) Automobile{
carA.setTurbo("yes");
return carA;
}
None of
the above. It is not possible to return an object.
12. Choose the
correct implementation of a public access modifier for the method divide. Mark for Review
(1) Points
divide(int
a, int b, public) {return a/b;}
divide(public
int a, public int b) {return a/b;}
public
int divide(int a, int b) {return a/b;} (*)
divide(int
a, int b) {public return a/b;}
13. Which
constructor code populates the instance variables of the class correctly? Mark for Review
(1) Points
(*)
14. What value
will return for j when the setValue method is called?
Mark for Review
(1) Points
10
31
32
11 (*)
15. What is
wrong with the following class declaration?
Mark for Review
(1) Points
There is
no constructor method and you have to make a constructor method.
There is
nothing wrong. (*)
Classes
cannot include strings.
Classes
cannot include mixed data types.
1. Which of
the following keywords are used to access the instance variables of an object
from within the class code for that object?
Mark for Review
(1) Points
private
public
this (*)
protected
2. The
following code creates an object of type Animal:
Animal a;
Mark for Review
(1) Points
True
False
(*)
3. What is
wrong with the following class declaration?
class Account{
private int number;
private String name;
public Account;
} Mark for
Review
(1) Points
The
constructor method has no definition. (*)
There is
nothing wrong.
Classes cannot
include mixed data types.
Classes
cannot include strings.
4. Where
should the constructor for a superclass be called? Mark for Review
(1) Points
The
first line of the constructor in the subclass. (*)
The last
line in the constructor of the subclass.
The
super constructor does not need to be called inside the subclass.
Inside
the main method of the subclass.
Anywhere
inside the subclass.
5. Which of
the following is the proper way to set the public variable length of the super
class equal to 5 from inside the subclass?
Mark for Review
(1) Points
super.length()
= 5
super.length(5)
super.length
= 5 (*)
super(length
= 5)
6. Which of
the following correctly describes the use of the keyword super? Mark for Review
(1) Points
A
keyword that restricts access to only inside the same class.
A
keyword that signals the end of a program.
A
keyword that allows subclasses to access methods, data, and constructors from
their parent class. (*)
A
keyword that allows access from anywhere.
7. Abstract
classes can be instantiated. True or false?
Mark for Review
(1) Points
True
False
(*)
8. Which of
the following is a goal of the object model?
Mark for Review
(1) Points
(Choose
all correct answers)
Providing
modular code that can be reused by other programs or classes. (*)
Protecting
information and limiting other classes' ability to change or corrupt data. (*)
Concealing
implementation. (*)
Data
abstraction. (*)
9. Abstract
classes cannot implement interfaces. True or false? Mark for Review
(1) Points
True
False
(*)
10. There is
only one copy a static class variable in the JVM. True or false? Mark for Review
(1) Points
True (*)
False
11. A final static variable can change at
runtime. True or false? Mark for Review
(1) Points
True
False
(*)
12. Which of the following access modifiers
doesn't work with a static variable?
Mark for Review
(1) Points
default
public
private
protected
friendly
(*)
13. How is it possible for overloading to work? Mark for Review
(1) Points
Java
Virtual Machine searches until it finds a constructor name and argument type
match. (*)
The
interpreter doesn't care what you name your constructors.
The code
has to be declared as private.
There is
no such thing as overloading.
14. Which of the following could be a reason to
return an object? Mark for
Review
(1) Points
Because
you wish to be able to use that object inside of the method.
It has
faster performance than returning a primitive type.
The
method makes changes to the object and you wish to continue to use the updated
object outside of the method. (*)
None of
the above. It is not possible to return an object.
15. Identify the error(s) in the class below.
Choose all that apply
Mark for Review
(1) Points
Final
cannot be used as an access modifier.
No
method named min is defined. (*)
Private
cannot be used as an access modifier.
Two
methods cannot have the same name.
The
parameters must be the same for all methods with the same name.
1. Following good programming guidelines,
what access modifier should be used for the class fields in the following
situation?
A car insurance company wants to create a class named
Customer that stores all data for a specified customer including the fields: vehicle
information, policy information, and a credit card number. Mark for Review
(1) Points
public
protected
private
(*)
default
All of
the above
2. Identify the error(s) in the class below.
Choose all that apply
Mark for Review
(1) Points
No
method named min is defined. (*)
The
parameters must be the same for all methods with the same name.
Final
cannot be used as an access modifier.
Two
methods cannot have the same name.
Private
cannot be used as an access modifier.
3. Which segment of code represents a
correct way to call a variable argument method counter that takes in integers
as its variable argument parameter?
Mark for Review
(1) Points
counter(String
a, int b);
counter(int[]
numbers);
counter("one","two",String[]
nums);
counter(1,
5, 8, 17, 11000005); (*)
4. The return value of a method can only be
a primitive type and not an object. True or false? Mark for Review
(1) Points
True
False
(*)
5. All objects, in Java, are created using
int. True or false? Mark for
Review
(1) Points
True
False
(*)
6. Which of
the following is true? Mark for
Review
(1) Points
Instance
variable names may only contain letters and digits.
int is
the name of a class available in the package java.lang.
In Java,
a method declared public generates a compilation error.
The more
comments in a program, the faster the program runs.
A class
always has a constructor (possibly automatically supplied by the java
compiler). (*)
7. If an
abstract class does not have implemented constructors or methods, it should be
implemented as an interface instead. True or false? Mark for Review
(1) Points
True (*)
False
8. What
allows Java to correctly and automatically determine which method to invoke
based on the type of object being referred to at the time the method is called? Mark for Review
(1) Points
Inheritance
Dynamic
Method Dispatch (*)
Polymorphism
Abstract
classes
9. What is
the Java keyword final used for in a program?
Mark for Review
(1) Points
It
terminates the program.
It
permits redefining methods of a parent class inside the child class, with the
same name, parameters, and return type.
It
restricts a class from being extendable and restricts methods from being
overridden. (*)
It
permits access to the class variables and methods from anywhere.
There is
no such keyword in Java.
10. Which of
the following correctly defines a superclass (or parent class)? Mark for Review
(1) Points
A class
that inherits methods and fields from a more general class.
A keyword
that allows or restricts access to data and methods.
The most
specific class of a hierarchy system of classes.
A class
that passes down its methods to more specialized classes. (*)
11. It is
possible for a subclass to be a superclass. True or false? Mark for Review
(1) Points
True (*)
False
12. Which of
the following correctly describes an "is-a" relationship? Mark for Review
(1) Points
It
restricts access to a specified segment of code.
A
programming philosophy that promotes simpler, more efficient coding by using
exiting code for new applications.
A
helpful term used to conceptualize the relationships among nodes or leaves in
an inheritance hierarchy. (*)
A
programming philosophy that promotes protecting data and hiding implementation
in order to preserve the integrity of data and methods.
13. Static
methods can write to instance variables. True or false? Mark for Review
(1) Points
True
False
(*)
14. Static
classes can't return instances of the parent class when the parent class uses a
private constructor. True or false? Mark
for Review
(1) Points
True
False
(*)
15. Static
classes can have different access specifiers than the parent class. True or
false? Mark for Review
(1) Points
True (*)
False
1. According
to the following class declaration, runSpeed can be modified in class Cat. True
or false
public class Tiger extends Cat{
public int runSpeed;
} Mark for
Review
(1) Points
True
False
(*)
2. If a
variable in a superclass is private, could it be directly accessed or modified
by a subclass? Why or why not?
Mark for Review
(1) Points
No. A
private variable can only be modified by the same class with which it is
declared regardless of its inheritance. (*)
No.
Nothing inherited by the super class can be changed in the subclass.
Yes. Any
variable passed through inheritance can be changed, but private methods cannot.
Yes. A
subclass inherits full access to all contents of its super class.
3. It is
possible to extend a class that already exists in Java, such as the Applet
class. True or false? Mark for Review
(1) Points
True (*)
False
4. If the
return type from a method is boolean then 2.5 is a valid return value. True or
false? Mark for Review
(1) Points
True
False
(*)
5. The
following statement compiles and executes. What do you know for certain?
tree.grows(numFeet);
Mark for Review
(1) Points
tree
must be a method.
grows
must be the name of a method. (*)
numFeet
must be an int.
tree
must be the name of the class.
grows
must be the name of an instance field.
6. A
constructor must have the same name as the class where it is declared. True or
false? Mark for Review
(1) Points
True (*)
False
7. If a
class is immutable then it must be abstract. True or false? Mark for Review
(1) Points
True
False
(*)
8. Which of
the following is a goal of the object model?
Mark for Review
(1) Points
(Choose
all correct answers)
Data
abstraction. (*)
Protecting
information and limiting other classes' ability to change or corrupt data. (*)
Providing
modular code that can be reused by other programs or classes. (*)
Concealing
implementation. (*)
9. Identify
the step (s) in creating a Triangle Applet that displays two triangles. Mark for Review
(1) Points
(Choose
all correct answers)
Draw the
triangle using the inherited fillPolygon method. (*)
Draw the
2nd triangle using the inherited fillPolygon method. (*)
Extend
Applet class to inherit all methods including paint. (*)
Override
the paint method to include the triangles. (*)
Run and
compile your code. (*)
10. How is it
possible for overloading to work? Mark
for Review
(1) Points
The code
has to be declared as private.
There is
no such thing as overloading.
The
interpreter doesn't care what you name your constructors.
Java
Virtual Machine searches until it finds a constructor name and argument type
match. (*)
11. A team is
working on a coding project. They desire that all portions of their code should
have access to the classes that they write. What access modifier should be used
for each class? Mark for Review
(1) Points
public
(*)
protected
private
default
All of
the above
12. It is
possible to overload a method that is not a constructor. True or False? Mark for Review
(1) Points
True (*)
False
13. A static
variable is always publicly available. True or false? Mark for Review
(1) Points
True
False
(*)
14. Static
methods can read instance variables. True or false? Mark for Review
(1) Points
True
False
(*)
15. Static
classes can exist as stand alone classes. True or false? Mark for Review
(1) Points
True
False
(*)
misi om.... numpang lewat jalan sini.
BalasHapuskali ada yg cocok. :D
Thanks for posting. Keep updating more and more java training
BalasHapusjava online training
في اساله مش موجوده لييييه
BalasHapus