CSC 2650 - Chapter 11 Quiz

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which of the following is incorrect? A. A constructor may be static. B. A constructor may be private. C. A constructor may invoke a static method. D. A constructor may invoke an overloaded constructor. E. A constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass?s constructor.

a. A constructor may be static.

Which of the following statements are true? A. A method can be overloaded in the same class. B. A method can be overridden in the same class. C. If a method overloads another method, these two methods must have the same signature. D. If a method overrides another method, these two methods must have the same signature. E. A method in a subclass can overload a method in the superclass.

a. A method can be overloaded in the same class. d. If a method overrides another method, these two methods must have the same signature. e. A method in a subclass can overload a method in the superclass.

What is the output of the following code? 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()); } } A. Person Person B. Person Student C. Student Student D. Student Person

a. Person Person

Which of the following statements are true? A. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. B. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. C. It is a compile error if two methods differ only in return type in the same class. D. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. E. 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.

a. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. b. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. c. It is a compile error is two methods differ only in return type in the same class. d. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. e. A static method cannot be overridden. If a static method defined in a superclass is redefined in a subclass, the method defined in the superclass is hidden.

Encapsulation means _.

a. that data fields should be declared private

Which of the following statements are true? A. A subclass is a subset of a superclass. B. A subclass is usually extended to contain more functions and more detailed information than its superclass. C. "class A extends B" means A is a subclass of B. D. "class A extends B" means B is a subclass of A.

b. A subclass is usually extended to contain more functions and more detailed information than its superclass. c. "class A extends B" means A is a subclass of B

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"; } } A. I B. II C. Both I and II D. Neither

b. II

What is the output of the following code? public class Test{ public static void main(String[] args){ new Person().printPerson(); new Student().printPerson(); } } class Student extends Person{ @Override public String getInfo(){ return "Student"; } } class Person{ public String getInfo(){ return "Person"; } public void printPerson(){ System.out.println(getInfo()); } } A. Person Person B. Person Student C. Student Student D. Student Person

b. Person Student

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; } } class B extends A{ public B90{ setI(20); //System.out.println("I from B is " + i); } @Override public void setI(int i){ this.i = 3 * i; } } A. The constructor of class A is not called. B. The constructor of class A is called and it displays "i from A is 7". C. The constructor of class A is called and it displays "i from A is 40". D. The constructor of class A is called and it displays "i from A is 60".

b. The constructor of class A is called and it displays "I from A is 7".

Analyze the following code: public class A extends B{ } class B { public B (String s) { } } A. The program has a compile error because A does not have a default constructor. B. The program has a compile error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor. C. The program would compile fine if you add the following constructor into A: A(String s) { } D. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

b. The program has a compile error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor. d. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

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); } } A. The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the length of the Square. B. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally. C. The program compiles fine, but it has a runtime error because of invoking the Square class's constructor illegally.

b. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.

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); } } A. The program does not compile because Test does not have a default constructor Test(). B. 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. C. The program would compile if a default constructor A(){ } is added to class A explicitly. D. The program compiles, but it has a runtime error due to the conflict on the method name print.

b. 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. c. The program would compile if a default constructor A() {} is added to class A explicitly.

Object-oriented programming allows you to derive new classes form existing classes. This is called _. A. encapsulation B. inheritance C. abstraction D. generalization

b. inheritance

Which of the following are Java keywords? A. instanceOf B. instanceof C. cast D. casting

b. instanceof

Inheritance means _.

b. that a class can extend another class

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(); } } A. Nothing displayed B. "The default constructor of B is invoked" C. "The default constructor of A is invoked" followed by "The default constructor of B is invoked" D. "The default constructor of B is invoked" followed by "The default constructor of A is invoked" E. "The default constructor of A is invoked"

c. "The default constructor of A is invoked" followed by "The default constructor of B is invoked"

Analyze the following code: 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; } } A. The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString()); B. When executing System.out.println(a1), the toString() method in the Object class is invoked. C. When executing System.out.println(a2), the toString() method in the Object class is invoked. D. When executing System.out.println(a1), the toString() method in the A class is invoked.

c. When executing System.out.println(a2), the toString() method in the Object class is invoked. d. When executing System.out.println(a1), the toString() method in the A class is invoked.

Which of the statements regarding the super keyword is incorrect? A. You can use super to invoke a super class constructor. B. You can use super to invoke a super class method. C. You can use super.super.p to invoke a method in superclass's parent class. D. You cannot invoke a method in superclass's parent class.

c. You can use super.super.p to invoke a method in superclass's parent class.

Which of the following classes cannot be extended?

c. final class A{}

Given the following code, find the compile error. 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 Student extends Person{ @Override public String toString(){ return "Student"; } } class Person extends Object{ @Override public String toString(){ return "Person"; } }

c. m(new Person()); causes an error d. m(new Object()); causes an error

Polymorphism means _.

c. that a variable of a super type can refer to a subtype

Which of the following statements is false? A. You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism. B. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time. C. A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime. D. Dynamic binding can apply to static methods. E. Dynamic binding can apply to instance methods.

d. Dynamic binding can apply to static methods.

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; } } A. The constructor of class A is not called. B. The constructor of class A is called and it displays "i from A is 7". C. The constructor of class A is called and it displays "i from A is 40". D. The constructor of class A is called and it displays "i from A is 60".

d. The constructor of class A is called and it displays "I from A is 60".

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){ } } A. The program has a compile error, because m is overridden with a different signature in B. B. The program has a compile error, because b.m(5) cannot be invoked since the method m(int) is hidden in B. C. The program has a runtime error on b.i, because i is not accessible from b. D. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

d. The method m is not overridden in B. B inherits the method m from A and defines an overload method m in B.

Compostition means _.

d. that a class contains a data field that references another object


Ensembles d'études connexes

Chapter 13: Employment Law II: Discrimination

View Set

Solve Quadratic Equations Std. A.REI.4.b

View Set

Chapter 11, 16, 18, 21 Questions

View Set

Intermediate financial accounting chapter 20

View Set

usmle step 3: uworld practice test 2:

View Set