Mid-Term study guide chapter 1 Multiple Choice Questions



Download 325.47 Kb.
Page1/4
Date31.01.2017
Size325.47 Kb.
#13210
  1   2   3   4
Mid-Term STUDY GUIDE

Chapter 1

Multiple Choice Questions:

1) A Java program is best classified as

a) hardware

b) software

c) storage

d) processor

e) input

Answer: b. Explanation: Programs are classified as software to differentiate them from the mechanisms of

the computer (hardware). Storage and the processor are two forms of hardware while input is the information that the program processes.
2) 6 bits can be used to represent ___ distinct items or values

a) 6


b) 20

c) 24


d) 32

e) 64


Answer: e. Explanation: With n bits, we can represent 2n different values. 26 = 64.
3) When executing a program, the processor reads each program instruction from

a) secondary memory (storage)

b) the Internet

c) registers stored in the processor

d) main memory

e) could be any of these

Answer: d. Explanation: The program is first loaded from secondary memory into main memory before it is executed so that the processor is not slowed down when reading each instruction. This idea of executing programs stored in memory is called the Stored Program Computer and was pioneered by Von Neumann in the 1940s.
4) Which memory capacity is the largest?

a) 1,500,000,000,000 bytes

b) 100 gigabytes

c) 3,500,000 kilobytes

d) 10 terabyte

e) 12,000,000 megabytes

Answer: e. Explanation: We convert each of these capacities to bytes (rounding off) to compare them. The

value in a remains the same, 1 ½ trillion bytes. The value in b is 100 billion bytes. The value in c is 3 ½

billion bytes. The value in d is 10 trillion bytes. The answer in e is 12 trillion bytes.
5) Volatility is a property of

a) RAM


b) ROM

c) disk


d) software

e) computer networks

Answer: a. Explanation: Volatility means that the contents of memory are lost if the electrical power isshut

off. This is true of RAM, but not ROM or disk. Software and computer networks are not forms of memory.


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

a) random access

b) sequential access

c) read-only access

d) fetch access

e) volatility

Answer: a. Random access is meant to convey that accessing any item is equally easy, and that any item is

retrievable based solely on its address. Random access is the form of access used by both RAM and ROM

memory. Disk access, called direct access, is a similar idea and direct and random access are sometimes

referred to synonymously. Sequential access is used by tape.


7) In order for a computer to be accessible over a computer network, the computer needs its own

a) MODEM


b) Communication line

c) Network address

d) Packet

e) Router

Answer: c. Explanation: In order to differentiate between the computers on a network, each is given its own,

unique, network address. In this way, a message intended for one computer can be recognized by that

computer through the message’s destination address. A MODEM is a device that is used to allow a computer

to communicate to another computer over the telephone line. The communication line is the network media

itself. A packet is a piece of information being sent from over a network. A router is a hardware device used

to take a message from one network and move it to another based on the message’s destination address.


8) A URL (Universal Resource Locator) specifies the address of

a) A computer on any network

b) A computer on the Internet

c) A local area network (LAN) on the Internet

d) A document or other type of file on the Internet

e) A java program on the Internet

Answer: d. URLs are used to locate documents (or other types of files such as an image or sound file)

anywhere on the Internet. The URL contains the address of the LAN or WAN and the specific computer from

which the file is to be retrieved, but specifies the file’s address, not just the computer’s address.
9) It is important to dissect a problem into manageable pieces before trying to solve the problem because

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

b) most problems are solved by multiple people and it is easy to assign each piece to a separate person

c) it is easier to integrate small pieces of a program into one program than it is to integrate one big chunk

of code into one program

d) our first solution may not solve the problem correctly

e) all of the above

Answer: a. Any interesting problem will be too complex to solve easily as a single activity. By decomposing

the problem, we can build small solutions to each piece and then integrate the pieces. Answer d is true, but is

not the reason why we will break a problem down into pieces.


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

a) the solution may not be the best (most efficient)

b) the solution may have errors and need testing and fixing before we are done

c) the solution may, at a later date, need revising to handle new specifications

d) the solution may, at a later date, need revising because of new programming language features

e) all of the above

Answer: e. Explanation: A program should not be considered as a finished product until we are reasonably

assured that it is efficient and error-free. Further, it is common that programs require modification in the future

because of a change to specifications or a change to the language or computer running the program.
11) Of the following list, which one is not true regarding Java as a programming language?

Lewis/Loftus/Cocking: Chapter 1 Test Bank TB 3

a) It is a relatively recent language, having been introduced in 1995

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

executed

