INSY 4305 Exam 2

Ace your homework & exams now with Quizwiz!

If the superclass contains only abstract method declarations, the superclass is used for ________. a. implementation inheritance. b. interface inheritance. c. Both. d. Neither.

b. interface inheritance.

All methods specified by an interface are __________. a. private b. public c. protected d. static

b. public

extends vs implements

In Java, we can inherit the fields and methods of a class by extending it using extends keyword. To implement an interface, class must use implements keyword.

When a method is declared with the __________ modifier, it cannot be overridden in a subclass. a. final b. super c. void d. public

a. final

Given the following declaration: enum Tree ( OAK, MAPLE, PINE ) What is the ordinal value of the MAPLE enum constant? a. 0 b. 1 c. 2 d. 3

b. 1

What will be the value of x[8] after the following code is executed? final int SUB = 12; int[] x = new int[SUB]; int y = 100; for(int i = 0; i < SUB; i++) { x[i] = y; y += 10; } a. 170 b. 180 c. 190 d. 200

b. 180

In the following statement, which is the superclass? public class ClassA extends ClassB implements ClassC a. ClassA b. ClassB c. ClassC d. cannot tell

b. ClassB

Given the following code, which statement is true? public class ClassB implements ClassA{ } a. ClassA must override each method in ClassB. b. ClassB must override each method in ClassA. c. ClassB inherits from ClassA. d. ClassA inherits from ClassB.

b. ClassB must override each method in ClassA.

Every class in Java, except ________, extends an existing class. a. Integer. b. Object. c. String. d. Class.

b. Object.

In __________, inheritance is shown with a line that has an open arrowhead at one end that points to the superclass. a. pseudocode b. a UML diagram c. CRC card d. a hierarchy chart

b. a UML diagram

The ___________ method is used to insert an item into an ArrayList. a. insert b. add c. store d. putItem

b. add

Binary search algorithm

*Array must be sorted to begin* Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise, narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

concrete class

A concrete class is a class that we can create an instance of, using the new keyword. In other words, it's a full implementation of its blueprint. A concrete class is complete.

Jagged array

A jagged array is an array of arrays such that member arrays can be of different sizes Ex) W e can create a 2-D array but with a variable number of columns in each row. int x[][] = new int[][] { new int[] {1}, new int[] {1, 2}, new int[] {1, 2, 3} }; or int x[][] = { new int[] {1}, new int[] {1, 2}, new int[] {1, 2, 3} };

Array subscripts

A subscript is an integer value between [ and ] that represents the index of the element you want to get to. Ex) x[4] = 100; The "4" in the above code is the subscript.

abstract class

Abstract class is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Two-dimensional array

An array of arrays. Also called matrix. Ex) int x[2][3]; Creates an empty matrix of 2 rows 3 columns. int x[][] = { {1, 2, 3}, {4, 5, 6} }; Creates an array of 2 rows 3 columns with values filled in. int [0][2] will return "3."

enum

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). Ex) enum Level { LOW, MEDIUM, HIGH } To access the enum "Level," you use "." Level x = Level.MEDIUM;

Interface vs. Class

Class - describes attributes and behaviors of objects, may contain abstract or concrete methods, and members can be public private protected or default Interface - contains behavior that class implements, contains only abstract methods, members are public by default

Declaring and initializing array

Ex) int x[4]; Creates an empty array of length 4 ints. Ex) int x[] = {1,2,3,4}; Creates an array of 4 ints filled with values "1," "2," "3," and "4."

Passing array to method

Ex) public void a(int []) Method called "a" that takes an array of ints as a parameter. Ex) public void a(int[][]) Method called "a" that takes a 2d array of ints as a parameter.

Overloading vs. Overriding

Overloading - when two or more methods in the same class have the same name but different parameters Overriding - when the method name and parameters are the same in the superclass and the child class

polymorphism

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

static key word in Class

The keyword static can be applied to variables, methods, blocks and nested class. In the Java programming language, the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type. This means that only one instance of that static member is created which is shared across all instances of the class.

private, protected, public, and package

The keywords allow access to the following: public - class, package, subclass, world protected - class package, subclass (same package), subclass (different package) (no modifier) - class, package, subclass (same package) private - class

Number of elements in array

