Prepare for Java Exam 1
Mistyping println as printn will result in
a syntax error
Which of the following will yield a pseudorandom number in the range of 10 and 15 inclusive (The range includes both 10 and 15), given: Random gen = new Random();
10 + gen.nextInt(6)
What will be displayed after the following statements are executed? int ans = 10; int x = 65; int y = 55; if (x >= y) { int ans = x + y; } System.out.println(ans);
120
What characters does NOT need to have an associated closing character in a Java program?
<
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 run-time error may occur during execution.
Java.text's NumberFormat class includes methods that
Allow you to format percentages, round their display during the formatting process and allow you to format currency
True or false When two strings are compared using the String class's compareTo method, the comparison is not case sensitive.
False
True or false: Programs never need more than one path of execution.
False
True or false: All it takes for an OR expression to be true is for one of the subexpressions to be true.
False
True or false: The values of (double)5/2 and (double)(5/2) are identical.
False
True or false: These two ways to set up a String will yield identical results: String my_string = "12345"; String my_string = 12345;
False
True or false: When testing for character values, the switch statement does not test for the case of the character.
False
True or false: In Java the variable named total is the same as the variable named Total.
False
True or false: You may use the Str.replace('b', 'n') method to remove characters from a String.
False
What is NOT a rule that must be followed when naming identifiers?
Identifiers can contain spaces.
What is the function of the dot operator?
It allows one to access the data within an object when given a reference to the object, and It allows one to invoke a method within an object when given a reference to the object.
What is TRUE regarding the remainder operator, %?
It can be performed on any numeric values and its result is always numeric.
The advantages of the DecimalFormat class compared to the NumberFormat class include
Precise control over the number of digits to be displayed and control's over the presence of a leading zero
________ operators are used to determine whether a specific relationship exists between two values.
Relational
An alias is when
two different reference variables refer to the same physical object
In Java, ________ must be declared before they can be use
variables
In Java, instantiation means
creating a new object of the class
Using Java naming convention, what would be a good variable name for the current value of a stock?
currentStockVal
A ________ is a boolean variable that signals when some condition exists in the program.
flag
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 switch statement is a ________.
multiple alternative decision structure
This is a variable whose content is read only and cannot be changed during the program's execution.
named constant
What does the following line of Java code do? //System.out.println("Hello");
nothing
Character literals are enclosed in ________; string literals are enclosed in ________.
single quotes; double quotes
What would require an explicit typecast?
storing a float in an int
If str1 and str2 are both String objects, which of the following expressions will correctly determine whether or not they are equal?
str1.equals(str2)
The word println is a(n)
method
Which of the following is the not equal operator?
!=
Enclosing a group of statements inside a set of braces creates
a block of statements.
What is the value of z after the following assignment statement is executed? double z = 5/10;
0.5
What will be the value of bonus after the following statements are executed? int bonus, sales = 10000; if (sales < 5000) bonus = 200; else if (sales < 7500) bonus = 500; else if (sales < 10000) bonus = 750; else if (sales < 20000) bonus = 1000; else bonus = 1250;
1000
Assume that x and y are ints equal to 10 and 5 respectively. What is the output of the following statement? System.out.println("" + x + y);
105
What is TRUE regarding Java syntax and semantics?
A Java compiler can determine if you have followed proper syntax but not proper semantics
What comment might be added to explain the following instruction? System.out.println("Hello World");
Demonstrate an output message
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.
What is NOT true regarding Java as a programming language?
Java is a language whose programs do not require translating into machine language before they are executed.
What cannot be used as identifiers in Java?
Key words
________ works like this: If the expression on the left side of the && operator is false, the expression the right side will not be checked.
Short-circuit evaluation
When the + operator is used with strings, it is known as the:
String concatenation operator
Consider the following programimport. java.util.Scanner; public class Questions33_34 { public static void main(String[] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt(); System.out.println("Enter another integer value"); y = scan.nextInt(); System.out.println("Enter a third integer value"); z = scan.nextInt(); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } Is this code computing the correct average?
The average of x, y, and z as a double but the result may not be accurate
Assume you write a program that uses 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 and you'll receive a syntax error about the missing class.
In an if-else statement, if the boolean expression is false then ________.
The statement or block following the else is executed
If the following Java statements are executed, what will be displayed? System.out.println("The top three winners are\n"); System.out.print("Jody, the Giant\n"); System.out.print("Buffy, the Barbarian"); System.out.println("Adelle, the Alligator");
The top three winners are Jody, the GiantBuffy, the BarbarianAdelle, the Alligator
What properties are TRUE of String objects?
Their length never changes and the shortest String has zero length.
True or false: All the methods in the Math class are declared to be static.
True
True or false: There are three ways that data conversion may occur: by assignment, by promotion, and by casting.
True
True or false: A variable's scope is the part of the program that has access to the variable.
True
True or false: All it takes for an OR expression to be true is for one of the subexpressions to be true.
True
True or false: In a switch statement, each of the case values must be unique.
True
To display the output on the next line, you can use the println method or use this escape sequence in the print method.
\n
Which character is NOT allowed in an identifier?
^
What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y = 50; if (x >= y) { ans = x + 10;x -= y; } else { ans = y + 10;y += x; }
ans = 60, x = 0, y = 50
Comments should
be insightful and explain the intention of an instruction or block of code
Which of the following expressions determines whether the char variable, chrA, is not equal to the letter 'A'?
chrA != 'A'
Variables are classified according to their:
data type
Variables of the boolean data type are useful for:
evaluating true/false conditions
What is a legal Java programmer's defined identifier?
i
What determines whether the variable temp is within the range of 0 through 100 (inclusive)?
if (temp >= 0 && temp <= 100)
An error in a program that results in the program outputting $100 instead of the correct answer, $250, is a
logical error
What is a legal Java identifier?
oneForAll
The main method for a Java program is defined by
public static void main(String[] args)
What is NOT syntactically legal in Java?
s t a t i c main(String[] args)
Given the following two lines of code, what can be said about s1 and s2? String s1 = "testing" + " 123"; String s2 = new String("testing 123");
s1 and s2 are references to different String objects.
The String class's compareTo method
yields 0 if the two strings are identical