Intro to Computer Science, Chapter 1-8 Multiple choice Answers

A Java program is best classified as

software

6 bits can be used to represent ________ distinct items or values

64

When executing a program, the processor reads each program instruction from

main memory

Which memory capacity is the largest?

12,000,000 megabytes

Binary numbers are composed entirely of

0s and 1s

Volatility is a property of

0s, 1s and 2s

The ability to directly obtain a stored item by referencing its address is known as

random access

Which phase of the fetch-decode-execute cycle might use a circuit in the arithmetic-logic unit?

execute

For a computer to be accessible over a computer network, the computer needs its own

network address

For a computer to communicate over the Internet, it must use

the combined TCP/IP protocol

A URL (Universal Resource Locator) specifies the address of

the TCP protocol

It is important to dissect a problem into manageable pieces before trying to solve the problem because

most problems are too complex to be solved as a single, large activity

Once we have implemented the solution, we are not done with the problem because

all of the above

Java is an example of a(n)

both C and D

In the following list, which statement is not true regarding Java as a programming language?

It is a language whose programs do not require translating into machine language before they are executed

Comments should

be insightful and explain what the instruction's intention is

The main method for a Java program is defined by

public static main(String[ ] args)

The line of Java code "// System.out.println("Hello");" will

do nothing

The instruction: System.out.println("Hello World"); might best be commented as

// used to demonstrate an output message

Which character below is not allowed in an identifier?

^

Which of the following is not syntactically legal in Java?

s t a t i c main(String[ ] args)

Which of the following is a legal Java identifier

i

A unique aspect of Java that allows code compiled on one machine to be executed on a machine of a different hardware platform is Java's

bytecodes

Java is similar in syntax to what other high level language?

c++

An error in a program that results in the program outputting $100 instead of the correct answer, $250 is

a logical error

Which of the following is true regarding Java syntax and semantics?

a Java compiler can determine if you have followed proper syntax but not proper semantics

Following Java naming convention, which of the following would be the best name for a class about store customers

StoreCustomer

Which of the following would be a good variable name for the current value of a stock?

currentStockVal

Which of the following is a legal Java identifier?

oneForAll

Which of the following characters does not need to have an associated "closing" character in a Java program?

<

Mistyping "println" as "printn" will result in

A syntat error

System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night"); This statement will output ________ lines of text

2

If you want to output the text "hi there", including the quote marks, which of the following could do that?

System.out.println("\"hi there\"");

The word println is a(n)

Method

A Java variable is the name of a

data value stored in memory that can change its value but cannot change its type during the program's execution

Of the following types, which one cannot store a numeric value?

char

What value will z have if we execute the following assignment statement? float z = 5 / 10;

z will equal 0.0

A cast is required in which of the following situations?

storing a float in an int

If x is an int and y is a float, all of the following are legal except which assignment statement?

x=y;

Given the following assignment statement, which of the following answers is true regarding the order that the operators will be applied based on operator precedence?

+,*,/,-

What will be the result of the following assignment statement? Assume b = 5 and c = 10. Int a= b * (-c+2)\2

-20

Which of the following is true regarding the mod operator, %?

It can be performed on any numeric values, and the result always is numeric

Assume that x, y, and z are all integers (int) equal to 50, 20, and 6 respectively. What is the result of x / y / z?

0

What is output with the statement System.out.println(x+y); if x and y are int values where x=10 and y=5?

15

What is output with the statement System.out.println(""+x+y); if x and y are int values where x=10 and y=5?

15

If you want to store into the String name the value "George Bush", you would do which statement?

Any of the above would work

Consider having three String variables a, b, and c. The statement c = a + b; also can be achieved by saying

c = b.concat(b);

If the String major = "Computer Science", what is returned by major.charAt(1)?

'o'

Which of the following would return the last character of the String x?

x.charAt(x.length( )-1);

Suppose that String name = "Frank Zappa". What will the instruction name.toUpperCase( ).replace('A', 'I'); return?

FRINK ZIPPI

Which library package would you import to use NumberFormat and DecimalFormat?

java.text