The number of elements can be accessed by using the "length" method Ex) x.length

super

The super keyword refers to superclass (parent) objects.

this

The this keyword refers to the current object in a method or constructor.

toString() method

The toString() method returns the String representation of an object.

Using for loop with array and ArrayList

To iterate through array using for loop: for(int i = 0; i < array.length; i++) To iterate through ArrayList: for(int i = 0; i < arrayList.size(); i++)

__________ is the term for the relationship created by object aggregation. a. "Has a" b. Inner class c. "Is a" d. One-to-many

a. "Has a"

What type of relationship exists between two objects when one object is a specialized version of another object? a. "is a" b. "contains a" c. "has a" d. "consists of"

a. "is a"

If the following is from the method section of a UML diagram, which of the statements below is true? + equals(object2:Stock) : Boolean a. This is a public method that accepts a Stock object as its argument and returns a boolean value. b. This is a public method that returns a reference to a String object. c. This is a private method that receives two objects from the Stock class and returns a boolean value. d. This is a private method that returns a boolean value.

a. This is a public method that accepts a Stock object as its argument and returns a boolean value.

A jagged array is __________. a. a two-dimensional array where the rows have different numbers of columns b. a one-dimensional array for which the number of elements is unknown c. a two-dimensional array for which the number of rows is unknown d. a partially initialized two-dimensional array of ranged values

a. a two-dimensional array where the rows have different numbers of columns

Every object in Java knows its own class and can access this information through method . a. getClass. b. getInformation. c. objectClass. d. objectInformation.

a. getClass.

The __________ method removes an item from an ArrayList at a specific index. a. remove b. pop c. deleteAt d. clear

a. remove

In order to do a binary search on an array __________. a. the array must first be sorted b. you must first do a sequential search to be sure the element you are looking for is there c. the values of the array must be numeric d. no requirements are necessary

a. the array must first be sorted

ArrayList methods

add(Object o) Adds "o" to end of list. add(int index, Object element) Adds "o" at index "index." clear() Clears list. clone() Returns shallow copy of list. get(int index) Returns object at "index" from list. remove(int index) Removes object at "index." set(int index, Object o) Replaces object at "index" with "o." size() Returns length of list. toArray() Returns an array containing all elements from list in correct order.

Assigning a subclass reference to a superclass variable is safe ________. a. because the subclass object has an object of its superclass. b. because the subclass object is an object of its superclass. c. only when the superclass is abstract. d. only when the superclass is concrete.

b. because the subclass object is an object of its superclass.

private fields of a superclass can be accessed in a subclass a. by calling private methods declared in the superclass. b. by calling public or protected methods declared in the superclass. c. directly. d. All of the above.

b. by calling public or protected methods declared in the superclass.

Which of the following is a correct method header for receiving a two-dimensional array as an argument? a. public static void passMyArray(int[1, 2]) b. public static void passMyArray(int[][]) c. public static void passMyArray[1][2]) d. public static void passMyArray(int[],int[] myArray)

b. public static void passMyArray(int[][])

When a reference variable is passed as an argument to a method __________. a. a copy of the variable's value is passed into the method's parameter b. the method has access to the object that the variable references c. the method becomes a static method d. the program terminates

b. the method has access to the object that the variable references

In an inheritance relationship __________. a. the subclass constructor always executes before the superclass constructor b. the superclass constructor always executes before the subclass constructor c. the constructor with the lowest overhead always executes first regardless of inheritance in subclasses d. the unified constructor always executes first regardless of inheritance

b. the superclass constructor always executes before the subclass constructor

The only limitation that static methods have is __________. a. they must be declared outside of the class b. they cannot refer to nonstatic members of the class c. they can only be called from static members of the class d. they can refer only to nonstatic members of the class

b. they cannot refer to nonstatic members of the class

What will be the value of x[8] after the following code is executed? final int SUB = 12; int[] x = new int[SUB]; int y = 20; for(int i = 0; i < SUB; i++) { x[i] = y; y += 5; } a. 50 b. 55 c. 60 d. 65

c. 60

What does <String> specify in the following statement? ArrayList<String> nameList = new ArrayList<String>(); a. It specifies that String objects may not be stored in the ArrayList object. b. It specifies that everything stored in the ArrayList object will be converted to a String object. c. It specifies that only String objects may be stored in the ArrayList object. d. It specifies that the ArrayList will be converted to a String array.

