Java: Chapter 10

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

An abstract class is not instantiated, but serves as a superclass for other classes. True/False

True

This annotation tells the Java compiler that a method is meant to override a method in the superclass. A) @Inherited B) @Override C) @Protected D) @Overload

B) @Override

Protected class members are denoted in a UML diagram with the symbol A) # B) - C) + D) *

A) #

Private members of the superclass cannot be accessed by the subclass. True/False

True

When an interface variable references an object, you can use the interface variable to call any and all of the methods in the class implementing the interface. True/False

False

In the following statement, which is the superclass? public class ClassA extends ClassB implements ClassC A) ClassA B) ClassC C) Cannot tell D) ClassB

D) ClassB

Every class has a toString method and an equals method inherited from the Object class. True/False

True

Every class is either directly or indirectly derived from the Object class. True/False

True

Inheritance involves a subclass, which is the general class, and a superclass, which is the specialized class. True/False

False

If ClassA extends ClassB, then: A) neither public or private members in ClassB can be directly accessed in ClassA B) private members in ClassB are changed to protected members in ClassA C) public and private members of ClassB are public and private, respectively, in ClassA D) public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA

D) public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA

Look at the following code. Which line in ClassA has an error? Line 1 public interface MyInterface Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double); Line 5 } Line 6 public class ClassA implements MyInterface Line 7 { Line 8 FIELDA = 60; Line 9 public int methodA(double) {} Line 10 } A) 7 B) 8 C) 6 D) 9

B) 8

When a subclass overloads a superclass method: A) Only the subclass method may be called with a subclass object B) Both methods may be called with a subclass object C) Neither method may be called with a subclass object D) Only the superclass method may be called with a subclass object

B) Both methods may be called with a subclass object

What is required for an interface method that has a body? A) The method header must begin with the key word default. B) The @Default annotation must precede the method header. C) All of these D) A class that implements the interface must override the method.

A) The method header must begin with the key word default.

When declaring class data members, it is best to declare them as: A) private members B) restricted members C) protected members D) public members

A) private members

What key word can you use to call a superclass constructor explicitly? A) super B) extends C) this D) goto

A) super

In an interface all methods have: A) protected access B) packaged access C) public access D) private access

C) public access

Look at the following code and determine what the call to super will do. public class ClassB extends ClassA { public ClassB() { super(10); } } A) This cannot be determined form the code shown. B) The method super will have to be defined before we can say what will happen. C) It will call the constructor of ClassA that receives an integer as an argument. D) It will call the method named super and pass the value 10 to it as an argument.

C) It will call the constructor of ClassA that receives an integer as an argument.

If a method in a subclass has the same signature as a method in the superclass, the subclass method overloads the superclass method. True/False

False

An anonymous inner class must: A) either implement an interface or extend another class B) implement an interface C) both implement an interface and extend another class D) extend another class

A) either implement an interface or extend another class

All methods in an abstract class must also be declared abstract. True/False

False

Given the following code which of the following is TRUE? public class ClassB implements ClassA{} A) ClassA must override each method in ClassB. B) ClassA inherits from ClassB. C) ClassB must override each method in ClassA. D) ClassB inherits from ClassA.

C) ClassB must override each method in ClassA.

Look at the following code. What is missing from ClassA? Line 1 public interface MyInterface Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double); Line 5 } Line 6 public class ClassA implements MyInterface Line 7 { Line 8 FIELDA = 60; Line 9 public int methodB(double) {} Line 10 } A) It does not overload methoda. B) Nothing is missing. It is a complete class. C) It does not override methoda. D) It does not have a constructor.

C) It does not override methoda.

If two methods have the same name but different signatures, they are: A) overridden B) subclass methods C) overloaded D) superclass methods

C) overloaded

A functional interface is simply an interface that has one abstract method. True/False

True

Look at the following code: Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB() {} Line 9 public void method1() {} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC() {} Line 14 public void method1() {} Line 15 } Which method will be executed when the following statements are executed? ClassC item1 = new ClassA(); item1.method1(); A) Line 4 B) Line 9 C) Line 14 D) This is an error and will cause the program to crash.

D) This is an error and will cause the program to crash.

In an inheritance relationship, the subclass constructor always executes before the superclass constructor. True/False

False

A compiler error will result if an anonymous inner class tries to use a variable that is not final, or not effectively final. True/False

True

Look at the following code. Which line will cause a compiler error? Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a) {} Line 5 public final int method2 (double b) {} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB() {} Line 10 public int method1 (int b) {} Line 11 public int method2 (double c) {} Line 12 } A) 5 B) 10 C) 4 D) 11

D) 11

Look at the following code. Which line has an error? Line 1 public interface Interface1 Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double) {} Line 5 A) 1 B) 3 C) 2 D) 4

D) 4

Look at the following code: Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB() {} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC() {} Line 14 public void method1() {} Line 15 } Which method1 will be executed when the following statements are executed? ClassA item1 = new ClassB(); item1.method1(); A) Line 9 B) Line 14 C) Line 4 D) This is an error and will cause the program to crash.

A) Line 9

When a method is declared with the ________ modifier, it cannot be overridden in a subclass. A) final B) public C) super D) extends

A) final

Which of the following is TRUE about protected access? A) Protected members may be accessed by methods in the same package or in a subclass, but only if the subclass is in the same package. B) Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package. C) Protected members cannot be accessed by methods in any other classes. D) Protected members are actually named constants.

B) Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package.

This is a variable whose value is never changed, but it isn't declared with the final key word. A) anonymous inner variable B) virtually constant variable C) effectively final variable D) default variable

