CS 300 Exam 1

Ace your homework & exams now with Quizwiz!

b.) Goal.score(); A static method belongs to the class. It can be called using the name of the class. An instance method belongs to an object. It must be called using a reference to an object. According the CS300 course style and API capitalization convention, the name of a class must be written in UpperCamelCase. Whereas, a reference to an object must be written in a lowerCamelCase. That's why Goal.score() references a call of a static method that belongs to the class Goal. game.start(); cannot reference a call to a static method car game cannot be a name of a class according to the CS300 course style. It can be a reference name.

Based on the capitalization conventions used in both CS300 and the Java API, which of the following method calls reference a static method? a.) game.start(); b.) Goal.score();

b.) No No, because the constructor of the class ArrayList<E> accepts only reference types as type of the elements to be stored in the arrayList. It does not accept any of the 8 primitive types defined in Java to create an ArrayList object.

Does the constructor of the class ArrayList<E> accept primitive types (such as int, double, float)? For instance, ArrayList<double> list = new ArrayList<double>(); a.) Yes b.) No

b.) HighSpeedException MUST be an unchecked exception HighSpeedException MUST be an unchecked exception, otherwise the code won't compile. Notice carefully that HighSpeedException is not handled in drive() method. The drive() method does not declare in its signature that it may throw HighSpeedException using throws keyword. Also, the line of code that throws the exception is not surrounded by a try-catch bloc.

Given the implementation of following method, which of the following statements is correct? a.) HighSpeedException MUST be a checked exception. b.) HighSpeedException MUST be an unchecked exception c.) HighSpeedException can be either a checked or an unchecked exception

4 String first = departments[3].roster[2].getFirstName(); The right part of this statement includes 5 references: a) departments b) departments[3] c) departments[3].roster d) departments[3].roster[2] e) departments[3].roster[2].getFirstName() Only the references a), b), c), and d) are used in this statement (for instance "departments" is used to get the element stored at index 3). If any of those references is null, a NullPointerException will be thrown. The last reference, meaning the output of the instance method "getFistName()", is not used. If "e)" is null, "first" will be simply assigned the value "null". --> String first = null The correct answer is 4.

How many references may be null and cause a NullPointerException to be thrown from the following line of code?

Yes

Is String a reference type?

a.) auto-boxing

The following line of code represents an ________________ operation. Double x = 1.5; a.) auto-boxing b.) auto-unboxing

b.) auto-unboxing

The following line of code represents an ________________ operation. int x = new Integer(4); a.) auto-boxing b.) auto-unboxing

False

True/False: A private method declared in a superclass can be overridden by a subclass.

False

True/False: An interface can contain signatures of private methods.

12 The correct answer is 12 because the constructor of class B invokes the parameterized constructor of its super class A by passing 123 as input argument. The constructor of A ignores this passed parameter and assign a value of 10 to its int field "value". Hence, 12 is returned by getValue() method and this result is displayed to the console.

We consider the classes A and B defined below. What would be the result printed out on the console when an instance of B is created? For instance: B bObject = new B();

a.) Item2 can have additional methods not declared in Item1. Only the answer key "Item2 can have additional methods not declared in Item1." is TRUE. All the other options are false.

We consider the following class declarations. Which of the following statements is TRUE? a.) Item2 can have additional methods not declared in Item1. b.) Item2 can directly access the private variable fields in Item1. c.) Item 2 is the base class for the derived class Item1. d.) Item 2 cannot override methods declared in Item1.

B

What does the Java array referred by arr look like in memory after the following segment of code is run? 1

A

What does the Java array referred by arr look like in memory after the following segment of code is run? 2

D

What does the Java array referred by arr look like in memory after the following segment of code is run? 3

b.) Compilation error. The second catch block is unreachable Correct answer: Compilation error The list of exceptions appears in an incorrect order that they may be caught when any of these exceptions may occur in the try block. When a subclass exception is mentioned after base class exception, then a compilation error occurs. Keep in mind, the most specific exception must be caught first, so it must appear first in the list of the catch blocks at the end of a try bloc. Here, ArrayIndexOutOfBoundsException is a subclass of RuntimeException. In other words, ArrayIndexOutOfBoundsException is a RuntimeException. As, ArrayIndexOutOfBoundsException has been already caught by super class RuntimeException, this program returns a compilation error. Please refer to the hierarchy class diagram for Exception class to determine the order how exceptions must appear at the end of a try block.