Which library package would you import to use the class Random

java.util

The Random class has a method nextFloat( ) which returns a random float value between

0 and 1

If you want to output a double so that at least 1 digit appears to the left side of the decimal point and exactly 1 digit appears to the right side of the decimal point, which pattern would you give a DecimalFormat variable when you instantiate it?

0.0

Consider the double value likelihood = 0.013885. What would be output if DecimalFormat dformatter = DecimalFormat("0.00##"); and you execute System.out.println(df.format(likelihood));

0.0139

Using getCurrencyInstance( ) formats a variable, automatically inserting

all three

What value will z have if we execute the following assignment statement? int z = 50 / 10.00;

none of the above, a run-time error arises because z is an int and 50 / 10.00 is not

Since you cannot take the square root of a negative number, you might use which of the following instructions to find the square root of the variable x?

Math.sqrt(Math.abs(x));

Assume that x is a double that stores 0.362491. To output this value as 36%, you could use the NumberFormat class with NumberFormat nf = NumberFormat.getPercentInstance( ); Which of the following statements then would output x as 36%?

System.out.println(nf.format(x))

If a, b, and c are int variables with a = 5, b = 7, c = 12, then the statement int z = (a * b - c) / a; will result in z equal to

4

Java is a strongly typed language. What is meant by "strongly typed"?

Every variable has a single type associated with it throughout its existence in the program, and the variable can only store values of that type

In java a variable may contain

a value or reference

If two variables contain aliases of the same object then

the object may be modified using either alias and the object will become an "orphan" if both variables are set to null

Which properties are true of String objects?

Their lengths never change and the shortest string has zero length

What happens if you attempt to use a variable before it has been initialized

A syntax error may be generated by the compiler and A runtime error may occur during execution

What is the function of the dot operator?

It allows one to invoke a method within an object when given a reference to the object and It is used to terminate commands (much as a period terminates a sentence in English)

In Java, "instantiation" means

creating a new object of the class

In the StringMutation program shown in Listing 3.1, if phrase is initialized to "Einstein" what will Mutation #3 yield if Mutation #1: mutation1 = phrase.concat(".")?

XINSTXIN

Say you write a program that makes use of the Random class, but you fail to include an import statement for java.util.Random (or java.util.*). What will happen when you attempt to compile and run your program.

The program won't compile-you'll receive a syntax error about the missing class.

Which of the following will yield a pseudorandom number in the range [ -5, +5 ) given the following:

gen.nextFloat( ) * 10 - 5

What will be displayed by this command: System.out.println(Math.pow(3, 3-1));

9

What can you say about s1 and s2? String s1 = "testing" + "123"; String s2 = new String("testing 123");

s1 and s2 are both references to different String objects

An "alias" is when

two different reference variables refer to the same physical object

The String class' compreTo method

yields zero if the two strings are identical

Given the following code fragment: String strA = "aBcDeFg";String strB = strA.toLowerCase( );strB = strB.toUpperCase( );String strC = strA.toUpperCase( );

strB.compareTo(strC) would yield 0

An API is

an Application Programming Interface

The advantage(s) of the Random class' pseudo-random number generators, compared to the Math.random method, is that

all but, the generators in Random are more efficient than the one in Math.

Java.text's NumberFormat class includes methods that

allow you to format currency and allow you to format percentages and round their display during the formatting process

The advantages of the DecimalFormat class compared with the NumberFormat class include

precise control over the number of digits to be displayed and control over the presence of a leading zero

The behavior of an object is defined by the object's

Methods

The relationship between a class and an object is best described as

objects are instances of classes

To define a class that will represent a car, which of the following definitions is most appropriate

public class Car

Which of the following reserved words in Java is used to create an instance of a class?

new

In order to preserve encapsulation of an object, we would do all of the following except for which one

make the class finale

If a method does not have a return statement, then

It must be a void method

Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution?

m2

A variable whose scope is restricted to the method where it was declared is known as a(n)

local variable

A class' constructor usually defines

parameter

Having multiple class methods of the same name where each method has a different number of or type of parameters is known as

