WorksheetsCPT 236 Final Exam Review
Total questions: 203
Worksheet time: 3hrs 2mins
________ contains predefined classes and interfaces for developing Java programs.
Java language specification
Java API
Java JDK
Java IDE
____________ is an operating system.
Java
C++
Windows
Visual Basic
Ada
________ is a technical definition of the language that includes the syntax and semantics of the Java programming language.
Java language specification
Java API
Java JDK
Java IDE
Which of the following statements is correct?
Every line in a program must end with a semicolon.
Every statement in a program must end with a semicolon.
Every comment line must end with a semicolon.
Every method must end with a semicolon.
Every class must end with a semicolon.
________ is the physical aspect of the computer that can be seen.
Hardware
Software
Operating System
Application Program
If a program compiles fine, but it produces incorrect result, then the program suffers __________.
a compilation error
a runtime error
a logic error
The extension name of a Java source code file is
.java
.obj
.class
.exe
Every statement in Java ends with ________.
a semicolon (;)
a comma (,)
a period (.)
an asterisk (*)
Which JDK command is correct to run a Java application in ByteCode.class?
________ is architecture-neutral.
Java
C++
C
Ada
Pascal
The extension name of a Java bytecode file is
.java
.obj
.class
.exe
________ is not an object-oriented programming language.
Java
C++
C
C#
Python
Which of the following is not permanent storage devices?
floppy disk
hard disk
flash stick
CD-ROM
main memory
_____________ is a program that runs on a computer to manage and control a computer's activities.
Operating system
Java
Modem
Interpreter
Compiler
Java compiler translates Java source code into _________.
Java bytecode
machine code
assembly code
another high-level language code
The main method header is written as:
public static void main(string[] args)
public static void Main(String[] args)
public static void main(String[] args)
public static main(String[] args)
public void main(String[] args)
________ provides an integrated development environment (IDE) for rapidly developing Java programs. Editing, compiling, building, debugging, and online help are integrated in one graphical user interface.
Java language specification
Java API
Java JDK
Java IDE
__________ is the brain of a computer.
Hardware
CPU
Memory
Disk
___________ translates high-level language program into machine language program.
An assembler
A compiler
CPU
The operating system
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count++ < 10) {
System.out.println("Welcome to Java");
}
8
9
10
11
0
Do the following two statements in (I) and (II) result in the same value in sum?
(I):
for (int i = 0; i < 10; ++i) {
sum += i;
}
(II):
for (int i = 0; i < 10; i++) {
sum += i;
}
yes
no
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
8
9
10
11
0
What is the value in count after the following loop is executed?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);
8
9
10
11
0
How many times is the println statement executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
System.out.println(i * j);
100
20
10
45
What is the number of iterations in the following loop?
for (int i = 1; i < n; i++) {
// iteration
}
2*n
n
n - 1
n + 1
Which of the following loops prints "Welcome to Java" 10 times?
for (int count = 1; count <= 10; count++) {
System.out.println("Welcome to Java");
}
for (int count = 0; count < 10; count++) {
System.out.println("Welcome to Java");
}
for (int count = 1; count < 10; count++) {
System.out.println("Welcome to Java");
}
for (int count = 0; count <= 10; count++) {
System.out.println("Welcome to Java");
}
What is the output of the following code?
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
x is 0
x is 1
x is 2
x is 3
x is 4
What is y after the following for loop statement is executed?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += 1;
}
9
10
11
12
How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 10);
8
9
10
11
0
How many times is the println statement executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < i; j++)
System.out.println(i * j)
100
20
10
45
How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
count++;
} while (count < 10);
8
9
10
11
0
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?
add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0.
How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (++count < 10);
8
9
10
11
0
Is the following loop correct?
for ( ; ; );
yes
no
What is the output for y?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += i;
}
System.out.println(y);
10
11
12
13
45
What is the value of balance after the following code is executed?
int balance = 10;
while (balance >= 1) {
if (balance < 9)
break;
balance = balance - 9;
}
-1
0
1
2
What will be displayed when the following code is executed?
int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + " ");
}
6 3 0
6 3
3 0
3 0 -3
0 -3
The following loop displays _______________.
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
i++;
}
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5
1 3 5 7 9
2 4 6 8 10
What is the output after the following loop terminates?
int number = 25;
int i;
boolean isPrime = true;
for (i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
System.out.println("i is " + i + " isPrime is " + isPrime);
i is 5 isPrime is true
i is 5 isPrime is false
i is 6 isPrime is true
i is 6 isPrime is false
Does the method call in the following method cause compile errors?
public static void main(String[] args) {
Math.pow(2, 4);
}
yes
no
Which of the following is the best for generating random integer 0 or 1?
(int)Math.random()
(int)Math.random() + 1
(int)(Math.random() + 0.5)
(int)(Math.random() + 0.2)
(int)(Math.random() + 0.8)
Consider the following incomplete code:
public class Test {
public static void main(String[] args) {
System.out.println(f(5));
}
public static int f(int number) {
// Missing body
}
}
The missing method body should be ________.
return "number";
System.out.println(number);
System.out.println("number");
return number;
Analyze the following code:
public class Test {
public static void main(String[] args) {
System.out.println(xMethod(5, 500L));
}
public static int xMethod(int n, long l) {
System.out.println("int, long");
return n;
}
public static long xMethod(long n, long l) {
System.out.println("long, long");
return n;
}
}
The program displays int, long followed by 5.
The program displays long, long followed by 5.
The program runs fine but displays things other than 5.
The program does not compile because the compiler cannot distinguish which xmethod to invoke.
__________ is to implement one method in the structure chart at a time from the top to the bottom.
Bottom-up approach
Top-down approach
Bottom-up and top-down approach
Stepwise refinement
Which of the following should be defined as a void method?
Write a method that prints integers from 1 to 100.
Write a method that returns a random integer from 1 to 100.
Write a method that checks whether a number is from 1 to 100.
Write a method that converts an uppercase letter to lowercase.
Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.
a heap
storage area
a stack
an array
You should fill in the blank in the following code with ______________.
public class Test {
public static void main(String[] args) {
System.out.print("The grade is ");
printGrade(78.5);
System.out.print("The grade is ");
printGrade(59.5);
}
public static __________ printGrade(double score) {
if (score >= 90.0) {
System.out.println('A');
}
else if (score >= 80.0) {
System.out.println('B');
}
else if (score >= 70.0) {
System.out.println('C');
}
else if (score >= 60.0) {
System.out.println('D');
}
else {
System.out.println('F');
}
}
}
int
double
boolean
char
void
Suppose your method does not return any value, which of the following keywords can be used as a return type?
void
int
double
public
None of the above
(int)(Math.random() * (65535 + 1)) returns a random number __________.
between 1 and 65536
between 1 and 65535
between 0 and 65535
between 0 and 65536
What is k after the following block executes?
{
int k = 2;
nPrint("A message", k);
}
System.out.println(k);
0
1
2
k is not defined outside the block. So, the program has a compile error
Does the return statement in the following method cause compile errors?
public static void main(String[] args) {
int max = 0;
if (max != 0)
System.out.println(max);
else
return;
}
yes
no
You should fill in the blank in the following code with ______________.
public class Test {
public static void main(String[] args) {
System.out.print("The grade is " + getGrade(78.5));
System.out.print("\nThe grade is " + getGrade(59.5));
}
public static _________ getGrade(double score) {
if (score >= 90.0)
return 'A';
else if (score >= 80.0)
return 'B';
else if (score >= 70.0)
return 'C';
else if (score >= 60.0)
return 'D';
else
return 'F';
}
}
int
double
boolean
char
void
All Java applications must have a method __________.
public static Main(String[] args)
public static Main(String args[])
public static void main(String[] args)
public void main(String[] args)
public static main(String[] args)
When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.
method invocation
pass by value
pass by reference
pass by name
__________ is a simple but incomplete version of a method.
A stub
A main method
A non-main method
A method developed using top-down approach
Arguments to methods always appear within __________.
brackets
parentheses
curly braces
quotation marks
The signature of a method consists of ____________.
method name
method name and parameter list
return type, method name, and parameter list
parameter list
Assume int[] t = {1, 2, 3, 4}. What is t.length?
0
3
4
5
Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))?
{1, 20, 30, 40, 50}
[1, 20, 30, 40, 50]
{1 20 30 40 50}
[1 20 30 40 50]
What is output of the following code:
public class Test {
public static void main(String[] args) {
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}
1 2 3 4 5 6
2 3 4 5 6 6
2 3 4 5 6 1
1 1 1 1 1 1
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.
0
1
2
3
4
The JVM stores the array in an area of memory, called _______, which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order.
stack
heap
memory block
dynamic memory
The reverse method is defined in this section. What is list1 after executing the following statements?
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
list1 is 1 2 3 4 5 6
list1 is 6 5 4 3 2 1
list1 is 0 0 0 0 0 0
list1 is 6 6 6 6 6 6
In the following code, what is the output for list1?
public class Test {
public static void main(String[] args) {
int[] list1 = {1, 2, 3};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
for (int i = 0; i < list1.length; i++)
System.out.print(list1[i] + " ");
}
}
1 2 3
1 1 1
0 1 2
0 1 3
The __________ method copies the sourceArray to the targetArray.
System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length);
System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length);
System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length);
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
What is output of the following code:
public class Test {
public static void main(String[] args) {
int[] x = {120, 200, 016};
for (int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
}
}
120 200 16
120 200 14
120 200 20
016 is a compile error. It should be written as 16.
Analyze the following code:
public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2];
for (int i = 0; i < y.length; i++)
System.out.print(y[i] + " ");
}
}
The program displays 1 2 3 4
The program displays 0 0
The program displays 0 0 3 4
The program displays 0 0 0 0
What is the correct term for numbers[99]?
index
index variable
indexed variable
array variable
array
Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return?
0
-1
1
2
-2
Suppose a method p has the following heading:
public static int[] p()
What return statement may be used in p()?
return 1;
return {1, 2, 3};
return int[]{1, 2, 3};
return new int[]{1, 2, 3};
The reverse method is defined in the textbook. What is list1 after executing the following statements?
int[] list1 = {1, 2, 3, 4, 5, 6};
list1 = reverse(list1);
list1 is 1 2 3 4 5 6
list1 is 6 5 4 3 2 1
list1 is 0 0 0 0 0 0
list1 is 6 6 6 6 6 6
What is the output of the following code?
int[] myList = {1, 2, 3, 4, 5, 6};
for (int i = myList.length - 2; i >= 0; i--) {
myList[i + 1] = myList[i];
}
for (int e: myList)
System.out.print(e + " ");
1 2 3 4 5 6
6 1 2 3 4 5
6 2 3 4 5 1
1 1 2 3 4 5
2 3 4 5 6 1
In the following code, what is the output for list2?
public class Test {
public static void main(String[] args) {
int[] list1 = {1, 2, 3};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
for (int i = 0; i < list2.length; i++)
System.out.print(list2[i] + " ");
}
}
1 2 3
1 1 1
0 1 2
0 1 3
How many elements are in array double[] list = new double[5]?
4
5
6
0
When you return an array from a method, the method returns __________.
a copy of the array
a copy of the first element
the reference of the array
the length of the array
The __________ method sorts the array scores of the double[] type.
java.util.Arrays(scores)
java.util.Arrays.sorts(scores)
java.util.Arrays.sort(scores)
Njava.util.Arrays.sortArray(scores)
When you pass an array to a method, the method receives __________.
a copy of the array
a copy of the first element
the reference of the array
the length of the array
What is the output of the following code?
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) {
max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);
0
1
2
3
4
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
3.4
2.0
3.5
5.5
undefined
Analyze the following code:
public class Test {
public static void main(String[] args) {
double[] x = {2.5, 3, 4};
for (double value: x)
System.out.print(value + " ");
}
}
The program displays 2.5, 3, 4
The program displays 2.5 3 4
The program displays 2.5 3.0 4.0
The program displays 2.5, 3.0 4.0
The program has a syntax error because value is undefined.
What is the representation of the third element in an array called a?
a[2]
a(2)
a[3]
a(3)
Analyze the following code:
public class Test {
public static void main(String[] args) {
int[] a = new int[4];
a[1] = 1;
a = new int[2];
System.out.println("a[1] is " + a[1]);
}
}
The program has a compile error because new int[2] is assigned to a.
The program has a runtime error because a[1] is not initialized.
The program displays a[1] is 0.
The program displays a[1] is 1.
Variables that are shared by every instances of a class are __________.
public variables
private variables
instance variables
class variables
__________ represents an entity in the real world that can be distinctly identified.
A class
An object
A method
A data field
Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is _________ in the class.
public static void main(String[] args) {
xMethod();
}
a static method
an instance method
a static method or an instance method
To declare a constant MAX_LENGTH as a member of the class, you write
final static MAX_LENGTH = 99.98;
final static float MAX_LENGTH = 99.98;
static double MAX_LENGTH = 99.98;
final double MAX_LENGTH = 99.98;
final static double MAX_LENGTH = 99.98;
Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate?
x contains an array of ten int values.
x contains an array of ten objects of the Circle type.
x contains a reference to an array and each element in the array can hold a reference to a Circle object.
x contains a reference to an array and each element in the array can hold a Circle object.
Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class.
public MyClass() {
xMethod();
}
a static method
an instance method
a static method or an instance method
An object is an instance of a __________.
program
class
method
data
The default value for data field of a boolean type, numeric type, object type is ___________, respectively.
true, 1, Null
false, 0, null
true, 0, null
true, 1, null
false, 1, null
To prevent a class from being instantiated, _____________________
don't use any modifiers on the constructor.
use the public modifier on the constructor.
use the private modifier on the constructor.
use the static modifier on the constructor.
You can declare two variables with the same name in __________.
a method one as a formal parameter and the other as a local variable
a block
two nested blocks in a method (two nested blocks means one being inside the other)
different methods in a class
The keyword __________ is required to declare a class.
public
private
class
All of the above.
A method that is associated with an individual object is called __________.
a static method
a class method
an instance method
an object method
________ is invoked to create an object.
A constructor
The main method
A method with a return type
A method with the void return type
Given the declaration Circle x = new Circle(), which of the following statement is most accurate.
x contains an int value.
x contains an object of the Circle type.
x contains a reference to a Circle object.
You can assign an int value to x.
_______ is a construct that defines objects of the same type.
A class
An object
A method
A data field
What modifier should you use on a class so that a class in the same package can access it but a class (including a subclass) in a different package cannot access it?
public
private
protected
Use the default modifier
Polymorphism means ______________.
that data fields should be declared private
that a class can extend another class
that a variable of supertype can refer to a subtype object
that a class can contain another class
Inheritance means ______________.
that data fields should be declared private
that a class can extend another class
that a variable of supertype can refer to a subtype object
that a class can contain another class
Encapsulation means ______________.
that data fields should be declared private
that a class can extend another class
that a variable of supertype can refer to a subtype object
that a class can contain another class
You can create an ArrayList using _________.
new ArrayList[]
new ArrayList[100]
new ArrayList<>()
ArrayList()
What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package?
public
private
protected
Use the default modifier.
Suppose an ArrayList list contains {"red", "green", "red", "green"}. What is the list after the following code?
list.remove("red");
{"red", "green", "red", "green"}
{"green", "red", "green"}
{"green", "green"}
{"red", "green", "green"}
What is the output of the following code:
public class Test {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
System.out.print((o1 == o2) + " " + (o1.equals(o2)));
}
}
false false
true true
false true
true false
Invoking _________ removes all elements in an ArrayList x.
x.remove()
x.clean()
x.delete()
x.empty()
x.clear()
Which of the following is incorrect?
A constructor may be static.
A constructor may be private.
A constructor may invoke a static method.
A constructor may invoke an overloaded constructor.
A constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass's constructor.
Assume Cylinder is a subtype of Circle. Analyze the following code:
Cylinder cy = new Cylinder(1, 1);
Circle c = cy;
The code has a compile error.
The code has a runtime error.
The code is fine.
What is the output of running class C?
class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}
class B extends A {
public B() {
System.out.println(
"The default constructor of B is invoked");
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
Nothing displayed
"The default constructor of B is invoked"
"The default constructor of A is invoked" followded by "The default constructor of B is invoked"
"The default constructor of B is invoked" followed by "The default constructor of A is invoked"
"The default constructor of A is invoked"
Invoking _________ returns the first element in an ArrayList x.
x.first()
x.get(0)
x.get(1)
x.get()
Which of the following are Java keywords?
instanceOf
instanceof
cast
casting
A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this?
The variable should be marked public.
The variable should be marked private.
The variable should be marked protected.
The variable should have no special access modifier.
The variable should be marked private and an accessor method provided.
Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ___________.
True
False
What is the output of the following code?
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
private String getInfo() {
return "Student";
}
}
class Person {
private String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
}
Person Person
Person Student
Stduent Student
Student Person
Object-oriented programming allows you to derive new classes from existing classes. This is called ____________.
encapsulation
inheritance
abstraction
generalization
What is the output of the following code:
public class Test {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.print((s1 == s2) + " " + (s1.equals(s2)));
}
}
false false
true true
false true
true false
Given the following code:
class C1 {}
class C2 extends C1 { }
class C3 extends C2 { }
class C4 extends C1 {}
C1 c1 = new C1();
C2 c2 = new C2();
C3 c3 = new C3();
C4 c4 = new C4();
Which of the following expressions evaluates to false?
c1 instanceof C1
c2 instanceof C1
c3 instanceof C1
c4 instanceof C2
To add a node to the the first row and second column in a GridPane pane, use ________.
pane.getChildren().add(node, 1, 2);
pane.add(node, 1, 2);
pane.getChildren().add(node, 0, 1);
pane.add(node, 0, 1);
pane.add(node, 1, 0);
To place a node in the left of a BorderPane p, use ___________.
p.setEast(node);
p.placeLeft(node);
p.setLeft(node);
p.left(node);
To add two nodes node1 and node2 into a pane, use ______.
pane.add(node1, node2);
pane.addAll(node1, node2);
pane.getChildren().add(node1, node2);
pane.getChildren().addAll(node1, node2);
Which of the following statements are true?
A Node can be placed in a Pane.
A Node can be placed in a Scene.
A Pane can be placed in a Control.
A Shape can be placed in a Control.
To remove two nodes node1 and node2 from a pane, use ______.
pane.remove(node1, node2);
pane.removeAll(node1, node2);
pane.getChildren().remove(node1, node2);
pane.getChildren().removeAll(node1, node2);
Analyze the following code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
public class Test extends Application {
// Override the start method in the Application class
public void start(Stage primaryStage) {
HBox pane = new HBox(5);
Circle circle = new Circle(50, 200, 200);
pane.getChildren().addAll(circle);
circle.setCenterX(100);
circle.setCenterY(100);
circle.setRadius(50);
pane.getChildren().addAll(circle);
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("Test"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
The program has a compile error since the circle is added to a pane twice.
The program has a runtime error since the circle is added to a pane twice.
The program runs fine and displays one circle.
The program runs fine and displays two circles.
What is the output of the following JavaFX program?
import javafx.application.Application;
import javafx.stage.Stage;
public class Test extends Application {
public Test() {
System.out.println("Test constructor is invoked.");
}
// Override the start method in the Application class
public void start(Stage primaryStage) {
System.out.println("start method is invoked.");
}
public static void main(String[] args) {
System.out.println("launch application.");
Application.launch(args);
}
}
launch application. start method is invoked.
start method is invoked. Test constructor is invoked.
Test constructor is invoked. start method is invoked.
launch application. start method is invoked. Test constructor is invoked.
launch application. Test constructor is invoked. start method is invoked.
To place two nodes node1 and node2 in a HBox p, use ___________.
p.add(node1, node2);
p.addAll(node1, node2);
p.getChildren().add(node1, node2);
p.getChildren().addAll(node1, node2);
What is the output of the following code?
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
public class Test {
public static void main(String[] args) {
IntegerProperty d1 = new SimpleIntegerProperty(1);
IntegerProperty d2 = new SimpleIntegerProperty(2);
d1.bind(d2);
System.out.print("d1 is " + d1.getValue()
+ " and d2 is " + d2.getValue());
d2.setValue(3);
System.out.println(", d1 is " + d1.getValue()
+ " and d2 is " + d2.getValue());
}
}
d1 is 2 and d2 is 2, d1 is 3 and d2 is 3
d1 is 2 and d2 is 2, d1 is 2 and d2 is 3
d1 is 1 and d2 is 2, d1 is 1 and d2 is 3
d1 is 1 and d2 is 2, d1 is 3 and d2 is 3
Suppose A is an anonymous inner class in Test. A is compiled into a file named _________.
A.class
Test$A.class
A$Test.class
Test$1.class
Test&1.class
Supose the follwoing program displays a pane in the stage. What is the output if the user presses the DOWN arrow key?
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
// import javafx classes omitted
public class Test1 extends Application {
public void start(Stage primaryStage) {
// Code to create and display pane omitted
Pane pane = new Pane();
Scene scene = new Scene(pane, 200, 250);
primaryStage.setTitle("MyJavaFX"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
pane.requestFocus();
pane.setOnKeyPressed(e ->
System.out.print("Key pressed " + e.getCode() + " "));
pane.setOnKeyTyped(e ->
System.out.println("Key typed " + e.getCode()));
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
Key pressed DOWN Key typed UNDEFINED
Key pressed DOWN Key typed
Key typed UNDEFINED
Key pressed DOWN
Fill in the code in the underlined location to display the mouse point location when the mouse is pressed in the pane.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Test extends Application {
// Override the start method in the Application class
public void start(Stage primaryStage) {
Pane pane = new Pane();
______________________________________
Scene scene = new Scene(pane, 200, 250);
primaryStage.setTitle("Test"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited JavaFX
* support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
pane.setOnMouseClicked((e) -> System.out.println(e.getX() + ", " + e.getY()));
pane.setOnMouseReleased(e -> {System.out.println(e.getX() + ", " + e.getY())});
pane.setOnMousePressed(e -> System.out.println(e.getX() + ", " + e.getY()));
pane.setOnMouseDragged((e) -> System.out.println(e.getX() + ", " + e.getY()));
Analyze the following code.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Test extends Application {
// Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a button and place it in the scene
Button btOK = new Button("OK");
btOK.setOnAction(e -> System.out.println("OK 1"));
btOK.setOnAction(e -> System.out.println("OK 2"));
Scene scene = new Scene(btOK, 200, 250);
primaryStage.setTitle("MyJavaFX"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited JavaFX
* support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
When clicking the button, the program displays OK1 OK2.
When clicking the button, the program displays OK1.
When clicking the button, the program displays OK2.
The program has a compile error, because the setOnAction method is invoked twice.
To handle the key pressed event on a pane p, register the handler with p using ______.
p.setOnKeyClicked(handler);
p.setOnKeyTyped(handler);
p.setOnKeyReleased(handler);
p.setOnKeyPressed(handler);
Suppose A is an inner class in Test. A is compiled into a file named _________.
A.class
Test$A.class
A$Test.class
Test&A.class
Analyze the following code.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Test extends Application {
// Override the start method in the Application class
public void start(Stage primaryStage) {
Button btOK = new Button("OK");
btOK.setOnAction(new EventHandler() {
public void handle(ActionEvent e) {
System.out.println("The OK button is clicked");
}
});
Scene scene = new Scene(btOK, 200, 250);
primaryStage.setTitle("MyJavaFX"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited JavaFX
* support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
The program has a compile error because no handlers are registered with btOK.
The program has a runtime error because no handlers are registered with btOK.
The message "The OK button is clicked" is displayed when you click the OK button.
The handle method is not executed when you click the OK button, because no handler is registered with btOK.
To register a source for an action event with a handler, use __________.
source.addAction(handler)
source.setOnAction(handler)
source.addOnAction(handler)
source.setActionHandler(handler)
A JavaFX action event handler contains a method ________.
public void actionPerformed(ActionEvent e)
public void actionPerformed(Event e)
public void handle(ActionEvent e)
public void handle(Event e)
Fill in the code below in the underline:
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.setAction(______________________________);
}
public void setAction(T1 t) {
t.m();
}
}
interface T1 {
public void m();
}
() -> System.out.print("Action 1! ")
(e) -> System.out.print("Action 1! ")
System.out.print("Action 1! ")
(e) -> {System.out.print("Action 1! ")}
Which of the following methods is not defined in the Animation class?
pause()
play()
stop()
resume()
Which statement is true about a non-static inner class?
It must implement an interface.
It is accessible from any other class.
It can only be instantiated in the enclosing class.
It must be final if it is declared in a method scope.
It can access private instance variables in the enclosing object.
A JavaFX event handler for event type T is an instance of _______.
ActionEvent
Action
EventHandler
EventHandler<T>
Suppose the following program displays a pane in the stage. What is the output if the user presses the key for letter B?
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
// import javafx classes omitted
public class Test1 extends Application {
public void start(Stage primaryStage) {
// Code to create and display pane omitted
Pane pane = new Pane();
Scene scene = new Scene(pane, 200, 250);
primaryStage.setTitle("MyJavaFX"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
pane.requestFocus();
pane.setOnKeyPressed(e ->
System.out.print("Key pressed " + e.getCode() + " "));
pane.setOnKeyTyped(e ->
System.out.println("Key typed " + e.getCode()));
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
Key pressed B Key typed UNDEFINED
Key pressed B Key typed
Key typed UNDEFINED
Key pressed B
A JavaFX action event handler is an instance of _______.
ActionEvent
Action
EventHandler
EventHandler<ActionEvent>
To handle the mouse click event on a pane p, register the handler with p using ______.
p.setOnMouseClicked(handler);
p.setOnMouseDragged(handler);
p.setOnMouseReleased(handler);
p.setOnMousePressed(handler);
Which of the following assignment statements is incorrect?
i = j = k = 1;
i = 1; j = 1; k = 1;
i = 1 = j = 1 = k = 1;
i == j == k == 1;
Suppose x is 1. What is x after x += 2?
0
1
2
3
4
-25 % 5 is _____
1
2
3
4
0
To declare an int variable number with initial value 2, you write
int number = 2L;
int number = 2l;
int number = 2;
int number = 2.0;
What is the exact output of the following code?
double area = 3.5;
System.out.print("area");
System.out.print(area);
3.53.5
3.5 3.5
area3.5
area 3.5
To assign a value 1 to variable x, you write
1 = x;
x = 1;
x := 1;
1 := x;
x == 1;
24 % 5 is _____
1
2
3
4
0
To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159.
variables
methods
constants
classes
According to Java naming convention, which of the following names can be variables?
FindArea
findArea
totalLength
TOTAL_LENGTH
class
____________ is the Java assignment operator.
==
:=
=
=:
To assign a double variable d to a float variable x, you write
x = (long)d
x = (int)d;
x = d;
x = (float)d;
Suppose a Scanner object is created as follows:
Scanner input = new Scanner(System.in);
What method do you use to read a real number?
input.nextDouble();
input.nextdouble();
input.double();
input.Double();
Which of the following expression results in a value 1?
2 % 1
15 % 4
25 % 5
37 % 6
The expression 4 + 20 / (3 - 1) * 2 is evaluated to
4
20
24
9
25
Math.pow(4, 1 / 2) returns __________.
2
2.0
0
1.0
1
To declare a constant MAX_LENGTH inside a method with value 99.98, you write
final MAX_LENGTH = 99.98;
final float MAX_LENGTH = 99.98;
double MAX_LENGTH = 99.98;
final double MAX_LENGTH = 99.98;
Which of the following is a constant, according to Java naming conventions?
MAX_VALUE
Test
read
ReadInt
COUNT
What is x after the following statements?
int x = 2;
int y = 1;
x *= y + 1;
x is 1
x is 2
x is 3
xis 4
What is the value of (double)(5/2)?
2
2.5
3
2.0
3.0
Suppose x is 1. What is x after x -= 1?
0
1
2
-1
-2
Assume x = 4, which of the following is true?
!(x == 4)
x != 4
x == 5
x != 5
Which of the following code displays the area of a circle if the radius is positive.
if (radius != 0) System.out.println(radius radius 3.14159);
if (radius >= 0) System.out.println(radius radius 3.14159);
if (radius >= 0) System.out.println(radius radius 3.14159);
if (radius <= 0) System.out.println(radius radius 3.14159);
The __________ method immediately terminates the program.
System.terminate(0);
System.halt(0);
System.exit(0);
System.quit(0);
System.stop(0);
The equal comparison operator in Java is __________.
<>
!=
==
^=
What is 1 + 1 + 1 + 1 + 1 == 5?
true
false
There is no guarantee that 1 + 1 + 1 + 1 + 1 == 5 is true.
Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x-- > 10).
9
10
11
What is the output of the following code?
int x = 0;
if (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
x is 0
x is 1
x is 2
x is 3
x is 4
In Java, the word true is ________.
a Java keyword
a Boolean literal
same as value 1
same as value 0
What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?
true
false
There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.
Analyze the following code.
boolean even = false;
if (even) {
System.out.println("It is even!");
}
The code displays It is even!
The code displays nothing.
The code is wrong. You should replace if (even) with if (even == true).
The code is wrong. You should replace if (even) with if (even = true).
Which of the following is a possible output from invoking Math.random()?
3.43
0.5
0.0
0.0
Assume x = 4 and y = 5, which of the following is true?
x < 5 && y < 5
x < 5 || y < 5
x > 5 && y > 5
x > 5 || y > 5
Which of the following are so called short-circuit operators?
+=
&
||
|
Analyze the following code:
boolean even = false;
if (even = true) {
System.out.println("It is even");
}
The program has a compile error.
The program has a runtime error.
The program runs fine, but displays nothing.
The program runs fine and displays It is even.
The "less than or equal to" comparison operator in Java is __________.
<
<=
=<
<<
!=
Suppose x is a char variable with a value 'b'. What is the output of the statement System.out.println(++x)?
a
b
c
d
The Unicode of 'a' is 97. What is the Unicode for 'c'?
96
97
98
99
The statement System.out.printf("%3.1f", 1234.56) outputs ___________.
123.4
123.5
1234.5
1234.56
1234.6
Which of the following statement prints smith\exam1\test.txt?
System.out.println("smith\exam1\test.txt");
System.out.println("smith\\exam1\\test.txt");
System.out.println("smith\"exam1\"test.txt");
System.out.println("smith"\exam1"\test.txt");
Which of the following is the correct expression of character 4?
4
"4"
'\0004'
'4'
Suppose s1 and s2 are two strings. What is the result of the following code?
s1.equals(s2) == s2.equals(s1)
True
False
What is Math.ceil(3.6)?
3.0
3
4.0
5.0
What is Math.round(3.6)?
3.0
3
4
4.0
Which of the following assignment statements is correct?
char c = 'd';
char c = '100';
char c = "d";
char c = "100";
What is the output of System.out.println('z' - 'a')?
25
26
a
z
What is Math.rint(3.5)?
3.0
3
4
4.0
5.0
Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i?
System.out.println(i);
System.out.println((char)i);
System.out.println((int)i);
System.out.println(i + " ");
What is the return value of "SELECT".substring(0, 5)?
"SELECT"
"SELEC"
"SELE"
"ELECT"
A Java character is stored in __________.
one byte
two bytes
three bytes
four bytes
The statement System.out.printf("%5d", 123456) outputs ___________.
12345
23456
123456
12345.6
"abc".compareTo("aba") returns ___________.
1
2
-1
-2
0
What is Math.floor(3.6)?
3.0
3
4
5.0
What is the return value of "SELECT".substring(4, 4)?
an empty string
C
T
E
The expression "Java " + 1 + 2 + 3 evaluates to ________.
Java123
Java6
Java 123
java 123
Illegal expression
An int variable can hold __________.
120L
120
120.0
"x"
"120"
The statement System.out.printf("%10s", 123456) outputs ___________. (Note: * represents a space)
123456****
23456*****
12345*****
****123456
To check whether a char variable ch is an uppercase letter, you write ___________.
(ch >= 'A' && ch >= 'Z')
(ch >= 'A' && ch <= 'Z')
(ch >= 'A' || ch <= 'Z')
('A' <= ch <= 'Z')
Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to ________.
66
B
A1
illegal expression
Which of the following is the correct statement to return JAVA?
toUpperCase("Java")
"Java".toUpperCase("Java")
"Java".toUpperCase()
String.toUpperCase("Java")
Will System.out.println((char)4) display 4?
YES
NO
