Module 2 - OOP Core Concepts and Recursion

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Assume you want to create a class Friend and you want to specify that a Friend is a Human (you want to specify that there is an inheritance relationship between class Friend and class Human) Which keyword should you use in the class declaration to specify that there is an inheritance relationship between the two classes? A. inherits B. derives C. is_a D. extends

D. extends

The programming example had to call the same methods on a dog, a sled dog, and a circus dog. In order to avoid code duplication, the shared code was extracted into a private method that included one parameter. What was the type of the parameter? A. CircusDog B. SledDog C. Dog D. Object

C. Dog

Complete the sentence: Which methods are accessible depend on the __________. ( type of the variable - type of the instance that is referenced ) Which specific method is executed depends on the _______. ( type of the variable - type of the instance that is referenced )

( type of the variable ) ( type of the instance that is referenced )

Assume you have two Circle objects c1 and c2. You know that c1.equals(c2) returns false. Is the following statement true or false? Given that c1.equals(c2) returns false we can be certain that c1.hashCode() is different from c2.hashCode(). True or False?

False

Complete the following sentence by providing the name of the class. If no superclass is specified, the class implicitly derives from class ____________.

Object

Assume you have a class Friend that is a sub-class of class Human. You have two variables: Friend boy1 = new Friend("Mike"); Human boy2 = new Human("Fritz"); Which of the following conditions checks whether boy2 is a friend? A. (boy2 instanceof boy1) B. (boy2 instanceof Friend) C. (instanceof(boy2, Friend)) D. (instanceof(boy2, boy1))

B. (boy2 instanceof Friend)

Only one of the following choices can be a valid class member of an abstract class. Which one is it? A. An instance of another abstract class B. A method with no body that has been declared with the key word abstract C. A static method that has no body and has been declared with the key word abstract D.A constructor that has been declared with the key word abstract

B. A method with no body that has been declared with the key word abstract

Assume you have two classes where class Cat derives from class Animal as shown in the UML class diagram. You also have two instances that were declared as follows: Cat myCat = new Cat(); Animal myAnimal = new Cat(); One of the following code statements won't compile because it requires explicit type conversion. Which one is it? A. Cat cat2 = new Cat(); B. Cat cat1 = myAnimal; C. Animal animal1 = myAnimal; D. Animal animal2 = myCat;

B. Cat cat1 = myAnimal;

[ClassX]-------------->[ClassY] The image above shows a unary association. Which of the following best describes the relationship between ClassX and ClassY? A. ClassY is a superclass from ClassX and ClassX is a subclass from ClassY B. ClassX knows about ClassY and ClassY knows nothing about ClassX C. ClassY knows about ClassX and ClassX knows nothing about ClassY D. ClassX is a superclass from ClassY and ClassY is a subclass from ClassX

B. ClassX knows about ClassY and ClassY knows nothing about ClassX

All 5 methods, that can be overridden, have an explicit general contract. Why is it so important to follow that contract? A. It isn't important to follow the contract. Even though it is called a contract it is merely a recommendation B. Other classes rely on the proper implementation of these methods C. If the contract is not followed, the code won't compile D. If the contract is not followed, an exception will be thrown

B. Other classes rely on the proper implementation of these methods

Which of the following statements describes best the access modifier protected? A. Protected members are accessible within its own class and its subclasses B. Protected members are accessible within its own class, its subclasses and other classes in the same package C. Protected members are accessible within its own class, its superclasses and other classes in the same package D. Protected members are accessible within its own class and its superclasses

B. Protected members are accessible within its own class, its subclasses and other classes in the same package

Which of the following diagrams shows an aggregation? A) [ClassX]<filled>--------->[ClassY] B) [ClassX]----------------->[ClassY] C) [ClassX]<>--------------->[ClassY]

C) [ClassX]<>--------------->[ClassY]

Classes that implement the interface specify the method signature and thus how to call a method. a. Classes that implement the interface b. Interfaces Implementation details are decided by the interface. a. interface b. classes that implement the interface