c) It is an object-oriented programming language

d) It is a language that embraces the idea of writing programs to be executed using the World Wide Web

e) All of the above are true

Answer: b. Explanation: All languages require translation into machine language. The other statements are

all true about Java.

12) Comments should

a) rephrase the code it explains in English

b) be insightful and explain what the instruction's intention is

c) only be included in code that is difficult to understand

d) be used to define variables whose names are not easy to understand

e) all of the above

Answer: b. Explanation: One might answer e, but that then includes a and c, making “all of the above”

incorrect. Comments should not rephrase in English what an instruction says, but instead should explain what

that instruction is doing in relation to the program. Introductory programmers often have difficult explaining

their code and wind up stating the obvious in their comments. While answer d is partially correct, it is not

entirely true, all variables should have comments that explain their use.
13) The main method for a Java program is defined by

a) public static void main( )

b) public static void main(String[ ] args);

c) public static void main(String[ ] args)

d) private static void main(String[ ] args)

e) the main method could be defined as in a, c or d but not b

Answer: c. Explanation: In a, the missing parameter is not allowed. The parameters are defined later in the

text, but in affect, they allow the user to run the program and include some initial arguments if the program

calls for it. In b, the semicolon at the end of the statement is not allowed. In d, “private” instead of “public”

would make the program unexecutable by anyone and thus makes the definition meaningless.


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

a) // prints "Hello World" to the screen

b) // prints a message

c) // used to demonstrate an output message

d) //

e) // meaningless instruction



Answer: c. Explanation: Comments in a and b state the obvious while the comments in d and e are

meaningless. The comment in c explains why the instruction appears in the program.


15) Which character below is not allowed in an identifier?

a) $


b) _

c) 0 (zero)

d) q

e) ^


Answer: e. Explanation: Java identifiers can consist of any letter, digit, $ or _ as long as the identifier starts

with a letter or _. ^ is not a legal character.


16) Which of the following would not be syntactically legal in Java?

a) public class Foo

b) System.out.println("Hi");

c) { }


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

e) only b is legally valid, all of the rest are illegal

Answer: d. Explanation: The Java compiler would not recognize “s t a t i c” as “static” because the Java

compiler treats white space (blanks) as separators between entities. The other statements are all legal,

including “{ }” which is a block that happens to have no statements inside of it.
17) Which of the following would be a legal Java identifier?

a) i


b) class

c) ilikeclass!

d) idon'tlikeclass

e) i-like-class

Answer: a. Explanation: Java identifiers cannot have the characters “!”, “'” or “-” in them making answer c, d

and e wrong. The word “class” is a reserved word in Java and cannot be used as an identifier. The identifier

“i” is perfectly legal although not necessarily a good identifier since it is not descriptive of its use.

18) Which of the following stores data in binary format?

a) ROM

b) RAM


c) a hard disk

d) a CD


e) all of the above

Answer: e. Explanation: All information in a computer is stored in binary, that is, using 0s and 1s.


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

a) a programmer error

b) a syntax error

c) a run-time error

d) a logical error

e) a snafu

Answer: d. Explanation: While this is an error (answer a), programmers classify the type of error in order to

more easily solve the problem. Syntax errors are caught by the compiler and the program cannot run without

fixing all syntax errors. Run-time errors arise during program execution and cause the program to stop running

abnormally. Logical errors are errors whereby the program can run to completion, but gives the wrong answer.

If the result should have been $250, then the logic of the program is wrong since it output $100. A snafu is a

term expressing a messed up situation in combat and should not be used by respectable programmers!


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

customers?

a) StoreCustomer

b) Store Customer

c) storeCustomer

d) STORE_CUSTOMER

e) Store-Customer

Answer: a. Explanation: The Java naming convention states that classes should all start with an upper case

letter and that multiple-word names should start each new name with an upper case letter while the remaining

characters are lower case. Words should either be connected together without spaces, or connected with the

“_” character. Answers b and e are not legal names, and using Java naming convention, c would qualify as a

variable name and d would qualify as a constant.


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

a) curstoval

b) theCurrentValueOfThisStockIs

Lewis/Loftus/Cocking: Chapter 1 Test Bank TB 5

c) currentStockVal

d) csv


e) current

Answer: c. Explanation: Java allows long variable names but the programmer must find a good compromise

between an excessive long name (as with b) and names too short to understand their use (a and d). The name

current might be reasonable if there are no other “current” values being referenced in the program.


22) Which of the following is a legal Java identifier?

a) 1ForAll

b) oneForAll

c) one/4/all

