CompSci II Midterm study guide

Ace your homework & exams now with Quizwiz!

What characterizes a branch node and a leaf node in a scene graph?

A branch node is one that has child nodes and a leaf node is a node in a scene graph that has no children: that is, it is a node that does not contain other nodes

What is a class type or reference variable? Explain why a String variable is a reference variable.

A class type variable does not hold the actual data item that it is associated with, but holds the memory address of the data item it is associated with. If name is a String class variable, then name can hold the memory address of a String object. When a class type variable holds the address of an object, it is said that the variable references the object. For this reason, class type variables are commonly known as reference variables.

What is a functional interface?

A functional interface is an interface that has one abstract method.

Describe what a generic class or method is and list three characteristics of one

A generic class or method is one whose definition uses a placeholder for one or more of the types it works with. 1. The placeholder is really a type parameter. 2. For a generic class, the actual type argument is specified when an object of the generic class is being instantiated. 3. For a generic method, the compiler deduces the actual type argument from the type of data being passed to the method.

What is a lambda expression?

A lambda expression is a special type of expression you can use to create an object that implements a functional interface.

What is a methods signature?

A method signature consists of the method's name and the data types of the method's parameters, in the order that they appear. The return type is not part of the signature.

What is the difference between a protected class member and a private class member?

A protected member of a class may be directly accessed by methods of the same class or methods of a subclass. In addition, and this is how protected members are different from private members, protected members may be accessed by methods of any class that are in the same package as the protected member's class. A protected member is not quite private, because it may be accessed by some methods outside the class.

What is a scene graph?

A scene graph is a tree-like structure that is formed by the containment relation among nodes that make up the user interface in a JavaFX application.

Can you access non static variable in static context?

A static variable in Java belongs to its class and its value remains the same for all its instances. A static variable is initialized when the class is loaded by the JVM. If your code tries to access a non-static variable, without any instance, the compiler will complain, because those variables are not created yet and they are not associated with any instance.

Why and with what do you use a toggle group?

A toggle group ensures that among a group of radio buttons, only one may be selected at any given time.

Describe one thing you cannot do with a static method.

Access a non-static member.

What is aggregation? Give an example.

Aggregation occurs when an instance of a class is a field in another class. For example, suppose you need an object to represent a course that you are taking in college. You decide to create a Course class, which will hold the following information: ● The course name ● The instructor's last name, first name, and office number ● The textbook's title, author, and publisher In addition to the course name, the class will hold items related to the instructor and the textbook. You could put fields for each of these items in the Course class. However, a good design principle is to separate related items into their own classes. In this example, an Instructor class could be created to hold the instructor-related data and a TextBook class could be created to hold the textbook-related data. Instances of these classes could then be used as fields in the Course class.

Why do you get an error when you try to add an Image to a pane such as a VBox?

An Image is not a subclass of the Node class, and so it cannot be directly attached to a scene graph.

What is an abstract class?

An abstract class is not instantiated itself, but serves as a superclass for other classes. The abstract class represents the generic or abstract form of all the classes that inherit from it.

Discuss Abstract Classes and Abstract Methods

An abstract class is not instantiated, but other classes extend it. An abstract method has no body and must be overridden in a subclass. An abstract method is a method that appears in a superclass, but expects to be overridden in a subclass. An abstract method has only a header and no body. Here is the general format of an abstract method header: AccessSpecifier abstract ReturnType MethodName( ParameterList); Notice that the key word abstract appears in the header, and that the header ends with a semicolon. There is no body for the method. When an abstract method appears in a class, the method must be overridden in a subclass. If a subclass fails to override the method, an error will result. Abstract methods are used to ensure that a subclass implements the method. When a class contains an abstract method, you cannot create an instance of the class. Abstract methods are commonly used in abstract classes. An abstract class is not instantiated itself, but serves as a superclass for other classes. The abstract class represents the generic or abstract form of all the classes that inherit from it.

What is an abstract method?

An abstract method is a method that appears in a superclass, but expects to be overridden in a subclass. An abstract method has only a header and no body.

When you instantiate an anonymous inner class, the class must do one of two things. What are they?

An anonymous inner class must implement an interface, or extend another class.

Discuss Interfaces

An interface specifies behavior for a class. In the previous section you learned that an abstract class cannot be instantiated, but is intended to serve as a superclass. You also learned that an abstract method has no body and must be overridden in a subclass. An interface is similar to an abstract class that has all abstract methods. It cannot be instantiated, and all of the methods listed in an interface must be written elsewhere. The purpose of an interface is to specify behavior for a class. An interface looks similar to a class, except the key word interface is used instead of the key word class, and the methods that are specified in an interface have no bodies, only headers that are terminated by semicolons

What is a ragged array? How do you create a ragged array? Give an example.

Because the rows in a two-dimensional array are also arrays, each row can have its own length. When the rows of a two-dimensional array are of different lengths, the array is known as a ragged array. You create a ragged array by first creating a two-dimensional array with a specific number of rows, but no columns.

Assuming that array1 and array2 are both array reference variables, why is it not possible to assign the contents of the array referenced by array2 to the array referenced by array1 with the following statement? array1 = array2;

Because this statement merely makes array1 reference the same array that array2 references. Both variables will reference the same array. To copy the contents of array2 to array1, the contents of array2's individual elements will have to be assigned to the elements of array1.