What does the following code print out to the console when it runs? a.) Runtime error b.) Compilation error. The second catch block is unreachable c.) Array is printed. Then, "ArrayIndexOutOfBoundsException" message is diplayed d.) Array is printed. Then, "error " message is displayed e.) Array is printed. No error message is displayed

c

What does the following program print out when it is run? a.) abdefc b.) abdec c.) abdgefc d.) abdefc e.) abcdefg

false The correct answer is "false" because String objects are immutable and "==" compares references and not the object values or state (contents). Their values cannot be edited or modified once set for the first time (initialized). String s3 = s2.concat("come"); // a new String object is created with the contents "welcome" and its reference is stored in s3.boolean result = s3 == s1; // s3 and s1 refers to different String objects with the same values. "==" operator compares references. So, result is evaluated to "false".Whereas, s3.equals(s1) is evaluated to "true".Notice that in order to compare objects, use the method .equals(). To compare references or primitive types, use the operator "==".

What does the following segment of code print out to the console when it is run? String s1 = "welcome"; String s2 = "wel"; String s3 = s2.concat("come"); boolean results = s3 == s1; System.out.print(result);

d.) John String objects are immutable

What does the following segment of code print out when it is run? String s = "John"; s.concat(" Doe"); System.out.println(s); a.) John Doe b.) Doe John c.) JohnDoes d.) John

4211 Keep in mind that static fields belong to the class and are shared among all objects. Instance fields belong to the objects and their values vary from an object to another.Use java visualizer to step through the execution of the provided code to convince yourself of the correct output.

What does this program print out to the console when it is run?

c.) double[ ] a = new double[5];

What is the correct syntax to create an array that can hold up to 5 doubles? * a.) double[] a = new double{1, 2, 3, 4, 5}; b.) double[] a = new double(5); c.) double[ ] a = new double[5]; d.) double[] a = new double[];

6

What is the minimum number of objects that should be present in the heap-memory after the following line of code is run? Integer[] data = new Integer[] {1, 2, new Integer(3), 4, new Integer(5)};

5

What is the minimum number of objects that should be present in the heap-memory after the following line of code is run? String[] values = new String[] {"A", "B", null, "D", null, "F"};

1

What is the minimum number of objects that should be present in the heap-memory after the following line of code is run? int[] integers = new int[] {1, 2, 3, 4};

b.) A NullPointerException because s is null s is not still initialized. It refers so to Null pointer. The invocation of s results so on a NullPointerException.

What is the output of the following code: String s = null; boolean result = s.equals(""); System.out.println(result) a.) true, because since s is null. So, it is empty it does equal "" b.) A NullPointerException because s is null

arrayRef[index] != null The correct answer is: arrayRef[index] != null Otherwise, a NullPointerException will be thrown.

What line of code should be added to the blank at Line A in the following code, so it can be run without errors.

1110 The correct answer is 1110. Notice that all the changes made to currentSize are lost when the method addElement returns.

What will the following code print out to the console when it is run?

325 The correct answer is 325. At the end of the program, the array contains 3 elements which are in order 1, 8, 5 stored at indices 0, 1, 2 respectively; and size is 2. Notice carefully that the change to currentSize after calling the method addElement() to add 5 is lost when the method returns.

What will the following code print out to the console when it is run?

5 3 The correct answer is "5 3" because add(newObject) method adds newObject at the end of the ArrayList list, add(newObject, index) adds newObject at index position within the ArrayList, remove(index) removes the element at index position from the ArrayList, and get(index) returns the element at index position from the ArrayList. When the above segment of code is all run, the contents of the ArrayList list is as follows: 5, 3, 2. So, list.get(0) returns 5, and list.get(1) returns 3.

What will the following code print out to the console when it is run?

53 The correct answer is 53. To convince yourself try to step through the execution of the code using java visualizer.Recall that in Java, all changes made to the variables used in a method are cleaned up from the memory when the method returns. The method runs of copies of the primitive input parameters and shallow copies of reference type input parameters.

What will the following code print out to the console when it is run?

b.) 0 The correct answer is 0 because the first line of code creates an empty ArrayList. So, its reference list is not null. But, refers to an empty ArrayList object. Its size is 0 and it will be increased each time an integer is added to the list and decremented each time an element is removed from this list.

