CS1 chapter 1, 2, & 3 test

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Of the following list, which one is not true, or are they all true, regarding Java as a programming language? a. It is 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. These are all true

? c.

A _____ error occurs when a semicolon is missing at the end of a statement. a) syntax b) run-time c) logic d) semantic

A

A(n) ______ is a program that behaves like a computer. a) interpreter b) compiler c) terminal I/O d) applet

A

Because it returns a value of true or false, the condition of an if statement must be a(n) ______ expression. a) Boolean b) control c) else d) entry-controlled

A

Caroline uses the ____ character to include quotation marks in the welcome message. a) escape b) output c) concatenation d) forward slash

A

Case 2-1 Jeremiah is a C++ programmer who has be asked by a client to create a program using Java. Jeremiah knows that Java is a) better suited to the internet than C++ b) more error-prone than C++ c) the world's most popular industrial-strength programming language d) superficially very different from C++

A

Consider the following two lines of code. What can you say about s1 and s2? String s1 = "testing" + "123"; String s2 = new String ("testing 123"); a) s1 and s2 are both references to different String objects. b) s1 and s2 are both references to the same String object. c) the line declaring s2 is legal Java; the line declaring s1 will produce a syntax error d) s1 and s2 will compare "equal" e) none of the above

A

Figure 2-1 above represents a(n) ____ user interface. a) GUI b) JVM c) terminal I/O d) variable

A

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) +, /, *, -

A

If Ari wants to change the order in which numbers are calculated, he should add ____ to them. a) parentheses b) literals c) slashes d) mixed-modes

A

In Java, "instantiation" means a) creating a new object of the class b) launching a method c) noticing the first time something is used d) creating a new alias to an existing object e) none of the above

A

Java is all of the following EXCEPT a) fast b) secure c) robust d) portable

A

Programming and natural languages share all of the following elements EXCEPT a) rigidity b) vocabulary c) syntax d) semantics

A

Suppose that the String name = "Frank Zappa". What will the instruction name.toUpperCase().replace('A', 'I'); return? a) "FRINK ZIPPI" b) "FRANK ZAPPA" c) "Frink Zippi" d) "Frank Zappa" e) "Frink ZippI"

A

The _____ statement is used to write count-controlled loops. a) for b) number c) cntr d) if-else

A

The decrement operator is a) -- b) ++ c) / d) !=

A

The expression (3+5)+2*2+4 will yield ____ as the answer. a) 16 b) 20 c) 24 d) 60

A

The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as a) flow of control b) conditional statements c) try and catch d) sequentiality e) boolean execution

A

The main factor affecting a program's readability is its a) layout b) spelling c) language d) development environment

A

The presence or absence of _____ symbol(s) can seriously affect the logic of a selection statement. a) { } b) // c) ; d) /* */

A

The string of characters that appears between parentheses following the message in Java code is called a(n) _____. a) parameter b) statement c) variable d) thread

A

Which of the following is NOT an example of a primitive data type? a) Strings b) Numbers c) Characters d) Booleans

A

You can reduce the number of logic error in a program by rereading code carefully, or a) proofreading b) debugging c) desk checking d) key wording

A

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

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 +.

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

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.

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

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.

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

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.

A ____ error occurs when the computer is instructed to divide by 0. a) syntax b) run-time c) logic d) semantic

B

Ari uses the expression (15+9)/(3+1)*2 to yield the value _____. a) 3 b) 12 c) 20 d) the expression is not valid

B

Boolean, if, import, and return are all examples of ______. a) user-defined symbols b) reserved words c) variables d) signatures

B

Carter thinks his program is stuck in an infinite loop. What should he do to his PC to make it stop? a) unplug the computer b) press Ctrl+C c) press Ctrl+Alt+Delete d) restart the computer

B

Case 2-1 Jeremiah is a C++ programmer who has be asked by a client to create a program using Java. Jeremiah will incorporate _____ into his Java program in order to allow multiple processes to occur at once, such as an image being transferred from one machine to another across a network, while another process simultaneously interacts with the user. a) variables b) threads c) panels d) applets

B