b. Interfaces b. classes that implement the interface

Check ALL statements that describe inheritance. a. It allows us to models a has-a relationship between classes. b. It allows us to models an is-a relationship between classes. c. Its purpose is dynamic binding which provides flexibility.

b. It allows us to models an is-a relationship between classes. c. Its purpose is dynamic binding which provides flexibility.

Match the description on the left to the corresponding class or method on the right. Each option on the right should be used exactly once. ( Abstract method - Static method - Final method - Abstract class - Final class) 1. This type of a class can only be subclassed but not instantiated. It can contain methods that are declared but not implemented. 2. This type of a class cannot be subclassed 3. This type of a method cannot be overridden by subclasses. 4. This type of a method is declared without an implementation (no method body just a semicolon) 5. This type of a method has no access to instance data. It belongs to the class and can be called on the type.

1. Abstract class 2. Final class 3. Final method 4. Abstract method 5. Static method

Match each description on the left to the corresponding category on the right. ( instance methods - default methods - static methods - abstract methods ) 1. Include no implementation in their method declaration 2. Allow interfaces to provide a default implementation 3. Have access to instance fields 4. Belong to the type/class and cannot be overridden

1. abstract methods 2. default methods 3. static methods 4. instance methods

Match each relationship on the left to the corresponding description on the right. ( class interface - extending an interface - implementing an interface ) 1. "is-a" relationship between classes 2. "is-a" relationship between a class and an interface 3. "is-a" relationship between internships

1. class interface 2. implementing an interface 3. extending an interface

See Question 5 https://slcc.instructure.com/courses/655149/quizzes/2221990?module_item_id=14558015

A. The move method overridden in class Boat

Below you find 4 diagrams. Which one best describes the relationship between Face and Eye? In this context we talk about naturals eyes only (no eye transplants nor glass eyes) A) [Face]<filled>--------->2[Eye] B) [Face]2<---------<filled>[Eye] C) [Face]<>--------------->2[Eye] C) [Face]2<---------------<>[Eye]

A) [Face]<filled>--------->2[Eye]

Sometimes we want to specify how many objects are in a given relationship. How do we indicate that there are one to four ( 1, 2, 3, or 4 ) objects in a UML class diagram? A. 1..4 B. 1-4 C. <4 D. 1-5 E. 1..5

A. 1..4

Which of the following options best describes downcasting? A. Casting a variable from a superclass to assign it to a variable of a subclass. B. Casting a variable from a subclass to assign it to a variable of a superclass. C. Assigning a variable from a subclass to a variable of a superclass.

A. Casting a variable from a superclass to assign it to a variable of a subclass.

Why should we use the @Override annotation? A. It allows the compiler to catch errors in the method signature and it makes the code more clear. B. It allows the compiler to catch logic errors and it makes the code run faster. C. It allows the compiler to catch logic errors and it makes the code more clear. D. It allows the compiler to catch errors in the method signature and it makes the code run faster.

A. It allows the compiler to catch errors in the method signature and it makes the code more clear.

__________________________...........................__________________ |________ClassA_______|...........................|____ClassB___| |____-my : ClassB____|------> - myB|_________________| |_+ doSomething()_|...........................|_+ actionB()_| Which of the options below best describes the diagram? A. This diagram has a problem. It is wrong to display the attribute myB twice. B. This diagram is all right. It shows some redundancy but that is no problem C. This diagram is excellent. It clearly shows the relationship between ClassA and ClassB

A. This diagram has a problem. It is wrong to display the attribute myB twice.

See Question 6 https://slcc.instructure.com/courses/655149/quizzes/2221986?module_item_id=14558025

B.

Which of the following statements about abstract methods is false? A. Abstract methods are intended to be overridden B. Abstract methods are implicitly final C. Abstract methods are declared with the keyword abstract D.Abstract methods have no method body (no implementation)

B. Abstract methods are implicitly final

