Test 3 Flashcards

(22 cards)

1
Q

Which of the following statements is true?

A

Constructors can specify parameters but not return types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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: }

A

line 4: public static int square(int n){

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Java allows you to declare methods with the same name in a class.

A

method overloading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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++;

}

}

A

The code has a compile error because xMethod is not declared static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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]);

}

}

A

The program runs fine and displays x[0] is 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Given the declaration circle x= new Circle(); which of the following statement is most accurate?

A

x contains a reference to a Circle object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Suppose Character x = new Character(‘a’), __________________ returns true.

A

x.equals(‘a’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which of the following class members should usually be private?

A

Variables (or fields)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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]);

}

}

A

The program compiles and runs fine and the output “Value is 2.0” is printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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);

}

}

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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);

}

}

A

The program has a compile error because Test does not have a default constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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);

}

}

A

The program has a compile error because the two methods m have the same signature of the type returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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);

}

}

A

The program has a runtime NullPointerException because test is null while executing test.x.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

}

}

}

A

The program prints two rows 1 3 4 5 followed by 1 2 6 33

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which of the following is NOT an object:

A

342

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Analyze the following code.

class Test {

public static void main(String[] args) {

 String s;  

 System.out.println("s is " + s); 

}

}

A

The program has a compile error because s is not initialized, but it is referenced in the println statement.

17
Q

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] + " "); 

}

}

18
Q

Which of the following statements are correct to declare an array?

A

char[][] charArray = {{‘a’, ‘b’}, {‘c’, ‘d’}};

19
Q

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++;
}

}

20
Q

Which of the following statements are true?

21
Q

Analyze the following code:
class Circle {

private double radius;
public Circle(double radius) {

radius = radius; 

}

}

A

The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.

22
Q

Which of the following is correct to declare an array?

A

int a[] = new int[2];