CSC205 Test 2 (Modules 4, 5, 6, 7, 8, 9)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

inheritance creates an ____ relationship between the parent and child classes

"is-a"

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

@Override

Which is true? The FileOutputStream class includes println( ) A PrintWriter object should be closed using close( ) A PrintWriter constructor requires an OutputStream object A FileOutputStream object opens an existing file

A PrintWriter constructor requires an OutputStream object

Which of the following is correct? A base class is a parent class or super class. A base class is a child class or derived class. A parent class is a subclass of its child. A child class is a super class of its parent. None of these are correct.

A base class is a parent class or super class. The terms base class, parent class, and super class are synonyms for one another. They all imply that the class will be used as a base for inheritance, and that subsequent classes will extend (inherit from) the base class.

public modifier

Can be applied to a class and/or individual data members and methods within a class; indicates that the given item is accessible across package boundaries, the following class and method are both accessible outside the package in which they're defined. package domain; public class Book { public void checkOut() { } } Same goes for fields, not just methods.

T/F All methods in an abstract class must also be declared abstract.

F

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

F

concrete class

In contrast with the term abstract class, a class with no abstract methods is called a concrete class. All the other classes we have been creating were all concrete classes.

What is inheritance?

Inheritance is the process of deriving a new class from an existing one.

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

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

All Java classes are derived, directly or indirectly, from the ______ class.

Object

Which XXX is needed for enterNumber() to read a value from standard input? public int enterNumber() XXX { int value; value = System.in.read(); return value; } import java.io throws SystemException nothing is needed throws IOException

throws IOException

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

are final and static

In order to be used by a derived class in a constructor, the super call must:

be the FIRST statement in the constructor

T/F Because every class directly or indirectly inherits from the Object class, every class inherits the Object class's members.

T

T/F Every class has a toString method and an equals method inherited from the Object class.

T

T/F The fact that the System.out.println() method is able to handle such a wide variety of objects, and print them correctly, is an example of the polymorphic nature of the println() method.

T Precisely! Since println() is highly polymorphic in nature, it is able to print a wide variety of pre-defined (library) and built-in (primitive) data correctly.

Local variables during a method call are stored in ___. local memory static memory the stack the heap

the stack

The _______ and ________ methods are inherited by every class in every Java program.

toString and equals methods

T/F Assume that Poodle is a derived class of Dog and that Dog d = new Dog(...) and Poodle p = new Poodle(...) where the ... are the necessary parameters for the two classes. The assignment statement d = p; is legal even though d is not a Poodle.

T Since Poodle extends Dog, Dog is a wider class (Poodle is more specific and therefore narrower). An assignment statement can assign a narrower item into a wider variable (since p is narrower, it can be assigned to d).

T/F A polymorphic reference can refer to different types of objects over time.

T That is what polymorphism is all about: late binding. This means that the same name will be associated with different semantics as the program executes.

T/F An reference variable of a class can refer to any object of any class that are subclasses of that class.

T This is one of the polymorphic functions of using a class name to declare a reference variable.

T/F An interface reference can refer to any object of any class that implements the interface.

T This is one of the polymorphic functions of using an interface name to declare a reference variable.

T/F A method's parameter can be polymorphic, giving the method flexible control of its arguments.

T To accomplish this, merely use an interface name or a base class name to declare the variable. Then the parameter is polymorphic, referring to the correct instance of the class during execution, with the correct semantics being accessed during execution.

What is class hierarchy?

The child of one class can be the parent of one or more other classes, creating a class hierarchy.

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

The method header must begin with the key word default.

What are the main programming mechanisms that constitute object-oriented programming? polymorphism, recursion, abstraction encapsulation, inheritance, polymorphism inheritance, polymorphism, recursion encapsulation, abstraction, inheritance None of these

encapsulation, inheritance, polymorphism Encapsulation is the isolation of portions of code so that they cannot be accidentally modified; inheritance provides for code reuse; polymorphism provides for one-name, many semantics. Abstraction is a useful attribute, but it is not a mechanism. Recursion is a control structure, providing for a different way to express looping or repetition.

Polymorphism is achieved by abstraction encapsulation overriding overloading embedding

overriding Overloading just provides alternatives for methods with differing parameter lists. Overriding provides for polymorphism, because the appropriate method is invoked depending upon the object that currently is being referenced. Embedding is the enclosing of classes within classes. Abstraction has nothing to do with polymorphism. Encapsulation is achieved using the visibility modifiers.