Why is the bubble sort inefficient for large arrays?

Bubble sort moves through arrays one element at a time.

Why is it important to avoid null References? Give an example of how to do this.

By default, a reference variable that is an instance field is initialized to the value null. This indicates that the variable does not reference an object. Because a null reference variable does not reference an object, you cannot use it to perform an operation that would require the existence of an object. If you attempt to perform an operation with a null reference variable the program will terminate. If you do not have a programmer-defined constructor, the default constructor will initialize any strings or objects to null. One way to prevent programs from crashing is to test for null in the accessor methods.

What is the superclass and what is the subclass in the following line? public class Pet extends Dog

Dog is the superclass and Pet is the subclass.

How can you determine the minimum and maximum values that may be stored in a variable of a given data type?

Each of the numeric wrapper classes has final static fields named MAX_VALUE and MIN_VALUE. These fields hold the maximum and minimum values for the data type.

Do generic types exist at the bytecode level?

Generic types do not exist at the bytecode level.

If a sequential search method is searching for a value that is stored in the last element of a 10,000-element array, how many elements will the search code have to read to locate the value?

It will have to read all 10,000 elements to find the value stored in the last element.

What is the difference between an Interface and an Abstract class?

Java provides and supports the creation both of abstract classes and interfaces. Both implementations share some common characteristics, but they differ in the following features: 1. All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and nonabstract methods. 2. A class may implement a number of Interfaces, but can extend only one abstract class. 3. In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract. 4. Abstract classes can implement interfaces without even providing the implementation of interface methods. 5. Variables declared in a Java interface is by default final. An abstract class may contain non-final variables. 6. Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public. 7. An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.

What is the difference between margin and padding?

Margin is the spacing around the outside of the component, while padding is the spacing just inside the border of the component.

What is method overriding?

Method overriding means having a different implementation of the same method in an inherited class. These two methods would have the same signature, but different implementations. One of these would exist in the base class and another in the derived class. These cannot exist in the same class.

A binary search function is searching for a value that is stored in the middle element of an array. How many array elements have to be examined before finding the value?

One element will be examined.

Look at the following method header: public < T extends Comparable<T> > T least( T arg1, T arg2) What constraint is placed on the type parameter T?

Only types that implement the Comparable interface may be passed to it.

What is the difference between overriding a superclass method and overloading a superclass method?

Overloading is when a method has the same name as one or more other methods, but a different parameter list. Although overloaded methods have the same name, they have different signatures. When a method overrides another method, however, they both have the same signature.

What is method overloading?

Overloading occurs when two or more methods in a class may have the same name as long as their parameter lists are different. This also applies to constructors.

Explain how recursion works

Recursion works like this: A base case is established. If matched, the method solves it and returns. If the base case cannot be solved now: the method reduces it to a smaller problem (recursive case) and calls itself to solve the smaller problem. By reducing the problem with each recursive call, the base case will eventually be reached and the recursion will stop.

What is the maximum number of comparisons that a binary search function will make when searching for a value in a 1,000-element array?

Ten comparisons is the maximum.

What is the "this" keyword?

The key word "this" is the name of a reference variable that an object can use to refer to itself. It is available to all non-static methods.

Why should you use StringBuilder objects instead of String objects in a program that makes lots of changes to Strings?

This will improve the program's efficiency by reducing the number of String objects that must be created and then removed by the garbage collector.

What is binding?

The process of matching a method call with the correct method is known as binding. The compiler uses the method signature to determine which version of the overloaded method to bind the call to.

What does the "static" keyword mean? Can you override private or static method in Java?

The static keyword denotes that a member variable or method can be accessed, without requiring an instantiation of the class to which it belongs. A user cannot override static methods in Java, because method overriding is based upon dynamic binding at runtime and static methods are statically binded at compile time. A static method is not associated with any instance of a class so the concept is not applicable.

Which constructor is called first, that of the subclass or the superclass?

The superclass constructor is called first.

Why are static methods useful in creating utility classes?

They can be called directly from the class, as needed. They can be used to create utility classes that perform operations on data, but have no need to collect and store data.

char

This data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

float

This data type is a single-precision 32-bit IEEE 754 floating point. As with the recommendations for byte and short, use a this (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

When a generic class is used in a program, when does type checking take place?

When a generic class is used in a program type checking takes place at compile time.

What is an "is-a" relationship?

When an "is a" relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special.

What is Erasure?

When processing generic code, Erasure is when the compiler replaces all generic types with the Object type, or with the constrained upper bound for generic type.

short

a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use this to save memory in large arrays, in situations where the memory savings actually matters.

int

a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use this data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1

long

a 64-bit two's complement integer. This has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use this data type to represent an unsigned 64-bit number, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int.

double

a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

byte

an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). This data type can be useful for saving memory in large arrays, where the memory savings actually matters.

boolean

data type that has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

In an average case involving an array of n elements, how many array elements does a sequential search algorithm have to access to locate a specific value?

n/2 times


Related study sets

(2 Prelim) 3 The Human Person In The Environment

View Set

Cell and Molec Bio Chapter 6, 7, 8

View Set

Chapter Exam 2 - Policy Provisions, Options and Riders

View Set

CHAPTER 1, 2, 4, 5 PRE LECTURE QUIZZES

View Set

Chapter 05 - Cultural Implications evolve questions

View Set

Binomial Distribution Assignment

View Set