AP Computer Science A True/False Review

Ace your homework & exams now with Quizwiz!

If an object reference is declared in a client class and initialized to null, a NullPointerException will be thrown.

False. A NullPointerException will be thrown if the client program tries to call an instance method on an object whose value is null. The initialization to null is legal.

The String class contains a concatenate(s1, s2) method that changes the calling object into a concatenated string consisting of the characters of s1 followed by the characters of s2.

False. A String object is immutable, which means that it cannot be changed by a mutator method.

The following declaration, declaring a new String object is illegal, and will cause a compile-time error: String str = "house";

False. A String object is unusual in that it can be declared like a primitive type, without the keyword new. Also, it is legal to have digits in a String, so the statement is fine.

The following declaration, declaring a new String object with only digits, is illegal and will cause a compile-time error: String str = "246";

False. A String object is unusual in that it can be declared like a primitive type, without the keyword new. Also, it is legal to have digits in a String, so the statement is fine.

A subclass can override a private method of its superclass by declaring the subclass to be private.

False. A private method is accessible only in the class in which is appears. Therefore, a private method in the superclass cannot be accessed by any of its subclass.

The use of an undeclared variable in a Java program will cause a run-time error.

False. A program with an undeclared variable will not compile. Therefore, the error is a compile-time error.

If an object reference whose value is null is passed as an argument of a method, an IllegalArgumentException will be thrown.

False. An IllegalArgumentException is thrown to indicate that a parameter (argument) does not satisfy a method's precondition. Sometimes a null value is acceptable in the precondition.

All methods in an abstract class must be declared abstract.

False. An abstract class can have just one abstract method. The rest could be abstract or concrete. For example, an abstract Shape class could have the abstract methods area and perimeter (which need specific coding, depending on the actual Shape) and could have the concrete method getName (which would have the same code, irrespective of Shape).

A binary search algorithm cannot be used to search an array of objects.

False. An array of objects can be searched with a binary search on any key data field of the object that is sorted. For example, if an array of Person objects is sorted alphabetically by name, a binary search for a given name can be performed on those names.

An enhanced for loop (for-each loop) can be used to remove all elements of a String array whose length exceeds a given value.

False. An enhanced for loop is used to access each element of an array but should not be used to remove or replace elements since position indexes are not provided by the loop.

An enhanced for loop (for-each loop) can be used to replace each element of an int array with the value 0.

False. An enhanced for loop is used to access each element of an array but should not be used to remove or replace elements.

An enhanced for loop (for-each loop) can be used to remove each Book object in an array of Book elements if the author of that Book is deceased.

False. An enhanced for loop may not be used to remove elements from an array. (Note that this is true for both primitive and reference types.)

In a two-dimensional array mat of int values, an enhanced for loop (nested for-each traversal) can be used to replace each negative value in the array with 0.

False. An enhanced for loop may not be used to replace elements in an array. A row-by-row, column-by-column traversal will have to be used to access each element and then replace an element if necessary.

In a two-dimensional array bookMatrix of Book objects, an enhanced for loop (nested for-each traversal) can be used to replace each Book in bookMatrix that is fiction with a nonfiction Book by the same author.

False. An enhanced for loop may not be used to replace elements in an array. This is true for all types of elements. A row-by-row, column-by-column traversal will have to be used to access each element and then replace an element if necessary.

An enhanced for loop (for-each loop) can be used to replace each element in an array of int with each elements perfect square.

False. An enhanced for loop should not be used for replacing elements of an array since position indexes are not provided by the loop.

An enhanced for loop (for-each loop) can be used to replace each object in an array of Book objects with a new Book object.

False. An enhanced for loop should not be used for replacing elements of an array, whether those elements are primitive or reference types, since position indexes are not provided by the loop.

A subclass inherits the default constructor from its superclass.

False. Constructors are never inherited. If a subclass has no constructor, the default constructor for the superclass is generated. If the superclass has no default constructor, a complie-time error will occur.

If an ArrayList<String>list has been initialized with 5 strings, the statement list.add(5, "cat"); will throw an IndexOutOfBoundsException.