c. It specifies that only String objects may be stored in the ArrayList object.

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

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

Given the following two-dimensional array declaration, which statement is true? int[][] numbers = new int[6][9]; a. The numbers array has 54 rows. b. The numbers array has 15 rows. c. The numbers array has 6 rows and 9 columns. d. The numbers array has 6 columns and 9 rows.

c. The numbers array has 6 rows and 9 columns.

It is common practice to use a __________ variable as a size declarator. a. static b. reference c. final d. boolean

c. final

If you have defined a class, SavingsAccount, with a public static data member named numberOfAccounts, and created a SavingsAccount object referenced by the variable account20, which of the following will assign numberOfAccounts to numAccounts? a. numAccounts = account20.numAccounts; b. numAccounts = numOfAccounts; c. numAccounts = SavingsAccount.numberOfAccounts; d. numAccounts = account20;

c. numAccounts = SavingsAccount.numberOfAccounts;

Which of the following statements correctly specifies two interfaces? a. public class ClassA implements [Interface1, Interface2] b. public class ClassA implements (Interface1, Interface2) c. public class ClassA implements Interface1, Interface2 d. public class ClassA implements Interface1 | Interface2

c. public class ClassA implements Interface1, Interface2

The __________ key word is used to call a superclass constructor explicitly. a. goto b. this c. super d. extends

c. super

What would be the result after the following code is executed? final int SIZE = 25; int[] array1 = new int[SIZE]; ... // Code that will put values in array1 int value = 0; for (int a = 0; a < array1.length; a++) { value += array1[a]; } a. value contains the highest value in array1. b. value contains the lowest value in array1. c. value contains the sum of all the values in array1. d. This code would cause the program to crash.

c. value contains the sum of all the values in array1.

When you make a copy of the aggregate object and of the objects that it references, __________. a. you are performing a shallow copy b. you are performing a nested copy c. you are performing a deep copy d. a compiler error will occur

c. you are performing a deep copy

If a subclass constructor does not explicitly call a superclass constructor, __________. a. the superclass's 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 immediately after the code in the subclass's constructor executes c. it must include the code necessary to initialize the superclass fields d. Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes

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

In 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. Line 4 b. Line 5 c. Line 10 d. Line 11

d. Line 11

When a subclass constructor calls its superclass constructor, what happens if the superclass's constructor does not assign a value to an instance variable? a. A syntax error occurs. b. A compile-time error occurs. c. A run-time error occurs. d. The program compiles and runs because the instance variables are initialized to their default values.

d. The program compiles and runs because the instance variables are initialized to their default values.

A class becomes abstract when you place the __________ key word in the class definition. a. super b. extends c. final d. abstract

d. abstract

Interfaces can have ________ methods. a. 0 b. 1 c. 2 d. any number of

d. any number of

Non-abstract classes are called ________. a. real classes. b. instance classes. c. implementable classes. d. concrete classes.

d. concrete classes.

If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array? a. for (int row = 1; row < numbers.length; row++) { for (int col = 1; col < numbers.length; col++) total += numbers[row][col]; } b. for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers.length; col++) total += numbers[row][col]; } c. for (int row = 0; row < numbers[row].length; row++) { for (int col = 0; col < numbers.length; col++) total += numbers[row][col]; } d. for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; }

d. for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; }

The ArrayList class is in the __________ package. a. java.arraylist b. java.lang c. java.array d. java.util

d. java.util

Given that String[] str has been initialized, to get a copy of str[0] with all the characters converted to uppercase, you would use the __________ statement. a. str.uppercase(); b. str[0].upperCase(); c. str.toUpperCase(); d. str[0].toUpperCase();

d. str[0].toUpperCase();

How to compare two objects?

equals() Ex) int x = 4; int y = 5; x.equals(y) //^Returns false

Subclass

subclass (child) - the class that inherits from another class

superclass

superclass (parent) - the class being inherited from


Related study sets

Biology II CH 25 active reading guide

View Set

Honan-Chapter 18: Nursing Management: Patients with Vascular Disorders and Problems of Peripheral Circulation

View Set

Relias: Data Collection, Behavior, & Decisions

View Set