d) 1_4_all

e) 1forall

Answer: b. Explanation: Java identifiers cannot start with a number (so the answers in a, d and e are illegal)

and cannot include the “/” character, so the answer in c is illegal.


23) A color image is broken down into individual pixels (points), each of which is represented by

a) a 1 for white and a 0 for black

b) 3 values denoting the shade of red, green and blue in the image

c) a single number indicating the intensity of color between white and black

d) two numbers, a value that denotes where between white and black the color is, and a brightness

e) none of the above, it is not possible to represent a color image.

Answer: b. Explanation: Black and white images are stored using 0s and 1s while color images are stored

using three values, one each for the degree of red, the degree of blue, and the degree of green.

24) Which of the following characters does not need to have an associated “closing” character in a Java program?

a) {


b) (

c) [


d) <

e) all of these require closing characters

Answer: d. Explanation: { is used to open a block, and so } is needed to close the block. ( is used to open an

expression and so ) is needed to close an expression. [ is used to start an array index so ] is needed to close the

array index. < is “less than” and > is “greater than” and these are not needed together, so < requires no closing

character.


25) Forgetting a semicolon will cause

a) a syntax error

b) a run-time error

c) a logical error

d) no error at all

e) converting the statement into a comment

Answer: a. Explanation: If a semicolon is missing where one should be present, the compiler will generate a

syntax error.


Chapter 2: Objects and Primitive Data

Multiple Choice Questions:

Use the following class definition to answer questions 1-4.

public class Questions1_4

{

public static void main(String[ ] args)



{

System.out.print("Here");

System.out.println("There " + "Everywhere");

System.out.println("But not" + "in Texas");

}

}

1) The program will print the word "Here" and then print



a) "There Everywhere" on the line after "Here"

b) "There" on the line after "Here" and "Everywhere" on the line after "There"

c) "There Everywhere" on the same line as "Here"

d) "ThereEverywhere" on the same line as "Here"

e) "ThereEverywhere" on the line after "Here"

Answer: c. Explanation: System.out.print will output the word "Here" but will leave the cursor at that point rather

than starting a new line. The next statement will output "There Everywhere" immediately after the word "Here".

Since there is a blank space within the quote marks for "There", there is a blank space inserted between "There" and

"Everywhere".
2) The final println command will output

a) "But not in Texas"

b) "But notin Texas"

c) "But not" on one line and "in Texas" on the next line

d) "But not+in Texas"

e) "But not + in Texas"

Answer: b. Explanation: The “+” performs String concatenation, so that "But not" and "in Texas" are

concatenated together. Notice that there is no blank space after "not" or before "in" so that when they are

concatenated, they are placed together without a blank space.
3) How many lines of output are provided by this program?

a) 1


b) 2

c) 3


d) 4

e) 5


Answer: b. Explanation: There will be one line of output for the first two statements combined because the print

statement does not return the cursor to start a new line. And since the second statement is a println, it returns the

cursor and the last println outputs its message on a separate line.

4) A reasonable comment for this program might be

a) // a program that demonstrates the differences between print, println and how + works

b) // a program that outputs a message about Texas

c) // a program that demonstrates nothing at all

d) // a program that outputs the message “Here There Everywhere But not in Texas”

e) // a program that has three output statements in it

Answer: a. Explanation: Remember that comments should not state the obvious (ruling out d and e) but instead

should explain what the program is doing or why. This program demonstrates print and println and +.
5) Consider the following statement:

System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night");

This statement will output ___ lines of text

a) 1


b) 2

c) 3


d) 4

e) 5


Answer: b. Explanation: The \t escape sequence inserts a tab, but leaves the cursor on the same line. The \n

escape sequence causes a new line to be produced so that “4 dinner” is output on the next line. The escape

sequence \r causes the carriage to return (that is, the cursor to be moved back to the left margin) but because it does

not start a new line, “2night” is output over “4 dinn” resulting in a second line that looks like “2nighter”.


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

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

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

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

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

e) none, it is not possible to output a quote mark because it is used to mark the beginning and ending of

the String to be output.

Answer: d. Explanation: \" is an escape sequence used to place a quote mark in a String, so it is used here to

output the quote marks with the rest of the String.
7) Of the following types, which one cannot store a numeric value?

a) int


b) double

c) char


d) all of these can store numeric values

e) none of these can store numeric values

Answer: c. Explanation: int is used to store whole numbers (integers) and double is used to store a real or floating

point value (value with a decimal point). A char stores a single character including letters, punctuation marks and