False. Given that there are 5 elements in list, the index slots are 0,1,2,3, and 4. It is legal to add a new element to slot 5. Legal slots for adding a new element are greater than or equal to 0 and are less than or equal to list.size().

If Date d1 = new Date(2, 17, 1948); and if String s1 = "February birthday!"; then the statement String s = s1 + d1; will cause a compile-time error.

False. If one of the operands is a String and the other is a reference type, then the non-String operand is converted to a String (using the toString method) and concatenation of the two strings occurs.

A subclass must define its own constructors.

False. If the superclass has a default (no-argument) constructor, then when the subclass object is created, the default constructor code will be slotted into the code for the subclass by using the keyword super.

In a large, sorted array of integers, a binary search will always use fewer comparisons than a sequential search to find a given way.

False. In the best case for a sequential search, the key will be in the first slot and found when just one comparison. In contrast, a binary search will start with the middle element in the array and therefore have at least two comparisons.

The expression "baseball".substring(8) throws a StringIndexOutOfBoundsException.

False. In the expression "baseball".substring(8), the starting index of the substring is the length of the string. This is allowed in the substring method and returns an empty string.

If State is an interface, then the following declaration will correctly create a new State object: State s = new State();

False. It is illegal to create an instance of an interface.

The standard Java class, Object, is an abstract class that contains only abstract methods.

False. Object is a concrete class, which means that all its methods have implementations. It is expected that each of these methods- for example, toString and equals- will be overridden in any class in which the implementation is unsuitable.

A binary search algorithm uses tail recursion.

False. Tail recursion occurs when the recursive call is the very last statement of the method. Since a binary search algorithm makes recursive calls in the middle of the method (whenever the search array is split in half), the algorithm does not use tail recursion.

If String s1 = "4"; and if String s2 = "3"; then the statement String s3 = s1 + s2; will assign the String value "7" to s3.

False. The + operator is the concatenation operator when it operates on two strings. Namely, it chains the strings together. In the given example, s3 contains the string "43".

A program that attempts to divide an integer by zero will cause a compile-time error.

False. The attempt to divide by zero will throw an ArithmeticException, which is a run-time error.

If String s1 = "abc"; and if String s2 = new String("abc"); the value of the boolean expression (s1 == s2) will be true.

False. The boolean expression (s1 == s2) returns true if s1 and s2 are the same reference. In the given example, they are not the same reference since s2 is created with new.

The expression "baseball".substring(3,4) returns "eb".

False. The correct return string is "e". The expression "baseball".substring(3,4) returns the substring starting with the character at position 3 in "baseball", namely, 'e', and ending with the character at position 3, namely 'e'. Notice that the position of the last character required is 3, one less than the second index.

Suppose Car is a subclass of Vehicle, and suppose Vehicle has a public display method that has not been overridden in Car. If the following declaration has been made: Vehicle car1 = new Car(); then the method call car1.display() will cause a compile-time error.

False. The display method of the Vehicle class is inherited by its subclass. The method call carl.display() will invoke the display method of its superclass.

The equals method in the Object class returns true if this object and its parameter object are the same String literal.

False. The equals method in the Object class returns true if this object and its parameter object refer to the same memory slot (namely, they are the same object). Often the equals method will be overridden to return true if the two objects have the same contents.

The expression "baseball".substring(0) returns the empty string.

False. The expression "baseball".substring(0) returns the substring starting with the character at position 0 in "baseball", namely 'b'. The returned substring is "baseball".

The expression "dog".substring(4) returns "".

False. The expression "dog".substring(4) throws a StringIndexOutOfBoundsException since 4 > "dog".length, which is 3.

The expression "hello".substring(-1, 1) returns "h".

False. The expression "hello".substring(-1,1) throws a StringIndexOutOfBoundException if the first parameter is less than 0 or greater than the length of the string.

The expression "park".substring(2,5) returns "rk".

False. The expression "park".substring(2,5) throws a StringIndexOutOfBoundsException if the second parameter is greater than "park".length(), which is 4.