Complete the sentence below: In Java, subclasses have direct access to all the public and protected class members of the super-class. In the example provided, class SledDog had direct access to ................. of class Dog. A. all methods B. the field, constructor and all methods C. the field and all methods D. the constructor and all methods

A. all methods

See Question 12 https://slcc.instructure.com/courses/655149/quizzes/2221986?module_item_id=14558025

C. MigratoryBird myMigratoryBird = myBird;

One of the followings statements about final methods is false. Which one is it? A. Private methods are implicitly final B. Static methods are implicitly final C. Final methods cannot be overridden in a subclass D. A subclass can provide its own implementation of final superclass methods

D.A subclass can provide its own implementation of final superclass methods

Check ALL options that describe a reason to use final classes or methods. a. efficiency b. security c. adapterbility d. consistency

a. efficiency b. security d. consistency

Assume you need to implement the equals method of class Circle. Here is the method header: public boolean equals(Object obj) What should you do if the argument passed was null? A. return false B. No need to handle this situation in code. It won't even compile C. throw an IllegalArgumentException D. throw a NullPointerException

A. return false

In order to implement an existing interface, we need to know about the interface and its methods. Let's assume you need to implement an interface from the Java API. Which of the following best describes how to find out enough information about the interface methods to start the implementation? A) - Look up the interface in the Java API - Find out how many methods are included - Implement all interface methods that need implementation. Any implementation is fine, as long as the method signatures are matched. B) - Look up the interface in the Java API - Find out how many methods are included - For each interface method that needs implementation, check whether there are requirements or specifications in the detailed description, for example, specifications regarding the expected return value. C) - Look on the internet for an existing implementation of the interface - Adapt the code for your specific situation D) - Since Java 8 there is no longer a need to look up information about interface methods. - If our implementation doesn't match the specification, the default implementation will be used.

B) - Look up the interface in the Java API - Find out how many methods are included - For each interface method that needs implementation, check whether there are requirements or specifications in the detailed description, for example, specifications regarding the expected return value.

Why is it a good practice to program to an interface? A. It makes the code run faster B. It makes the code more flexible C. It provides additional information about the object

B. It makes the code more flexible

One of the statements does not compile because it misses a cast. Which one is it? A. Mammal myMammal = new Lion(); B. Lion myLion = new Mammal(); C. Mammal myMammal = new Mammal(); D. Lion myLion = new Lion();

B. Lion myLion = new Mammal();

Which of the following code statements is an example of programming to an interface? A. ArrayList<String> wordList = new List<>(); B. List<String> wordList = new ArrayList<>(); C. List<String> wordList = new List<>(); D. ArrayList<String> wordList = new ArrayList<>();

B. List<String> wordList = new ArrayList<>();

See question 2. https://slcc.instructure.com/courses/655149/quizzes/2222010?module_item_id=14558009

C.

Assume you have a class that implements an interface. Which of the following statements best identifies the interface members that should be overridden? A. All static methods and optionally default methods B. All methods that are declared in the interface C. All abstract methods and optionally default methods D. All abstract and static methods

C. All abstract methods and optionally default methods

What are the three pillars of object-oriented programming? A. Classes, Methods, Objects B. Classes, Inheritance, Polymorphism C. Encapsulation, Inheritance, Polymorphism D. Inheritance, Interface, Polymorphism

C. Encapsulation, Inheritance, Polymorphism

We can redefine the implementation of an inherited method from the superclass by overriding the method. Java doesn't require us to use the @Override annotation, but there are two significant advantages of doing so. What are those? A. It makes the code more concise and more efficient (faster) B. It makes the code more clear and more concise C. It allows the compiler to catch errors in the method signature and it makes the code more clear D. It allows the compiler to catch errors in the method signature and it makes the code more efficient (faster)

C. It allows the compiler to catch errors in the method signature and it makes the code more clear