digits. However, storing the numeric digit ‘5’ is not the same as storing the number 5.
8) What value will z have if we execute the following assignment statement?

double z = 5 / 10;

a) z will equal 0.0

b) z will equal 0.5

c) z will equal 5.0

d) z will equal 0.05

e) none of the above, a run-time error arises because z is a double and 5 / 10 is an int

Answer: a. Explanation: 5 and 10 are both int values, so 5 / 10 is an integer division. The result is 0. Even

though z is a double and can store the real answer, 0.5, it only gets 0 because of the integer division. In order to get

0.5, we would have to first cast 5 or 10 as a double.


9) What value will z have if we execute the following assignment statement?

int z = 50 / 10.00;

a) 5

b) 5.0


c) 50

d) 10


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

Answer: e. Explanation: Because 10.00 is not an int, the division produces a double precision value which cannot

be stored in the int z. For this to work, the result of the division must be cast as an int before being stored in z, or

the value 10.00 would have to first be cast as an int before the division takes place.


10) A cast is required in which of the following situations?

a) using charAt to take an element of a String and store it in a char

b) storing an int in a double

c) storing a double in a double

d) storing a double in an int

e) all of the above require casts

Answer: d. Explanation: For a, charAt returns a char, so there is no problem. In b, the situation is a widening

operation taking a narrower type and storing the value in a wider type. Only in d is there a situation where a wider

type is being stored in a narrower type, so a cast is required.
11) If x is an int and y is a double, all of the following are legal except which assignment statement?

1) y = x;

2) x = y;

3) y = (double) x;

4) x = (int) y;

5) all of the above are legal

Answer: b. Explanation: Since x is an int, it cannot accept a double unless the double is cast as an int. There is no

explicit cast in the assignment statement in b. In a, a cast is not necessary because a double (y) can accept an int

value (x), and in c and d, explicit casts are present making them legal.
12) 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?

a = (b + c) * d / e – f;

a) *, /, +, -

b) *, +, /, -

c) +, *, /, -

d) +, /, *, -

e) +, -, *, /

Answer: c. Explanation: Order of precedence is any operator in ( ) first, followed by * and / in a left-to-right

manner, followed by + and – in a left-to-right manner. So, + is first since it is in ( ), followed by * followed by /

since * is to the left of /, followed finally by -.
13) What will be the result of the following assignment statement? Assume b = 5 and c = 10.

int a = b * (-c + 2) / 2;

a) 30

b) –30


c) 20

d) –20


e) –6

Answer: d. Explanation: The unary minus is applied first giving –c + 2 = -8. Next, the * is performed giving 5 * -

8 = -40, and finally the / is performed giving –40 / 2 = -20.
14) Assume that x, y and z are all ints equal to 50, 20 and 6 respectively. What is the result of x / y / z?

a) 0


b) 12

c) 16


d) A syntax error as this is syntactically invalid

e) A run-time error because this is a division by 0

Answer: a. Explanation: This division is performed left to right, so first 50 / 20 is performed. Since 50 and 20 are

ints, this results in 2. Next, 2 / 6 is performed which is 0. Notice that if the division were performed right to left,

the evaluation would instead be 50 / (20 / 6) = 50 / 3 = 16.
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?

a) 15


b) 105

c) 10 5


d) x+y

e) An error since neither x nor y is a String

Answer: a. Explanation: Java first computes x+y and then casts it as a String to be output. x + y = 10 + 5 = 15,

so the statement outputs 15.

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

a) 15


b) 105

c) 10 5


d) x+y

e) An error since neither x nor y is a String

Answer: b. Explanation: The "" casts the rest of the expression as a String, and so the two + signs are used as

String concatenation, and so x + y becomes x concatenated with y, or 105.


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

a) String name = "George Bush";

b) String name = new String("George Bush");

c) String name = "George" + " " + "Bush";

d) String name = new String("George" + " " + "Bush");

e) Any of the above would work

Answer: e. Explanation: There are two ways to store a character string into a String variable, by constructing a

new String using "new String(string value); " or by using an assignment statement, so either a or b will work. In c

and d, we have variations where the String concatenation operator + is used. So all four approaches will work.

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

a) c = a.length( ) + b.length( );

b) c = (int) a + (int) b;

c) c = a.concat(b);

d) c = b.concat(a);

e) c = a.plus(b);

Answer: c. The statement c = a + b uses the concatenation operator + (not to be confused with numeric addition).

The same result can be achieved by passing a the concat message with b as the parameter. Answer d will set c to be

b + a rather than a + b.


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