The expression "park".substring(3,2) returns "k".

False. The expression "park".substring(3,2) throws a StringIndexOutOfBoundsException if the second parameter is less than the first parameter.

If ob 1 is an Integer with value 8 and if ob2 is a Double with value 8.0, then the expression (ob1.compareTo(ob2) == 0)) will have the value true.

False. The expression (ob1.compareTo(ob2) == 0)) will cause an error since ob1 and ob2 are different types. You can't directly compare an Integer to a Double.

If s1 is a String containing "fur" and if s2 is a String containing "further", then the expression (s1.compareTo(s2) > 0)) will have value true.

False. The expression (s1.compareTo(s2) > 0)) is true if the left-hand operand (in this case, "fur") follows the right-hand operand (in this case, "further") in dictionary order. In this case it doesn't. If two string literals, s1 and s2, have identical characters but if s1 terminates before s2, then s1 will precede s2 in the dictionary. So the expression is false.

If s1 is a String containing "mno" and if s2 is a String containing "mno", then the expression (s1.compareTo(s2)) will have the value true.

False. The expression (s1.compareTo(s2)) is an incorrect usage of the compareTo method. The expression s1.compareTo(s2) == 0) is true since (s1.equals(s2)) is true. Namely, they have the same contents.

If String str is the empty string, then the expression str.indexOf("ha") throws a NullPointerException.

False. The expression is legal and returns a value of -1 since "ha" is not a substring of the empty string.

If a two-dimensional array mat us declared as follows: int[][]mat = new int[3][4]; then the expression mat[3][4] represents the slot in the bottom right-hand corner of the matrix.

False. The expression mat[3][4] is out of range, and an ArrayIndexOutOfBoundsException will be thrown. The rows in mat range from 0 to 2, and the columns range from 0 to 3. The slot in the bottom right-hand corner of the matrix will be mat[2][3].

If String str has the value "brouhaha", then the expression str.indexOf(0) returns the value of "b".

False. The expression str.indexOf(0) will cause a compile-time error since the indexOf method takes a String parameter and returns an int, not the other way around.

If String str has value "brouhaha", then the expression str.indexOf("ha") returns a value of 6.

False. The expression string.indexOf("ha") returns the index of the first occurrence of "ha" in "brouhaha", namely 4.

If a two-dimensional array table is declared as follows: int[][]table = new int[3][4]; then the expression table [2] in the program will cause a compile-time error.

False. The expression table[2] represents the array of int elements in row 2 of the table matrix.

The null string is equivalent to an empty string.

False. The null string is the same as the null reference; whereas an empty string is an existing string that has no characters. If String s1 is set equal to null (the null reference) and if String s2 is set equal to "" (the empty string), then s1.length() causes a NullPointerException whereas s2.length() returns a value of 0.

If String s1 has the value "cake", then s1.length() has the value 6.

False. The quotes are not part of the string. Therefore, the length of the string is 4.

The rows in a two-dimensional array mat are numbered (indexed) from 0 to mat.length.

False. The rows of a matrix are numbered from 0 to mat.length-1. Thus if mat has 3 rows, the rows will be represented as mat[0], mat[1], and mat[2].

The scope of a local variable is the method in which the variable is declared.

False. The scope of an variable is the block in which the variable is declared. A block is a piece of code enclosed in a {} pair. Thus, a local variable can be declared in a loop, in which case the variable's scope will be the loop body. When the loop is existed, the memory for that local variable will be recycled.

The following declaration is illegal and will cause a compile-time error: String str = house;

False. The statement could be legal. If house is a String variable that has been previously declared, it is legal to assign the house reference to str. However, if house is not an existing variable, there will be an error since a String literal must be enclosed in double quotes. The following statement will be fine: String str = "house"

The statement int[] arr = new int[10]; initializes each element in array arr to 10.

False. The statement creates a new array arr with 10 slots, each initialized to 0.

The toString method in a class returns the particular object in string form.