Colors in most computer system use the ____ scheme. a) color class b) RGB c) CMYK d) GUI

B

Figure 4-2 above (one with purple shapes) represents the behavior of a(n) ______ statement. a) if-else b) while c) random d) infinite

B

Gabriel is writing a program that finds the average of a list of positive numbers. Gabriel uses _____ as the sentinel. a) 0 b) -1 c) 1 d) 1.0

B

Given the nested if-else structure below; answer question below. if (a>0) if (b<0) x=x+5 else if (a>5) x=x+4; else x=x+3; else x=x+2; If x is currently 0, a=0 and b=-5, what will x become after the above statement is executed? a) 0 b) 2 c) 3 d) 4 e) 5

B

If the String major = "Computer Science", what is returned by major.charAt(1)? a) 'm' b) 'o' c) 'C' d) "C" e) "Computer"

B

If x is an int and y is a float, all of the following are legal except which assignment statement? a) y = x; b) x = y; c) y = (float)x; d) x = (int)y; e) all of the above are legal

B

In a(n) _____-controlled loop, the continuation condition is tested at the top of the loop on each pass. a) task b) entry c) count d) all of the above

B

In order to create a constant, you would which of the following Java reserved words? a) private b) final c) static d) class e) int

B

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(".")? a) EINSTEIN. b) XINSTXIN. c) einstein. d) xinstxin. e) Einstein.

B

Some JVMs translate byte code instructions into machine language when they are first encountered, using the _____ technique. a) IDE b) JIT c) variable d) interpreter

B

SomeClass someObject = new SomeClass (some parameters); is an example of a) assignment b) instantiation c) byte code d) interpretation

B

Text that appears between an opening /* and a closing */ is a(n) a) end-of-line statement b) multiline comment c) debugging statement d) expression

B

What will be displayed by this command: System.out.println(Math.pow(3,3-1)); a) 4 b) 9 c) 27 d) 8 e) 6

B

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) -20 c) -6 d) 30 e) 20

B

Which of the following is a difference between programming and natural languages? a) semantics b) literalness c) exception d) origin

B

Which of the following numeric data types is used for smaller, less precise floating-point numbers? a) double b) float c) short d) byte

B

Which of the following will yield a pseudorandom number in the range [-5, +5] given the following: Random gen = newRandom(); a) gen.nextInt(10)-5 b) gen.nextFloat()*10-5 c) gen.nextInt()*10-5 d) gen.nextFloat()*5 e) gen.nextFloat()*5-10

B

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? a) if (x>0) x++; else x--; b) if (x>0) x++; else if (x<0) x--; c) if (x>0) x++; if (x<0) x--; else x=0; d) if (x==0) x=0; else ++; e) x--; x++; x--;

B

____ is when the same name is used for two different methods. a) infinite loop b) overloading c) iteration d) sentinel

B

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

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.

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

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.

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"

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.

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

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".

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); } } 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

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.

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"); } } How many lines of output are provided by this program? a) 1 b) 2 c) 3 d) 4 e) 5

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.

In the String major = "Computer Science", what is returned by major.charAt(1)? a) 'C' b) 'o' c) 'm' d) "C" e) "Computer"

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'.

_______ statements execute within each pass and implement the calculation in question.

Body

A loop that continues until something specific is accomplished is called a(n) ______-controlled loop. a) count b) entry c) task d) outcome

C

All of the following are examples of Java vocabulary EXCEPT ______. a) = b) * c) (f-32)*/9 d) Fahrenheit

C

Case 4-1 Carter has written a program that has some errors that he needs to fix. Carter realizes that the loop in his program goes around once more than necessary, meaning that he ha a(n) _______ error. a) termination b) count-controlled c) off-by-one d) infinite loop

C

Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending one 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'; a) This code will not work correctly under any circumstances. b) This code will work correctly in all cases. c) This code will work correctly only if grade < 70 d) This code will work correctly only if grade < 60 e) This code will work correctly only if grade >= 60

C

Figure 2-2 above represents a(n) ______ interface. a) GUI b) JVM c) terminal I/O d) variable

C