Which statement does NOT describe polymorphism? A. It is the characteristic of being able to assign a different meaning or usage to something in different contexts B. It facilitates the design and implementation of extensible systems C. It allows you to assign superclass instances to subclass variables without casting D. It enables you to write programs that process objects that share the same superclass as if they're all objects of the superclass

C. It allows you to assign superclass instances to subclass variables without casting

5 methods in class Object can be overridden. Which are they? A. equalTo, finalize, hashCode, toString, wait B. equals, finalize, hashCode, toString, wait C. clone, equals, finalize, hashCode, toString D. clone, equalTo, finalize, hashCode, toString

C. clone, equals, finalize, hashCode, toString

Java provides an operator that allows you to check whether an instance can be safely casted. Identify this operator among the options below. A. typeof B. typecheck C. instanceof D. safecast

C. instanceof

Assume you have two classes: class Sailboat and class Vehicle.Class Sailboat is a subclass of class Vehicle.The toString method of class Vehicle provides information about the vehicle (e.g. size or speed). Now you need to override the toString method from class Sailboat. It should include all the information about the vehicle (e.g. size or speed) and in addition some information about the sail. To avoid code duplication, the toString method of the subclass (class Sailboat) should call the toString method of the superclass (class Vehicle). Which of the following options correctly calls the toString method of the superclass? A. toString.super() B. super(toString) C. super.toString() D. super().toString()

C. super.toString()

See question 4 https://slcc.instructure.com/courses/655149/quizzes/2221964?module_item_id=14558014

D. diagnose and heal

The methods of class Object can be called on arrays. True or False?

True

One of the most commonly used interfaces is the interface Comparable. It has a method that compares two elements. This allows us to establish an order between elements, which in turn enables us to sort elements of the given type. What is the name of this method? A. compareTo B. orderTo C. order D. compare

A. compareTo

See Question 5 https://slcc.instructure.com/courses/655149/quizzes/2221964?module_item_id=14558014

B. The method overridden in class Surgeon

Which of the following statements best describes inheritance? A. An instance of a subclass has-a superclass B. An instance of a superclass is-a subclass C. An instance of a superclass has-a subclass D. An instance of a subclass is-a superclass

D. An instance of a subclass is-a superclass

An association is often described as an is-a relationship. True or False?

False

Complete the sentences below: Abstract classes are [ Select ] ["always", "never", "sometimes"] used to instantiate objects. Concrete classes [ Select ] ["always", "never", "sometimes"] provide implementation for abstract methods they inherit. Abstract methods [ Select ] ["never", "always", "sometimes"] have a method body.

( never ) ( always ) ( never )

Three out of the four statements below describe reasons to use interfaces. However, one does not. Which of the following is NOT a reason to use interfaces? A. To allow objects of unrelated classes to be processed polymorphically B. To allow unrelated classes to share common implementation C. To make the code more flexible D. To use the same signature for the same functionality and thus make the code more clear and easier to read

B. To allow unrelated classes to share common implementation

Complete the sentence below: Both .............................. describe a whole-part relationship . A. association and aggregation B. aggregation and composition C. association and composition D. aggregation and correlation

B. aggregation and composition

Complete the sentence below to describe dynamic binding. All calls to overridden methods are resolved ................. . A. at compile time B. at runtime C. at random D. @Override

B. at runtime

Two of the Object methods have closely related contracts. Whenever one of these methods is overridden the other one needs to be overridden as well Which two methods are these? A. hashCode and getClass B. equals and hashCode C. equals and toString D. getClass and toString

B. equals and hashCode

Which keyword is used to indicate that a class implements an interface? A. super B. implements C. derives D. extends

B. implements

Complete the sentence below. The constructor of a subclass always need to call the constructor of the superclass However, it doesn't always have to be done explicitly. The constructor of the superclass can be called implicitly if ........................... A. the fields of the superclass can be initialized with the default values of their respective types B. the superclass has a default or no-argument constructor C. the fields of the superclass are never accessed D. if the superclass has exactly one constructor