False. The statement is true only if the toString method is overridden to represent a particular object. For example, a Point object could have its toString method return the string form of an ordered pair that gives the location of that Point. (If there is no overridden toString method for the class, the method will return the name of the class, an "@" sing, and the hexadecimal form of the hashcode of the class.

If an array arr of int has the values 5,4,2,0, then the statement System.out.print(arr); will print the elements all on one line, with the cursor remaining on that line and following the element 0.

False. The statement will output the reference to arr, which is generally not useful. To print the elements of the array, an explicit piece of code is needed to access each element in turn and print it.

The statement double x = math.random(); will cause a compile-time error.

False. The statement will store in x a random real number r such that 0 <= r < 1.0

If SouthernState has no explict constructors and is a subclass of State, which has a constructor with parameters, then the following declaration will correctly create a new SouthernState object: State s = new SoutherState();

False. The statement will work only if the superclass State also has a default (no parameter) constructor. Otherwise, there will be a compile-time error.

A superclass inherits all the public methods of its subclass.

False. The statement would be correct if it said: A subclass inherits all the public methods of its superclass. For example, if Dog is a subclass of Animal, a Dog objet will inherit all the public methods of Animal.

If an array of int has elements 2,4,6, then the statement System.out.print(arr.get(0)); will print 2.

False. There will be a compile-time error. The code to access the first element of the array is arr[0]. Therefore, the correct statement will be: System.out.print(arr[0]);

If int n1 = 4; and if int n2 = 3; then the statement String s3 = n1 + n2; will assign the String value "43" to s3.

False. There will be an error message. The + operator is the concatenation operator when it operates on two strings. In order to work as intended, at least one of hte operands must be a String.

It is not possible to create an instance of a subclass that doesn't define its own constructor.

False. This is possible if the superclass has a default (no-argument) constructor. The code for the constructor will be slotted into the subclass code.

An enhanced for loop (for-each loop) can be used to modify each element in an array of int such that each element is incremented by 2.

False. To add 2 to each element means that each element is being replaced. An enhanced for loop should not be used for replacing elements of an array since position indexes are not provided by the loop.

If String str is the null string, then the expression str.indexOf("") returns a value of -1.

False. the expression throws a NullPointerException since you can't call an instance method with the null reference.

The following declaration is illegal and will cause a compile-time error: String str = 246;

True. A String object is unusual in that it can be declared like a primitive type, without the keyword new. However, a String literal must be enclosed in double quotes. The following statement will be fine: String str = "246";

A binary search algorithm cannot be used to search an array of random, unsorted int values.

True. A binary search can be used only for an array whose elements are sorted.

A class can have an array of objects as a private instance variable.

True. A class can have any legal Java type as a private instance variable.

A string literal can contain zero characters.

True. A string with zero characters is called the empty string and is represented as "".

The method in the Java Math class are all static.

True. All of the methods and variables are static, which means there are no instances of Math objects.

If a code segment contains an attempt to perform integer division by zero, a run-time error will occur

True. An ArithmeticException will be thrown. Note that this is not true for floating-point division by zero, where the output is Infinity.

If a two-dimensional array mat is declared as follows: int[][] mat = new int[3][4]; then the expression mat[3] in the program will cause a run-time error.

True. An ArrayIndexOutOfBoundsException will be thrown since, from the declaration, there are just 3 rows in the matrix, represented by mat[0], mat[1], and mat[2]. In the expression mat[3], the 3 is out of range.

If a class has a private instance variable that is an array of objects, the elements should be initialized in the class conductor.

True. An attempt to access uninitialized array elements will result in a NullPointException since the default element values will be null.

An enhanced for loop (for-each loop) can be used to modify each object in an array of Book elements by calling a mutator method called updateEdition.

True. An enhanced for loop may be used for modifying elements of an array of objects by accessing each element and invoking a mutator method on it.

In a two-dimensional array bookMatrix of Book objects, an enhanced for loop (nested for-each traversal) can be used to update each Book in bookMatrix by setting its boolean method, isBestSeller, to true or false.

True. An enhanced for loop may not be used to replace or remove elements in an array of object. It can, however, be used to access each element and modify an element using a mutator method.

An escape sequence can be part of a string literal.

True. An escape sequence is a backslash followed by a single character. It is used to invoke an alternative interpretation of the character. For example, \n represents new line, \" represents double quote, and \\ represents backslash. Thus, if the string literal "Go\nhome" is printed, the output will be: Go home

A class can both extend a superclass and implement an interface.

True. Consider an owl class, which is a subclass of Animal and Flyer, where Animal is a concrete superclass and Flyer is an interface. (An owl is-a Animal and is-a Flyer) Note that in the declaration, the keyword extends must precede the keyword implements: public class Owl extends Animal implements Flyer.

The standard Java class, Object, is a superclass for every class created in Java.

True. Every class automatically extends Object and inherits all methods of the Object class.

An abstract class can have more than one subclass.

True. For example, an abstract class, BattleShip, could have the subclasses Destroyer, Frigate, Submarine, and so on.

A recursive method can have an exponential run time.

True. For example, any recursive method that has 2 recursive calls in one line of the method will have double the number of recursive calls each time the method is called. Thus, for 2 recursive calls, the method will be called 4 times. For 3 recursive calls the method will be called 8 times, and so on. This repeated doubling is exponential.

A subclass can add private instance variables that are not in its superclass.

True. For example, if GradStudent is a subclass of Student, it may add an instance variable gradStudentID, which makes sense for a GradStudent but not necessarily for a Student.

A class can implement more than one interface.

True. For example, suppose an interface, Flyer, has one method, isFlying, and suppose another interface, Animal, has methods getName and getSpecies. It is possible for a Bird object to implement both Flyer and Animal since all of the above methods apply to a Bird.

If an ArrayList<String>list has been initialized with 5 strings, the statement list.add(6, "cat"); will throw an IndexOutOfBoundsException.

True. Given that there are 5 elements in list, the index slots are 0,1,2,3, and 4. It is illegal to add a new element to slot 6. An IndexOutOfBoundsException will be thrown if the given statement uses a slot less than 0 or greater than list.size().

If super is used in the implementation of a subclass constructor, it must be used in the first line of the constructor body

True. If super is used at any place in the constructor's code, other than in the first line, a compile-time error will occur.

If MazeComponent is an abstract class, then the following declaration will cause an error: MazeComponent m = new MazeComponent();

True. It is illegal to create an instance of an abstract class.

In a nonempty, rectangular two-dimensional array mat, the number of columns is equal to mat[0].length.

True. Note that mat[0].length is the length of the array mat[0]. Suppose the array has 2 rows. The number of columns is also equal to mat[1].length, namely the length of the array mat[1]. However, if you refer to mat[2].length, an ArrayIndexOutOfBoundsException will be thrown since 0 and 1 are the only in-range indexes for a matrix with 2 rows.

When an array is passed as a parameter, the elements of the array can be modified by the method.

True. Passing an array as a parameter means passing its object reference. Thus, when changes are made to the parameter array, the same changes are also being made to the actual array since the parameter array and the actual array have the same reference.

In order for polymorphism to apply in a Java computer program, the program must have a superclass and at least one subclass with an overridden method.

True. Polymorphism is the mechanism whereby the correct overridden method for a particular object is called during run time.

The number of comparisons in a selection sort algorithm is independent of the initial arrangement of data.

True. Selection sort finds the smallest element in the array and then swaps it into the first position of the array. Then selection sort finds the second smallest element and swaps it into the second position, and so on. Thus, the same number of comparisons will occur, irrespective of the arrangement of elements in the array.

In a two-dimensional array mat of int values, an enhanced for loop (nested for-each traversal) can be used to count the number of negative values in an array.

True. Since counting negative values does not change or replace elements in the array, an enhanced for loop is a legal, convenient way to access elements in the array.

If String s = "4"; and if String n = "3"; then the statement String s3 = s + n; will assign the String value "43" to s3.

True. The + operator is the concatenation operator when it operates on two strings. Namely, it chains the strings together. If one of the operands is a String and the other is a primitive type, then the non-String operand is converted to a String and concatenation of the two Strings occurs. In the given example, n will be converted to String "3" and s3 will contain the String "43".

If an array arr is initialized to contain n elements of type int, the statement arr[n] = 0; will throw an ArrayIndexOutOfBoundsException.

True. The array elements are arr[0], arr[1]. arr[2],... arr[n-1]. Thus, arr[n] is out of bounds.

If String s1 = "abc"; and if String s2 = new String("abc"); the value of the boolean expression (s1.equals(s2)) will be true.

True. The boolean expression (s1.equals(s2)) returns true if s1 and s2 have the same contents. In the given example, they do: "abc"

If an ArrayList<Integers> intList has the element 2,4,6, then the statement System.out.print(intList.get(0)); will print 2.

True. The code to access the first element of an ArrayList uses the List, method get, whose parameter is the index of the required element.

The expression "baseball".substring(0,4) returns "base".

True. The expression "baseball".substring(0,4) returns the substring starting with the character at position 0 in "baseball", namely 'b', and ending with the character at position 3, namely 'e'. Think of the first parameter as the first position you wand and the second parameter as the first position you don't want.

The expression "baseball".substring(2) returns "seball".

True. The expression "baseball".substring(2) returns the substring starting with the character at position 2 in "baseball", namely the "s".

If ob1 is an Integer with value 8 and if ob2 is an Integer with value 18, then the expression (ob1.compareTo(ob2) < 0)) will have value true.