What will the following code print out to the console when it is run? a.) NullPointerException will be thrown when trying to call size() method using the list reference because the first line creates an empty ArrayList. So, list is null. b.) 0 c.) 10

[RED, BLUE, then NullPointerException The correct answer is "[RED, BLUE, then NullPointerException".When the following line of code System.out.print(arrayRef[index].toUpperCase()); will be run on index 2, arraRef[2] is null. The user tries to call the instance method toUpperCase() using a null reference. That's why a NullPointerException will be thrown after displaying [RED, BLUE and the program crashes.

What will the following code will print out to the console when it is run?

an empty array int[] data = new int[0]; // This line of code creates an empty array which capacity is 0. This data array cannot contain any element. data.length == 0; This is a useless array.

What will the following line of code create?

U65XYZ

What would the following program print out to the console when it is run?

RED BLUE then a NullPointerException

What would the following segment of code print out to the console when it is run?

c.) Constructor of Base class called. Constructor of Derived class called. Note carefully that if the constructor of the super class is not explicitly called, the no-argument constructor of the super class (super()) is implicitly called as the first instruction in the constructor of the subclass.

What would the following segment of code print out to the console when it is run? a.) Constructor of Derived class called. b.) Constructor of Derived class called. Constructor of Base class called. c.) Constructor of Base class called. Constructor of Derived class called.

b.) a: 5, 8.0

What would the following segment of code print out to the console when it is run? a.) square a: 25, 8.0 b.) a: 5, 8.0 c.) a: 5, 15 d.) 25, 15.0

a.) int[] data = new int[ ]{10, 20, 30, 40, 50};

Which line of code creates an array of integers and initializes its content? * a.) int[] data = new int[ ]{10, 20, 30, 40, 50}; b.) int[] data = new int{10, 20, 30, 40, 50};

d.) A subclass can extend just one parent class and can implement zero or more interfaces.

Which of the following is TRUE about inheritance in Java? a.) A subclass can extend a parent class or implement an interface, but not do both. b.) A subclass can extend zero or more parents, and can implement zero or more interfaces. c.) A subclass can extend just one parent class and can implement just one interface. d.) A subclass can extend just one parent class and can implement zero or more interfaces.

a.) int[ ][ ] c = new int[5][4];

Which of the following lines of code creates a two dimensional array of 20 integers? * a.) int[ ][ ] c = new int[5][4]; b.) int[] int[] a = new int[5][4]; c.) int[ ] a = new int[5][4]; d.) int[5][4] b = new int[20];

c.) list.add(value); The correct answer is the third key answer: list.add(value);Make sure to review the Java API of ArrayList class available at https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html The first option will throw an IndexOutOfBoundsException since ArrayList.set(index, newObject) method replaces the element at the specified position in this list with the specified element. and throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).The second option is incorrect car value should be added at the end of the list (i.e. at position list.size() and not at index position list.size() - 1). indexOf() method returns the index of the first match with value if it exists in the list, and (-1) otherwise.

Which of the following pieces of code will add a new value to the end of an ArrayList list? a.) list.set(list.size(),value); b.) list.add(list.size()-1, value); c.) list.add(value); d.) list.indexOf(value);

b.) an unchecked exception ArithmeticException extends RuntimeException. It is so an unchecked exception.

public class Throwable extends Object public class Exception extends Throwable public class RuntimeException extends Exception public class ArithmeticException extends RuntimeException public class PurpleException extends Exception According to the above "Reference Section", ArithmeticException is _________________________ a.) a checked exception b.) an unchecked exception

a.) a checked exception

public class Throwable extends Object public class Exception extends Throwable public class RuntimeException extends Exception public class ArithmeticException extends RuntimeException public class PurpleException extends Exception According to the above "Reference Section", PurpleException is _________________________ a.) a check exception b.) an unchecked exception


Related study sets

Testové otázky - stabilní katastr 4

View Set

Social Studies 10: Government of Canada

View Set

Web Accessibility Specialist Quiz

View Set

Psychology HW: Anxiety-Related Disorders; Depressive Disorders and Bipolar Disorders

View Set

Nutrition: Chapter 7 Practice. Test

View Set

3.05 Ionic Bonding and Writing Formulas

View Set