Test 3 Flashcards
(22 cards)
Which of the following statements is true?
Constructors can specify parameters but not return types
You should add the static keyword in the place of ? in Line in the following code:
1: public class Test {
2: private int age;
3:
4: public ? int square(int n){
5: return n * n;
6: }
7:
8: public ? int getAge() {
9: return age;
10: }
11: }
line 4: public static int square(int n){
Java allows you to declare methods with the same name in a class.
method overloading
Analyze the following code.
public class Test {
public static void main(String[] args) { int n = 2;
xMethod(n);
System.out.println(“n is “ + n);
}
void xMethod(int n) { n++;
}
}
The code has a compile error because xMethod is not declared static
Analyze the following code.
public class Test {
public static void main(String[] args) {
int[] x = new int[3];
System.out.println(“x[0] is “ + x[0]);
}
}
The program runs fine and displays x[0] is 0.
Given the declaration circle x= new Circle(); which of the following statement is most accurate?
x contains a reference to a Circle object.
Suppose Character x = new Character(‘a’), __________________ returns true.
x.equals(‘a’)
Which of the following class members should usually be private?
Variables (or fields)
What would be the result of attempting to compile and run the following code?
public class Test { public static void main(String[] args) {
double[] x = new double[]{1, 2, 3};
System.out.println(“Value is “ + x[1]);
}
}
The program compiles and runs fine and the output “Value is 2.0” is printed.
What is the printout of the following code?
public class Foo {
private boolean x;
public static void main(String[] args) {
Foo foo = new Foo();
System.out.println(foo.x);
}
}
False
Analyze the following code.
public class Test { int x;
public Test(String t) { System.out.println(“Test”);
}
public static void main(String[] args) { Test test = new Test(); System.out.println(test.x);
}
}
The program has a compile error because Test does not have a default constructor.
Analyze the following code.
public class Test {
public static void main(String[] args) {
System.out.println(m(2));
}
public static int m(int num) {
return num;
}
public static void m(int num) {
System.out.println(num);
}
}
The program has a compile error because the two methods m have the same signature of the type returned.
Analyze the following code.
public class Test {
int x;
public Test(String t) {
System.out.println(“Test”);
}
public static void main(String[] args) {
Test test = null;
System.out.println(test.x);
}
}
The program has a runtime NullPointerException because test is null while executing test.x.
What is the printout of the following program?
public class Test {
public static void main(String[] args) {
int[][] values = {{3, 4, 5, 1 }, {33, 6, 1, 2}};
for (int row = 0; row < values.length; row++) {
java.util.Arrays.sort(values[row]);
for (int column = 0; column < values[row].length; column++)
System.out.print(values[row][column] + “ “);
System.out.println();
}
}
}
The program prints two rows 1 3 4 5 followed by 1 2 6 33
Which of the following is NOT an object:
342
Analyze the following code.
class Test {
public static void main(String[] args) {
String s; System.out.println("s is " + s);
}
}
The program has a compile error because s is not initialized, but it is referenced in the println statement.
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
Which of the following statements are correct to declare an array?
char[][] charArray = {{‘a’, ‘b’}, {‘c’, ‘d’}};
Show the output of the following code:
public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
increase(x);
int[] y = {1, 2, 3, 4, 5};
increase(y[0]);
System.out.println(x[0] + “ “ + y[0]);
}
public static void increase(int[] x) {
for (int i = 0; i < x.length; i++)
x[i]++;
}
public static void increase(int y) {
y++;
}
}
1 1
Which of the following statements are true?
All are true
Analyze the following code:
class Circle {
private double radius;
public Circle(double radius) {
radius = radius;
}
}
The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.
Which of the following is correct to declare an array?
int a[] = new int[2];