Gabrielle is using a text file to input the list of numbers. Which of the following is NOT true? a) if the file is missing, the JVM will throw an I/O exception b) the numbers must be separated by whitespace characters c) Gabrielle uses the PrintWriter class to read the data from the text file d) the text file must contain a special character that marks the end of the file

C

Given the following code fragment String strA = "aBcDeFg"; String strB = strA.toLowerCase(); strB = strB.toUpperCase(); String strC = strA.toUpperCase(); a) strA.equals(strC) would be true b) strB.equals(strC) would be true c) strB.compareTo(strC) would yield 0 d) strA.compareTo(strC) would yield 0 e) none of the above

C

How many times will the following loop iterate? int x = 10; while (x>0) { System.out.println(x); x--; } a) 1 time b) 11 times c) 10 times d) 0 times e) 9 times

C

If a method's signature is labeled ____, the message is sent to the method's class instead of to an object. a) class b) iterative c) static d) count-controlled

C

In an import package, you can substitute a class or subsection name with the ____ character to indicate that all of the subsections in the package of classes in the subsections are to be imported. a) \ b) ? c) * d) #

C

Java is a strongly typed language. What is meant by "strongly typed"? a) Variables are allowed to change types during their existence in a program but only if the change is to a narrower type b) Every variable must have an associated type before you can use it c) 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 d) Variables are allowed to change type during their existence in the program as long as the value it currently stores is of the type it is currently declared to be e) Variables can be used without declaring type

C

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? a) The program won't run, but it will compile with a warning about the missing class. b) The program will encounter a runtime error when it attempts to access any member of the Random class. c) The program won't compile-you'll receive a syntax error about the missing class. d) The program will compile, but you'll receive a warning about the missing class. e) none of the above

C

The Random class has a method nextFloat() which returns a float value between a) -1 and +1 b) 0 and 99 c) 0 and 1 d) -2,147,483,648 and +2,147,483,647 e) 1 and 100

C

The three steps to writing a Java program, in order, are ______. a) enter, edit, compile b) edit, execute, compile c) edit, compile, execute d) enter, interpret, compile

C

To get out of a loop prematurely, us a(n)_____ statement. a) exit b) false c) break d) then

C

Use a(n) _____ statement rather than two if statements when the alternative courses of action are mutually exclusive. a) while b) for c) if-else d) whether

C

What value will z have if we execute the following assignment statement? float z = 5/10; a) z will equal 0.05 b) z will equal 0.5 c) z will equal 0.0 d) z will equal 5.0 e) none of the above, a run-time error arises because z is a float and 5/10 is an int

C

Which library package would you import to use the class Random? a) java.io b) java.beans c) java.util d) java.text e) java.lang

C

Which of the following is NOT true about Java? a) Java is an object-based language. b) Java creates programs that are portable. c) The Java compiler translates into machine language. d) Java supports threads.

C

Which of the following is NOT true about variables? a) Their values can change during execution. b) Before using a variable for the first time, the program must declare its type. c) The type of data a variable contains can be changed. d) All of the above are true.

C

____ errors are also called bugs. a) syntax b) run-time c) logic d) semantic

C

____ is an assignment operator. a) + b) * c) = d) all of the above

C

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

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.

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

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.

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);

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.

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) +, -, *, /

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 -.

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"

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".

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);

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.

A ____ is used to mark the end of each statement in a program. a) comma (,) b) slash (/) c) period (.) d) semicolon (;)

D

A cast is required in which of the following situation? a) using charAt to take an element of a String and store it in a char b) storing an int in a float c) storing a float in a double d) storing a float in an int e) all of the above requires casts

D

A typical loop has four parts, including all of the following EXCEPT _______ statements. a) initializing b) body c) update d) verification

D

A(n) ______ is a small Java program that runs on Web pages. a) JIT b) parameter c) JVM d) applet

D

Caroline is writing a program in Java which asks the user to input their first and last name, and then will output a welcome message. Caroline uses the _____ operator to join the first and last name strings for the welcome message. a) comment b) signature c) keyword d) concatenation

D

If x is an int where x = 1, what will x be after the following loop terminates? while (x<100) x*=2 a) 100 b) 64 c) 2 d) 128 e) none of the above, this is an infinite loop