What is the string in args[1] for the following: java AddInfo Brenda Stern 47 AddInfo Brenda Stern 47

Stern

Which of the following is TRUE about Java Classes? All classes must have one parent but may have any number of children (derived or extended). All classes can have any number (zero or more) of parent classes and any number of children (derived or extended) classes. All classes must have one parent and may have a single child (derived or extended) but may have any number of parent classes. All classes must have one parent and may have a single child (derived or extended) class. All classes can have either zero or one parent class and any number of child (derived or extended) classes.

All classes must have one parent but may have any number of children (derived or extended). Java supports inheritance but not multiple inheritance, so a Java class can have any number of children but only one parent. Further, since all Java classes inherit either directly or indirectly from the Object class, all Java classes have exactly 1 parent class.

Which statement is true? Command-line arguments are stored as an array of integers The value 24 could not be entered as a command-line argument since it is a number A program typically crashes if the user provides too many command-line arguments Command-line arguments are stored as an array of Strings

Command-line arguments are stored as an array of Strings

Which is true? A program must import java.io.system to use System.out System.output.print() only outputs objects of type String The output of println() for an object reference includes all data stored in the object Data written to System.out are placed in a buffer and eventually output

Data written to System.out are placed in a buffer and eventually output

T/F In an inheritance relationship, the subclass constructor always executes before the superclass constructor.

F

private modifier

The private modifier specifies that the member can only be accessed in its own class. In order to access that member in a child class, a parent method or super method must be used. For example, in the Teacher.java class (the child class of parent), the field name is initialized in the constructor by calling super(name). the name field is private

protected modifier

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

What is output? double pi = 3.14159; System.out.printf("The value of PI is -%4.3f", pi); The value of PI is -%4.3f The value of PI is -3.142 The value of PI is 0003.142 The value of PI is 3.14159

The value of PI is -3.142

abstract class

a class that defines attributes and methods for subclasses but is never instantiated Example, Animal and Cat class. The animal class is abstract and has abstract methods because it doesn't make sense to have an object "Animal". It does make sense to have an object "Cat". The Animal class can have methods like "makeNoise" that is derived by the child classes and MUST be defined in EVERY child class.

When a subclass overloads a superclass method ________. only the subclass method may be called with a subclass object neither method may be called with a subclass object only the superclass method may be called with a subclass object both methods may be called with a subclass object

both methods may be called with a subclass object

In order to determine the type that a polymorphic variable refers to, the decision is made by the programmer at the time the program is written by the Java run-time environment at run time by the operating system when the program is loaded into memory by the user at run time by the compiler at compile time

by the Java run-time environment at run time The polymorphic variable can take on many different types, but it is not known which type it has taken on until the program is executing. At the time the variable is referenced, then the decision must be made. That decision is made by the run-time environment based on the latest assignment of the variable.

Java program instructions are stored in a memory location called ___. code static memory the stack the heap

code

Inheritance through an extended (derived) class supports which of the following concepts? modulary correctness code reuse interfaces information hiding

code reuse By extending a class and inheriting from it, the new class does not have to reimplement any of those inherited methods or instance data, thus saving the programmer time and effort. So, code reuse is the ability to reuse someone else's code for your benefit by extending it for your needs.

Java does not support multiple inheritance but some of the abilities of multiple inheritance are available by using public rather than protected or private modifiers creating aliases implementing interfaces importing classes overriding parent class methods

implementing interfaces Since a class can implement any number of interfaces, that class is in essence using the interface classes as if those interfaces were defined in this class. So this class is inheriting the methods and constants of the interfaces. Further, the class could extend another class and thus inherit directly and indirectly from multiple classes. This is not the exact same as multiple inheritance, but it is as close as Java comes to that concept.

An unreachable object ___. has an object reference count of -1 is marked for deallocation when a programmer calls freeMemory( ) is marked for deallocation is immediately deallocated by the Java virtual machine

is marked for deallocation

Which values are marked for deallocation after the sequence? Integer obj1 = new Integer(10); Integer obj2 = new Integer(20); Integer obj3 = obj2; Integer obj4 = new Integer(40); obj2 = null; obj4 = obj1; only 20 only 40 20 and 40 no values are marked for deallocation

