ITP 120 final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

1) The increment operator is:

++

36) What will be returned from the following method? public static float[] getValue(int x)

An array of float values

1) Methods are commonly used to: A) speed up the compilation of a program B) break a problem down into small manageable pieces C) emphasize certain parts of the logic D) document the program

B

17) A special variable that holds a value being passed into a method is called what? A) Modifier B) Parameter C) Alias D) Argument

B

30) This type of method performs a task and then terminates. A) value-returning B) void C) local D) simple

B

17) What do you normally use with a partially-filled array? A) A class that does nothing but manage the array B) An accompanying parallel array C) An accompanying integer value that holds the number of items stored in the array D) An accumulator

C

34) Which of the following statements will create a reference, str, to the string, "Hello, world"? (1) String str = new String("Hello, world"); (2) String str = "Hello, world"; A) 1 B) 2 C) 1 and 2 D) neither 1 or 2

C

35) Overloading means multiple methods in the same class: A) have the same name, but different return types B) have different names, but the same parameter list C) have the same name, but different parameter lists D) perform the same function

C

7) Most programming languages that are in use today are: A) procedural B) logic C) object-oriented D) functional

C

7) This part of a method is a collection of statements that are performed when the method is executed. A) method header B) return type C) method body D) method modifier

C

When you create a class and do not provide a(n) ____, Java automatically supplies you with a default one.

Constructor

The exception classes are in packages in the _____________.

Java API

T/F: 7) Any items typed on the command-line, separated by space, after the name of the class are considered to be one or more arguments that are to be passed into the main method.

T

41) Given the following two-dimensional array declaration, which statement is TRUE? int [][] numbers = new int [6] [9];.

The array numbers has 6 rows and 9 columns.

35) What would be the results of the following code? 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]; }

Value contains the sum of all the values in array1.

An Exception (as distinct from an exception) is

an object

18) To return an array of long values from a method, use this as the return type for the method.

long[]

T/F: 13) Java does not limit the number of dimensions that an array may have.

t

In a multi-catch, (introduced in Java 7) the exception types are separated in the catch clause by this symbol:

|

30) If final int SIZE = 15 and int[] x = new int[SIZE], what would be the range of subscript values that could be used with x[]?

0 through 14

16) How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);

1

10) What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; }

100

18) How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!!!");

12

3) What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z);

28

32) What will be the value of x[1] after the following code is executed? int[] x = {22, 33, 44}; arrayProcess(x[1]); ... public static void arrayProcess(int a) { a = a + 5; }

33

19) What will be the value of x after the following code is executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y;

40

28) What will be the value of x[8] after the following code has been 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; }

60

23) What is the value of scores[2][3] in the following array? int [] [] scores = { {88, 80, 79, 92}, {75, 84, 93, 80}, {98, 95, 92, 94}, {91, 84, 88, 96} };

94

39) This is a sum of numbers that accumulates with each iteration of a loop. A) Running total B) Final total C) Grand finale D) Galloping total

A

4) What do you call the number that is used as an index to pinpoint a specific element within an array? A) subscript B) global unique identifier C) element D) argument

A

41) Quite often you have to use this statement to make a group of classes available to a program. A) import B) use C) link D) assume

A

43) Look at the following statement. import java.util.*; This is an example of: A) a wildcard import B) an explicit import C) unconditional import D) conditional import

A

If a method does not handle a possible checked exception, what must the method have?

A throws clause in its header

29) What will be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5};

An array of 6 values ranging from 0 through 5 and referenced by the variable x will be created.

25) It is common practice in object-oriented programming to make all of a class's: A) methods private B) fields private C) fields public D) fields and methods public

B

3) In the cookie cutter metaphor, think of the ________ as a cookie cutter and ________ as the cookies. A) object; classes B) class; objects C) class; fields D) attribute; methods

B

48) Assume that the following method header is for a method in class A. public void displayValue(int value) Assume that the following code segments appear in another method, also in class A. Which contains a legal call to the displayValue method? A) int x = 7; void displayValue(x); B) int x = 7; displayValue(x); C) int x = 7; displayValue(int x); D) int x = 7; displayValue(x)

B

6) What is stored by a reference variable? A) A binary encoded decimal B) A memory address C) An object D) A string

B

7) What will be the value of x[8] after the following code has been 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

The class used as a basis for inheritance is the ____ class.

Base

11) Each array in Java has a public field named ________ that contains the number of elements in the array. A) size B) capacity C) length D) limit

C

39) Which of the following is NOT involved in finding the classes when developing an object-oriented application? A) Describe the problem domain. B) Identify all the nouns. C) Write the code. D) Refine the list of nouns to include only those that are relevant to the problem.

C

Which statement correctly declares a sedan object of the Car class?

Car sedan = new Car();

4) Which of the following are classes from the Java API? A) Scanner B) Random C) PrintWriter D) All of the above

D

42) Local variables can be initialized with: A) constants B) parameter values C) the results of an arithmetic operation D) any of the above

D

42) This is an item that separates other items. A) Controller B) Partition C) Doorway D) Delimiter

D

43) A value-returning method must specify this as its return type in the method header. A) an int B) a double C) a boolean D) any valid data type

D

44) The following package is automatically imported into all Java programs. A) java.java B) java.default C) java.util D) java.lang

D

44) What will be returned from the following method? public static int methodA() { double a = 8.5 + 9.5; return a; } A) 18.0 B) 18 (as an integer) C) 8.0 D) This is an error.

