comp sci Unit 6 plus review questions (REVIEW THIS ONE BEFORE TESTS)

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

What is Integer.MIN_VALUE?

-2^31

how to return the value of an Integer?

.intValue()

What to do to get Tree.getTemperature(); to run?

getTemperature() must be declared static.

public class A; public class B extends A; public class C extends A; public class D extends B; public class E extends B; Is B and child of E?

No.

Overriding vs Overloading

Both create methods with the same name. Overloading is in the same class and is chosen by their signatures (parameters). Overriding is when you define a method in the parent class and you write the same one with possibly same signature in child class.

why analyze algorithms?

Predict performance Make decisions

The two most common methods from Object:

equals and toString

selection sort:

find smallest value, put it as first element, start from 2nd element and start searching, put that as 2nd element, etc.

search algorithm

finding an element in an array

keyword to use interfaces:

implements

Which of the following is not a method of java.util.ArrayList? get remove insert contains add

insert

How are primitives essentially different from classes and thus sometimes necessitate wrappers?

int x = 9; Integer n1 = new Integer(45); whereas java labels a box of memory called x and stores binary value for 9, java has a reference to some point in memory where all your Integer(45) values are stored, and this sails better with ArrayLists

Classes use ______ to define their behavior. methods constructors references variables parameters

methods, I think

when can you use a constructor of a different class?

when there's a connection (in terms of extending); Clothing x = new Women();

What's the catch to using abstract?

You can't instantiate an object of that abstract class.

What is output by the following code? System.out.print(21.0/5); What is output?

4.2

Consider the following recursive method: public static int recur(int x) { if (x >= 0) return x + recur(x - 1); return 0; } What is returned by the method call recur(9)?

45

Consider the following methods: public static double average(int nums[]) { int sum =0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; } return (1.0 * sum) / nums.length; } //average public static int[] mystery(String a[]) { int temp [] = new int[a.length]; for (int i = 0; i < a.length; i++) { temp[i] = a[i].length(); } return temp; } //mystery What is output by running the following? String spelling[] = {"against", "forms", "belief", "government", "democratic", "movement", "understanding", "single", "followed", "scenario"}; System.out.println( average( mystery(spelling)));

8.1

the limitation to keyword 'extends'

A class can only extend one class but we can have several classes extending each other in a chain.

Which of the following is not true about a constructor? A) It must have a void or return type. B) It should be declared public. C) It must have the same name as the class. D) It must not have a void or return type. E) It initializes the instance variables of the class.

A.

Write a statement that creates an ArrayList called bookList of Book objects:

ArrayList <Book> bookList = new ArrayList <Book>();

from which class do all classes in java inherit?

At the top, ALL classes inherit from Object.

What does Comparable necessitate?

a defined compareTo method

Adder a = new Adder(200); Adder b = new Adder(200); Adder c = a; a.increase(200); System.out.println(a.getValue() + " " + b.getValue() + " " + c.getValue()); Why is the output "400 200 400"?

Because when you do Adder c = a, you're setting c equal to a reference in memory equal to a. When you changed the value of a, you changed a variable in some memory reference that c also refers to.

What method is used to return a private variable?

accessor

4 traits of algorithms

Patterns that we use to solve problems Have an order Have clear instructions Stop in a finite amount

Super Class

Class that contains the code with both state and behavior that can be extended by its subclasses.

Sub Class

Class that extends from and inherits code from a super class. Has access to both the state and behavior.

what interface does Integer implement?

Comparable

Consider the complete class definition below: public class Die { public static void rollIt() { /* Missing Code */ } } Which of the following is the proper way to call the function rollIt() from another class? A) rollIt(); B)None of the above C) Die.rollIt(); D) Die d = new Die(); d.rollIt(); E) Die d = new Die(); rollIt(d);

D I think

Consider the following method checking whether a student has passing grades: public boolean isPassing(int finalExam, int cumulativeAverage) { /* Missing Code */ } A student can pass a class if one of the following is true: The final exam is at least 98 The cumulative average is over 60 Which of the following correctly replaces /* Missing Code */ so that the method works as intended? I. if ((finalExam >= 98) || (cumulativeAverage > 60)) return true; return false; II. boolean pass = false; if (finalExam >= 98) pass = true; if (cumulativeAverage > 60) pass = true; return pass; III. if (finalExam >= 98 && cumulativeAverage >= 60) return true; return false; A)II only B)III only C)I only D)I and II only E)II and III only

D, I and II only

Inheritance

Defines relationship between objects that share characteristics - the process where one class acquires the properties (methods and fields) of another.

Two most important wrapper classes

Double for double, Integer for int

Has-A relationship

Has-A means an instance of one class "has a" reference to an instance of another class or another instance of same class. There is no specific keyword to implement Has-A relationship but mostly we are depended upon "new" keyword.

Method overriding

Methods from the superclass can be overridden by the subclass. Example: toString() method.

Consider: public class MyClass { private int myNumber; public MyClass() { myNumber = 0; } public MyClass(int x) { myNumber = x; } } class MyOtherClass extends MyClass { public MyOtherClass() { super(0); } } Does MyOtherClass e = new MyOtherClass(5); compile?

No because no constructor accepts a paramter of 5.

Consider: public class A; public class B extends A; public class C extends A; public class D extends B; public class E extends B; Is D a child of C?

No.

Super

Super calls methods/constructors from the parent class. If the parent class does not have the method, Java looks at the next parent class up the ladder.

Wrapper classes

Wraps a primitive in a class type. There is one for each of the primitive types.

consider: public class A; public class B extends A; public class C extends A; public class D extends B; public class E extends B; is E a child of A?

Yes.

