Deck 2: Data and Expressions

Full screen (f)
exit full mode
Question
If String name = "George W. Bush"; then the instruction name.length(); will return 14.
Use Space or
up arrow
down arrow
to flip the card.
Question
A variable of type boolean will store either a 0 or a 1.
Question
In Java, 'a' and 'A' are considered to be different values.
Question
A double is wider than a float and a float is wider than an int.
Question
The values of (double)5/2 and (double)(5/2) are identical.
Question
Unlike the String class where you must pass a message to an object (instance) of the class, as in x.length(), in order to use either the Scanner or Math classes, you pass messages directly to the class name, as in Math.abs()or scan.nextInt().
Question
If you want to output the text "hi there" including the quote marks, which of the following would you use?

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 of these; it is not possible to output a quote mark because it is used to mark the beginning and end of the String to be displayed.
Question
As explained in the Software Failure section in the text, the Mars Lander most likely crash landed when its descent engines cut off too high over the Mars surface.
Question
Example Code Ch 02-1
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");
}
}
Refer to the class definition in Example Code Ch 02-1. How many lines of output are provided by this program?

A) 1
B) 2
C) 3
D) 4
E) 5
Question
If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will return the original value of x.
Question
Example Code Ch 02-1
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");
}
}
Refer to the class definition in Example Code Ch 02-1. 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"
Question
If x is a String, then x = new String("OH"); and x = "OH"; will accomplish the same thing.
Question
There are three ways that data conversion may occur: by assignment, by promotion, and by casting.
Question
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.
Question
If String a = "ABCD" and String b = "abcd" then
a.equals(b); returns false but
a.equalsIgnoreCase(b); returns true.
Question
The following statement will output __________ lines of text. System.out.println("1 big bad wolf\t8 the 3 little pigs\n4
Dinner\r2night");

A) 1
B) 2
C) 3
D) 4
E) 5
Question
Example Code Ch 02-1
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");
}
}
Refer to the class definition in Example Code Ch 02-1. The program will print the word "Here" and then print

A) "There Everywhere" on the line after "Here"
B) "The" 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"
Question
The word println is a(n)

A) method
B) reserved word
C) variable
D) class
E) String
Question
Example Code Ch 02-1
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");
}
}
Refer to the class definition in Example Code Ch 02-1. A reasonable documentation 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 contains three output statements
Question
In order to generate a random number, you must use Math.random().
Question
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
Question
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);

A) 15
B) 105
C) 10 5
D) x + y
E) this would cause an error since neither x nor y is a String
Question
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
Question
A Java variable is the name of a

A) numeric data value stored in memory
B) data value stored in memory that cannot change during the program's execution
C) data value stored in memory that can change its value but cannot change its type during the program's execution
D) data value stored in memory that can change both its value and its type during the program's execution
E) data value or a class stored in memory that can change both its value and its type during the program's execution
Question
Given String name = "Arleen Crabtree". What will the following instruction return? name.toUpperCase().replace('R', 'Z');

A) "ARLEEN CRABTREE"
B) "AZLEEN CZABTREE"
C) "Azleen Czabtree"
D) "Arleen Crabtree"
E) "ArZleen CrZabtree"
Question
Of the following types, which one cannot store a numeric value?

A) int
B) double
C) float
D) char
E) All of these can store numeric values
Question
If x is an int and y is a float, which of the following statements is not a legal assignment statement?

A) y = x;
B) x = y;
C) y = (float)x;
D) x = (int)y;
E) All of these are legal assignment statements
Question
If you want to output a double so that at least one digit appears to the left side of the decimal point and exactly one digit appears to the right side, which pattern would you give a DecimalFormat variable when you instantiate it?

A) "0.0"
B) "0.#"
C) "0.0#"
D) "0.##"
E) "#.#"
Question
Which library package would you import to use NumberFormat and DecimalFormat?

A) java.beans
B) java.io
C) java.lang
D) java.text
E) java.util
Question
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) this would cause a syntax error
E) this would cause a run-time error because it is a division by zero
Question
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;
E) x.charAt(x.length());
Question
What is the value of z after the following assignment statement is executed? float z = 5/10;