D

Usually, the subclass constructor only needs to initialize the ____ that are specific to the subclass.

Data Fields

By convention, a class diagram contains the ____ following each attribute or method.

Data type

22) Which of the following values can be passed to a method that has an int parameter variable? A) float B) double C) long D) All of the above E) None of the above

E

All exceptions are instances of classes that extend this class.

Exception

All of the exceptions that you will handle are instances of classes that extend this class

Exception

Write a statement that declares a reference variable e suitable for holding a reference to an Exception object.

Exception e;

The Java keyword that creates inheritance is _____.

Extends

25) Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents?

FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);

In a catch statement, what does the following code do? System.out.println(e.getMessage());

It prints the error message for an exception.

50) The following statement creates an ArrayList object. What is the purpose of the <String> notation? ArrayList<String> arr = new ArrayList<String>();

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

The keyword super always refers to the _____ of the class in which you use it.

Parent class

T/F: 12) When you pass the name of a file to the PrintWriter constructor, and the file already exists, it will be erased and a new empty file with the same name will be created.

T

T/F: 2) Declaring an array reference variable does not create an array.

T

T/F: 2) The while loop has two important parts: (1) a boolean expression that is tested for a true or false value, and (2) a statement or block of statements that is repeated as long as the expression is true.

T

T/F: 2) Two general categories of methods are void methods and value returning methods.

T

T/F: 3) To compare the contents of two arrays, you must compare the elements of the two arrays.

T

What will be the result of the following code? FileOutputStream fstream new FileOutputStream("Output.dat"); DataOutputStream outputFile = new DataOutputStream(fstream);

The outputFile variable will reference an object that is able to write binary data to the Output.dat file.

When you create parent and child classes of your own, the child classes use ____ constructors.

Three

Besides Object all Exceptions and Errors are descended from the _________________class

Throwable

Arrows used in a ____ to show inheritance relationships extend from the descendant class and point to the original class.

UML Diagram

Programmers and analysts sometimes use a graphical language called _____ to describe classes and object-oriented processes.

Unified Modeling Language

15) When an object, such as a String, is passed as an argument, it is: A) actually a reference to the object that is passed B) passed by value like any other parameter value C) encrypted D) necessary to know exactly how long the string is when writing the program

a

An exception (as distinct from an Exception) is:

a circumstance that a programmer chooses to handle outside the code that handles "normal cases"

45) This ArrayList class method is used to insert an item into an ArrayList.

add

9) If you are using a block of statements, don't forget to enclose all of the statements in a set of:

braces

A __________________exception is one that must be either caught and handled in a method or declared in a throws clause for the method.

checked

23) Given the following statement, which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");

diskOut.println("Calvin");

A(n) ________ is an object that is generated in memory as the result of an error or an unexpected event.

exception

T/F: 11) A sorting algorithm is used to locate a specific item in a larger collection of data.

f

The try statement may have an optional ____________ clause, which must appear after all of the catch clauses.

finally

22) This type of loop is ideal in situations where the exact number of iterations is known.

for loop

When you write a method that throws a checked exception, you must:

have a throws clause in the method header.

24) When using the PrintWriter class, which of the following import statements would you write near the top of your program?

import java.io.*;

12) ________ is the process of inspecting data given to the program by the user and determining if it is valid.

input validation

44) Which of the following is a valid declaration for a ragged array?

int[][] ragged = new int[5][];

51) The ArrayList class is in this package.

java.util

Employing inheritance reduces errors because _____.

many of the methods you need have already been used and tested

The ability to catch multiple types of exceptions with a single catch is known as ____________, and was introduced in Java 7.

multi catch

Write an expression whose value is a reference to a newly created Exception object with no specific message.

new Exception()

49) You use this method to determine the number of items stored in an ArrayList object.

numberItems

24) If numbers is a two-dimensional array, which of the following would give the length of row r?

numbers[r].length

If a programming language does not support ____, the language is not considered object-oriented.

polymorphism

In Java, using the same method name to indicate different implementations is called _____.

polymorphism

8) This type of loop will always be executed at least once.

post-test loop

You would like to make a member of a class visible in all subclasses regardless of what package they are in. Which one of the following keywords would achieve this?

protected

Which of the following statements will create a class named Red that is based on the class Color?

public class Red extends Color

25) Which of the following is a correct method header for receiving a two-dimensional array as an argument?

public static void passArray(int [][])

48) This ArrayList class method deletes an item from an ArrayList.

remove

20) This is a value that signals when the end of a list of values has been reached.

sentinel

47) You can use this ArrayList class method to replace an item at a specific location in an ArrayList.

set

20) Given that String[] str has been initialized, to get a copy of str[0] with all characters converted to upper case, use the following statement:

str[0].toUpperCase();

T/F: 1) Java provides a set of simple unary operators designed just for incrementing and decrementing variables.

t

T/F: 12) The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.

t

T/F: 14) An ArrayList object automatically expands in size to accommodate the items stored in it.

t

22) In order to do a binary search on an array:

the array must first be sorted in ascending order

33) When an individual element of an array is passed to a method:

the method does not have direct access to the original array

Write the clause needed to identify a method as one that may throw an IOException:

throws IOExecption

The RunTimeException class is the ancestor class of all ______________exceptions.

unchecked

13) This type of loop allows the user to decide the number of iterations.

user controlled loop

40) The binary search algorithm:

will cut the portion of the array being searched in half each time the loop fails to locate the search value

2) What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++;