a) 'C'


b) 'o'

c) 'm'


d) "C"

e) "Computer"

Answer: b. Explanation: Neither d nor e would be correct because charAt returns a char (single character)

whereas these answers are Strings. So, the question is, which character is returned? In Java, the first character of a

String is numbered 0. So charAt(1) returns the second character of the String, or 'o'.
20) Which of the following would return the last character of the String x?

a) x.charAt(0);

b) x.charAt(last);

c) x.charAt(length(x));

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

e) x.charAt(x.length( ));

Answer: d. Explanation: Since last is not defined, b is syntactically invalid. The 0th character is the first in the

String, so a is true only if the String has a single character. The answer in c is syntactically invalid as length can

only be called by passing the message to x. Finally, d and e are syntactically valid, but since length returns the size

of the String, and since the first character starts at the 0th position, the last character is at x.length()-1, so e would

result in a run-time error.
21) Which library package would you import to use the class Random?

a) java.beans

b) java.io

c) java.lang

d) java.text

e) java.util

Answer: e. Explanation: This is a java utility, and so is found in the java.util package.
22) 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?

a) Math.sqrt(x*x);

b) Math.sqrt((int) x);

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

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

e) Math.sqrt(-x);

Answer: c. Explanation: Math.abs returns the absolute value of x. If x is negative, Math.sqrt(x) causes a run-time

error, but Math.sqrt(Math.abs(x)) does not since x is first converted to its positive equivalent before the square root

is performed. Answer a returns x (square root of x2 is x). In answer b, casting x to an int will not resolve the

problem if x is negative. In answer d, the two Math functions are performed in opposite order and so if x is

negative, it still generates a run-time error. Answer e will only work if x is not positive and so if x is positive, it

now generates a run-time error.
23) 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%?

a) System.out.println(x);

b) System.out.println(nf);

c) System.out.println(nf.x);

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

e) System.out.println(format(x));

Answer: d. Explanation: nf is an object and so must be passed a message to use it. The method to format a double

is called format and the value to be formatted is the parameter passed to format. Therefore, the proper way to do

this is nf.format(x). The answer in a will merely output 0.362491 while the answers to b, c and e are syntactically

invalid.
For questions 24 and 25, refer to the class defined below:

import cs1.Keyboard;

public class Questions

{

public static void main(String[ ] args)



{

int x, y, z;

double average;

System.out.println("Enter an integer value");

x = Keyboard.readInt( );

System.out.println("Enter another integer value");

y = Keyboard.readInt( );

System.out.println("Enter a third integer value");

z = Keyboard.readInt( );

average = (x + y + z) / 3;

System.out.println("The result of my calculation is " + average);

}

}


24) Questions computes

a) The correct average of x, y and z as a double

b) The correct average of x, y and z as an int

c) The average of x, y and z as a double, but the result may not be accurate

d) the sum of x, y and z

e) the remainder of the sum of x, y and z divided by 3

Answer: c. Explanation: Because the division is an int division, even though the result is stored in a double,

the resulting double may not be accurate. For instance, if x, y and z are 1, 2 and 4, the double average should

be 2.33333 but average will instead be 2.00000.
25) What is output if x = 0, y = 1 and z = 1?

a) 0


b) 0.0

c) 0.6666666666666666

d) 0.6666666666666667

e) 0.67


Answer: b. Explanation: The division is performed as an int division since x, y, z and 3 are all ints.

Therefore, average gets the value 0.0. It is output as 0.0 instead of 0 because average is a double, which

outputs at least one decimal digit unless specified otherwise using the DecimalFormat class.


Directory: cms -> lib4 -> va01000195 -> centricity -> domain
domain -> Pages 816-820 Truman’s domestic policies after the war—Truman was tasked with reconversion to a peacetime economy and introducing his domestic agenda
domain -> Pioneers of Sports & Entertainment Industries Matching Exercise
domain -> Unit 3: Introduction to Sports & Entertainment Business Principles
domain -> Apush period two key concepts review (1607-1754) This review refers to some examples we did not go over in class – so don’t stress about those!
domain -> The Sound of the Sea Henry Wadsworth Longfellow
domain -> Apush period one key concepts review (1491-1607) This review refers to some examples we did not go over in class – so don’t stress about those!
domain -> The Space Program Notes nasa
domain -> And Then There Were None Name: By Agatha Christie Guided Reading Questions Chapters 1, 2, & 3

Download 325.47 Kb.

Share with your friends:
  1   2   3   4




The database is protected by copyright ©ininet.org 2024
send message

    Main page