A) 0.0
B) 0.5
C) 5.0
D) 0.05
E) None of these; a run-time error will occur because z is a float and 5/10 is an int.
Question
Given the double likelihood = 0.013885 and given DecimalFormat dformatter = DecimalFormat("0.00##");
What would be the output if you execute the following:
System.out.println(df.format(likelihood));

A) 0.013885
B) 0.0139
C) 0.0145
D) .0138
E) .014
Question
If the String major = "Computer Science", what is returned by major.charAt(1)?

A) 'C'
B) 'o'
C) 'm'
D) "C"
E) "Computer"
Question
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);

A) 15
B) 105
C) 10 5
D) x + y
E) this would cause an error since neither x nor y is a String
Question
Which of the following is true regarding the mod operator, %?

A) It can only be performed on int values and its result is a double.
B) It can only be performed on int values and its result is an int.
C) It can only be performed on float or double.values and its result is an int.
D) It can only be performed on float or double.values and its result is a double.
E) It can be performed on any numeric values and its result is always numeric.
Question
Which of the following situations would require a cast?

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 these require casts
Question
If you want to store the value "Harry Potter" in the String variable name, which of the following statements could you use?

A) String name = "Harry Potter";
B) String name = new String("Harry Potter");
C) String name = "Harry" + " " + "Potter";
D) String name = new String("Harry" + " " + "Potter");
E) Any of these would work
Question
Using getCurrencyInstance() formats a variable, automatically inserting

A) a decimal point for cents
B) a dollar sign
C) a percent sign
D) all three of these
E) a decimal point for cents and a dollar sign but not a percent sign
Question
What will be the result of the following assignment statement, given that b = 5 and c = 10? int a = b * (-c + 2)/2;

A) 30
B) -30
C) 20
D) -20
E) -6
Question
Using the various String methods, manipulate a String called current to be the last character of current followed by the remainder of its characters in order, placing the result in a String called rearranged.
Question
Given three int variables with the values a = 5, b = 7, and c = 12, what is the value of z after the following statement is executed? int z = (a * b - c) / a;

A) 0
B) 4
C) 5
D) -5
E) 23
Question
As presented in the Software Failure section of the text, the root cause of the Mars Climate Orbiter problem was

A) the cost of the project
B) an inability to track the orbiter at long distances
C) atmospheric friction
D) a communication issue between subsystems
E) None of these
Question
Explain what the following statement computes:
int z = (int)Math.ceil(Math.sqrt(x)/Math.sqrt(y));
Question
Example Code Ch 02-2
import 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);
}
}
Refer to Example Code Ch 02-2. What is the output if x = 0, y = 1, and z = 1?

A) 0
B) 0.0
C) 0.6666666666666666
D) 0.6666666666666667
E) 0.67
Question
Given two points in an applet represented by the four int variables x1, y1, x2 and y2, write a paint method to draw a line between the two points and write the location of the two points next to the two points.
Question
Write an assignment statement to compute the gas mileage of a car where the int values milesTraveled and gallonsNeeded have already been input. The variable gasMileage needs to be declared and should be a double.
Question
How many ways are there to test to see if two String variables, a and b, are equal to each other if we ignore their case (for instance, "aBCd" would be considered equal to "ABcd")?
Question
Given four int values, x1, x2, y1, y2, write the code to compute the distance between the two points (x1, y1) and (x2, y2), storing the result in the double distance.
Question
Write a set of instructions to prompt the user for an int value and input it using the Scanner class into the variable x and prompt the user for a float value and input it using the Scanner class into the variable y.
Question
How do the statements "import java.util.*;" and "import java.util.Random;" differ from each other?
Question
Java is a strongly typed language. What is meant by "strongly typed"?