x = 33, y = 9

31) What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; x = y; y = x;

x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}

12) You should not define a class field that is dependent upon the values of other class fields: A) in order to avoid having stale data B) because it is redundant C) because it should be defined in another class D) in order to keep it current

A

15) The scope of a private instance field is: A) the instance methods of the same class B) inside the class, but not inside any method C) inside the parentheses of a method header D) the method in which they are defined

A

17) Which of the following statements will create a reference, str, to the String, "Hello, World"? A) String str = "Hello, World"; B) string str = "Hello, World"; C) String str = new "Hello, World"; D) str = "Hello, World";

A

30) When an object is created, the attributes associated with the object are called: A) instance fields B) instance methods C) fixed attributes D) class instances

A

31) In the following code, Integer.parseInt(str), is an example of: int num; string str = "555"; num = Integer.parseInt(str) + 5; A) a value-returning method B) a void method C) a local variable D) a complex method

A

5) Subscript numbering always starts at what value? A) 0 B) 1 C) -1 D) None of the above

A

38) Instance methods do not have this key word in their headers: A) public B) static C) private D) protected

B

4) In the following code, System.out.println(num) is an example of: double num = 5.4; System.out.println(num); num = 0.0; A) a value-returning method B) a void method C) a complex method D) a local variable

B

40) A sentinel value ________ and signals that there are no more values to be entered. A) is a different data type than the values being processed B) is a special value that cannot be mistaken as a member of the list C) indicates the start of a list D) guards the list

B

40) This is a group of related classes. A) archive B) package C) collection D) attachment

B

41) This type of loop is ideal in situations where you always want the loop to iterate at least once. A) while loop B) do-while loop C) for loop D) if statement

B

42) Look at the following statement. import java.util.Scanner; This is an example of A) a wildcard import B) an explicit import C) unconditional import D) conditional import

B

45) You can use this method to determine whether a file exists. A) The Scanner class's exists method B) The File class's exists method C) The File class's canOpen method D) The PrintWriter class's fileExists method

B

8) Java allows you to create objects of this class in the same way you would create primitive variables. A) Random B) String C) PrintWriter D) Scanner

B

13) Given the following method header, which of the method calls would be an error? public void displayValues(int x, int y) A) displayValue(a,b); // where a is a short and b is a byte B) displayValue(a,b); // where a is an int and b is a byte C) displayValue(a,b); // where a is a short and b is a long D) They would all give an error.

C

26) After the header, the body of the method appears inside a set of: A) brackets, [] B) parentheses, () C) braces, {} D) double quotes, ""

C

26) Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached? A) while (inputFile != null) { ... } B) while (!inputFile.EOF) { ... } C) while (inputFile.hasNext()) { ... } D) while (inputFile.nextLine == " ") { ... }

C

27) In UML diagrams, this symbol indicates that a member is private: A) * B) # C) - D) +

C

10) Data hiding, which means that critical data stored inside the object is protected from code outside the object, is accomplished in Java by: A) using the public access specifier on the class methods B) using the private access specifier on the class methods C) using the private access specifier on the class definition D) using the private access specifier on the class fields

D

11) For the following code, which statement is NOT true? public class Sphere { private double radius; public double x; private double y; private double z; } A) x is available to code that is written outside the Circle class. B) radius is not available to code written outside the Circle class. C) radius, x, y, and z are called members of the Circle class. D) z is available to code that is written outside the Circle class.

D

13) What does the following UML diagram entry mean? + setHeight(h : double) : void A) this is a public attribute named Height and is a double data type B) this is a private method with no parameters and returns a double data type C) this is a private attribute named Height and is a double data type D) this is a public method with a parameter of data type double and does not return a value

D

15) When an array is passed to a method: A) a reference to the array is passed B) it is passed just as an object C) the method has direct access to the original array D) All of the above

D

28) In UML diagrams, this symbol indicates that a member is public. A) / B) @ C) - D) +

D

29) Breaking a program down into small manageable methods: A) makes problems more easily solved B) allows for code reuse C) simplifies programs D) all of the above

D

5) To create a method you must write its: A) header B) return type C) body D) definition

D

Under Windows, which of the following statements will open the file InputFile.txt that is in the root directory on the C: drive?

D. FileReader freader = new FileReader("C:\\InputFile.txt");

Look at the following code: FileInputStream fstream = new FileInputStream("MyInfo.dat"); DataInputStream inputFile = new DataInputStream(fstream); This code can also be written as:

DataInputStream inputFile = new DataInputStream(new FileInputStream("MyInfo.dat"));

T/F: 1) An array can hold multiple values of several different data types simultaneously.

F

T/F: 3) In the method header, the method modifier public means that the method belongs to the class, not a specific object.

F

T/F: 3) The do-while loop is a pre-test loop.

F

T/F: 4) In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value.

F

T/F: 5) When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.

F

T/F: 8) In a for statement, the control variable can only be incremented.

F

T/F: 8) In the method header the static method modifier means the method is available to code outside the class.

F

T/F: 8) Java limits the number of dimensions that an array may have to 15.

F

T/F: 9) If a[] and b[] are two integer arrays, the expression a == b compares the array contents.

F

The throw statement informs the compiler that a method throws one or more exception.

F

In versions of Java prior to Java 7, each catch clause can handle only one type of exception.

T

T/F: 1) Methods are commonly used to break a problem into small manageable pieces.

T

T/F: 10) A file must always be opened before using it and closed when the program is finished using it.

T

