Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 4
(Answer all questions in this section)
1The following code is an example of instantiating a String object:
Mark for
.
Review
String str = String( "Hello" );
(1) Points
True or false?
True
False (*)
Correct
2Consider the following code snippet. What is printed?
Mark for
. Review
(1) Points
55555
The code does not compile.
An ArrayIndexOutofBoundsException is thrown.
87668 (*)
AtlanticPacificIndianArcticSouthern
Correct
3Consider the following code snippet. What is printed?
Mark for
.
Review
String river = new String("Hudson"); System.out.println(river.length());
(1) Points
6 (*)
Hudson
8
river
7
Correct
4Suppose that str1 and str2 are two strings. Which of the statements or
Mark for
. expressions are valid?
Review
(1) Points
Str1 -= str2;
str1 >= str2
String str3 = str1 - str2;
str1 += str2; (*)
Correct
5Consider the following code snippet. What is printed?
Mark for
. Review
String ocean = new String("Atlantic Ocean"); (1) Points
System.out.println(ocean.indexOf('a'));
2
3 (*)
12
0
11
Correct
Page 1 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 4
(Answer all questions in this section)
6. Which of the following defines a driver class?
Mark for Review
(1) Points
Contains a main method and other static methods. (*)
Contains classes that define objects.
Contains a main method, a package, static methods, and classes
that define objects.
None of the above.
Correct
7. The following defines a package keyword:
Mark for Review
(1) Points
Provides the compiler information that identifies outside classes
used within the current class.
Defines where this class lives relative to other classes, and
provides a level of access control. (*)
Precedes the name of the class.
Correct
8. Which of the following is not a legal name for a variable?
Mark for Review
(1) Points
4geeks (*)
to_be_or_not_to_be
dgo2sleep
R2d2
Correct
9. Which of the following statements displays 12345?
Mark for Review
(1) Points
I. System.out.println( 123 * 100 + 45);
II. System.out.println("123" + 45);
III. System.out.println( 12 + "345");
All of the above. (*)
I only.
I and II only.
II and III only.
None of the above.
Correct
10.Which of the following is a legal identifier?
Mark for Review
(1) Points
grand Total
boolean
7up
apple (*)
Correct
Page 2 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 4
(Answer all questions in this section)
11.A workspace can have one or more stored projects. True or false?
Mark for Review
(1) Points
True (*)
False
Correct
12.In a project, 2 of the classes must contain a main method. True or
Mark for Review
False?
(1) Points
True
False (*)
Correct
13.Eclipse does not provide views to help you navigate a hierarchy of
Mark for Review
information. True or False? (1) Points
True
False (*)
Correct
14.Eclipse provides an edit area to help you navigate a hierarchy of
Mark for Review
information. True or False?
(1) Points
True
False (*)
Correct
15.Four variables are required to support a conversion of one unit of
Mark for Review
measure to another unit of measure. True or False? (1) Points
True
False (*)
Correct
Page 3 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 5
(Answer all questions in this section)
16.Which of the following are relational operators in Java?
Mark for Review
(1) Points
(Choose all correct answers)
< (*)
<= (*)
=
!= (*)
All of the above.
Correct
17.Which of the following could be a reason to use a switch statement
Mark for Review
in a Java program?
(1) Points
Because it allows the user to enter an input in the console screen
and prints out a message that the user input was successfully
read in.
Because it allows the program to run certain segments of code
and neglect to run others based on the input given. (*)
Because it terminates the current loop.
Because it allows the code to be run through until a certain
conditional statement is true.
Correct
18.What will print if the following Java code is executed?
Mark for Review
(1) Points
if ((5.1 > 4.3 && 6.2 < 8.4) && !(7.2 < 3.5 || 1.2 == 2.1 || 2.2 !=
2.25))
System.out.print("TRUE");
else
System.out.print("FALSE");
True
False (*)
Correct
19.In a for loop, the counter is automatically incremented after each
Mark for Review
loop iteration. True or False?
(1) Points
True
False (*)
Correct
20.In a for loop the counter is not automatically incremented after each
Mark for Review
loop iteration. Code must be written to increment the counter. True (1) Points
or false?
True (*)
False
Correct
Page 4 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 5
(Answer all questions in this section)
21.Which of the following are types of loops in Java?
Mark for Review
(1) Points
(Choose all correct answers)
if/else
do-while (*)
while (*)
for (*)
Correct
Section 6
(Answer all questions in this section)
22.Which of the following correctly matches the symbol with its
Mark for Review
function?
(1) Points
(Choose all correct answers)
= (single equals sign) compares the value of primitive types such
as int or char.
.equals() compares the value of non-primitive objects. (*)
== (two equal signs) compares the values of non-primitive
objects.
== (two equal signs) compares values of primitive types such as
int or char. (*)
== (two equal signs) compares the memory location of non-
primitive objects. (*)
Correct
23.Suppose you are writing a program where the user is prompted to the
Mark for Review
give coordinates where they believe the princess is inside of the
(1) Points
castle.
Your program moves the prince to the coordinates that the user
specified. If the princess is not found at those coordinates, the user is
given a clue that helps them guess coordinates closer to the princess.
The user is allowed to enter their new guess of where the princess is.
Assume your program does not take into consideration the
possibility that the user may enter coordinates outside of the castle
where the princess could not be. What would be the result of the user
entering coordinates outside of the castle? How could this be handled
in your code?
(Choose all correct answers)
An error would occur. Errors cannot be handled by code.
An exception would occur but could not be handled inside your
code. The user would have to restart the program and enter
proper coordinates.
An exception would occur. This could be handled by throwing
an exception in your code if the user enters invalid coordinates.
When the exception is caught, the user could be prompted to
enter coordinates within the given range of the castle. (*)
An exception would occur. This could be handled by throwing
the exception in your code if the user enters invalid coordinates.
When the exception is caught, the prince could be moved to the
coordinate inside the castle that is closest to those that the user
specified. (*)
Correct
24.Which line of code shows the correct way to throw an exception?
Mark for Review
(1) Points
throw Exception("Array index is out of bounds");
throws new Exception("Array index is out of bounds");
throw new Exception("Array index is out of bounds"); (*)
new throw Exception("Array index is out of bounds");
Correct
25.A logic error occurs if an unintentional semicolon is placed at the
Mark for Review
end of a loop initiation because the interpreter reads this as the only
(1) Points
line inside the loop, a line that does nothing. Everything that follows
the semicolon is interpreted as code outside of the loop. True or
false?
True
False (*)
Correct
Page 5 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 6
(Answer all questions in this section)
2double array[] = new double[8];
Mark for
6.
Review
After execution of this statement, which of the following are true?
(1) Points
array[4] is null
array[2] is 8
array[0] is undefined
array.length is 8 (*)
Correct
2What is the output of the following segment of code?
Mark for
7.
Review
int array[][] = {{1,2,3},{3,2,1}};
(1) Points
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
System.out.print(2*array[1][1]);
123321
222222
444444 (*)
246642
This code doesn't compile.
Correct
2What is the output of the following segment of code?
Mark for
8.
Review
(1) Points
262423242322
1286864 (*)
666666
This code does not compile.
643432
Correct
2What is the output of the following segment of code?
Mark for
9.
Review
(1) Points
555555
987654
456789
777777 (*)
This code doesn't compile.
Correct
Section 7
(Answer all questions in this section)
3The following code creates an object of type Animal. True or false?
Mark for
0. Review
Animal a=new Animal(); (1) Points
True (*)
False
Correct
Page 6 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 7
(Answer all questions in this section)
3Which of the following is true?
Mark for
1. Review
(1) Points
The more comments in a program, the faster the program runs.
A class always has a constructor (possibly automatically supplied by
the java compiler). (*)
int is the name of a class available in the package java.lang.
Instance variable names may only contain letters and digits.
In Java, a method declared public generates a compilation error.
Correct
3Which of the following creates a method that compiles with no errors in
Mark for
2.the class?
Review
(1) Points
(*)
All of the above.
None of the above.
Correct
3If the return type from a method is boolean then 2.5 is a valid return value.
Mark for
3.True or false?
Review
(1) Points
True
False (*)
Correct
3A constructor is used to create objects. True or false?
Mark for
4.
Review
(1) Points
True (*)
False
Correct
3Consider:
Mark for
5. Review
public class MyClass{ (1) Points
public MyClass()
{/*code*/}
// more code...}
To instantiate MyClass, what would you write?
MyClass m = new MyClass(); (*)
MyClass m = new MyClass;
MyClass m = MyClass();
MyClass m = MyClass;
Correct
Page 7 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 7
(Answer all questions in this section)
3Which of the following is true?
Mark for
1.
Review
(1) Points
The more comments in a program, the faster the program runs.
A class always has a constructor (possibly automatically supplied by
the java compiler). (*)
int is the name of a class available in the package java.lang.
Instance variable names may only contain letters and digits.
In Java, a method declared public generates a compilation error.
Correct
3Which of the following creates a method that compiles with no errors in
Mark for
2.the class?
Review
(1) Points
(*)
All of the above.
None of the above.
Correct
3If the return type from a method is boolean then 2.5 is a valid return value.
Mark for
3.True or false?
Review
(1) Points
True
False (*)
Correct
3A constructor is used to create objects. True or false?
Mark for
4.
Review
(1) Points
True (*)
False
Correct
3Consider:
Mark for
5.
Review
public class MyClass{
(1) Points
public MyClass()
{/*code*/}
// more code...}
To instantiate MyClass, what would you write?
MyClass m = new MyClass(); (*)
MyClass m = new MyClass;
MyClass m = MyClass();
MyClass m = MyClass;
Correct
Page 7 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 7
(Answer all questions in this section)
41.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 programming philosophy that promotes protecting data and
hiding implementation in order to preserve the integrity of data
and methods.
A helpful term used to conceptualize the relationships among
nodes or leaves in an inheritance hierarchy. (*)
Correct
42.What is the Java Applet?
Mark for Review
(1) Points
(Choose all correct answers)
There is no such thing as a Java Applet.
A graphic visual included in Java. (*)
A web-based Java program that is embedded into a web browser.
(*)
It is the virtual machine that translates Java code into a
representation that the computer can understand.
Correct
43.Which of the following is the correct way to call an overriden
Mark for Review
method needOil() of a super class Robot in a subclass
(1) Points
SqueakyRobot?
SqueakyRobot.needOil();
needOil(Robot);
super.needOil(); (*)
Robot.needOil(SqueakyRobot);
Correct
44.Which of the following correctly defines a subclass (or child class)?
Mark for Review
(1) Points
The most general class of a hierarchy system.
A class that passes down its methods to more specialized classes.
A class that inherits methods and fields from a more general
class. (*)
A keyword that allows or restricts access to data and methods.
Correct
45.Static variables can't use which of the following modifiers?
Mark for Review
(1) Points
protected
friendly (*)
default
private
public
Correct
Page 9 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 7
(Answer all questions in this section)
46.You can assign new values to static variables by prefacing them with
Mark for Review
the this keyword and a dot or period. True or false? (1) Points
True (*)
False
Correct
47.Which of the following statements about static methods is true?
Mark for Review
(1) Points
They exist once in each instance.
They cannot access static variables declared outside the method.
They can be overridden by a subclass.
They can access any instance variable.
They exist once per class. (*)
Correct
48.Would this code be correct if a Dog is a HousePet? Why or Why
Mark for Review
not?
(1) Points
HousePet Scooby = new Dog();
Yes, because polymorphism allows this since Dog is a subclass
of HousePet. (*)
Maybe. There is no way to tell without seeing the methods for
Dog and the methods for HousePet.
No, because ref must be declared either a HousePet or a Dog, but
not both.
Yes, because it is an abstract class.
Correct
49.Identify the correct way to declare an abstract class.
Mark for Review
(1) Points
abstract public class ClassName{...}
public class abstract ClassName(...)
public abstract class ClassName{...} (*)
public abstract ClassName(...)
Correct
50.Which of the following would be most beneficial for this scenario?
Mark for Review
(1) Points
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?
By overriding the methods of Book.
Using polymorphism. (*)
By ignoring the subject type and initializing all the book as
objects of type Book.
This is not possible. Joe must find another way to collect the
books.
Correct
Page 10 of 10
Test: Java Fundamentals Final Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.
Section 7
(Answer all questions in this section)
36.How is it possible for overloading to work?
Mark for Review
(1) Points
There is no such thing as overloading.
The interpreter doesn't care what you name your constructors.
The code has to be declared as private.
Java Virtual Machine searches until it finds a constructor name
and argument type match. (*)
Correct
37.Which of the following is the definition of a constructor?
Mark for Review
(1) Points
A way to call a method with a variable number of arguments
using an elipse.
A variable in a method declaration that gets passed into the
method.
A special method that is used to assign initial values to instance
variables in a class. (*)
A keyword that specifies accessibility of code.
Correct
38.Which of the following is the correct definition of a parameter?
Mark for Review
(1) Points
A type of access specifier.
A variable in a method declaration that gets passed into the
method. (*)
A keyword that specifies accessibility of code.
It is used to assign initial values to instance variables of a class;
the structure is very similar to that of a method.
A way to call a method with a variable number of arguments
using an elipse.
Correct
39.Which of the following is the definition for a variable argument
Mark for Review
method?
(1) Points
A type of argument that enables calling the same method with a
different number of arguments. (*)
A way to create a new class.
Having more than one constructor with the same name but
different arguments.
Specifies accessibility to code.
Correct
40.A team is working on a coding project. They desire that all portions
Mark for Review
of their code should have access to the classes that they write. What
(1) Points
access modifier should be used for each class?
public (*)
protected
private
default
All of the above
Correct
Page 8 of 10