Uploaded by Wesley Yawn

Final

advertisement
__________ represents an entity in the real world that can be distinctly identified.
An object
_______ is a template that defines objects of the same type.
A Class
An object is an instance of a __________.
Class
The keyword __________ is required to define a class.
Class
________ is invoked to create an object.
A constructor
Which of the following statements are true? Multiple constructors can be defined in a class. Constructors do not have a return type, not even void. Constructors must have the same name as
the class itself. Constructors are invoked using the new operator when an object is created
Which of the following statements are true?
A default constructor is provided automatically if no constructors are explicitly defined in the class.
Given the declaration Circle x = new Circle(), which of the following statements is most accurate.
x contains a reference to a Circle object.
Which are correct?
A reference variable references to an object. A data field in a class can be of an object type
The default value for data field of a boolean type, numeric type, object type is ___________, respectively.
false, 0, null
A method that is associated with an individual object is called __________.
an instance method
To declare a constant MAX_LENGTH as a member of the class, you write
final static double MAX_LENGTH = 99.98;
Variables that are shared by every instance of a class are __________.
class variables
Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is _________ in the class.
a static method
Which is the advantage of encapsulation?
It enables changes to the implementation without changing a class's contract and causes no consequential changes to other code.
Which are true?
Use the private modifier to encapsulate data fields. Encapsulating data fields makes the program easy to maintain. Encapsulating data fields helps prevent
programming errors.
Suppose you wish to provide an accessor method for a boolean property finished, what should the signature of this method?
public boolean isFinished()
Encapsulation means ______________.
that data fields should be declared private
Given the declaration Circle[] x = new Circle[10], which of the following statements is most accurate?
x contains a reference to an array and each element in the array can hold
a reference to a Circle object.
Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true? dates[0] is null. dates = new java.util.Date[5] is fine, which assigns a new array to dates.
You can declare two variables with the same name in __________.
different methods in a class
Object-oriented programming allows you to derive new classes from existing classes. This is called ____________. Inheritance
Which are true
subclass is usually extended to contain more functions and more detailed information than its superclass. "class A extends B" means A is a subclass of B.
Inheritance means ______________.
that a class can extend another class
Which of the statements regarding the super keyword is incorrect?
You can use super.super.p to invoke a method in superclass's parent class.
All are true:
To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. Overloading a
method is to provide more than one method with the same name but with different signatures to distinguish them. It is a compile error if two methods differ only in return type in the same
class. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. A static method cannot be
overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.
Polymorphism means ______________.
that a variable of supertype can refer to a subtype object
Which of the following statements is false?
Dynamic binding can apply to static methods.
Which of the following are Java keywords?
Instanceof
Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ___________.
True
Given two reference variables t1 and t2, if t1.equals(t2) is true, t1 == t2 ___________.
may be true or false
Which of the following class definitions defines a legal abstract class?
abstract class A { abstract void unfinished(); }
Which of the following declares an abstract method in an abstract Java class?
public abstract void method();
Which of the following statements regarding abstract methods is false?
An abstract class can have instances created using the constructor of the abstract class.
Which of the following statements regarding abstract methods is false?
A data field can be declared abstract.
Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg constructor. Which of the following is correct?
A a = new B();B b = new B();
Which of the following is a correct interface?
interface A { void print();}
Which are incorrect?
The constructors in an abstract class are private. You may declare a final abstract class. An interface may contain constructors.
_______ is not a reference type
A primitive type
Suppose A is an interface, B is a concrete class with a no-arg constructor that implements A. Which of the following is correct? A a = new B();B b = new B();
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 has a compile error because
TempClass does not have a constructor
with an int argument.
11111111111111111
What is wrong in the following code?
1 class Test {
2 public static void main(String[] args) {
3 A a = new A();
4 a.print();
5 }
6 }
7
8 class A {
9 String s;
10
11 A(String newS) {
12 s = newS;
13 }
14
15 public void print() {
16 System.out.print(s);
17 }
18 }
The program does not compile because
new A() is used in class Test, but class A
does not have a default constructor. See
the second NOTE in the Section,
"Constructors."
1111111111
Analyze the following code.
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.
11111111111111
Analyze the following code.
public class Test {
int x;
public 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.
111111111111111
1. Can you invoke an instance method or
reference an instance variable from a static
method?
2. Can you invoke a static method or
reference a static variable from an instance
method?
3. What is wrong in the following code?
1 public class C {
2 Circle c = new Circle();
3
4 public static void main(String[] args) {
5 method1();
6 }
7
8 public void method1() {
9 method2();
10 }
11
12 public static void method2() {
13 System.out.println("What is radius " +
c.getRadius());
14 }
15 }
1. You cannot invoke an instance method or
reference an instance variable from a static
method.
2. You can invoke a static method or
reference a static variable from an instance
method.
3. (a) The main method is static and cannot
invoke the instance method method1. (b) c
is an instance variable, which cannot be
accessed from the static context in
method2.
111111111111111
What is the output of the following code?
public class Test {
public static void main(String[] args) {
A a1 = new A();
System.out.print(a1.i);
A a2 = new A();
System.out.print(" " + a2.i);
}
}
class A {
int i = 1;
static int j = 1;
A() {
i++;
j++;
}
}
22
111111111111
What is the output of the following code?
public class Test {
public static void main(String[] args) {
A a1 = new A();
System.out.print(a1.j);
A a2 = new A();
System.out.print(" " + a2.j);
}
}
class A {
int i = 1;
static int j = 1;
A() {
i++;
j++;
}
}
23
11111111111
What is the output of the second println
statement in the main method?
public class Foo {
int i;
static int s;
public static void main(String[] args) {
Foo f1 = new Foo();
System.out.println("f1.i is " + f1.i + " f1.s
is " + f1.s);
Foo f2 = new Foo();
System.out.println("f2.i is " + f2.i + " f2.s
is " + f2.s);
Foo f3 = new Foo();
System.out.println("f3.i is " + f3.i + " f3.s
is " + f3.s);
}
public Foo() {
i++;
s++;
}
}
f2.i is 1 f2.s is 2
1111111111
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++;
}
}
The code has a compile error because
xMethod is not declared static.
11111111111111
What is wrong in the following code?
1 public class Test {
2 public static void main(String[] args) {
3 java.util.Date[] dates = new
java.util.Date[10];
4 System.out.println(dates[0]);
5 System.out.println(dates[0].toString());
6 }
7 }
(Line 4 prints null since dates[0] is null. Line
5 causes a NullPointerException since it
invokes toString() method from the null
reference.)
1111111111111111111111
What is the output of the following
program?
public class Test {
private static int i = 0;
private static int j = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}
i + j is 23 (because i (value of 2) is
concatenated with string i + j is, then j
(value of 3) is concatenated with string i + j
is 2.)
k is 2
j is 0
1111111111111
What is the output for the first print
statement in the main method?
public class Foo {
static int i = 0;
static int j = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}
i + j is 23
11111111111
What is the output for the second print
statement in the main method?
public class Foo {
static int i = 0;
static int j = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}
k is 2
1111111111
What is the output for the third print
statement in the main method?
public class Foo {
static int i = 0;
static int j = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}
j is 0
1111111111
Analyze the following code:
class Circle {
private double radius;
public Circle(double radius) {
radius = radius;
}
}
The program will compile, but you cannot
create an object of Circle with a specified
radius. The object will always have radius 0.
11111111111
What is wrong in the following code?
public class Test {
private int id;
public void m1() {
this.id = 45;
}
public void m2() {
Test.id = 45;
}
}
Test.id = 45; This is wrong, since id is an
instance member and cannot be accessed
from a class.
11111111111
Which of the following can be placed in
the blank line in the following code?
public class Test {
private int id;
public void m1() {
_____.id = 45;
}
}
this
1111111111111
Analyze the following code:
class Test {
private double i;
public Test(double i) {
this.t();
this.i = i;
}
public Test() {
System.out.println("Default
constructor");
this(1);
}
public void t() {
System.out.println("Invoking t");
}
}
this.t() may be replaced by t().
this(1) must be called before
System.out.println("Default constructor").
11111111111111111
What is wrong in the following code?
1 public class C {
2 private int p;
3
4 public C() {
5 System.out.println("C's no-arg
constructor invoked");
6 this(0);
7 }
8
9 public C(int p) {
10 p = p;
11 }
12
13 public void setP(int p) {
14 p = p;
15 }
16 }
Swap line 5 with line 6
Lines 10, 14, should be this.p = p;
1111111111111111111111111
Suppose you create a class Square to be a
subclass of GeometricObject. Analyze the
following code:
class Square extends GeometricObject {
double length;
Square(double length) {
GeometricObject(length);
}
}
The program has a compile error because
you attempted to invoke the
GeometricObject class's constructor
illegally.
111111111111111
Analyze the following code:
public class Test extends A {
public static void main(String[] args) {
Test t = new Test();
t.print();
}
}
class A {
String s;
A(String s) {
this.s = s;
}
public void print() {
System.out.println(s);
}
}
The program has an implicit default
constructor Test(), but it cannot be
compiled, because its super class does not
have a default constructor. The program
would compile if the constructor in the
class A were removed.
The program
would compile if a default constructor A(){ }
is added to class A explicitly
1111111111111111111
What is the output of running the class C in
(a)? What problem arises in compiling the
program in (b)?
(a)
class A {
public A() {
System.out.println("A's no-arg
constructor is invoked");
}
}
class B extends A {
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
(b)
class A {
public A(int x) {
}
}
class B extends A {
public B() {
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
(a) The output is
A's no-arg constructor is invoked
(b) The no-arg constructor of B attempts to
invoke the default of constructor of A, but
class A's default constructor is not defined.
11111111111111111111
Analyze the following code:
public class Test {
public static void main(String[] args) {
B b = new B();
b.m(5);
System.out.println("i is " + b.i);
}
}
class A {
int i;
public void m(int i) {
this.i = i;
}
}
class B extends A {
public void m(String s) {
}
}
The method m is not overridden in B. B
inherits the method m from A and defines
an overloaded method m in B.
111111111111
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() {
return "A string";
}
}
II
11111111111111111
Analyze the following code:
public class Test {
public static void main(String[] args) {
new B();
}
}
class A {
int i = 7;
public A() {
System.out.println("i from A is " + i);
}
public void setI(int i) {
this.i = 2 * i;
}
}
111111111111111111111
Given the following code, find the compile
error. Please select all that apply.
public class Test {
public static void main(String[] args) {
m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object());
}
public static void m(Student x) {
System.out.println(x.toString());
}
}
class GraduateStudent extends Student {
}
class B extends A {
public B() {
setI(20);
// System.out.println("i from B is " + i);
}
@Override
public void setI(int i) {
this.i = 3 * i;
}
}
The constructor of class A is called and it
displays "i from A is 7".
1111111111111
Analyze the following code:
public class Test {
public static void main(String[] args) {
new B();
}
}
class A {
int i = 7;
public A() {
setI(20);
System.out.println("i from A is " + i);
}
public void setI(int i) {
this.i = 2 * i;
}
}
class B extends A {
public B() {
// System.out.println("i from B is " + i);
}
@Override
public void setI(int i) {
this.i = 3 * i;
}
}
The constructor of class A is called and it
displays "i from A is 60"
111111111111111
Analyze the following code. Please select all
that apply.
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new Object();
System.out.println(a1);
System.out.println(a2);
}
}
class A {
int x;
@Override
public String toString() {
return "A's x is " + x;
}
}
When executing System.out.println(a2), the
toString() method in the Object class is
invoked
When
executing System.out.println(a1), the
toString() method in the A class is invoked.
class Student extends Person {
@Override
public String toString() {
return "Student";
}
}
class Person extends Object {
@Override
public String toString() {
return "Person";
}
}
m(new Person()) causes an error
m(new Object()) causes an error
11111111111111111
Show the output of the following code:
(a)
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
1111111111111111
Show the output of following program:
1 public class Test {
2 public static void main(String[] args) {
3 A a = new A(3);
4 }
5 }
6
7 class A extends B {
8 public A(int t) {
9 System.out.println("A's constructor is
invoked");
10 }
11 }
12
13 class B {
14 public B() {
15 System.out.println("B's constructor is
invoked");
16 }
17 }
Is the no-arg constructor of Object invoked
when new A(3) is invoked?
B's constructor is invoked
A's constructor is invoked
1111111111111111
Show the output of following program:
public class Test {
public static void main(String[] args) {
new A();
new B();
}
}
class A {
int i = 7;
public A() {
setI(20);
System.out.println("i from A is " + i);
}
public void setI(int i) {
this.i = 2 * i;
}
class Student extends Person {
@Override
public String getInfo() {
return "Student";
}
}
}
class Person {
public String getInfo() {
return "Person";
}
public void setI(int i) {
this.i = 3 * i;
}
}
i from A is 40
i from A is 60
i from B is 60
11111111111111
What is the output of the following code?
public void printPerson() {
System.out.println(getInfo());
}
}
(b)
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class B extends A {
public B() {
System.out.println("i from B is " + i);
}
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
private String getInfo() {
return "Student";
}
}
class Student extends Person {
@Override
public String getInfo() {
return "Student";
}
}
class Person {
private String getInfo() {
return "Person";
}
class Person {
public String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
}
(a)
Person
Student
public void printPerson() {
System.out.println(getInfo());
}
}
Person Student
11111111111
What is the output of the following code?
(b)
Person
Person
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
private String getInfo() {
return "Student";
}
}
class Person {
private String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
}
Person Person
11111111111111
Assume Cylinder is a subtype of Circle.
Analyze the following code:
}
}
Program 1 displays true and Program 2
displays false
111111111111111111
What is the output of the following code?
public class Test {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.print((s1 == s2) + " " +
(s1.equals(s2)));
}
}
false true
1111111111111111
What is the output of running class Test?
public class Test {
public static void main(String[] args) {
new Circle9();
}
}
Circle c = new Circle (5);
Cylinder c = cy;
The code has a compile error.
1111111111111111111
Given the following classes and their
objects:
public abstract class GeometricObject {
protected GeometricObject() {
System.out.print("A");
}
class C1 {};
class C2 extends C1 {};
class C3 extends C1 {};
protected GeometricObject(String
color, boolean filled) {
System.out.print("B");
}
}
C2 c2 = new C2();
C3 c3 = new C3();
Analyze the following statement:
c2 = (C2)((C1)c3);
You will get a runtime error because you
cannot cast objects from sibling classes.
1111111111111
Given the following code, which of the
following expressions evaluates to false?
class C1 {}
class C2 extends C1 { }
class C3 extends C2 { }
class C4 extends C1 {}
C1 c1 = new C1();
C2 c2 = new C2();
C3 c3 = new C3();
C4 c4 = new C4();
c4 instanceof C2
1111111111111111111
Analyze the following code.
// Program 1:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(Object a) {
return this.x == ((A)a).x;
}
public class Circle9 extends GeometricObje
ct {
/** No-arg constructor */
public Circle9() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius
*/
public Circle9(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius,
filled, and color */
public Circle9(double radius, String
color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}
BEDC
111111111111111
Show the output of running the class Test in
the following code lines:
interface A {
}
class C {
}
class D extends C {
}
class B extends D implements A {
}
}
// Program 2:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
public class Test {
public static void main(String[] args) {
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of
A");
if (b instanceof C)
System.out.println("b is an instance of
C");
}
}
b is an instance of A followed by b is an
instance of C.
1111111111111111111111
Download