T/F: 10) No statement outside the method in which a parameter variable is declared can access the parameter by its name.

T

T/F: 10) Objects in an array are accessed with subscripts, just like any other data type in an array.

T

T/F: 11) The expression in a return statement can be any expression that has a value.

T

T/F: 11) When you open a file with the PrintWriter class, the class can potentially throw an IOException.

T

T/F: 12) A value-returning method can return a reference to a non-primitive type.

T

T/F: 4) Constants, variables, and the values of expressions may be passed as arguments to a method.

T

T/F: 4) Once an array is created, its size cannot be changed.

T

T/F: 5) A parameter variable's scope is the method in which the parameter is declared.

T

T/F: 5) When an array of objects is declared, but not initialized, the array values are set to null.

T

T/F: 6) A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.

T

When the code in a try block may throw more than one type of exception, you need to write a catch clause for each type of exception that could potentially be thrown.

T

T/F: 1) An object can store data.

TRUE

T/F: 10) Instance methods do not have the key word static in their headers.

TRUE

T/F: 13) The term "no-arg constructor" is applied to any constructor that does not accept arguments.

TRUE

14) In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number "); int number = keyboard.nextInt(); while (number < 100 && number > 500) { System.out.print("Enter another number "); number = keyboard.nextInt(); }

The boolean condition can never be true

34) What would be the results of the following code? 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]; }

This would cause the program to crash.

46) You can use this ArrayList class method to insert an item at a specific location in an ArrayList.

add

38) Which of the following for loops is valid, given the following declaration? String[] names = {"abc", "def", "ghi", "jkl"};

for (int i = 0; i < names.length; i++) System.out.println(names[i].length());

42) 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?

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

5) If a loop does not contain within itself a way to terminate, it is called a(n):

infinite loop

39) A search algorithm:

is a way to locate a specific item in a larger collection of data

11) When an argument is passed to a method: A) its value is copied into the method's parameter variable B) its value may be changed within the called method C) values may not be passed to methods D) the method must not assign another value to the parameter that receives the argument

A

12) What would be the results of the following code? int[] x = { 55, 33, 88, 22, 99, 11, 44, 66, 77 }; int a = 10; if(x[2] > x[5]) a = 5; else a = 8; A) a = 5 B) a = 8 C) a = 10 D) This is a compilation error, you cannot compare array elements.

A

18) When you pass an argument to a method, be sure that the argument's data type is compatible with: A) the parameter variable's data type B) the method's return type C) the version of Java currently being used D) IEEE standards

A

19) A parameter variable's scope is: A) the method in which the parameter is declared B) the class to which the method belongs C) the main method D) All of the above

A

23) In your textbook the general layout of a UML diagram is a box that is divided into three sections. The top section has the ________; the middle section holds ________; the bottom section holds ________. A) class name; attributes or fields; methods B) class name; object name; methods C) object name; attributes or fields; methods D) object name; methods; attributes or fields

A

24) What will be returned from the following method? public static double methodA() { double a = 8.5 + 9.5; return a; } A) 18.0 B) 18 (as an integer) C) 8 D) This is an error.

A

28) What will be the value of x after the following code is executed? int x, y = 15, z = 3; x = (y--) / (++z); A) 3 B) 4 C) 5 D) 6

A

3) This type of method performs a task and sends a value back to the code that called it. A) value-returning B) void C) complex D) local

A

34) You should always document a method by writing comments that appear: A) just before the method's definition B) just after the method's definition C) at the end of the file D) only if the method is more than five lines long

A

36) If you attempt to use a local variable before it has been given a value: A) a compiler error will occur B) the local variable will always contain the value 0 C) the results will be unpredictable D) the local variable will be ignored

A

41) Values stored in local variables: A) are lost between calls to the method in which they are declared B) retain their values from the last call to the method in which they are declared C) may be referenced by the calling method D) may be referenced by any other method, if the method in which they are declared is a public method

A

44) Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file? A) int number = inputFile.nextInt(); B) int number = inputFile.next(); C) int number = inputFile.readInt(); D) int number = inputFile.integer();

A

46) The process of breaking a problem down into smaller pieces is sometimes called: A) divide and conquer B) scientific method C) top-down programming D) whole-into-part

A

5) When you are working with a ________, you are using a storage location that holds a piece of data. A) primitive variable B) reference variable C) numeric literal D) binary number

A

6) By default, Java initializes array elements with what value? A) 0 B) 100 C) 1 D) -1

A

6) In the header, the method name is always followed by this: A) parentheses B) return type C) data type D) braces

A

37) For the following code, what would be the value of str[2]? String[] str = {"abc", "def", "ghi", "jkl"};

A reference to the String "ghi"

1) One or more objects may be created from a(n): A) field B) class C) method D) instance

B

1) This indicates the number of elements, or values, the array can hold. A) the new operator B) the array's size declarator C) the array's data type D) the version of Java

B

10) Values that are sent into a method are called: A) variables B) arguments C) literals D) types

B

13) What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; for(int a = 0; a < x.length; a++) { x[a] = y[a]; y[a] = x[a]; } A) x[] = {36, 78, 12, 24} and y[] = {23, 55, 83, 19} B) x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24} C) x[] = {23, 55, 83, 19} and y[] = {23, 55, 83, 19} D) This is a compilation error.

B

14) Methods that operate on an object's fields are called: A) instance variables B) instance methods C) public methods D) private methods

B

