You are on page 1of 23

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)
1. What is the output of the following lines of code?
int j=6,k=4,m=12,result;
result=j/m*k;
System.out.println(result);

Mark for
Review
(1) Points

2
0 (*)
48
24
Correct
2. Which line of Java code will assign the value of the square root of 11 to a
variable named a?

Mark for
Review
(1) Points

double a=11^(1/2);
double a=sqrt(11);
int a=Math.sqrt(11);
double a=Math.sqrt*11;
double a=Math.sqrt(11); (*)
Correct
3. A _______________ is used to organize Java related files.

Mark for
Review
(1) Points

Project
Workspace
Package (*)
Collection
Correct

4. You need to _______________ Java code to generate a .class file

Mark for
Review
(1) Points

Collect
Compile (*)
Package
Assemble
Correct
5. A workspace can not have more than one stored projects. True or false?

Mark for
Review
(1) Points

True
False (*)
Correct

6.
A perspective is described as:
(1) Points

Mark for Review

A combination of views and editors (*)

A combination of views and windows

A combination of editor tabs

None of the above

Correct

7.

Multiple windows are used when more than one file is open in the edit area. True or False?

Mark for Review


(1) Points

True

False (*)

Correct

8.

The String methods equals and compareTo perform similar functions and differ in their return

type. True or false?

Mark for

Review
(1) Points

True (*)

False

Correct

9.

The following program prints "Not Equal":

True or false?

Mark for

Review
(1) Points

True (*)

False

Incorrect. Refer to Section 4 Lesson 4.

10. What will the following code segment output?


String s="\\\n\"\n\\\n\"";
System.out.println(s);

Mark for

Review
(1) Points

\" \"

""\
""
\
""

\
"
\
" (*)

"
\
"
\
"
"

Correct
11.

The following program prints "Not Equal". True or false?

Mark for Review


(1) Points

True

False (*)

Correct

12.
String s1 = "abcdef";

Given the code

String s2 = "abcdef";
String s3 = new String(s1);
Which of the following would equate to false?
(1) Points

Mark for Review

s1 == s2

s1 = s2

s3 == s1 (*)

s1.equals(s2)

s3.equals(s1)

Correct

13.
form of a Java program?

Which of the two diagrams below illustrate the general

Mark for Review


(1) Points

Example A

Example B (*)

Correct

14.

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

Section 5
(Answer all questions in this section)
15.

How would you use the ternary operator to rewrite this

if statement?
if (balance < 500)<
fee = 10;
else
fee = 0;
(1) Points

Mark for Review

fee = ( balance < 500) ? 0 : 10;

fee= ( balance < 500) ? 10 : 0; (*)

fee = ( balance >= 5) ? 0 : 10;

fee = ( balance >= 500) ? 10 : 0;

fee = ( balance > 5) ? 10 : 0;

Correct

16.

This keyword is used to instruct specific code when the input for a switch statement that

does not match any of the cases.


(1) Points

Mark for Review

Switch

Case

Break

Default (*)

None of the above

Correct

17.

switch statements work on all input types including, but not limited to, int, char, and

String. True or false?

Mark for

Review
(1) Points

True

False (*)

Correct

18.

All of the following are essential to initializing a for loop, except which one?

Mark for Review


(1) Points

Initializing the iterator(i).

Having a conditional statement.

Updating the counter.

Having an if statement. (*)

Correct

19.

In a for loop the counter is not automatically incremented after each loop iteration. Code

must be written to increment the counter. True or false?

Mark for

Review
(1) Points

True (*)

False

Correct

20.

Updating the input of a loop allows you to implement the code with the next element

rather than repeating the code always with the same element. True or false?

Mark for

Review
(1) Points

True (*)

False

Correct
Section 6
(Answer all questions in this section)

21. Of the options below, what is the fastest run-time?


Review
(1) Points

n^2

lg(n) (*)

Mark for

n*lg(n)

Correct

22. Selection sort is a sorting algorithm that involves finding the minimum value in the list,
swapping it with the value in the first position, and repeating these steps for the remainder of the list.
True or false?

Mark for

Review
(1) Points

True (*)

False

Correct

23. Why might a sequential search be inefficient?

Mark for

Review
(1) Points

It utilizes the "divide and conquer" method, which makes the algorithm more
error prone.

It requires incrementing through the entire array in the worst case, which is
inefficient on large data sets. (*)

It involves looping through the array multiple times before finding the value,
which is inefficient on large data sets.

It is never inefficient.

Correct

24. Big-O Notation is used in Computer Science to describe the performance of Sorts and Searches
on arrays. True or false?
Review
(1) Points

Mark for

True (*)

False

Correct

