Final Review Flashcards
An object is an instance of a ________.
data
class
method
program
class
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.
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.
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.
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
Variables that are shared by every instances of a class are \_\_\_\_\_\_\_\_. public variables class variables private variables instance variables
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++; } }
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
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.
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
this
\_\_\_\_\_\_\_\_ 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
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
What is displayed by the following statement?
System.out.println("Java is neat".replaceAll("is", "AAA")); Java AAAneat JavaAAA neat Java AAA neat JavaAAAneat
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
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")