True. The expression (ob1.compareTo(ob2) < 0)) is true if the left hand operand (in this case, ob1) is less than the right-hand operand (in this case, ob2). Since 8 < 18, (ob1.compareTo(ob2) < 0)) is true.

If s1 is a String containing "car" and if s2 is a String containing "cat", then the expression (s1.compareTo(s2) < 0)) will have value true.

True. The expression (s1.compareTo(s2) < 0)) is true if the left-hand operand (in this case, "car") precedes the right-hand operand s2 ( in this case, "cat") in dictionary order.

If String str has the value "chicken", then the expression str.indexOf("ben") has the value -1.

True. The expression str.indexOf("ben") returns -1 since "ben" is not a substring of "chicken".

If String str has the value "chicken", then the expression str.indexOf("ken") has the value 4.

True. The expression str.indexOf("ken") returns the index of the first occurrence of "ken" in "chicken", namely 4.

Private instance variables can be used in all methods of the class in which they are declared.

True. The scope of any variable is the block in which the variable is declared. A block is a piece of code enclosed in a {} pair. Thus, a private instance variable, declared in a class, has scope within the class braces and can be used by all methods of the class.

If Date d1 = new Date(2, 17, 1948); and if Date d2 = new Date(5, 22, 1960); then the statement String s = d1 + d2; will cause a compile-time error.

True. There will be an error message indicating that the + operator (concatenation operator) is not defined for two non-String objects. In order to work as intended, at least one of the operands must be a String.

If an ArrayList<String> list is initialized with String values, the statement System.out.println(list); will print the strings of the list on one line, enclosed in brackets, and separated with commas. Additionally, the cursor will move to a new line after printing.

True. This is an advantage of using an ArrayList versus an array, which does not automatically print the elements.

Suppose Car is a subclass of Vehicle, and suppose Vehicle has a public display method that has been overridden in Car. If the following declaration has been made: Vehicle car1 = new Car(); then the method call car1.display() will invoke the overridden method in Car.

True. This is an example of polymorphism. The display method for the particular instance of the Vehicle object will be called at run time.

An enhanced for loop (for-each loop) can be used to find the total of all the elements in an array of double.

True. an enhanced for loop can be used for accessing each element and adding that element to a running total.


Related study sets

Salesforce JavaScript Developer 1 Exam

View Set

MGMT 680 Ch 13 (Strategic Entrepreneurship_

View Set

Lifespan Review (previous quizzes)

View Set

8.04 Lesson Assessment: Field Study - Bulgaria Unit Test

View Set

Chapter 16 (Supporting Mobile Operating Systems)

View Set