D

In Java a variable may contain a) a class b) a method c) a package d) a value or a reference e) any of the above

D

Of the following if statements, which one correctly executes three instructions if the condition is true? a) if (x<0) a=b*2; y=x; z=a-y; { b) if (x<0) a=b*2; y=x; z=a-y; } c) if { (x<0) a=b*2; y=x; z=a-y; } d) if (x<0) { a=b*2; y=x; z=a-y; } e) B, C, and D are all correct, but not A

D

Sending a message to a variable before the corresponding object has been instantiated causes a(n) _____ expression. a) no such method b) syntax c) desk checking d) null pointer

D

The ____ character is used to indicate that punctuation such as commas and quotation marks should be taken literally. a) * b) = c) ( d) \

D

The _____ operator means not equal to. a) -- b) == c) <= d) !=

D

To run Java byte code, you must install a(n) a) GUI b) JIT c) IDE d) JVM

D

To use a method successfully, you must know a) what type of value it returns b) its name c) the number and type of the parameters it expects d) all of the above

D

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());

D

_____ define the rules for interpreting the meaning of statements. a) syntax b) rigidity c) logic d) semantics

D

______ always occur in pairs. a) semicolons b) conditions c) statements d) braces

D

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

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.

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( ));

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.

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

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.

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.

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.

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));

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.

Given the nested if-else structure below; answer questions below. if (a>0) if (b<0) x=x+5 else if (a>5) x=x+4; else x=x+3; else x=x+2; If x is currently 0, a=1 and b=-1, what will x become after the above statement is executed? a) 0 b) 2 c) 3 d) 4 e) 5

E

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 be equal to a) 5 b) -5 c) 23 d) 0 e) 4

E

If two variables contain aliases of the same object then a) the object may be modified using either alias b) the object cannot be modified unless there's but a single reference to it c) a third alias is created if/when the object is modified d) the object will become an "orphan" if both variables are set to null e) answers A and D are correct

E

If x is an int where x = 0, what will x be after the following loop terminates? while (x < 100) x*=2; a) 128 b) 2 c) 64 d) 100 e) none of the above, this is an infinite loop

E

What happens if you attempt to use a variable before it has been initialized? a) a syntax error may be generated by the compiler b) a runtime error may occur during execution c) a "garbage" or "uninitialized" value will be used in the computation d) a value of zero is used if a variable has not been initialized e) answers A and B are correct

E

What is the function of the dot operator? a) it serves to separate the integer from the fractional part of a floating point number b) it allows one to access the data within an object when given a reference to the object c) it allows one to invoke a method within an object when given a reference to the object d) it is used to terminate commands (much as a period terminates a sentence in English) e) both B and C are correct

E

Which of the following reserved words in Java is used to create an instance of a class? a) public b) class c) public or private, either could be used d) import e) new

E

Which properties are true of String objects? a) their lengths never change b) the shortest string has zero length c) the index of the first character in a string is one d) individual characters within a String may be changed using the replace method e) only A and B are true

E

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

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.

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

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.

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

E Explanation: This is a java utility, and so is found in the java.util package

True or False? A variable of type boolean will store either a 0 or a 1.

False

True or False? If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will return the original version of x

False

True or False? If you need to import not only the top-level of a package, but all its secondary levels as well, you should write: import package.*.*;

False

True or False? In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).

False

True or False? Java is able represent 255*255*255=16,581,375 distinct colors.

False

True or False? System.out.println("123" + 4) will display the value 127.

False

True or False? The Java graphics coordinate system is similar to the coordinate system one finds in mathematics in every respect.

False

True or False? The values of (double)5/2 and (double)(5/2) are identical

False

True or False? These two ways of setting up a String yield identical results: a) String string = new String (12345); b) String string = new String ("12345");

False

True or False? These two ways of setting up a String yield identical results: a) String string = "12345"; b) String string = 12345;

False

True or False? You may use the String replace () method to remove characters from a String.

False

True or False? Only difficult programming problems require a pseudocode solution before the programmer creates the implementation (program) itself.

False