method over overloading

Instance data for a Java class

may be primitive types or objects

An example of passing message to a String where the message has a String parameter would occur in which of the following messages?

equals

Consider a Rational class designed to represent rational numbers as a pair of int's, along with methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common divisor of two int's), as well as methods for addition, subtraction,

Because they will only be called from methods inside of Rational

Consider a method defined with the header: public void foo(int a, int b). Which of the following method calls is legal?

foo(0 / 1, 2 * 3);

Consider a method defined with the header: public void doublefoo(double x). Which of the following method calls is legal?

doublefoo(0); and doublefoo(0.555); and doublefoo(0.1 + 0.2);

In a UML diagram for a class

all of the above

Visibility modifiers include

public, private, protected

The expressions that are passed to a method in an invocation are called

actual parameters

What happens if you declare a class constructor to have a void return type?

You'll likely receive a syntax error

The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as

flow of control

Of the following if statements, which one correctly executes three instructions if the condition is true?

if (x < 0){a = b * 2; y = x; z = a - y;}

Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0?

if (x > 0) x++; else if (x < 0) x--;

Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score.

if (score >= 90) grade = 'A'; if (score >= 80) grade = 'B'; if (score >= 70) grade = 'C'; if (score >= 60) grade = 'D'; else grade = 'F'; D

Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following if (count != 0 && total / count > max) max = total / count;

The condition short circuits and the assignment statement is not executed

What is wrong, logically, with the following code? if (x > 10) System.out.println("Large"); else if (x > 6 && x <= 10) System.out.println("Medium"); else if (x > 3 && x <= 6) System.out.println("Small"); else System.out.println("Very small");

There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional

Consider the following outline of a nested if-else structure which has more if clauses than else clauses. Which of the statements below is true regarding this structure? if (condition1) if (condition2) statement1; else statement2;

statement2 will only execute if condition1 is false and condition2 is false

Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A', and examine the following conditions: Condition 1: (x < y && x > 0) Condition 2: (a != d

x != 5) Condition 3: !(true && false) Condition 4: (x > y || a == 'A' || d != 'A') || Conditions 2, 3 and 4 are all true, Condition 1 is not

The break statement does which of the following?

transfers control out of the current control structure such as a loop or switch statement

If a break occurs within the innermost loop of a nested loop that is three levels deep

when the break is encountered just the innermost loop is "broken

Every Interactor

has a hasNext() method

If x is an int where x = 1, what will x be after the following loop terminates? while (x < 100) x *= 2;

128

If x is an int where x = 0, what will x be after the following loop terminates? while (x < 100) x *= 2;

none of the above

How many times will the following loop iterate? int x = 10; while (x > 0) {System.out.println(x); x--;}

10 times

You might choose to use a switch statement instead of nested if-else statements if

the variable being tested might equal one of only a few int values

If a switch statement is written that contains no break statements whatsoever

none of the above

A continue statement

may be used within any Java loop statement

The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as

y = (x < 0) ? x : 0;

Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates? for (int i=0;i<5;i++) x += i;

10

The do loop differs from the while loop in that

the do loop will always execute the body of the loop at least once

How many times will the following loop iterate? int x = 10; do {System.out.println(x); x--;} while (x > 0);

11 times

Given that s is a String, what does the following loop do? for (int j = s.length( ); j > 0; j--) System.out.print(s.charAt(j-1));

Prints S out backwords

The following nested loop structure will execute the inner most statement (x++) how many times? for (int j = 0; j < 100; j++) for (int k = 100; k > 0; k--) x++;

10,000

Which of the following statements are true about Java loops?

all three loop statements are functionally equivalent

During program development, software requirements specify

what the task is that the program must perform

Once we have implemented the solution, we are not done with the problem

all of the above

Of the various phases in software development, which of the following is usually the lengthiest?

maintenance

A bad programming habit is to build an initial program and then spend a great deal of time modifying the code until it is acceptable. This is known as

The build-fix approach

The activities of the development cycle are generally thought to

Overlap

The idea of having programmers and developers meet in order to critique a software design or implementation is known as

