13
35) If you want to draw a red circle inside of a green square in an applet where the paint method
is passed a Graphics object called page, which of the following sets of commands might you
use?
A) page.setColor(Color.green);
page.fillRect(50, 50, 100, 100);
page.setColor(Color.red);
page.fillOval(60, 60, 80, 80);
B) page.setColor(Color.red);
page.fillOval(60, 60, 80, 80);
page.setColor(Color.green);
page.fillRect(50, 50, 100, 100);
C) page.setColor(Color.green);
page.fillRect(60, 60, 80, 80);
page.setColor(Color.red);
page.fillOval(50, 50, 100, 100);
D) page.setColor(Color.red);
page.fillOval(50, 50, 100, 100);
page.setColor(Color.green);
page.fillRect(60, 60, 80, 80);
E) any of the above would accomplish this
36) The figure drawn in this applet is
A) a blue square
B) a blue square with two white lines drawn horizontally through it
C) a blue square with two white lines drawn diagonally through it
D) a blue square with two white lines drawn vertically through it
E) a white square with two blue lines drawn vertically through it
37) The String “A nice box” is drawn
A) above the box
B) to the left of the box
C) to the right of the box
D) below the box
E) inside the box
38) You specify the shape of an oval in a Java applet by defining the oval’s
A) arc locations
B) center and radius
C) bounding box
D) foci
E) none of the above, it is not possible to draw an oval in a Java applet
39) In order to create a constant, you would use which of the following Java reserved words?
A) private
B) static
C) int
D) final
E) class
40) 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 result in z equal to
A) 0
B) 4
C) 5
D) -5
E) 23
41) Java is a strongly typed language. What is meant by “strongly typed”?
A) Every variable must have an associated type before you can use it
B) Variables can be used without declaring their type
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 are allowed to change types during their existence in the program but only if the
change is to a narrower type
42) As presented in the Software Failure, 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 the above
16
© Pearson Education Limited, 2015
2.2 True/False Questions
1) If x is a string, then x = new String(“OH”); and x = “OH”; will accomplish the same thing.
2) If x is the String “Hi There”, then x.toUpperCase( ).toLowerCase( ); will return the original
version of x.
3) If String name = “George W. Bush”; then the instruction name.length( ); will return 14.
4) If String a = “ABCD” and String b = “abcd” then a.equals(b); returns false but
a.equalsIgnoreCase(b); returns true.
5) 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( ).
6) In order to generate a random number, you must use Math.random( ).
7) A double is wider than a float and a float is wider than an int.
8) A variable of type boolean will store either a 0 or a 1.
9) In Java, ‘a’ and ‘A’ are considered to be different values.
10) 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.
11) The values of (double) 5 / 2 and (double) (5 / 2) are identical.
12) The Java graphics coordinate system is similar to the coordinate system one finds in
mathematics in every respect.
13) Many Java drawing shapes are specified using a bounding rectangle.
14) There are three ways that data conversion may occur by assignment, by promotion, by
casting.
15) Java is able to represent 255 * 255 * 255 = 16,581,375 distinct colors.
16) As explained in the Software Failure, the Mars Lander most likely crash landed when its
descent engines cut off too high over the Mars surface.
1) 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.
2) Provide an example of how you might use a boolean, a float, a char, a String, and an int.
3) Explain, in words, what the following statement computes:
int z = (int) Math.ceil(Math.sqrt(x) / Math.sqrt(y));
4) 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.
5) Write an assignment statement to compute the gas mileage of a car where the int values
miles_traveled and gallons_needed have already been input. The variable gas_mileage needs to
be declared and should be a double.
6) 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 400×400 box and the smallest
is bounded by a 100×100 box. Each circle is centered on an applet that is 400×400. Make each
circle a different color of your choice.
}
7) 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.
8) An applet is 200×200. Write the Strings “North”, “South”, “East”, and “West” at the four
edges of the applet where they should appear as if on a map. Use the color black for the text.
}
9) 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.
10) Using the various String methods, manipulate a String called current to be current’s last
character followed by the remainder of its characters in order, placing the result in a String called
rearranged.
11) 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”).
12) How do the statements “import java.util.*;” and “import java.util.Random;” differ from each
other?
13) 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);
14) Write a program which will input an int value x, and compute and output the values of 2x
and x10 as int values.
}
}
15) What is wrong with the following assignment statement? Assume x and y are both String
objects.
String z = x.equals(y);
16) 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.
}
}
17) Write an output statement which will output the following characters exactly as shown: / ‘
\” / ‘ \
18) 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.
19) 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);
}
}