You are on page 1of 20

Test: JF Java Fundamentals Final Exam

Section 4
(Answer all questions in this section)

1. Given the code below, which of the following calls are valid?

String s = new String("abc");

Mark for Review

(1) Points

s.toUpperCase()(*)

s.substring(2)(*)

s.trim()(*)

s.replace('a', 'A')(*)

s.setCharAt(1,'A')

Incorrect. Refer to Section 4 Lesson 4.

2. Consider the following code snippet. What is printed?

String ocean = new String("Atlantic Ocean");


System.out.println(ocean.indexOf('a'));

Mark for Review

(1) Points

12

11
3 (*)
Correct

3. Given the code:

String s = new String("abc");

Which of the following statements will change the length of s to the largest length?

Mark for Review

(1) Points

s.trim()

s.replace("a", "aa")

s.substring(2)

s.toUppercase()

None of the above will change the length of s. (*)


Correct

4. The following code is an example of a correct initialization statement:

char c="c";

Mark for Review

(1) Points

True

False (*)
Correct

5. Consider the following code snippet


String forest = new String("Black");
System.out.println(forest.length());

What is printed?

Mark for Review

(1) Points

Black

Forest

5 (*)

7
Correct

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 class keyword:


Mark for Review

(1) Points
Provides the compiler information that identifies outside classes used within
the current class.
Precedes the name of the class. (*)
Defines where this class lives relative to other classes, and provides a level
of access control.
Correct
8. Eclipse does not provide views to help you navigate a hierarchy of
information. True or False?
Mark for Review

(1) Points
True
False (*)
Correct

9. The ______________ is the location into which you will store and save
your files.
Mark for Review

(1) Points
Perspective
Workspace (*)
Editor
None of the above
Correct

10. In Eclipse, when you run a Java Application, the results may be
displayed in the Console View. True or False?
Mark for Review

(1) Points
True (*)
False
Correct
Previous
11. What is the purpose of the Eclipse Editor Area and Views?
Mark for Review

(1) Points
To choose the file system location to delete a file.
To modify elements.(*)

To navigate a hierarchy of information.(*)


Correct
12. You can return to the Eclipse Welcome Page by choosing Welcome from
what menu?
Mark for Review

(1) Points
File
Help (*)
Edit
Close
Correct

13. Which of the following statements correctly assigns "3 times 10 to the


4th power" to the variable number?
Mark for Review

(1) Points
double number=3e4; (*)
double number=3(e4);
double number=3*10^4;
double number=3*10e4;
Correct

14. Which line of code does not assign 3.5 to the variable x?


Mark for Review

(1) Points
x=7.0/2.0;
x=3.5;
double x=3.5
3.5=x; (*)
Correct

15. Which of the following is the name of a Java primitive data type?


Mark for Review

(1) Points
Rectangle
String
int (*)
Object
Correct
Previous
Section 5
(Answer all questions in this section)

16. Which of the following are types of loops in Java?

Mark for Review

(1) Points

while(*)

do-while(*)

for(*)

if/else

Correct

17. One advantage to using a while loop over a for loop is that a while loop always
has a counter. True or false?

Mark for Review

(1) Points

True

False (*)
Correct

18. In the code fragment below, the syntax for the for loop's initialization is correct.
True or false?

public class ForLoop {


public static void main (String args[])
{
for ((int 1=10) (i<20) (i++))
{
System.out.println ("i: " + i);
}
}
}

Mark for Review

(1) Points

True

False (*)
Correct

19. The three logic operators in Java are:

Mark for Review

(1) Points

&&, ||, ! (*)

&,|,=

&&,!=,=

!=,=,==
Correct

20. How would you use the ternary operator to rewrite this if statement?

if (gender == "female") System.out.print("Ms.");


else
System.out.print("Mr.");

Mark for Review


(1) Points

System.out.print( (gender == "female") ? "Mr." : "Ms." );

(gender == "female") ? "Ms." : "Mr." ;

System.out.print( (gender == "female") ? "Ms." : "Mr." ); (*)

(gender == "female") ? "Mr." : "Ms." ;


Correct
Previous

21. The following prints Yes on the screen. True or false?

Mark for Review

(1) Points

True

False (*)
Correct

Section 6
(Answer all questions in this section)

22. What is the output of the following segment of code?


Mark for Review

(1) Points

This code doesn't compile.

456789

777777 (*)

987654

555555
Correct

23. What is the output of the following segment of code?

Mark for Review

(1) Points

642246 (*)

642

312213

This code doesn't compile.

321123
Correct
24. What is the output of the following segment of code if the command line
arguments are "apples oranges pears"?

Mark for Review

(1) Points

3 (*)

This code does not compile.

0
Correct

25. Which of the following statements adds 5 to every element of the one


dimensional array prices and then prints it to the screen?

Mark for Review

(1) Points

for(int i=1;i<prices.length;i++)
System.out.println(prices[i]+5);