B. the superclass has a default or no-argument constructor

Complete the sentence below: .................. describes a whole-part relationship where the life cycle of the part class depends on the whole class. A. Correlation B. Association C. Aggregation D. Composition

D. Composition

Which of the following can NOT be included in an interface declaration? A. Abstract methods B. Default methods C. Nested Types D. Instance fields

D. Instance fields

Assume you have a class called Human and it has a single constructor with one parameter of type String: the name. Assume you declare a class Friend. Class Friend is a subclass of class Human. In the constructor of class Friend, you call the constructor of the superclass explicitly. Which of the following is the correct call of the superclass constructor? A. super(); B. Human.super(friendName); C. Human(friendName); D. super(friendName);

D. super(friendName);

Complete the sentence: The first task of a subclass constructor is _______________ . A. to allocate memory on the heap B. to initialize the instance fields C. to call the constructor of class Object D. to call the constructor of its direct superclass

D. to call the constructor of its direct superclass

Assume you need to write a class Racedog. It should be a sub-class of class Dog. Racedogs take part in dog races and they are very fast. You want to override the method move of class Dog by printing the text it always printed (run) but you want to add " fast" (run fast) In order to do that your implementation of the overridden move method calls the move method of the super-class. Which of the following method calls of the super-class implementation is correct? A. super(); B. super(move); C. move(super); D. move.super(); E. super.move();

E. super.move();

Below are a number of statements but not all of them are true. Check ALL statements that correctly describe Java interfaces. a. Methods that are declared in a Java interface are assumed to be public and abstract unless specified otherwise. b. Java interface names should be camelCased. c. Java interfaces are reference types d. Java interfaces include no implementation

a. Methods that are declared in a Java interface are assumed to be public and abstract unless specified otherwise. c. Java interfaces are reference types

Below you find a number of statements. However, not all of those statements are true. Check ALL statements, that correctly describe traits and restrictions of superclasses and subclasses. a. Superclasses tend to be more general b. You can add additional class members to a subclass. c. One subclass can have multiple direct superclasses

a. Superclasses tend to be more general b. You can add additional class members to a subclass.

Abstract classes are always used in the context of inheritance. Select four (4) of the following five options to identify when to use abstract classes.term-43 a. When we need to model an "is-a" relationship between classes b. When we need to prevent the class from being instantiated itself. c. When we need to stop the inheritance hierarchy at the given class d. When we need to specify what is in common among subclasses e. When we need to include some implementation or fields

a. When we need to model an "is-a" relationship between classes b. When we need to prevent the class from being instantiated itself. d. When we need to specify what is in common among subclasses e. When we need to include some implementation or fields

Check ALL statements that are correct. a. If a class implements an interface, it can choose which of the interface methods should be implemented b. If a class implements an interface, it must implement all methods that are declared in the interface c. Interfaces do not specify how exactly methods should be implemented d. Interfaces form a contract

b. If a class implements an interface, it must implement all methods that are declared in the interface c. Interfaces do not specify how exactly methods should be implemented d. Interfaces form a contract

[ClassC]--------------[ClassD] Check ALL options that correctly describe the relationship between ClassC and ClassD. a. ClassC knows about ClassD or ClassD knows about ClassC b. unary association c. binary association d. ClassC knows about ClassD and ClassD knows about ClassC e. Neither ClassC nor ClassD know about each other

c. binary association d. ClassC knows about ClassD and ClassD knows about ClassC


संबंधित स्टडी सेट्स

Understanding Animal Biology: Unit 9

View Set

PED Final Exam Course point ?'s Unit 2

View Set

PBHLTH150B everything to know - human health and enviro in a changing world

View Set

Exam 1 - Ch 11, 12 (sepsis, CV surgery) (ch 16: DIC)

View Set

D'accord 3: Leçon 7: À la recherche du progrès

View Set

Advanced Security Practitioner 2

View Set

9th grade Bible unit 7 study guide

View Set