________ is the fastest growing programming language in the world.

Java

True or False? A double is wider than a float and a float is wider than an int.

True

True or False? If String name = "George W. Bush"; then the instruction name.length(); will return 14.

True

True or False? If x is a string, then x = new String ("OH"); and x = "OH"; will accomplish the same thing.

True

True or False? In Java, 'a' and 'A' are considered to be different values.

True

True or False? Many Java drawing shapes are specified using a bound rectangle.

True

True or False? There are three way that data conversion may occur--by assignment, by promotion, by casting.

True

True or False? These two ways of setting up a String yield identical results: a) String string = new String ("123.45"); b) String string = "" + 123.45;

True

True or False? These two ways of setting up a string yield identical results: a) String string = new String ("string"); b) String string = "string";

True

True or False? When comparing any primitive type of variable, == should always be used to test to see if two values are equal.

True

True or False? You cannot cast a String to be a char and you cannot cast a String which stores a number to be an int, float, or double.

True

True or False? If String a = "ABCD" and String b = "abcd" then a.equals(b); returns false but a.equalsIgnoreCase(b); returns true

True

What is the value of myInterger after this line of code? int myInteger = (int)5.6; a. 5 b. 5.6 c. 5.5 d. 6 e. 9

a. 5

Which of the following statements is true about the primary and secondary memory? a. Most kind of primary memory loses their contents when you turn off the power. b. primary memory is usually slower than secondary memory c. primary memory is usually cheaper than secondary memory d. secondary memory is essential for the correct operation of a computer: primary memory is not. e. There is no difference between primary and secondary memory

a. Most kind of primary memory loses their contents when you turn off the power.

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

a. StoreCustomer

Data written to a memory location overwrites and destroys any information that was stored at that location. a. True b. False

a. True

A compiler is: a. a program that translates high-level language to machine language b. an early, second-generation computer system c. an input / output device d. another word for a programmer e. a program that translates assembly language to machine language

a. a program that translates high-level language to machine language

Which of the following would be a legal Java variable name? a. i b. class c. ilikeclass! d. idon'tlikeclass e. i-like-class

a. i

Suppose s and t are String variables. Why is s == t false, even when s and t have same value? a. s and t are different object b. s and t have different values c. if s and t have the same value, then s == t is true d. for String the == operator means !=, and the != operator means ==. e. none of these answers are valid

a. or c.

Forgetting a semicolon will cause a. syntax error b. a run-time error c. a logical error d. no error at all e. converting the statement into a comment

a. syntax error

A comical animated character on a Web page is likely run using a Java program called a(n) _____.

applet

A(n) ______ expression consists of operands and operators.

arithmetic

what does this program display? System.out.print("123"); System.out.print("456"); System.out.println("789"); System.out.println("0"); a. 123 456 789 0 b. 123456789 0 c. 123 456789 0 d. 123567 7890 e. 123 456 7890

b. 123456789 0

What is the value of x after this statement executes? double x = 2.5 + 5 / 2 - 1.25; a. 3.75 b. 3.25 c. 2.5 d. 5.75 e. 3.5

b. 3.25

What does this program segment display? int x = 5; if (x > 0) { x = x - 3; } else { x = x + 3; } x = x + 2; System.out.println(x); a. 7 b. 4 c. 10 d. 13 e. 11

b. 4

What is the result of this expression? 5 + 2 / 3 + 1 a. 5 b. 6 c. 6.6667 d. 7 e. 0

b. 6

Java byte codes are directly executable whereas Java source code is not. a. True b. False

b. False

Java is a case-sensitive language meaning that Current, current, and CURRENT will all reference the same identifier. a. True b. False

b. False

The line of java code: //System.out.println("Hello"); will cause Hello to be the output. a. True b. False

b. False

A Java program is best classified as: a. Hardware b. Software c. Storage d. Processor e. Input

b. Software

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 defines variables whose names are not easy to understand e. all of the above

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

Which of the following is a legal identifier? a. 1ForAll b. oneForAll c. one/4/all d. 1_4_all e. 1forall

b. oneForAll

