Final Exam Flashcards
Every class definition must include a constructor.
True
False
False
While multiple objects of the same class can exist, in a given program there can be only one version of each class.
True
False
True
A method defined in a class can access the class’ instance data without needing to pass them as parameters or declare them as local variables.
True
False
True
Accessors and mutators provide mechanisms for controlled access to a well-encapsulated class.
True
False
True
All Java classes must contain a main method which is the first method executed when the Java class is called upon.
True
False
False
In Java, the symbol “=” and the symbol “==” are used synonymously (interchangeably).
True
False
False
As in the other members of the C family of languages (C, C++, C#), Java interprets a zero value as false and a non-zero value as true.
True
False
True
In Java, selection statements consist of the if and if-else statements.
True
False
False
When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
True
False
False
The statement if (a >= b) a++; else b–; will do the same thing as the statement if (a < b) b–; else a++;.
True
False
True
An if statement may or may not have an else clause, but an else clause must be part of an if statement.
True
False
True
In order to compare int, float and double variables, you can use , ==, !=, <=, >=, but to compare char and String variables, you must use compareTo( ), equals( ) and equalsIgnoreCase( ).
True
False
False
Control in a switch statement jumps to the first matching case.
True
False
True
Each case in a switch statement must terminate with a break statement.
True
False
False
It is possible to convert any type of loop (while, do, or for) into any other.
True
False
True
The following loop is syntactically valid.
for (int j = 0; j < 1000; j++) j--;
True
False
True
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
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = “Help” and String t = “Goodbye”.
The expression (!done && x <= y) is true.
True
False
True
A dialog box is a device which accepts voice commands.
True
False
False
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = “Help” and String t = “Goodbye”.
The expression (done | | s.compareTo(t) < 0) is true.
True
False
False
If a language uses 240 unique letters and symbols, how many bits would be
needed to store each character of a document?
a. 1
b. 3
c. 7
d. 8
e. 2
D
Which of the following is not valid Java identifier?
a. Factorial
b. level2
c. hook&ladder
d. highest$
C
Categorize the following as a compile-time error, run-time error, or logical error.
Q6.Multiplying two numbers when you meant to add them
a. logical error
b. run-time error
c. compile-time error
A
Categorize the following as a compile-time error, run-time error, or logical error.
Q6.Multiplying two numbers when you meant to add them
a. logical error
b. run-time error
c. compile-time error
A
Producing inaccurate results
a. logical error
b. run-time error
c. compile-time error
A
Typing a { when you should have typed (
a. logical error
b. run-time error
c. compile-time error
C
Use the following class definition to answer the questions below.
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"); } }
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
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
How many lines of output are provided by this program? A) 1 B) 2 C) 3 D) 4 E) 5
B
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
The word println is a(n) A) method B) reserved word C) variable D) class E) String
A
What value will z have if we execute the following assignment statement?
float 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 float and 5 / 10 is an int
A
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 float
C) storing a float in a double
D) storing a float in an int
E) all of the above require casts
D
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
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
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
Suppose that String name = "Frank Zappa". What will the instruction name.toUpperCase( ).replace('A', 'I'); return? A) "FRANK ZAPPA " B) "FRINK ZIPPI" C) "Frink Zippi" D) "Frank Zappa" E) "FrInk ZIppI"
B
Which library package would you import to use the class Random? A) java.beans B) java.io C) java.lang D) java.tex E) java.util
E
Examine the following example answers to questions A and B below. Next pick the correct M/C answer at the end.
A. What is a Java Virtual Machine? Explain its role.
Answer: A Java Virtual Machine (JVM) is a software interpreter that executes Java bytecode.
Since bytecode is a low-level representation of a program, but not tied to any particular
hardware architecture, any computer with a JVM can execute Java code, no matter
what machine it was compiled on. That makes Java architecture-neutral, and therefore
highly portable.
B. What do we mean when we say that the English language is ambiguous? Give two examples of English ambiguity (other than the example used in this chapter) and explain the ambiguity. Why is ambiguity a problem for programming languages?
Answer: Something is ambiguous if it has two or more possible meanings. For example, the
statement, “Mary is the nicest teaching assistant who has helped me all day long” might
mean 1) of all the teaching assistants who have helped me today, Mary is the nicest, or
2) of those teaching assistants who have helped me for an entire day, Mary is the
nicest. As another example, the statement, “Bananas help those who help themselves”
might mean 1) bananas are good for those who attend to their own welfare or 2)
bananas are good for those who eat as many bananas as they please. If a
programming language statement could be interpreted in two or more ways, it would be
impossible to predict with certainty how it would be interpreted and what result would
be produced.
M/C answers below:
A. Both A. and B are true B. A is true, but B is false C. B is true, but A is false D. A is false, but B is true E. B is false, but A is true F. Both A and B are false
A
The Random class has a method nextFloat( ) which returns a random float value between A) -1 and +1 B) 0 and 1 C) 0 and 99 D) 1 and 100 E) -2,147,483,648 and +2,147,483,647
B
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?
A) "0.0" B) "0.#" C) "0.0#" D) "0.##" E) "#.#"
A
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
Using getCurrencyInstance( ) formats a variable, automatically inserting A) decimal point for cents B) dollar sign C) percent sign D) all three E) A and B but not C
E
The relationship between a class and an object is best described as A) classes are instances of objects B) objects are instances of classes C) objects and classes are the same thing D) classes are programs while objects are variables E) objects are the instance data of classes
B
Which of the following reserved words in Java is used to create an instance of a class?
A) class
B) public
C) public or private, either could be used
D) import
E) new
E
If x is an int where x = 1, what will x be after the following loop terminates?
while (x < 100)
x *= 2; A) 2 B) 64 C) 100 D) 128 E) none of the above, this is an infinite loop
D
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? A) m1 B) m2 C) m3 D) m5 E) main
B
In a UML diagram for a class A) classes are represented as rectangles B) there may be a section containing the name of the class C) there may be a section containing the attributes (data) of the class D) there may be a section containing the methods of the class E) all of the above
E
How many times will the following loop iterate?
int x = 10; while (x > 0) { System.out.println(x); x--; }
A) 0 times B) 1 time C) 9 times D) 10 times E) 11 times
D
Which of the following are true statements about check boxes?
A) they may be checked or unchecked
B) radio buttons are a special kind of check boxes
C) they are Java components
D) you can control whether or not they will be visible
E) all of the above
E
The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as A) boolean execution B) conditional statements C) try and catch D) sequentiality E) flow of control
E
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
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 = 5 and b = 5, what will x become after the above statement is executed? A) 0 B) 2 C) 3 D) 4 E) 5
C
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++;
A) 100 B) 200 C) 10,000 D) 20,000 E) 1,000,000
C
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';
A) This code will work correctly in all cases
B) This code will work correctly only if grade >= 60
C) This code will work correctly only if grade < 60
D) This code will work correctly only if grade < 70
E) This code will not work correctly under any circumstances
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;
A) The condition short circuits and the assignment statement is not executed
B) The condition short circuits and the assignment statement is executed without problem
C) The condition does not short circuit causing a division by zero error
D) The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error
E) The condition will not compile because it uses improper syntax
A
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");
A) 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
B) There is no logical error, but there is no need to have (x > 6) in the second conditional or (x > 3) in the third conditional
C) The logical error is that no matter what value x is, “Very small” is always printed out
D) The logical error is that no matter what value x is, “Large” is always printed out
E) There is nothing wrong with the logic at all
A
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;
A) syntactically it is invalid to have more if clauses than else clauses
B) statement2 will only execute if condition1 is false and condition2 is false
C) statement2 will only execute if condition1 is true and condition2 is false
D) statement2 will only execute if condition1 is false, it does not matter what condition2 is
E) statement2 will never execute
B
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')
A) All 4 Conditions are true
B) Only Condition 2 is true
C) Condition 2 and Condition 4 are true only
D) Conditions 2, 3 and 4 are all true, Condition 1 is not
E) All 4 Conditions are false
D
Every Interator
A) has a hasNext( ) method B) has a hasFirst( ) method C) has a hasNextInt( ) method D) has a isEmpty( ) method E) none of the above
A
If x is an int where x = 0, what will x be after the following loop terminates?
while (x < 100) x *= 2;
A) 2 B) 64 You Answered C) 100 D) 128 E) none of the above, this is an infinite loop
E
Given the following switch statement where x is an int, answer the questions below
switch (x) { case 3 : x += 1; case 4 : x += 2; case 5 : x += 3; case 6 : x++; case 7 : x += 2; case 8 : x--; case 9 : x++ }
4) If x is currently equal to 5, what will the value of x be after the switch statement executes? You Answered A) 8 B) 6 C) 11 D) 5 E) 10
C
The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as
A) y = (x < 0) ? x : 0; B) x = (x < 0) ? y : 0; C) (x < 0) ? y = x : y = 0; D) y = (x < 0); E) y = if (x < 0) x : 0;
A
How many times will the following loop iterate?
int x = 10; do { System.out.println(x); x--; } while (x > 0);
A) 0 times B) 1 time C) 9 times D) 10 times E) 11 times
E
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));
A) it prints s out backwards
B) it prints s out forwards
C) it prints s out backwards after skipping the last character
D) it prints s out backwards but does not print the 0th character
E) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0
A