1/76
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
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
Is the following loop correct?
for ( ; ; );
Yes
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)
45
What will be displayed when the following code is executed?
int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + " ");
}
3 0 -3
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 4
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
10
The following loop displays _______________.
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
i++;
}
1 3 5 7 9
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);
10
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
Which of the following loops prints "Welcome to Java" 10 times?
A:
for (int count = 1; count <= 10; count++) {
System.out.println("Welcome to Java");
}
B:
for (int count = 0; count < 10; count++) {
System.out.println("Welcome to Java");
}
C:
for (int count = 1; count < 10; count++) {
System.out.println("Welcome to Java");
}
D:
for (int count = 0; count <= 10; count++) {
System.out.println("Welcome to Java");
}
BD
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?
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);
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 false
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
What is the number of iterations in the following loop?
for (int i = 1; i < n; i++) {
// iteration
}
n - 1
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count++ < 10) {
System.out.println("Welcome to Java");
}
10
What is the output for y?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += i;
}
System.out.println(y);
45
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);
10
What is y after the following for loop statement is executed?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += 1;
}
10
__________ is a simple but incomplete version of a method.
A stub
__________ is to implement one method in the structure chart at a time from the top to the bottom.
Top-down approach
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');
}
}
}
void
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.
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';
}
}
char
(int)(Math.random() * (65535 + 1)) returns a random number __________.
between 0 and 65535
Arguments to methods always appear within __________.
parentheses
What is k after the following block executes?
{
int k = 2;
nPrint("A message", k);
}
System.out.println(k);
k is not defined outside the block. So, the program has a compile error
Suppose your method does not return any value, which of the following keywords can be used as a return type?
void
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;
}
No
Which of the following is the best for generating random integer 0 or 1?
(int)(Math.random() + 0.5)
When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.
pass by value
The signature of a method consists of ____________.
method name and parameter list
Does the method call in the following method cause compile errors?
public static void main(String[] args) {
Math.pow(2, 4);
}
No
All Java applications must have a method __________.
public static void main(String[] args)
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;
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 stack
Which of the following should be defined as a void method?
Write a method that prints integers from 1 to 100.
When you pass an array to a method, the method receives __________.
the reference of the array
How many elements are in array double[] list = new double[5]?
5
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);
1
When you return an array from a method, the method returns __________.
the reference of the array
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 14
The __________ method copies the sourceArray to the targetArray.
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
The __________ method sorts the array scores of the double[] type.
java.util.Arrays.sort(scores)
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 displays a[1] is 0.
Suppose a method p has the following heading:
public static int[] p()
What return statement may be used in p()?
return new int[]{1, 2, 3};
What is the representation of the third element in an array called a?
a[2]
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.
heap
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
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] + " ");
}
}
0 1 2
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.0 4.0
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 1 1 1 1 1
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
2.0
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
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 6 5 4 3 2 1
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]
Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return?
2
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 1 2 3 4 5
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] + " ");
}
}
0 1 2
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.
3
What is the correct term for numbers[99]?
indexed variable
Assume int[] t = {1, 2, 3, 4}. What is t.length?
4
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
Variables that are shared by every instances of a class are __________.
class variables
An object is an instance of a __________.
class
A method that is associated with an individual object is called __________.
an instance method
________ is invoked to create an object.
A constructor
To prevent a class from being instantiated, _____________________
use the private modifier on the constructor.
To declare a constant MAX_LENGTH as a member of the class, you write
final static double MAX_LENGTH = 99.98;
You can declare two variables with the same name in __________.
different methods in a class
_______ is a construct that defines objects of the same type.
A class
The default value for data field of a boolean type, numeric type, object type is ___________, respectively.
false, 0, null
Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class.
public MyClass() {
xMethod();
}
an instance method
Given the declaration Circle x = new Circle(), which of the following statement is most accurate.
x contains a reference to a Circle object.
_________ represents an entity in the real world that can be distinctly identified.
An object
The keyword __________ is required to declare a class.
class
Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate?
x contains a reference to an array and each element in the array can hold a reference to a Circle object.