What are the values of x and y after this program segment executes? int x = 0, y = 1; x = y++; x = y; a. x has, y has 2 b. x has 2, y has 2 c. x has 0, y has 1 d. x has 0, y has 2 e. x has 1, y has 1

b. x has 2, y has 2

Which expression returns the 1's place of an integer x? a. x/10 b. x%10 c. x/100 d. x%100 e. x+1

b. x%10

What does this program segment display? int x = 5; if (x > 0) { x = x - 3; } x = x + 3; x = x + 2; System.out.println(x); a. 0 b. 13 c. 7 d. 4 e. 10

c. 7

Which of the following is not a primitive type? a. int b. char c. String d. boolean e. double

c. String

Which of the following is the beginning of a valid if statement? a. if(title + "J") . . . b. if(1 - b) . . . c. if(voltage != 100) . . . d. if(pulse =< 60) . . . e. if(selection = 'C') . . .

c. if(voltage != 100) . . .

The first step in writing a program is to: a. analyze all possible algorithms to see which is most efficient b. writing the program c. understand the problem d. come up with an algorithm e. test the program

c. understand the problem

Given: int a = 5, b = 4; double x = 5.0, y = 4.0 Which of the following expressions have the value 1? a. Only II b. Only III c. I and III d. II and III e. None of these

d. II and III

Which character below is not allowed in an identifier? a. $ b. _ c. 0 (zero) d. ^ e. q

d. ^

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

d. a document or other type of file on the internet

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

d. a logical error

Consider the following variable declarations. String s = "crunch"; int a = 3, b = 1; What is printed by the following statements? System.out.print(s + a + b); System.out.println(b + a + s); a. crunch413crunch b. crunch3113crunch c. crunch44crunch d. crunch314crunch e. Nothing is printed due to a run-time error

d. crunch314crunch

What is the proper syntax to initialize a double called temperature to have the value 70.4? a. int temperature = 70.4; b. double temperature = 70.4; c. temperature = 70.4; d. float temperature = 70.4; e. String temperature = 70.4;

d. float temperature = 70.4;

Computers can understand which type of programming language directly (that is, without further translation)? a. assembly language b. high level language c. There is no programming language that a computer can understand directly d. machine language e. natural language (English)

d. machine language

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

d. main memory

Which statement will generate a random number between 60 and 100 inclusive? a. num = (int)(Math.random( )*101); b. num = 60 + (int)(Math.random( ) * 101); c. num = 60 + (int)(Math.random( ) * 40); d. num = 60 + (int)(Math.random( ) * 41); e. num = (int)(Math.random( ) * 160 - 60)

d. num = 60 + (int)(Math.random( ) * 41);

If b is defined as a Boolean variable, what values can we assign to b? a. T, F b. yes, no c. 0, 1 d. true, false e. t, f

d. true, false

The _____ operator decreases a variable's value by one.

decrement

Numbers with decimals are the _______ numeric data type.

double

6 bits can be used to represent ________ distinct items or values. a. 6 b. 20 c. 24 d. 32 e. 64

e. 64

Which character below is allowed in an identifier? a. / b. # c. * d. + e. None of these are allowed in an identifier

e. None of these are allowed in an identifier

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 to handle new programing language features e. all of the above

e. all of the above

Which of the following stores data in binary format? a. ROM b. RAM c. a hard disk drive d. a CD e. all of the above

e. all of the above

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

e. none of the above, a run-time error arises

A(n) _______ is a programmer who breaks into computer systems in an unauthorized way.

hacker

The ______ operator increases a variable's value by one.

increment

Each pass through a loop is called a(n) _______.

iteration

Items in a program whose values do not change are called _______.

literals

A(n) _______ control statement is located inside another control statement.

nested

Programmers share code using a mechanism called a(n) ________.

package

A(n) ______ is a computer program that can replicate itself and move from computer to computer.

virus


संबंधित स्टडी सेट्स

Ibrahim Breaks the Idols/ قصّة سيّدنا إبراهيم: إِبْرَاهِيم يَكْسِرُ الأصْنَام Story 4

View Set

Jarvis Chapter 22 Review, EXAM 3 FINALE

View Set