A) Every variable must have an associated type before it can be used.
B) Variables can be used without declaring their types.
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 a program so long as the value a variable currently stores is of the type it currently declared to be.
E) Variables are allowed to change types during their existence in a program but only if the change is to a narrower type.
Question
Since you cannot take the square root of a negative number, which of the following could you use 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);
Question
Given x is a double and has the value 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.format(x));
D) System.out.println(nf.x);
E) System.out.println(format(x));
Question
Write the paint method for an applet so that it contains 4 concentric circles (each circle is inside the previous circle), where the largest circle is bounded by a 400x400 box and the smallest is bounded by a 100x100 box. Each circle is centered on an applet that is 400x400. Make each circle a different color of your choice.
Question
Example Code Ch 02-2
import 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);
}
}
Refer to Example Code Ch 02-2. This code 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 as an int
E) The remainder of the sum of x, y, and z divided by 3
Question
An employer has decided to award a weekly pay raise to all employees by taking the square root of the difference between his weight and the employee's weight. For instance, an employee who weighs 16 pounds less than the employer will get a $4 per week raise. The raise should be in whole dollars (an int). Assume that employerWeight and employeeWeight are both int variables that have been input. Write an assignment statement to compute the int value for raise.
Question
Provide an example of how you might use a boolean, a float, a char, a String, and an int.
Question
What will be the value of z after the following statement is executed? int z = 50 /10.00;

A) 5
B) 5.0
C) 50
D) 10
E) None of thse; a run-time error will occur because z is an int and 50/10.00 is not
Question
In order to create a constant, which of the following Java reserved words is used?

A) private
B) static
C) int
D) final
E) class
Question
Write a program that will input some number of cents (less than 100) and output the number of quarters, dimes, nickels and pennies needed to add up to that amount.
Question
Provide three examples of code using assignment statements where one assignment statement would result in a syntax error, one would result in a logical error, and one would result in a run-time error.
Question
What is wrong with the following assignment statement? Assume x and y are both String objects.
String z = x.equals(y);
Question
Write an output statement which will output the following characters exactly as shown:
/ ' \" / ' \
Question
Assume that a = "1", b = "2", y = 3, and z = 4. How do the following two statements differ?
System.out.println(a + b + y + z);
System.out.println(y + z + a + b);
Question
What are the syntax errors from the following program?
public class Enigma
{
public static void main(String[] args)
{
System.out.println("Input a String");
String x = scan.nextString();
int size = x.length;
char last = x.charAt(size);
System.out.println("The last character in your string
", x, " is ", last);
}
}
Question
Write a program which will input an int value, x, and compute and output the values of 2^x and x^10 as int values.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/67
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 2: Data and Expressions
1
If String name = "George W. Bush"; then the instruction name.length(); will return 14.
True
2
A variable of type boolean will store either a 0 or a 1.
False
3
In Java, 'a' and 'A' are considered to be different values.
True
4
A double is wider than a float and a float is wider than an int.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
5
The values of (double)5/2 and (double)(5/2) are identical.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
6
Unlike the String class where you must pass a message to an object (instance) of the class, as in x.length(), in order to use either the Scanner or Math classes, you pass messages directly to the class name, as in Math.abs()or scan.nextInt().
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
7
If you want to output the text "hi there" including the quote marks, which of the following would you use?

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 of these; it is not possible to output a quote mark because it is used to mark the beginning and end of the String to be displayed.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
8
As explained in the Software Failure section in the text, the Mars Lander most likely crash landed when its descent engines cut off too high over the Mars surface.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
9
Example Code Ch 02-1
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");
}
}
Refer to the class definition in Example Code Ch 02-1. How many lines of output are provided by this program?

A) 1
B) 2
C) 3
D) 4
E) 5
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
10
If x is the String "Hi There", then x.toUpperCase().toLowerCase(); will return the original value of x.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
11
Example Code Ch 02-1
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");
}
}
Refer to the class definition in Example Code Ch 02-1. 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"
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
12
If x is a String, then x = new String("OH"); and x = "OH"; will accomplish the same thing.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
13
There are three ways that data conversion may occur: by assignment, by promotion, and by casting.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
14
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.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
15
If String a = "ABCD" and String b = "abcd" then
a.equals(b); returns false but
a.equalsIgnoreCase(b); returns true.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
16
The following statement will output __________ lines of text. System.out.println("1 big bad wolf\t8 the 3 little pigs\n4
Dinner\r2night");

A) 1
B) 2
C) 3
D) 4
E) 5
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
17
Example Code Ch 02-1
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");
}
}
Refer to the class definition in Example Code Ch 02-1. The program will print the word "Here" and then print

A) "There Everywhere" on the line after "Here"
B) "The" 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"
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
18
The word println is a(n)

