INSY 4305 Exam 2

Ace your homework & exams now with Quizwiz!

False. A class that is derived from another class is called a SUBCLASS. It is also called a derived class, extended class, or child class

(T/F) A class that is derived from another class is called a superclass

False. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

(T/F) A subclass inherits all the members (fields, methods, and nested classes, constructor) from its superclass.

True

(T/F) In a subclass, a call to the superclass constructor can only be written in the subclass constructor.

True. It is also called a base class or a parent class.

(T/F) The class from which the subclass is derived is called a superclass

True

(T/F) The ordinal() method returns the position of an enum constant

c. It specifies that only String objects may be stored in the ArrayList object. Format: ArrayList<Type> arrayList= new ArrayList<>();

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

b). Index of 13: 1 Index of 50: -1 if index is out of the range. Note: If the specified element doesn't exist in the list, the indexOf() method returns -1.

What does this segment of code return? // insert element to the arraylist numbers.add(22); numbers.add(13); // find the position of 13 int position1 = numbers.indexOf(13); System.out.println("Index of 13: " + position1); // find the position of 50 int position2 = numbers.indexOf(50); System.out.println("Index of 50: " + position2); a). Index of 13: 1 Index of 50: 0 b). Index of 13: 1 Index of 50: -1 c). Index of 13: 1 Index of 50: 1 d). None of the above

c. super baseClassConstructor() { super(someParams); }

What key word must be used with the explicit call to the superclass's default constructor a. sub b. access c. super d. extends

c. clear

What method removes all the elements in an arrayList? a. removeAll b. delete c. clear d. remove

b). get returns the element present in the specified position. > indexOf returns the POSITION

What method returns the element present in specified position in an arraylist? a). indexOf b). get c). contains d). index

c. "has a" "has a" is also known as Composition. - Unidirectional - Both are independent of es

Whenever an instance of one class is used in another class, it is called _____ relationship. a . "is a" b. "contains a" c. "has a" d. "consists of"

c. The enum can be overloaded and specify any number of parameters.

Which is not a restriction of enum declaration that declare an enum class? a. enum constants are implicitly final. b. enum constants are implicitly static. c. enum constructor cannot be overloaded. d. Any attempt to create an object of an enum type with operator "new" results in a compilation error.

a). trimToSize The trimToSize() method of ArrayList in Java trims the capacity of an ArrayList instance to be the list's current size. This method is used to trim an ArrayList instance to the number of elements it contains.

Which method would reduce the capacity of the arraylist to the size of the number of elements? //add element to ArrayList languages.add("Java"); languages.add("JavaScript"); System.out.println("ArrayList: " + languages); // change capacity languages.________(); a). trimToSize() b). resize c). trim() d). remove()

a). System.out.println("The size of the array list is: " + numbers.size()); size() returns the number of elements in an arraylist. > Good to use with loops as the size of arraylist as it is a dynamic array

Which of these code segments would return the number of elements in the arraylist Numbers? a). System.out.println("The size of the array list is: " + numbers.size()); b). System.out.println("The size of the array list is: " + numbers.length()); c). System.out.println("The size of the array list is: " + numbers.index()); d). System.out.println("The size of the array list is: " + numbers.count());

e. All are terms for the relationship created by object association

__________ is NOT a term for the relationship created by object association. a. Many-to-one b. Many-to-many c. One-to-one d. One-to-many e. All are terms for the relationship created by object association

c. "Part of" Composition is a restricted form of Aggregation in which two entities are highly dependent on each other. > In composition, both entities are dependent on each other. > When there is a composition between two entities, the composed object cannot exist without the other entity.

__________ is the term for the relationship created by object composition. a. "Has a" b. "Is a" c. "Part of" d. One-to-many

False. All enum constant is always public static final by default.

(T/F) An enum constant is always "private static final" by default.

a). indexOf returns the position of the specified element from the arraylist Note: If the specified element doesn't exist in the list, the indexOf() method returns -1.

What method returns the position of the specified position in an arraylist? a). indexOf b). get c). postion d). index

b. 1

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

java.util.ArrayList

What class needs to be imported for an Arraylist to be used?

b. public static void passMyArray(int[][]) Format for method header: - modifier(s) returnType methodName fullParameter( dataType parameterName) - E.g. public double maxTime (int a, int b, String c) - E.g. public static void passMyArray(int[][])

term-0Which 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. ClassB must override each method in ClassA.

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.

d. java.util

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

a. remove Overloaded. The remove() method removes the FIRST occurrence of the specified value or the element at the specified index.

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

b. add Overloaded to add an element to the END of the ArrayList or at a specific index in the ArrayList.

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