You have written a class called Frog and have not written a toString method. Frog f = new Frog(); System.out.println(f); What happens?

The toString method in Object is called.

Interface

The ultimate to-do list - defines common features of behavior that could be used by any class or program that uses those behaviors. Contains the methods, no instance variables or code, and all methods are public abstract methods.

what if a child class of an abstract class doesn't define all the abstract methods?

Then that child class itself is abstract

Is-A relationship

This refers to inheritance or implementation. Expressed using keyword "extends".

What does class Object essentially only have access to?

data type and memory address

how to make interface:

public interface Polygon { abstract int getPerimeter(); abstract int getArea(); }

public static int[] p() { // ... } what can be returned? return new int[]{1, 2, 3}; return {1, 2, 3}; return true return int[]{1, 2, 3}; return 1;

return new int[]{1, 2, 3};

the marks of a good algorithm:

speedy easier to code less memory use

Wrapper classes:

wraps a primitive in a class type

in a constructor, how do you use super()?

You place the super() constructor as very first line of the cosntructor.

What keyword is used in Java to create a parent/child relationship between two classes?

extends

True or false: A constructor must have a return statement.

false

restriction on overriding

must have same return type as method in parent class

sort algorithms

order the elements in specific order

What is Integer.MAX_VALUE?

that is the maximum value of all integers in Java ever, which is 2^31-1.

Extends

the keyword used to inherit the properties of a class. It links the child class to its parent.

public class NewPoint extends Point { private int x,y; public NewPoint() { x=0; y=0; super(x,y); } public void toOrigin() { x=0; y=0; } } What's wrong with this?

the super is the last line when it should be the first line.

the use of abstract

to modify a parent class so that you list certain abstract methods with no code inside so that child classes have to define them

Consider: public class MyClass { private int myNumber; public MyClass() { myNumber = 0; } public MyClass(int x) { myNumber = x; } } class MyOtherClass extends MyClass { public MyOtherClass() { super(0); } } Does MyClass b = new MyClass(); compile?

yes

Consider: public class MyClass { private int myNumber; public MyClass() { myNumber = 0; } public MyClass(int x) { myNumber = x; } } class MyOtherClass extends MyClass { public MyOtherClass() { super(0); } } Does MyOtherClass d = new MyOtherClass(); compile?

yes.

what is insertion sort:

you start with second element, compare it with the first, put it front or back. Go to third element, compared with its former two, place it accordingly.

What is the List interface?

A way for objects to be stored using standard behavior

List interface

A way for objects to be stored using standard behavior (behaviors: add, remove, etc.)

Which of the following statements must be true in order for Square to access the constructor in Polygon? (Square extends Quadrilateral Quadrilateral extends Polygon) A) Square must first access Quadrilateral's constructor, and Quadrilateral's constructor must call Polygon's constructor. B) Square has access to all public methods and variables in Polygon and will never need to directly call a constructor. C) The call to Polygon's constructor must be the first line in Square's constructor. D) The call to Polygon's constructor must be the last line in Square's constructor. E) Square's constructor must be declared private.

A. You can't just assume B, C doesn't even work, and D makes no sense.

You have written a program to create a grocery list. As each item is placed into your basket you call method called removeItem and it should remove the item from your list. Which of the statements about the code below are true? public static void removeItem(ArrayList<String> li, String remove) { for (String s: li) { if (s.equals(remove)) { li.remove(s); } } } A) No changes are made to the ArrayList because the if (s.equals(remove)) is never true. B)Nothing, changes made to object data types are not preserved after method calls. C) All elements in the ArrayList are removed. D) The list will have all of the instances of the word passed in as a parameter removed. E) An exception will be thrown.

E. There will most likely be an error because the loop goes past the end of the ArrayList.

Which of the following should be true of a superclass? A. A superclass should contain methods that will be used only by the superclass. B. Superclass can contain methods that will be used by all subclasses. C. Superclass data should be made public so the subclasses can access it.

The correct answer is B. A is wrong simply because the whole point of a class being a superclass is that it has possible methods usable by subclasses, and C is wrong because both public and protected

True or false: A subclass can call all of its parents' public methods.

True.

How does super() manifest itself in child constructors?

When you run a constructor for a child class, it calls the constructor, and it runs an inherent super() if applicable within the constructor, and runs the parent constructor, then the child constructor.

The point of inheritance:

With the use of inheritance, the information is made manageable in a hierarchical order.

Consider: public class MyClass { private int myNumber; public MyClass() { myNumber = 0; } public MyClass(int x) { myNumber = x; } } class MyOtherClass extends MyClass { public MyOtherClass() { super(0); } } Does MyClass c = new MyOtherClass(); compile?

Yes.

Which of the following is true about overloaded methods? a. None of the items listed. b. Java cannot use a method's return type to tell two overloaded methods apart. c. Java cannot use a method's parameters to tell two overloaded methods apart. d. You can only overload methods that have parameters. All overloaded methods must have different names.

b. Apparently Java is too dumb enough to distinguish return types (I mean even if it's between int and float types, shouldn't this have been addressed?)

how to instantiate compareTo:

public class Book implements Comparable { //define compareTo method here public int compareTo (Object b) { Book temp = (Book) b; if (author.compareTo (temp.author) == 0) { return title.compareTo (temp.title); } } }


Ensembles d'études connexes

Psychology of death and loss (456) Chapter 2 quiz

View Set

Chemistry but it's getting hot in here

View Set

Part 2 Quiz: Condominium Ownership

View Set

PEDIATRIC SUCCESS GASTROINTESTINAL DISORDERS CHAPTER 8

View Set

COMM 169 Quiz 3 (stem questions)

View Set

Princeton Review MCAT Demo Test Bio/Biochem

View Set