Final Review Flashcards

1
Q

An object is an instance of a ________.

data
class
method
program

A

class

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

Analyze the following code:

class TempClass {
  int i;
public void TempClass(int j) {
  int i = j;
}
}
public class C {
  public static void main(String[] args) {
    TempClass temp = new TempClass(2);
}
}

The program compiles and runs fine.

The program has a compile error because TempClass does not have a constructor with an int argument.

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

The program compiles fine, but it does not run because class C is not public.

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

Analyze the following code:

class Test{
  int x;
public String 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.

The program has a compile error because x has not been initialized.

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

The program has a compile error because you cannot create an object from the class that defines the object.

The program has a compile error because test is not initialized.

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

Analyze the following code:

class Test{
  public static void main(String[] args) {
    double radius;
    final double PI = 3.15169;
    double area = radius * radius * PI;
    System.out.println("Area is " + area);
}
}

The program compiles and runs fine.

The program has a compile error because a constant PI is defined inside a method.

The program has compile errors because the variable radius is not initialized.

The program has no compile errors but will get a runtime error because radius is not initialized.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Variables that are shared by every instances of a class are \_\_\_\_\_\_\_\_.
		public variables
		class variables
		private variables
		instance variables
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
To prevent a class from being instantiated, \_\_\_\_\_\_\_\_.
		use the private modifier on the constructor
		use the public modifier on the constructor
		use the static modifier on the constructor
		don't use any modifiers on the constructor
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which is the advantage of encapsulation?

Only public methods are needed.

It changes the implementation without changing a class’s contract and causes no consequential changes to other code.

It changes a class’s contract without changing the implementation and causes no consequential changes to other code.

Making the class final causes no consequential changes to other code.

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

Which of the following can be placed in the blank line in the following code?

private int id;
public void m1(){
\_\_\_\_\_.id = 45;
}
}

A. this
B. Test

A

this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
\_\_\_\_\_\_\_\_ is attached to the class of the composing class to denote the aggregation relationship with the composed object.
		A solid oval
		An empty oval
		An empty diamond
		A solid diamond
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Which of the following statements will convert a string s into a double value d?
		d = Double.parseDouble(s);
		d = Double.valueOf(s).doubleValue();
		d = (new Double(s)).doubleValue();
		All of these
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is displayed by the following statement?

System.out.println("Java is neat".replaceAll("is", "AAA"));
		Java AAAneat
		JavaAAA neat
		Java AAA neat
		JavaAAAneat
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is displayed by the following code?

System.out.print(“A,B;C”.replaceAll(“,;”, “#”) + “ “);
System.out.println(“A,B;C”.replaceAll(“[,;]”, “#”));

A B C A B C
A B C A#B#C
A#B#C A#B#C
A,B;C A#B#C

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Assume StringBuilder strBuf is "ABCDEFG", after invoking \_\_\_\_\_\_\_\_, strBuf contains "ABCRRRRDEFG".
		strBuf.insert(1, "RRRR")
		strBuf.insert(3, "RRRR")
		strBuf.insert(4, "RRRR")
		strBuf.insert(2, "RRRR")
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
Assume StringBuilder strBuf is "ABCCEFC", after invoking \_\_\_\_\_\_\_\_, strBuf contains "ABTTEFT".
		strBuf.replace('C', 'T')
		strBuf.replace("C", "T")
		strBuf.replace('C', "TT")
		strBuf.replace(2, 7, "TTEFT")
		strBuf.replace("CC", "TT")
A
16
Q

The equals method is defined in the Object class. Which of the following is correct to override it in the String class?
public boolean equals(String other)
public boolean equals(Object other)
public static boolean equals(Object other)
public static boolean equals(String other)

A
17
Q

T/F. Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ________.

A
18
Q
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
		protected
		private
		Use the default modifier.
A
19
Q
A Java exception is an instance of \_\_\_\_\_\_\_\_.
		RuntimeException
		Exception
		Error
		Throwable
		NumberFormatException
A
20
Q
An instance of \_\_\_\_\_\_\_\_ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.
		RuntimeException
		Exception
		Error
		Throwable
		NumberFormatException
A
21
Q
A method must declare to throw \_\_\_\_\_\_\_\_.
		checked exceptions
		unchecked exceptions
		Error
		RuntimeException
A
22
Q
Which of the following statements are true?
		If a directory (e.g., c:\liang) does not exist, new File("c:\liang") creates a new directory named c:\liang.
		If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") creates a new file named c:\temp.txt.
		If a directory (e.g., c:\liang) does not exist, new File("c:\liang") returns null.
		If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") returns null.
		None of these
A
23
Q
Which method can be used to write data?
		exist
		print
		rename
		close
A
24
Q
\_\_\_\_\_\_\_\_ is not a reference type.
		An array type
		An interface type
		A class type
		A primitive type
A
25
Q
Which of the following declares an abstract method in an abstract Java class?
		public abstract method();
		public abstract void method();
		public void abstract method();
		public void method() {}
		public abstract void method() {}
A