a. two-dimensional array where the rows have different numbers of columns A ragged array is an array of arrays (2D array) with a variable number of columns in each row. Think non-uniform array where one row has 5 columns and another row has 3 columns. Also know as "jagged array"

A ragged array is __________. 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

b. public

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

a . "is a" Whenever one class inherits another class, it is called an IS-A relationship. - The use of the "extends" keyword - Unidirectional: we can say that a bulb is a device, but vice versa; a device is a bulb is not possible since all the devices are not bulbs.

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. "Has a"

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

c). 4, the number of columns (second dimension)

Given the following two-dimensional array declaration, what is the value of "rating[0].length" ? int[][] rating = new int[3][4]; a). 3, the number of columns (first dimension) b). 3, the number of rows (second dimension) c). 4, the number of columns (second dimension) d). 4, the number of rows (first dimension) e). None of the above

d. str[0].toUpperCase(); The toUpperCase() method converts a string to upper case letters. The syntax is: specificString.toUpperCase() Note: The toLowerCase() method converts a string to lower case letters.

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.toUpperCase(); c. str[0].upperCase(); d. str[0].toUpperCase();

b). 3, the number of rows (first dimension)

Given the following two-dimensional array declaration, what is the value of "rating.length"? int[][] rating = new int[3][4]; a). 3, the number of columns (second dimension) b). 3, the number of rows (first dimension) c). 4, the number of columns (first dimension) d). 4, the number of rows (second dimension) e). None of the above

c. The numbers array has 6 rows and 9 columns. Format of a 2D array: dataType [][] arrayName = new dataType [rows][columns]

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.

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

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). ArrayIndexOutOfBoundsException

If an array element does not exists, the Java runtimesystem will give you what error? a). InterruuptedException b). IOException c). IndexOutOfBoundsException d). ArrayIndexOutOfBoundsException e). None of the above

d. for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; } The code would go: total = total + numbers[0][0] total = total + numbers[0][1] total = total + numbers[0][2] total = total + numbers[1][0] total = total + numbers[1][1] total = total + numbers[1][2]

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]; }

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

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.

c. numAccounts = SavingsAccount.numberOfAccounts; numberOfAccounts is an object created by the SavingsAccount class. Static data: is data associated with an instance. To refer to the static (class) variable, the format is : className.staticVarName Example: ParkingLot.LOTSIZE

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;

b. the superclass constructor always executes before the subclass constructor A superclass must be constructed before a subclass could be constructed too, otherwise some fields that should be available in the subclass could be not initialized (from the superclass).

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

a. the array must first be sorted the Binary Search is a searching algorithm used in a SORTED array by repeatedly dividing the search interval in half. Binary search algorithm: we basically ignore half of the elements just after one comparison. 1. Compare x with the middle element. 2.a. If x matches with the middle element, we return the mid index. 2.b. Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So we recur for the right half. 2.c. Else (x is smaller) recur for the left half.

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

c. Final **why**

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

contains Returns true if the ArrayList contains the specifies element; otherwise, returns false. Format: arraylist.contains(Object obj) Examples: // checks if 1 is present in the arraylist System.out.print("Is 1 present in the arraylist: "); System.out.println(numbers.contains(1)); // checks if C++ is present in languages System.out.print("Is C++ present in the arraylist: "); System.out.println(languages.contains("C++"));

The ___________ method checks if the specified element in present in the arraylist.

b. they cannot refer to non-static members of the class Static methods have access to class variables (static variables) without using the class's object (instance). Restrictions in Static Methods: 1. Non-static data members or non-static methods cannot be used by static methods, and static methods cannot call non-static methods directly. 2. In a static environment, "this" and "super" aren't allowed to be used.

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

b. 180 The value of x[0] is 100 The value of x[1] is 110 The value of x[2] is 120 The value of x[3] is 130 The value of x[4] is 140 The value of x[5] is 150 The value of x[6] is 160 The value of x[7] is 170 The value of x[8] is 180

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

c. 60 The value of x[0] is 20 The value of x[1] is 25 The value of x[2] is 30 The value of x[3] is 35 The value of x[4] is 40 The value of x[5] is 45 The value of x[6] is 50 The value of x[7] is 55 The value of x[8] is 60

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 e. None of the above

c. value contains the sum of all the values in array1. This line of code "value += array1[a];" is value = value = value + array1[a]

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.

b. the method has access to the object that the variable references Pass-by-reference: accessing the original object

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

a. a copy of the variable's value is passed into the method's parameter Pass-by-value: changes to the called method's copy do not affect the original variable's value in the caller.

When a value 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

c. you are performing a deep copy

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


Related study sets

Medical Terminology Final Exam #12

View Set

Organization Behavior Chapter 13 Power & Politics

View Set

Gallbladder and Billiary System - Ch: 6

View Set

Micro Chapter 17 Wage Determination

View Set