16) All @param tags in a method's documentation comment must: A) end with a */ B) appear after the general description of the method C) appear before the method header D) span several lines

B

16) What would be the results of the following code? int[] array1 = new int[25]; ... // Code that will put values in array1 int value = array1[0]; for (int a = 1; a < array1.length; a++) { if (array1[a] < value) 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) Value contains the average of the values in array1.

B

18) Two or more methods in a class may have the same name as long as: A) they have different return types B) they have different parameter lists C) they have different return types, but the same parameter list D) you cannot have two methods with the same name

B

22) Another term for an object of a class is: A) access specifier B) instance C) member D) method

B

25) In a @return tag statement the description: A) cannot be longer than one line B) describes the return value C) must be longer than one line D) describes the parameter values

B

26) When a method tests an argument and returns a true or false value, it should return: A) a zero for true and a one for false B) a boolean value C) a zero for false and a non-zero for true D) a method should not be used for this type test

B

29) In a UML diagram to indicate the data type of a variable enter: A) the variable name followed by the data type B) the variable name followed by a colon and the data type C) the class name followed by the variable name followed by the data type D) the data type followed by the variable name

B

31) When an object is passed as an argument to a method, what is passed into the method's parameter variable? A) the class name B) the object's memory address C) the values for each field D) the method names

B

34) What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x < 5); A) 10 B) 200 C) This is an infinite loop. D) The loop will not be executed, the initial value of x > 5.

B

35) When an argument value is passed to a method, the receiving parameter variable is: A) declared within the body of the method B) declared in the method header inside the parentheses C) declared in the calling method D) uses the declaration of the argument

B

36) A loop that executes as long as a particular condition exists is called a(n): A) sentinel loop B) conditional loop C) count-controlled loop D) infinite loop

B

8) Java performs ________, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for the array. A) active array sequencing B) array bounds checking C) scope resolution binding D) buffer overrun protection

B

8) Which of the following is NOT part of a method call? A) method name B) return type C) parentheses D) all of the above are part of a method call

B

12) What is wrong with the following method call? displayValue (double x); A) There is nothing wrong with the statement. B) displayValue will not accept a parameter. C) Do not include the data type in the method call. D) x should be a String.

C

14) What will be the value of x[1] after the following code is executed? int[] x = {22, 33, 44}; arrayProcess(x); ... public static void arrayProcess(int[] a) { for(int k = 0; k < 3; k++) { a[k] = a[k] + 5; } } A) 27 B) 33 C) 38 D) 49

C

14) Which of the following would be a valid method call for the following method? public static void showProduct (int num1, double num2) { int product; product = num1 * (int)num2; System.out.println("The product is " + product); } A) showProduct(5.5, 4.0); B) showProduct(10.0, 4); C) showProduct(10, 4.5); D) showProduct(33.0, 55.0);

C

16) A constructor: A) always accepts two arguments B) has return type of void C) has the same name as the class D) always has an access specifier of private

C

2) Class objects normally have ________ that perform useful operations on their data, but primitive variables do not. A) fields B) instances C) methods D) relationships

C

20) A class specifies the ________ and ________ that a particular type of object has. A) relationships; methods B) fields; object names C) fields; methods D) relationships; object names

C

23) The header of a value-returning method must specify this. A) The method's local variable names B) The name of the variable in the calling program that will receive the returned value C) The data type of the return value D) All of the above

C

27) The phrase divide and conquer is sometimes used to describe: A) the backbone of the scientific method B) the process of dividing functions C) the process of breaking a problem down into smaller pieces D) the process of using division to solve a mathematical problem

C

29) In all but rare cases, loops must contain within themselves: A) arithmetic statements B) if statements C) a way to terminate D) nested loops

C

3) It is common practice to use a ________ variable as a size declarator. A) static B) reference C) final D) boolean

C

30) Which of the following are pre-test loops? A) while, for, do-while B) while, do-while C) while, for D) for, do-while

C

33) Which of the following is included in a method call? A) return type B) method modifiers C) parentheses D) return variable

C

36) Given the following code, what will be the value of finalAmount when it is displayed? public class Order { private int orderNum; private double orderAmount; private double orderDiscount; public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public double finalOrderTotal() { return orderAmount - orderAmount * orderDiscount; } } public class CustomerOrder { public static void main(String[] args) { Order order; int orderNumber = 1234; double orderAmt = 580.00; double orderDisc = .1; order = new Order(orderNumber, orderAmt, orderDisc); double finalAmount = order.finalOrderTotal(); System.out.printf("Final order amount = $%,.2f\n", finalAmount); } } A) 528.00 B) 580.00 C) 522.00 D) There is no value because the object order has not been created.

C

37) A class's responsibilities include: A) the things a class is responsible for doing B) the things a class is responsible for knowing C) both A and B D) neither A nor B

C

37) What will be the result of the following code? int num; string str = "555"; num = Integer.parseInt(string str) + 5; A) num will be set to 560. B) str will have a value of "560". C) The last line of code will cause an error. D) Neither num or str will be changed.

C

38) Given the following method header, which of the method calls would be an error? public void displayValues(double x, int y) A) displayValue(a,b); // where a is a long and b is a byte B) displayValue(a,b); // where a is an int and b is a byte C) displayValue(a,b); // where a is a short and b is a long D) They would all give an error.

C

38) What will be printed after the following code is executed? for (int number = 5; number <= 15; number +=3) System.out.print(number + ", "); A) 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, B) 5, 8, 11, 14, 17, C) 5, 8, 11, 14, D) This is an invalid for statement.

