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")
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")
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)
T/F. Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ________.
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 Java exception is an instance of \_\_\_\_\_\_\_\_. RuntimeException Exception Error Throwable NumberFormatException
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 method must declare to throw \_\_\_\_\_\_\_\_. checked exceptions unchecked exceptions Error RuntimeException
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
Which method can be used to write data? exist print rename close
\_\_\_\_\_\_\_\_ is not a reference type. An array type An interface type A class type A primitive type
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() {}