C) effectively final variable

A subclass can directly access: A) only public and protected members of the superclass B) only public and private members of the superclass C) only protected and private members of the superclass D) all members of the superclass

A) only public and protected members of the superclass

All fields declared in an interface: A) have protected access B) are final and static C) have private access D) must be initialized in the class implementing the interface

B) are final and static

A subclass may call an overridden superclass method by: A) using the extends keyword before the method is called B) prefixing its name with the name of the superclass in parentheses C) prefixing its name with the super key word and a dot (.) D) calling the superclass method first and then calling the subclass method

C) prefixing its name with the super key word and a dot (.)

It is not possible for a superclass to call a subclass's method. True/False

True

In the following statement, which is the subclass? public class ClassA extends ClassB implements ClassC A) ClassA B) ClassC C) Cannot tell D) ClassB

A) ClassA

What is wrong with the following code? IntCalculator square = new IntCalculator() { public int calculate(int number) { return number + number; }} A) The statement does not end with a semicolon. B) The outer braces are not needed. C) The inner braces are not needed. D) The new key word is not needed.

A) The statement does not end with a semicolon.

In an inheritance relationship: A) The superclass constructor always executes before the subclass constructor B) The constructor with the lowest overhead always executes first regardless of inheritance C) The subclass constructor always executes before the superclass constructor D) The unified constructor always executes first regardless of inheritance

A) The superclass constructor always executes before the subclass constructor

When an "is a" relationship exists between objects, it means that the specialized object has: A) all the characteristics of the general object, plus additional characteristics B) none of the characteristics of the general object C) some of the characteristics of the general class, but not all, plus additional characteristics D) some of the characteristics of the general object, but not all

A) all the characteristics of the general object, plus additional characteristics

In the following code, what will the call to super do? public class ClassB extends ClassA { public ClassB() { super(40); System.out.print("This is the last statement " + "in the constructor."); } A) It will call the method super and pass the value 40 to it as an argument. B) It will call the constructor of ClassA that receives an integer as an argument. C) The method super will have to be defined before we can say what will happen. D) This cannot be determined from the code.

B) It will call the constructor of ClassA that receives an integer as an argument.

If a subclass constructor does not explicitly call a superclass constructor: A) the superclass fields will be set to the default values for their data types B) Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes C) Java will automatically call the superclass's default or no-arg constructor immediately after the code in the subclass's constructor executes D) it must include the code necessary to initialize the superclass fields

B) Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes

Look at the following code: Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB() {} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC() {} Line 14 public void method1() {} Line 15 } Which method1 will be executed as a result of the following statements? ClassA item1 = new ClassC(); item1.method1(); A) Line 9 B) Line 14 C) Line 4 D) This is an error and will cause the program to crash.

B) Line 14

Replacing inadequate superclass methods with more suitable subclass methods is known as what? A) Method upgrading B) Method overriding C) Method overloading D) Tactical inheritance

B) Method overriding

What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { super (40); System.out.println("This is the last statement " + "in the constructor."); } } A) No values may be passed to super. B) Nothing is wrong with the code. C) The method super is not defined. D) No values may be passed to super.

B) Nothing is wrong with the code.

If ClassC extends ClassB, which extends ClassA, this would be an example of: A) packaging B) a chain of inheritance C) multiple inheritance D) a family tree

B) a chain of inheritance

The super statement that calls the superclass constructor: A) can appear in any method of the subclass B) must be the first statement in the subclass's constructor C) must be the first statement in the superclass's constructor D) is deprecated and is no longer supported in newer versions of Java

B) must be the first statement in the subclass's constructor

If a class contains an abstract method: A) All of these. B) the method will have only a header, but not a body, and end with a semicolon C) the method cannot be overridden in subclasses D) you must create an instance of the class

B) the method will have only a header, but not a body, and end with a semicolon

If a superclass does not have a default constructor or a no-arg constructor: A) then a class that inherits from it, does not inherit the data member fields from the superclass. B) then a class that inherits from it, must call one of the constructors that the superclass does have. C) then a class that inherits from it, must initialize the superclass values. D) then a class that inherits from it, must contain the default constructor for the superclass.

B) then a class that inherits from it, must call one of the constructors that the superclass does have.

Which of the following is an example of a lambda expression? A) All of these B) IntCalculator = new divider(x, 2); C) int x = x * factor; D) IntCalculator multiplier = x and x * factor;

D) IntCalculator multiplier = x and x * factor;

What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { int init = 10; super (40); } } A) No values may be passed to super. B) Nothing is wrong with the code. C) The method super is not defined. D) The call to the method super must be the first statement in the constructor.

D) The call to the method super must be the first statement in the constructor.

In UML diagrams, inheritance is shown: A) With a line that has a closed arrowhead at one end that points to the superclass B) With a line that has an open arrowhead at one end that points to the subclass C) With a line that has a closed arrowhead at one end that points to the subclass D) With a line that has an open arrowhead at one end that points to the superclass

D) With a line that has an open arrowhead at one end that points to the superclass

Like a family tree, a ________ shows the inheritance relationship between classes. A) flowchart B) class map C) binary tree D) class hierarchy

D) class hierarchy

When one object is a specialized version of another object, there is this type of relationship between them. A) direct B) has a C) contains a D) is a

D) is a

If two methods in the same class have the same name but different signatures, the second overrides the first. True/False

False

When a subclass extends a superclass, the public members of the superclass become public members of the subclass. True/False

True


Kaugnay na mga set ng pag-aaral

Irish History: Fenianism to Home Rule

View Set

Chap 22- Auditory and Visual Problems

View Set