25. If an exception is thrown by a method, where can the catch for the exception be?
Mark for Review
(1) Points

There does not need to be a catch in this situation.

The catch must be in the method that threw the exception.

The catch can be in the method that threw the exception or in any other
method that called the method that threw the exception. (*)

The catch must be immediately after the throw.

Incorrect. Refer to Section 6 Lesson 3.


26.

Which of the following statements add all of the elements of the one dimensional

array prices, and then prints the sum to the screen?


(1) Points

int total = 0;
for(int i = 0; i
total+=prices[i];

int total = 0;
for(int i = 0; i
total+=prices[i];
System.out.println(total); (*)

int total = 0;
for(int i = 1; i
total = total+prices[i];
System.out.println(prices);

Mark for Review

int total = 0;
for(int i = 0; i
total+=prices[i];
System.out.println(prices);

Correct

27.

int[] y = new int[5];


(1) Points

The following array declaration is valid. True or false?

Mark for Review

True (*)

False

Correct

28.

What is the output of the following segment of code?

M
ark for Review
(1) Points

321111

11 (*)

111

1111

This code doesn't compile.

Correct

29.

What is the output of the following segment of code if the command line

arguments are "a b c d e f"?

M
ark for Review
(1) Points

6 (*)

This code doesn't compile.

Correct

Section 7
(Answer all questions in this section)
30.

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 (*)

Incorrect. Refer to Section 7 Lesson 3.


Section 7
(Answer all questions in this section)
31.

There is only one

copy a static class variable in the JVM. True or false?

Mark for

Review
(1) Points

True (*)

False

Correct

32.

A linear recursive

method directly calls how many copies of itself in the recursive case?

Mark for

Review
(1) Points

1 (*)

2 or more

Correct

33.

Where should the

constructor for a superclass be called?


Review
(1) Points

Anywhere inside the subclass.

Inside the main method of the subclass.

The last line in the constructor of the subclass.

Mark for

The first line of the constructor in the subclass. (*)

The super constructor does not need to be called inside the subclass.

Correct

34.

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 allows subclasses to access methods, data, and constructors


from their parent class. (*)

A keyword that signals the end of a program.

A keyword that allows access from anywhere.

Correct

35.

What is the Java

Applet?
(1) Points

Mark for Review

(Choose all correct answers)

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

There is no such thing as a Java Applet.

Incorrect. Refer to Section 7 Lesson 4.


36.
(1) Points

What is encapsulation?

Mark for Review

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

Correct

37.

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.

Correct

38.
Mark for Review
(1) Points

Which of the following correctly defines overloading?

Having more than one constructor with the same name but different
arguments. (*)

Having more than one constructor with different names and the same
arguments.

A variable argument method that returns an array.

A type of access specifier that only allows access from inside the same class.

Correct

39.

Which of the following are access modifiers?

Mark for Review


(1) Points
(Choose all correct answers)

protected (*)

public (*)

secured

default (no access modifier) (*)

private (*)

Correct

40.
False?
(1) Points

It is possible to overload a method that is not a constructor. True or


Mark for Review

True (*)

False

Correct
41.

Choose the correct implementation of a public access modifier for the method

divide.
(1) Points

Mark for Review

divide(int a, int b, public) {return a/b;}

public divide(int a, int b) {return a/b;} (*)

divide(int a, int b) {public return a/b;}

divide(public int a, public int b) {return a/b;}

Correct

42.

Abstract classes can be instantiated. True or false?

Mark for Review


(1) Points

True

False (*)

Correct

43.

Which of the following can be declared final?

Mark for Review


(1) Points

Classes

Methods

Local variables

Method parameters

All of the above (*)

Correct

44.

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

Correct

45.
Mark for Review
(1) Points

Which of the following creates a method that returns a boolean value?

(*)

None of the above.

Correct
46.
method is called?

What value will return for j when the setValue

Mark for Review


(1) Points

31

32

10

11 (*)

Correct

47.

If the return type from a method is boolean then

2.5 is a valid return value. True or false?


(1) Points

Mark for Review

True

False (*)

Correct

48.
least one parameter. True or false?
(1) Points

The constructor method must always have at


Mark for Review

True

False (*)

Correct

49.

Which of the following creates an instance of the

class below?

Mark for Review


(1) Points

ThisClass t=new ThisClass();

ThisClass t;

ThisClass t=new ThisClass(3,4);

ThisClass t=new ThisClass(5); (*)

Correct

50.

What is wrong with the following class

declaration?
class Account{ ;
private int number;
private String name;;
public Account;
}
Mark for Review
(1) Points

Classes cannot include strings.

Classes cannot include mixed data types.

The constructor method has no definition. (*)

There is nothing wrong.

Correct

You might also like