C

40) When writing the documentation comments for a method, you can provide a description of each parameter by using a: A) @comment tag B) @doc tag C) @param tag D) @return tag

C

43) Which of the following will open a file named MyFile.txt and allow you to read data from it? A) File file = new File("MyFile.txt"); B) Scanner inputFile = new Scanner("MyFile.txt"); C) File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); D) PrintWriter inputFile = new PrintWriter("MyFile.txt");

C

45) To document the return value of a method, use this in a documentation comment. A) The @param tag B) The @comment tag C) The @return tag D) The @returnValue tag

C

47) Any method that calls a method with a throws clause in its header must: A) handle the potential exception B) have the same throws clause C) both of the above D) do nothing, the called program will take care of the throws clause

C

9) If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens? A) Control is returned to method A. B) Control is returned to method B. C) Control is returned to method C. D) The program terminates.

C

9) In Java, you do not use the new operator when you use a(n): A) array size declarator B) initialization list C) two-dimensional array D) all of the above Answer: B 10) What will be the results of the following code? final int ARRAY_SIZE = 5; double[] x = new double[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } A) All the values in the array are initialized to 10.0. B) All the values, except the first, are set to 10.0. C) An error will occur when the program runs. D) There will be a compilation error.

C

19) Given the following code, what will be the value of finalAmount when it is displayed? public class Order { private int orderNum; private double orderAmount; private double orderDiscount; public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public int getOrderAmount() { return orderAmount; } public int getOrderDisc() { return orderDisc; } } public class CustomerOrder { public static void main(String[] args) { int ordNum = 1234; double ordAmount = 580.00; double discountPer = .1; Order order; double finalAmount = order.getOrderAmount() — order.getOrderAmount() * order.getOrderDisc(); System.out.printf("Final order amount = $%,.2f\n", finalAmount); } } A) 528.00 B) 580.00 C) There is no value because the constructor has an error. D) There is no value because the object order has not been created.

D

2) What does the following statement do? double[] array1 = new double[10]; A) Declares array1 to be a reference to an array of double values B) Creates an instance of an array of 10 double values C) Will allow valid subscripts in the range of 0 - 9 D) All of the above

D

2) Which of the following is NOT a benefit derived from using methods in programming? A) Pproblems are more easily solved. B) simplifies programs C) code reuse D) All of the above are benefits.

D

20) The lifetime of a method's local variable is: A) the duration of the program B) the duration of the class to which the method belongs C) the duration of the method that called the local variable's method D) only while the method is executing

D

21) Local variables: A) are hidden from other methods B) may have the same name as local variables in other methods C) lose the values stored in them between calls to the method in which the variable is declared D) All of the above

D

21) This refers to the combining of data and code into a single object. A) Data hiding B) Abstraction C) Object D) Encapsulation

D

24) For the following code, which statement is NOT true? public class Circle { private double radius; public double x; private double y; } A) x is available to code that is written outside the Circle class. B) radius is not available to code written outside the Circle class. C) radius, x, and y are called members of the Circle class. D) y is available to code that is written outside the Circle class.

D

27) What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y--; A) x = 12, y = 5 B) x = 16, y = 4 C) x = 17, y = 5 D) x = 17, y = 4

D

27) Which of the statements are TRUE about the following code? final int ARRAY_SIZE = 10; long[] array1 = new long[ARRAY_SIZE]; A) Declares array1 to be a reference to an array of long values B) Creates an instance of an array of 10 long values C) Will allow valid subscripts in the range of 0 - 9 D) All of the above

D

28) In a general sense, a method is: A) a plan B) a statement inside a loop C) a comment D) a collection of statements that performs a specific task

D

31) What will be the value of x after the following code is executed? int x = 10; while (x < 100); { x += 10; } A) 90 B) 100 C) 110 D) This is an infinite loop.

D

32) A constructor is a method that: A) returns an object of the class. B) never receives any arguments. C) with the name ClassName.constructor. D) performs initialization or setup operations.

D

32) What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; y += 20; } A) 90 B) 110 C) 130 D) 210

D

32) Which of the following is NOT a part of the method header? A) return type B) method name C) parentheses D) semicolon

D

33) In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number "); int number = keyboard.nextInt(); while (number < 100 || number > 500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); } A) Numbers less than 100 B) Numbers greater than 500 C) Numbers in the range 100 - 499 D) Numbers in the range 100 - 500

D

33) The scope of a public instance field is: A) only the class in which it is defined B) inside the class, but not inside any method C) inside the parentheses of a method header D) the instance methods and methods outside the class

D

35) How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100); A) 1 B) 3 C) 4 D) 5

D

37) A for loop normally performs which of these steps? A) initializes a control variable to a starting value B) tests the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value C) updates the control variable during each iteration D) all of the above E) none of the above

D

39) Which of the following would be a valid method call for the following method? public static void showProduct(double num1, int num2) { double product; product = num1 * num2; System.out.println("The product is " + product); } A) showProduct("5", "40"); B) showProduct(10.0, 4.6); C) showProduct(10, 4.5); D) showProduct(3.3, 55);

D

9) A UML diagram does not contain: A) the class name B) the method names C) the field names D) object names

D

If your code does not handle and exception when it is thrown, this prints an error message and crashes the program.

Default exception handler

Which of the following choices is the best example of a parent class/child class relationship?

Dog/Poodle

If the code in a method can potentially throw a checked exception, then that method must:

Either A or B