only 40

Aside from permitting inheritance, the visibility modifier protected is also used to: permit access to the protected item by any static class ensure that the class cannot throw a NullPointException define abstract elements of an interface permit access to the protected item by any class defined in the same package permit access to the protected item by any parent class

permit access to the protected item by any class defined in the same package The visibility modifier protected is used to control access to the item in a protected (guarded) manner. The protection is that access is restricted to the current class (like private items), classes in the same package, or extended classes of this class.

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

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

Assume ArrayList productList contains 10,000 items. Which operation is performed slowest? productList.set(0, item) productList.remove(9999) productList.add(0, item) productList.add(item)

productList.add(0, item)

All methods specified by an interface are ________. protected private static public

public

In an interface all methods have: private access protected access public access packaged access

public access

Which of the following statements correctly specifies three interfaces? public class ClassA implements Interface1, Interface2, Interface3 public class ClassA implements [Interface1, Interface2, Interface3] public class ClassA implements (Interface1, Interface2, Interface3) public class ClassA implements Interface1 Interface2 Interface3

public class ClassA implements Interface1, Interface2, Interface3

A parent's constructor can be invoked using the ______ reference.

super

The ________ key word is used to call a superclass constructor explicitly. extends goto this super

super

T/F Is it possible to use both overloading and overriding in the same method?

F

T/F Assume that Poodle is a derived class of Dog and that Dog d = new Dog(...) and Poodle p = new Poodle(...) where the ... are the necessary parameters for the two classes. The assignment statement p = d; is legal even though d is not a Poodle?

F Inheritance is an is-a relationship. This is not legal because a dog is not always a poodle. We are assigning a dog reference to a poodle which is a special type of a dog and might have methods which a dog does not even know about. These are the reasons that this is not a legal assignment in Java.

The instruction super(); does which of the following? It calls the constructor as defined in the current class's parent class. It calls the method super as defined in the current class's parent class. It calls the method super as defined in the current class. It calls the constructor as defined in the current class. It calls the method super as defined in java.lang.

It calls the constructor as defined in the current class's parent class. The instruction super(); represents an invocation of something in the current class's parent class. Since there is no message but merely super(), it is an invocation of the parent class's constructor.

Which is true? Local reference variables do not automatically go out of scope when the method returns A programmer must set an object reference to null to ensure it will be marked for deallocation An object is deallocated when its reference count increments to one Local reference variables are automatically marked for deallocation when the method returns

Local reference variables are automatically marked for deallocation when the method returns

What is the primary purpose of inheritance?

One purpose of inheritance is to reuse existing software.

What is output? (_ represents a blank space) int age = 50; System.out.printf("She is %4d years old", age); She is 50 years old She is %4d years old She is 0050 years old She is __50 years old

She is __50 years old

T/F A functional interface is simply an interface that has one abstract method.

T

final modifier

use "final" to create a constant. The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change. For example, the following variable declaration defines a constant named PI, whose value is an approximation of pi (the ratio of the circumference of a circle to its diameter): static final double PI = 3.141592653589793;

Which statement copies the contents of sw to userInput? StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); pw.println("Java is awesome!"); String userInput; userInput = pw userInput = sw.toString(); userInput = sw; userInput = pw.print();

userInput = sw.toString();

If you instantiate an abstract class, the class or object you wind up with is an interface is a normal class is a reference to an Object is also an abstract class you can't instantiate an abstract class.

you can't instantiate an abstract class

If you instantiate an abstract class, the class or object you wind up with is also an abstract class is an interface is a normal class is a reference to an Object you can't instantiate an abstract class

you can't instantiate an abstract class You only can instantiate concrete classes, not abstract ones. But you can extend abstract classes as well as interfaces.

If a class contains an abstract method ________. you must create an instance of the class the method cannot be overridden in subclasses None of the others are true the method will only have a header, but not a body, and will end with a semicolon

you must create an instance of the class


Set pelajaran terkait

Broker Management and Supervision of Salespersons, Segment 1: The Supervisory Duties and Responsibilities of a Broker

View Set

02/25/16 FG_Growth of the Cranium and the Cranial Base

View Set

N372 Week 15 Hematology/Oncology

View Set

ap environmental unit 2 progress check q's

View Set

NEMCC Intro to Business Ms Bowlin FINIAL

View Set