What is wrong with the following code?
if radius >= 0 {
area = radius * radius * PI;
System.out.printf("The area for the circle of " +
"radius %d is %.0f\n", radius, area);
}
Missing parentheses around radius >= 0
The correct way to print a double num up to 4 decimal places.
System.out.printf("%.4f", num);
Number of parts in a for-loop declaration.
3
What is the return type of a main method?
void
How do you change the size of an already created array?
You can't
Which of the following is a possible output from invoking Math.random()?
a) 34
b) 0.0
c) 0.5
d) 1.0
e) 0.234
b, c, e
This is the index of the first character in a string.
0
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count > 10) {
System.out.println("Welcome to Java");
count++;
}
0
What is method overloading?
A class has more than one method of the same name and their parameters are different
What is the output of the following code?
int x = 30;
int[] numbers = new int[x]; x = 60; System.out.println("x is " + x); System.out.println("The size of numbers is " + numbers.length);
x is 60
The size of numbers is 30
Assume that x and y are int type. Which of the following are legal Java expressions?
a) x > y > 0
b) x = y && y
c) x /= y
d) x or y
e) x and y
f) (x != 0) || (x = 0)
c
This is the result of character.isLetter('a')
An error
Character needs to be capitalized.
Number of iterations in the following nested loop
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
System.out.println(i);
}
}
20
Given two method definitions,
public static double m(double x, double y)
public static double m(int x, double y)
tell which of the two methods is invoked for:
a) double z = m(4, 5);
b) double z = m(4, 5.4);
c) double z = m(4.5, 5.4);
a) method 2
b) method 2
c) method 1
If you declare an array double[] list = new double[5], the highest index in array list is __.
4
What is y after the following switch statement is executed?
x = 3; y = 3;
switch (x + 3) {
case 6: y = 1;
default: y += 1;
}
2
a) Math.max(2, Math.min(3, 4))
b) Math.round(Math.abs(-2.5))
3
3
Will the following program terminate?
int balance = 10;
while (true) {
if (balance < 9)
break;
balance = balance - 9;
}
Yes
Identify and correct the errors in the following program:
public class Test {
public static void main(String[] args) {
nPrintln(5, "Welcome to Java!");
}
public static void nPrintln(String message, int n) {
int n = 1;
for (int i = 0; i < n; i++)
System.out.println(message);
}
}
Two errors:
1. the arguments are not passed in the right order.
2. n is a parameter, but is redeclared in the nPrintln method in line 7.
What happens when your program attempts to access an array element with an invalid index?
A runtime exception ArrayIndexOutOfBounds occurs.
Rewrite the following if statements using the conditional operator.
if (ages >= 16)
ticketPrice = 20;
else
ticketPrice = 10;
ticketPrice = (ages >= 16) ? 20 : 10;
Evaluate the following method calls:
a) Math.rint(-2.5)
b) Math.round(-2.5)
c) Math.rint(2.5)
d) Math.round(2.5)
-2.0
-2
2.0
3
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.print(i + " ");
}
Odd numbers 1-9
What is wrong in the following program?
public class Test {
public static void method(int x) { }
public static int method(int y) {
return y;
}
}
Methods public static void method(int x) and public static int method(int y) have the same signature method(int).
Identify and fix the errors in the following code:
public void main(String[] args) {
double[100] r;
for (int i = 0; i < r.length(); i++);
r(i) = Math.random * 100;
}
Line 1: missing static
Line 2: the array declaration is wrong. It should be double[]. The array needs to be created before its been used. e.g. new double[10]
Line 4: The semicolon (;) at the end of the for loop heading should be removed.
Line 4: r.length() should be r.length.
Line 5: random should be random()
Line 5: r(i) should be r[i].