A walkthrough

Modifying a program in order to eliminate deficiencies is done in the ________ phase of the development cycle

Maintenance

It is easier to correct errors found in a program if

they are identified early in the development cycle

In general, spending more time in development to ensure better software will

greatly reduce maintenance efforts

In order to create a constant, you would use which of the following Java reserved words?

final

Which of the following methods is a static method? The class in which the method is defined is given in parentheses following the method name.

sqrt(Math)

Static methods cannot

reference non-static instance data

An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?

this

Having multiple class methods of the same name where each method has a different number of or type of parameters is known as

method overloading

It is important to dissect a problem into manageable pieces before trying to solve the problem because

most problems are too complex to be solved as a single, large activity

The goal of testing is to

find logical and run-time errors

In which phase of program development would you expect the programmer(s) to create the pseudocode?

Software design

In which phase of program development would you expect the programmer(s) to determine the classes and objects needed?

software design

If a programmer follows the four phases of program development as intended, which of the four phases should require the least amount of creativity?

software implementation

int array that is currently filled to capacity, with the following values: 9, 4, 12, 2, 6, 8, 18

2

int array that is currently filled to capacity, with the following values: 9, 4, 12, 2, 6, 8, 18

7

Which of the following loops would adequately add 1 to each element stored in values?

for (j=0; j<values.length; j++) values[j]++;

The statement System.out.println(values[7]); will

cause an ArrayOutOfBoundsException to be thrown

Which of the following is a legal way to declare and instantiate an array of 10 Strings?

String[ ] s = new String[10];

In Java, arrays are

objects

The "off-by-one" error associated with arrays arises because

the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far

An int array stores the following values 9, 4, 12, 2, 6, 8, 18 Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?

2, 4, 12, 9, 6, 8, 18

an int array stores the following values 9, 4, 12, 2, 6, 8, 18 Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm?

2, 4, 12, 9, 6, 8, 18

An int array stores the following values 9, 4, 12, 2, 6, 8, 18 Which of the following lists of numbers would accurately show the array after the fourth pass of the Selection Sort algorithm?

2, 4, 6, 8, 12, 9, 18

An int array stores the following values 9, 4, 12, 2, 6, 8, 18 How many passes will it take in all for Selection Sort to sort this array?

6

For the questions below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all. What does the following code do? Scanner scan

adds value2 to the number of bars sold by child value1

For the questions below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all. Which of the following code could be used to

for (int j=0; j<12; j++) sum += candy[j];

For the questions below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all. What does the following method do? public int

It returns the index of the child who sold the most candy bars

We compare sorting algorithms by examining

the number of instructions executed by the sorting algorithm

Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr?

It stores 5 elements with legal indices between 0 and 4

If an int array is passed as a parameter to a method, which of the following would adequately define the parameter list for the method header?

(int[ ] a)

If int[ ] x = new int[15]; and the statement x[-1] = 0; is executed, then which of the following Exceptions is thrown?

ArrayIndexOutOfBoundsException

Assume that BankAccount is a predefined class and that the declaration BankAccount[ ] firstEmpireBank; has already been performed. Then the following instruction reserves memory space for firstEmpireBank = new BankAccount[1000];

1000 reference variables, each of which point to a single BankAccount entry

The following code accomplishes which of the tasks written below? Assume list is an int array that stores positive int values only. int foo = 0; for (int j =0 ; j < list.length; j++) if (list[j] > foo) foo = list[j];

It stores the largest value in list (the maximum) in foo

If x is a char, and values is an int array, then values[x]

casts x as an int based on x's ASCII value (for instance, if x is 'a' then it uses 97 and if x is 'z' then it uses 122)

Given the following declarations, which of the following variables are arrays? int[ ] a, b; int c, d[ ];

A,B,D

If a and b are both int arrays, then a = b; will

Create an alias

The statement int[ ] list = {5, 10, 15, 20};

initializes list to have 4 int values

To initialize a String array names to store the three Strings "Huey", "Duey" and "Louie", you would do

String[ ] names = {"Huey", "Duey", "Louie"};