A) method
B) reserved word
C) variable
D) class
E) String
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
19
Example Code Ch 02-1
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");
}
}
Refer to the class definition in Example Code Ch 02-1. A reasonable documentation 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 contains three output statements
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
20
In order to generate a random number, you must use Math.random().
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
21
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
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
22
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);

A) 15
B) 105
C) 10 5
D) x + y
E) this would cause an error since neither x nor y is a String
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
23
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
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
24
A Java variable is the name of a

A) numeric data value stored in memory
B) data value stored in memory that cannot change during the program's execution
C) data value stored in memory that can change its value but cannot change its type during the program's execution
D) data value stored in memory that can change both its value and its type during the program's execution
E) data value or a class stored in memory that can change both its value and its type during the program's execution
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
25
Given String name = "Arleen Crabtree". What will the following instruction return? name.toUpperCase().replace('R', 'Z');

A) "ARLEEN CRABTREE"
B) "AZLEEN CZABTREE"
C) "Azleen Czabtree"
D) "Arleen Crabtree"
E) "ArZleen CrZabtree"
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
26
Of the following types, which one cannot store a numeric value?

A) int
B) double
C) float
D) char
E) All of these can store numeric values
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
27
If x is an int and y is a float, which of the following statements is not a legal assignment statement?

A) y = x;
B) x = y;
C) y = (float)x;
D) x = (int)y;
E) All of these are legal assignment statements
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
28
If you want to output a double so that at least one digit appears to the left side of the decimal point and exactly one digit appears to the right side, which pattern would you give a DecimalFormat variable when you instantiate it?

A) "0.0"
B) "0.#"
C) "0.0#"
D) "0.##"
E) "#.#"
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
29
Which library package would you import to use NumberFormat and DecimalFormat?

A) java.beans
B) java.io
C) java.lang
D) java.text
E) java.util
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
30
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) this would cause a syntax error
E) this would cause a run-time error because it is a division by zero
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
31
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;
E) x.charAt(x.length());
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
32
What is the value of z after the following assignment statement is executed? float z = 5/10;

A) 0.0
B) 0.5
C) 5.0
D) 0.05
E) None of these; a run-time error will occur because z is a float and 5/10 is an int.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
33
Given the double likelihood = 0.013885 and given DecimalFormat dformatter = DecimalFormat("0.00##");
What would be the output if you execute the following:
System.out.println(df.format(likelihood));

A) 0.013885
B) 0.0139
C) 0.0145
D) .0138
E) .014
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
34
If the String major = "Computer Science", what is returned by major.charAt(1)?

A) 'C'
B) 'o'
C) 'm'
D) "C"
E) "Computer"
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
35
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);

A) 15
B) 105
C) 10 5
D) x + y
E) this would cause an error since neither x nor y is a String
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
36
Which of the following is true regarding the mod operator, %?

A) It can only be performed on int values and its result is a double.
B) It can only be performed on int values and its result is an int.
C) It can only be performed on float or double.values and its result is an int.
D) It can only be performed on float or double.values and its result is a double.
E) It can be performed on any numeric values and its result is always numeric.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
37
Which of the following situations would require a cast?

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 these require casts
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
38
If you want to store the value "Harry Potter" in the String variable name, which of the following statements could you use?

A) String name = "Harry Potter";
B) String name = new String("Harry Potter");
C) String name = "Harry" + " " + "Potter";
D) String name = new String("Harry" + " " + "Potter");
E) Any of these would work
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
39
Using getCurrencyInstance() formats a variable, automatically inserting

A) a decimal point for cents
B) a dollar sign
C) a percent sign
D) all three of these
E) a decimal point for cents and a dollar sign but not a percent sign
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
40
What will be the result of the following assignment statement, given that b = 5 and c = 10? int a = b * (-c + 2)/2;

A) 30
B) -30
C) 20
D) -20
E) -6
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
41
Using the various String methods, manipulate a String called current to be the last character of current followed by the remainder of its characters in order, placing the result in a String called rearranged.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
42
Given three int variables with the values a = 5, b = 7, and c = 12, what is the value of z after the following statement is executed? int z = (a * b - c) / a;

A) 0
B) 4
C) 5
D) -5
E) 23
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
43
As presented in the Software Failure section of the text, the root cause of the Mars Climate Orbiter problem was

