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.
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(String other)
public static boolean equals(Object other)
public boolean equals(Object other)
Invoking ______ returns the number of elements in an ArrayList x.
x.size()
If a method is declared protected in the superclass, you may declare the method private in the subclass. T/F
false
You can always successfully cast a superclass to a subclass. T/F
false
Which of the follow are true?
__Override the methods equals and toString defined in the Object class whenever possible.
__Override the hashCode method whenever the equals method is overridden. By contract, two equal objects must have the same hash code.
__A public default no-arg constructor is assumed if no constructors are defined explicitly.
__You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.
All of them ^^^^
Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be _____.
true
Encapsulation means ______.
that data fields should be declared private
that a class can extend another class
that a variable of supertype can refer to a subtype object
that a class can contain another class
the data fields should be declared private
The getValue() method is overridden in two ways. Which one is correct?
I:
public class Test {
public static void main(String[] args) {
A a = new A ();
System.out.println (a.getValue ());
}
}
class B {
public String getValue() {
return “Any object”;
}
}
class A extends B {
public Object getValue () {
return “A string”;
}
}
II:
public class Test {
public static void main(String[] args) {
A a = new A ();
System.out.println(a.getValue());
}
}
class B {
public Object getValue() {
return “Any object”;
}
}
class A extends B {
public String getValue() {
returns “A string”;
}
}
II
What is the output of running class C?
class A {
public A () {
System.out.println(
“The default constructor of A is invoked”);
}
}
class B extends A {
public B () {
System.out.println(
“The default constructor of B is invoked”);
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
The default constructor of A is invoked
The default constructor of B is invoked