Final Exam (part 3) Flashcards
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.
What code may be filled in the blank without causing syntax or runtime errors?
public class Test {
java.util.Date date;
public static void main(String[] args) { Test test = new Test(); System.out.println(\_\_\_\_\_\_\_); } }
test.date
date
test.date.toString()
date.toString()
test.date
public class Test {
public static void main(String[] args) {
A a = new A ();
a.print();
}
}
class A {
String s;
A (String s) { this.s = s; } void print() { System.out.println (s); } }
__The program has a compilation error because class A is not a public class.
__The program has a compilation error because class A does not have a default constructor.
__The program compiles and runs fine and prints nothing.
__The program would compile and run if you change A a = new A() to A a = new A(“5”).
The program has a compilation error because class A does not have a default constructor.
What is wrong in 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 has a compilation error because TempClass does not have a constructor with an int argument.
How many JFrame objects can you create and how many can you display?
unlimited
Which of the following statements are true about an immutable object?
__The contents of an immutable object cannot be modified
__All properties of an immutable object must be private.
__All properties of an immutable object must be of primitive types.
__An object type property in an immutable object must also be immutable.
__An immutable object contains no mutator methods.
contents cannot be modified
properties must be private
object type properties must also be immutable
contains no mutator methods
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 }
10 }
line 4
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.
Which of the following is the correct statement to return a string from an array a of characters?
toString(a)
new String(a)
convertToString(a)
String.toString(a)
new String(a)
The StringBuilder methods _____ not only change the contents of a string buffer, but also returns a reference to the string buffer.
delete
append
insert
reverse
replace
delete
append
insert
reverse
replace
Assume StringBuilder strBuf is “ABCDEFG”, after invoking ________, strBuf contains “ABCRRRRDEFG”.
strBuf.insert(3, “RRRR”)
You cannot append a string to a string buffer if the resulting new string exceeds the capacity. T/F
false
Which of the following statements will convert a string s into a double value d?
d = Double.parseDouble(s);
d = (new Double(s)).doubleValue();
d = Double.valueOf(s).doubleValue();
Two strings with same contents are ALWAYS allocated to the same object. T/F
false
public class Test {
public static void main(String[] args) {
String s = new String(“Welcome to Java”);
Object o = s;
String d = (String)o;
}
}
__When assigning s to o in Object o = s, a new object is created.
__When casting o to s in String d = (String)o, a new object is created.
__When casting o to s in String d (String)o, the contents of o is changed.
__s, o, and d reference the same String object.
s, o, and d reference the same String object.