An Exception-like class that represents conditions from which there is generally no reliable recovery is the _________ class.

Error

T/F: 9) Only constants and variables may be passed as arguments to methods.

F

T/F: 11) The term "default constructor" is applied to the first constructor written by the author of a class.

FALSE

T/F: 12) When a local variable in an instance method has the same name as an instance field, the instance field hides the local variable.

FALSE

T/F: 5) Instance methods should be declared static.

FALSE

T/F: 8) The public access specifier for a field indicates that the attribute may not be accessed by statements outside the class.

FALSE

T/F: 9) A method that gets a value from a class's field but does not change it is known as a mutator method.

FALSE

When a subclass method has the same name and argument types as a superclass method, the subclass method _____ the superclass method.

Overrides

Within a subclass, you cannot override _____ methods.

Static

____ polymorphism is the ability of one method to work appropriately for subclasses of the same parent class.

Subtype

If a method has been overridden but you want to use the superclass version within the subclass, you can use the keyword _____ to access the parent class method.

Super

If the class SerializedClass contains references to objects of other classes as fields, those classes must also implement the Serializable interface in order to be serialized.

T

T/F: 6) You must have a return statement in a value-returning method.

T

T/F: 7) Any method that calls a method with a throws clause in its header must either handle the potential exception or have the same throws clause.

T

T/F: 7) The do-while loop must be terminated with a semicolon.

T

T/F: 9) When the continue statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.

T

T/F; 6) You can use the PrintWriter class to open a file for writing and write data to it.

T

The call stack is an internal list of all the methods that are currently executing.

T

When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.

T

When deserializing an object using the readObject method, you must cast the return value to the desired class type.

T

T/F: 14) The java.lang package is automatically imported into all Java programs.

TRUE

T/F: 2) A class in not an object, but a description of an object

TRUE

T/F: 3) An access specifier indicates how the class may be accessed.

TRUE

T/F: 4) A method that stores a value in a class's field or in some other way changes the value of a field is known as a mutator method.

TRUE

T/F: 6) A constructor is a method that is automatically called when an object is created.

TRUE

T/F: 7) Shadowing is the term used to describe where the field name is hidden by the name of a local or parameter variable.

TRUE

26) A ragged array is:

a two-dimensional array where the rows are of different lengths

19) In memory, an array of String objects:

consists of elements, each of which is a reference to a String object

17) A loop that repeats a specific number of times is known as a(n):

counter controlled loop

Classes that inherit from the Error class are:.

for exceptions that are thrown when a critical error occurs, and the application program should not try to handle them.

An exception's default error message can be retrieved using this method.

getMessage()

The following catch statement can: catch (Exception e) {...}

handle all exceptions that are instances of the Exception class or a subclass of Exception.

If jrStudent is an object of Student, which of the following statements will result in a value of true?

jrStudent instanceof Student

When you create a class by making it inherit from another class, the new class automatically contains the data fields and _____ of the original class.

methods

43) Which of the following is a correct method header for receiving a two-dimensional array as an argument?

public static void passArray(int [][])

21) Before entering a loop to compute a running total, the program should first do this.

set the accumulator value to where the total value will be kept to an initial value, usually zero

If the program does not handle an unchecked exception:

the program is halted and the default exception handler handles the exception.

11) What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; }

this is an infinite loop

15) What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x > 5);.

this is an infinite loop

Write the clause needed to identify a method as one that may throw an IOException or a WidgetException:

throws IOException, WidgetException

21) The sequential search algorithm:

uses a loop to sequentially step through an array, starting with the first element

The IllegalArgumentException class extends the RuntimeException class, and is therefore:

an unchecked exception class

You can use the ____ modifier with methods when you don't want the method to be overridden.

final

In Java, the concept of keeping data private is known as _____.

information hiding

When an exception is thrown:

it must be handled by the program or by the default exception handler.

6) Each repetition of a loop is known as what?

iteration

4) This is a control structure that causes a statement or group of statements to repeat.

loop

7) This variable controls the number of times that the loop iterates.

loop control variable

A class named Building has a public, nonstatic method named getFloors(). If School is a child class of Building, and modelHigh is an object of type School, which of the following statements is valid?

modelHigh.getFloors();

If the only constructor in a superclass requires arguments, its subclass _____.

must contain a constructor

Which of the following statements depicts the valid format to call a superclass constructor from a subclass constructor?

super(name, score);

When an exception is thrown by code in the try block, the JVM begins searching the try statement for a catch clause that can handle it and passes control of the program to

the first catch clause that can handle the exception.

In a try/catch construct, after the catch statement is executed:

the program resumes at the statement that immediately follows the try/catch construct.

A way to discover which of two classes is the base class and which is the subclass is to _____.

try saying the two class names together in a sentence with the phrase "is a(n)

This is a section of code that gracefully responds to exceptions when they are thrown

Exception handler

You use the keyword ____ to achieve inheritance in Java.

Extends

A catch clause that uses a parameter variable of the Exception type is capable of catching any exception that extends the Error class.

F

The throws clause causes an exception to be thrown.

F

To read data from a binary file you create objects from the following classes:

FileInputStream and DataInputStream

To write data to a binary file you create objects from the following classes:

FileOutputStream and DataOutputStream

If you want to append data to the existing binary file, BinaryFile.dat, use the following statements to open the file.

FileOutputStream fstream = new FileOutputStream("BinaryFile.dat", true); DataOutputStream binaryOutputFile = new DataOutputStream(fstream);