A) the cost of the project
B) an inability to track the orbiter at long distances
C) atmospheric friction
D) a communication issue between subsystems
E) None of these
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
44
Explain what the following statement computes:
int z = (int)Math.ceil(Math.sqrt(x)/Math.sqrt(y));
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
45
Example Code Ch 02-2
import 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);
}
}
Refer to Example Code Ch 02-2. What is the output if x = 0, y = 1, and z = 1?

A) 0
B) 0.0
C) 0.6666666666666666
D) 0.6666666666666667
E) 0.67
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
46
Given two points in an applet represented by the four int variables x1, y1, x2 and y2, write a paint method to draw a line between the two points and write the location of the two points next to the two points.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
47
Write an assignment statement to compute the gas mileage of a car where the int values milesTraveled and gallonsNeeded have already been input. The variable gasMileage needs to be declared and should be a double.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
48
How many ways are there to test to see if two String variables, a and b, are equal to each other if we ignore their case (for instance, "aBCd" would be considered equal to "ABcd")?
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
49
Given four int values, x1, x2, y1, y2, write the code to compute the distance between the two points (x1, y1) and (x2, y2), storing the result in the double distance.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
50
Write a set of instructions to prompt the user for an int value and input it using the Scanner class into the variable x and prompt the user for a float value and input it using the Scanner class into the variable y.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
51
How do the statements "import java.util.*;" and "import java.util.Random;" differ from each other?
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
52
Java is a strongly typed language. What is meant by "strongly typed"?

A) Every variable must have an associated type before it can be used.
B) Variables can be used without declaring their types.
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 a program so long as the value a variable currently stores is of the type it currently declared to be.
E) Variables are allowed to change types during their existence in a program but only if the change is to a narrower type.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
53
Since you cannot take the square root of a negative number, which of the following could you use 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);
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
54
Given x is a double and has the value 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.format(x));
D) System.out.println(nf.x);
E) System.out.println(format(x));
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
55
Write the paint method for an applet so that it contains 4 concentric circles (each circle is inside the previous circle), where the largest circle is bounded by a 400x400 box and the smallest is bounded by a 100x100 box. Each circle is centered on an applet that is 400x400. Make each circle a different color of your choice.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
56
Example Code Ch 02-2
import 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);
}
}
Refer to Example Code Ch 02-2. This code 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 as an int
E) The remainder of the sum of x, y, and z divided by 3
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
57
An employer has decided to award a weekly pay raise to all employees by taking the square root of the difference between his weight and the employee's weight. For instance, an employee who weighs 16 pounds less than the employer will get a $4 per week raise. The raise should be in whole dollars (an int). Assume that employerWeight and employeeWeight are both int variables that have been input. Write an assignment statement to compute the int value for raise.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
58
Provide an example of how you might use a boolean, a float, a char, a String, and an int.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
59
What will be the value of z after the following statement is executed? int z = 50 /10.00;

A) 5
B) 5.0
C) 50
D) 10
E) None of thse; a run-time error will occur because z is an int and 50/10.00 is not
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
60
In order to create a constant, which of the following Java reserved words is used?

A) private
B) static
C) int
D) final
E) class
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
61
Write a program that will input some number of cents (less than 100) and output the number of quarters, dimes, nickels and pennies needed to add up to that amount.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
62
Provide three examples of code using assignment statements where one assignment statement would result in a syntax error, one would result in a logical error, and one would result in a run-time error.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
63
What is wrong with the following assignment statement? Assume x and y are both String objects.
String z = x.equals(y);
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
64
Write an output statement which will output the following characters exactly as shown:
/ ' \" / ' \
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
65
Assume that a = "1", b = "2", y = 3, and z = 4. How do the following two statements differ?
System.out.println(a + b + y + z);
System.out.println(y + z + a + b);
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
66
What are the syntax errors from the following program?
public class Enigma
{
public static void main(String[] args)
{
System.out.println("Input a String");
String x = scan.nextString();
int size = x.length;
char last = x.charAt(size);
System.out.println("The last character in your string
", x, " is ", last);
}
}
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
67
Write a program which will input an int value, x, and compute and output the values of 2^x and x^10 as int values.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 67 flashcards in this deck.