System.out.println(prices[i]+5);

for(int i=0;i<prices.length;i++)
System.out.println(prices[i]+5); (*)
for(int i=0;i<prices.length;i++)
System.out.println(prices[1]+5);
Correct
Previous

Section 6
(Answer all questions in this section)

26. Which of the following could be a reason to throw an exception?

Mark for Review

(1) Points

To make the user interface harder to navigate.

To eliminate exceptions from disrupting your program. (*)

You have encountered a Stack Overflow Error.

You have a fatal error in your program.


Correct

27. Which of the following defines an Exception?

Mark for Review

(1) Points

Code that has no errors and therefore runs smothly.

An interpreter reading your code.

A very severe non-fixable problem with interpreting and running your code.

A problem that can be corrected or handled by your code. (*)


Correct

28. What are exceptions used for in Java?


Mark for Review

(1) Points

Helping the interpreter compile code quicker and handle user interfaces.

Correcting mistakes made in your code and handling extraordinary cases. (*)

Making the program easier to use for the user and reducing the possibilities of
errors occuring.

Exceptions have no use, they are just a part of the Java language.
Incorrect. Refer to Section 6 Lesson 2.

29. Which of the following correctly matches the symbol with its function?

Mark for Review

(1) Points

= (single equals sign) compares the value of primitive types such as int or char.

== (two equal signs) compares values of primitive types such as int or char.(*)

.equals() compares the value of non-primitive objects.(*)

== (two equal signs) compares the memory location of non-primitive objects.(*)

== (two equal signs) compares the values of non-primitive objects.

Correct

Section 7
(Answer all questions in this section)

30. Public static variables can't have their value reset by other classes. True or
false?

Mark for Review

(1) Points

True
False (*)
Correct
Previous

Section 7
(Answer all questions in this section)

31. Which of the following access modifiers doesn't work with a static variable?

Mark for Review

(1) Points

public

default

friendly (*)

protected

private
Correct

32. A final static variable can change at runtime. True or false?

Mark for Review

(1) Points

True

False (*)
Correct

33. Which of the following correctly defines a superclass (or parent class)?

Mark for Review


(1) Points

A keyword that allows or restricts access to data and methods.

The most specific class of a hierarchy system of classes.

A class that inherits methods and fields from a more general class.

A class that passes down its methods to more specialized classes. (*)


Correct

34. Which of the following demonstrates the correct way to create an applet


Battlefield?

Mark for Review

(1) Points

public class Battlefield(Applet){...}

public class Applet extends Battlefield{...}

public class Battlefield extends Applet{...} (*)

public Applet Battlefield{...}


Correct

35. What is the Java Applet?

Mark for Review

(1) Points

There is no such thing as a Java Applet.

It is the virtual machine that translates Java code into a representation that the computer can

understand.

A web-based Java program that is embedded into a web browser.(*)

A graphic visual included in Java.(*)


Correct
Previous

36. 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)
Correct

37. Which of the following can be declared final?


Mark for Review

(1) Points
Classes
Methods
Local variables
Method parameters
All of the above (*)
Correct

38. 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
Correct

39. 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.
This is not possible. Joe must find another way to collect the books.
Using polymorphism. (*)
By ignoring the subject type and initializing all the book as objects of type
Book.
Correct

40. Which of the following correctly defines overloading?


Mark for Review

(1) Points
Having more than one constructor with the same name but different
arguments. (*)
A variable argument method that returns an array.
A type of access specifier that only allows access from inside the same class.

Having more than one constructor with different names and the same
arguments.
Correct
Previous
41. Which of the following are access modifiers?
Mark for Review

(1) Points
secured
default (no access modifier)(*)

protected(*)

private(*)

public(*)
Correct
42. 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
Correct

43. 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.
Correct

44. Which of the following specifies accessibility to variables, methods, and


classes?
Mark for Review

(1) Points
Overload constructors
Access modifiers (*)
Parameters
Methods
Correct
45. What value will be returned when the setValue method is called?

Mark for Review

(1) Points
38
35
36
37 (*)
Correct
Previous
46. Which of the following creates a Object from the Animal class listed
below?

Mark for Review

(1) Points
Animal dog=new Animal(50);
Animal dog=new Animal();
Animal dog=Animal(50,30);
Animal dog=new Animal(50,30); (*)
Correct

47. The following statement compiles and executes. What do you know for
certain?

tree.grows(numFeet);
Mark for Review

(1) Points
grows must be the name of a method. (*)
numFeet must be an int.
tree must be a method.
grows must be the name of an instance field.
tree must be the name of the class.
Correct

48. 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 (*)
Correct

49. Which of the following creates a method that compiles with no errors in


the class?
Mark for Review

(1) Points
(*)

All of the above.


None of the above.
Incorrect. Refer to Section 7 Lesson 1.

50. 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 (*)
Correct
Previous

You might also like