When you want every child class to use the original parent class version of a method, use the _____ with the method.

Final

You use a _____ method access specifier when you create methods for which you want to prevent overriding in extended classes.

Final

When you employ ____, your data can be altered only by the methods you choose and only in ways that you can control.

Information hiding

____ is a mechanism that enables one class to acquire all the behaviors and attributes of another class.

Inheritance

A compiler can decide to _____ a final method—that is, determine the code of the method call when the program is compiled.

Inline

You are never aware that ____ is taking place; the compiler chooses to use this procedure to save the overhead of calling a method.

Inlining

Assume that the classes BlankISBN, NegativePrice, and NegativeNumberOrdered are exception classes that inherit from Exception. The following code is a constructor for the Book class. What must be true about any method that instantiates the Book class with this constructor? public Book(String ISBNOfBook, double priceOfBook, int numberOrderedOfBook) throws BlankISBN, NegativePrice, NegativeNumberOrdered { if (ISBNOfBook == "") throw new BlankISBN(); if (priceOfBook < 0) throw new NegativePrice(priceOfBook); if (numberedOrderedOfBook < 0) throw new NegativeNumberOrdered(numberOrderedv); ISBN = ISBNOfBook; price = priceOfBook; numberedOrdered = numberOrderedOfBook; }

It must handle all of the possible exceptions thrown by the constructor or have its own throws clause specifying them.

The ability to use inheritance in Java makes programs easier to write, ____, and more quickly understood.

Less error prone

What is demonstrated by the following code? try { (try block statements . . .) } catch(NumberFormatException | IOException ex) { respondToError(); }

Multi-catch, a catch clause that can handle more than one exception, beginning in Java 7

In the following code, assume that inputFile references a Scanner object that has been successfully used to open a file: double totalIncome = 0.0; while (inputFile.hasNext()) { try { totalIncome += inputFile.nextDouble(); } catch(InputMismatchException e) { System.out.println("Non-numeric data encountered " + "in the file."); inputFile.nextLine(); } finally { totalIncome = 35.5; } } What will be the value of totalIncome after the following values are read from the file? 2.5 8.5 3.0 5.5 abc 1.0

35.5

Which of the following statements is true?

A child class inherits from a parent class.

Given the following constructor code, which of the statements are true? public Book(String ISBNOfBook, double priceOfBook, int numberOrderedOfBook) { if (ISBNOfBook == "") throw new BlankISBN(); if (priceOfBook < 0) throw new NegativePrice(priceOfBook); if (numberedOrderedOfBook < 0) throw new NegativeNumberOrdered(numberOrderedv); ISBN = ISBNOfBook; price = priceOfBook; numberedOrdered = numberOrderedOfBook; }

All of the above

Why does the following code cause a compiler error? try { number = Integer.parseInt(str); } catch (IllegalArgumentException e) { System.out.println("Bad number format."); } catch (NumberFormatException e) { System.out.println(str + " is not a number."); }

Because NumberFormatException inherits from IllegalArgumentException. The code should handle NumberFormatException before IllegalArgumentException.

What will the following code display? String input = "99#7"; int number; try { number = Integer.parseInt(input); } catch(NumberFormatException ex) { number = 0; } catch(RuntimeException ex) { number = 1; } catch(Exception ex) { number = -1; }

C. 0

Unchecked exceptions are those that inherit from:

Error class or the RuntimeException class.

The numeric classes' "parse" methods all throw an exception of this type if the string being converted does not contain a convertible numeric value.

NumberFormatException

Sometimes the superclass data fields and methods are not entirely appropriate for the subclass objects; in these cases, you want to ____ the parent class members.

Override

When you instantiate an object that is a member of a subclass, the _____ constructor executes first.

Parent class

The methods in a subclass can use all of the data fields and methods that belong to its parent, with one exception: ____ members of the parent class are not accessible within a child class's methods.

Private

In Java, _____ members are those that can be used by a class and its descendants.

Protected

Using the keyword ____ provides you with an intermediate level of security between public and private access.

Protected

If a ____ method has the same name as a parent class method and you use the name with a child class object, the child method hides the original.

Static

You can use the keyword _______ within a method in a derived class to access an overridden method in a base class.

Super

A base class can also be called a _____.

Superclass

A class must implement the Serializable interface in order for objects of the class to be serialized.

T

Beginning in Java 7, multi-catch can reduce a lot of duplicated code in a try statement that needs to catch multiple exceptions, but perform the same operation for each one.

T

When an object is serialized, it is converted into a series of bytes that contain the object's data.

T

What will be the result of the following statements? FileInputStream fstream = new FileInputStream("DataIn.dat"); DataInputStream inFile = new DataInputStream(fstream);

The inFile variable will reference an object that is able to read binary data from the Input.dat file.

All exceptions are instances of classes that extend this class

Throwable


Set pelajaran terkait

Info Sec Chapter 10 Implementing Security

View Set

EverFi: Repaying Loans Responsibly

View Set

Lewis Chapter 59 Chronic Neurologic Problems Questions ( Test 6)

View Set

Unit 2 Quiz 7 - Blood Vessels of the Upper Limb

View Set

Reproduction & Development FINISH AND Switch to NOTION

View Set

Question 8, Question 15, Question 16

View Set

chapter 1 intro to global business mangement

View Set

Iggy Chapter 62: Care of Patients With Pituitary and Adrenal Gland Problems

View Set

Thomas Jefferson (3rd President)

View Set